@@ -5,6 +5,17 @@ import { PatronBlockingRule } from "../interfaces";
55import EditableInput from "./EditableInput" ;
66import WithRemoveButton from "./WithRemoveButton" ;
77
8+ function extractErrorMessage ( error : FetchErrorData ) : string | null {
9+ if ( ! error || error . status < 400 ) return null ;
10+ const resp = error . response as string ;
11+ if ( ! resp ) return null ;
12+ try {
13+ return JSON . parse ( resp ) . detail || resp ;
14+ } catch {
15+ return resp ;
16+ }
17+ }
18+
819export interface PatronBlockingRulesEditorProps {
920 value ?: PatronBlockingRule [ ] ;
1021 disabled ?: boolean ;
@@ -91,6 +102,7 @@ export default class PatronBlockingRulesEditor extends React.Component<
91102 render ( ) : JSX . Element {
92103 const { disabled, error } = this . props ;
93104 const { rules, clientErrors } = this . state ;
105+ const serverErrorMessage = extractErrorMessage ( error ) ;
94106
95107 return (
96108 < div className = "patron-blocking-rules-editor" >
@@ -104,19 +116,32 @@ export default class PatronBlockingRulesEditor extends React.Component<
104116 content = "Add Rule"
105117 />
106118 </ div >
119+ { serverErrorMessage && rules . length > 0 && (
120+ < p className = "patron-blocking-rule-field-error text-danger" >
121+ { serverErrorMessage }
122+ </ p >
123+ ) }
107124 { rules . length === 0 && (
108125 < p className = "no-rules-message" > No patron blocking rules defined.</ p >
109126 ) }
110127 < ul className = "patron-blocking-rules-list list-unstyled" >
111128 { rules . map ( ( rule , index ) => {
112129 const rowErrors = clientErrors [ index ] || { } ;
130+ const nameClientError = ! ! rowErrors . name ;
131+ const ruleClientError = ! ! rowErrors . rule ;
132+
113133 return (
114134 < li key = { index } className = "patron-blocking-rule-row" >
115135 < WithRemoveButton
116136 disabled = { disabled }
117137 onRemove = { ( ) => this . removeRule ( index ) }
118138 >
119139 < div className = "patron-blocking-rule-fields" >
140+ { nameClientError && (
141+ < p className = "patron-blocking-rule-field-error text-danger" >
142+ Rule Name is required.
143+ </ p >
144+ ) }
120145 < EditableInput
121146 elementType = "input"
122147 type = "text"
@@ -133,6 +158,11 @@ export default class PatronBlockingRulesEditor extends React.Component<
133158 this . updateRule ( index , "name" , value )
134159 }
135160 />
161+ { ruleClientError && (
162+ < p className = "patron-blocking-rule-field-error text-danger" >
163+ Rule Expression is required.
164+ </ p >
165+ ) }
136166 < EditableInput
137167 elementType = "textarea"
138168 label = "Rule Expression"
0 commit comments