-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathFixCard.js
More file actions
221 lines (204 loc) · 6.5 KB
/
Copy pathFixCard.js
File metadata and controls
221 lines (204 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import { __, sprintf } from '@wordpress/i18n';
import { Button, Spinner, Notice, ToggleControl, TextControl, TextareaControl } from '@wordpress/components';
import { useState, useEffect, memo } from '@wordpress/element';
import { decodeEntities } from '@wordpress/html-entities';
import apiFetch from '@wordpress/api-fetch';
/**
* FixCard — renders inline settings form for a single fix slug.
*
* @param {Object} props Component props.
* @param {string} props.slug Fix slug (e.g. 'meta_viewport_scalable').
* @param {Function} props.onSave Called after a successful save.
* @param {Function} props.onError Called with an error message string on failure.
*/
const FixCard = ( { slug, onSave, onError } ) => {
const [ fixInfo, setFixInfo ] = useState( null );
const [ isLoading, setIsLoading ] = useState( true );
const [ error, setError ] = useState( null );
const [ formValues, setFormValues ] = useState( {} );
const [ isSaving, setIsSaving ] = useState( false );
const [ notice, setNotice ] = useState( null );
useEffect( () => {
let cancelled = false;
setIsLoading( true );
setError( null );
apiFetch( { path: `/accessibility-checker/v1/fix-fields/${ slug }`, method: 'GET' } )
.then( ( response ) => {
if ( cancelled ) {
return;
}
if ( response.success ) {
setFixInfo( response );
const nextValues = Object.keys( response.fields || {} ).reduce( ( acc, key ) => {
const field = response.fields[ key ] || {};
const rawValue = field.value ?? '';
acc[ key ] = field.type === 'checkbox'
? rawValue === true || rawValue === 1 || rawValue === '1'
: rawValue;
return acc;
}, {} );
setFormValues( nextValues );
} else {
const msg = response.message || sprintf( __( 'Failed to load %s fix information.', 'accessibility-checker' ), slug );
setError( msg );
onError?.( msg );
}
} )
.catch( ( err ) => {
if ( cancelled ) {
return;
}
const msg = err?.message || sprintf( __( 'Error loading %s fix information.', 'accessibility-checker' ), slug );
setError( msg );
onError?.( msg );
} )
.finally( () => {
if ( ! cancelled ) {
setIsLoading( false );
}
} );
return () => {
cancelled = true;
};
}, [ slug ] );
const handleFieldChange = ( key, value ) => {
setFormValues( ( prev ) => ( { ...prev, [ key ]: value } ) );
};
const handleSave = async () => {
if ( ! fixInfo?.fix_slug ) {
return;
}
setIsSaving( true );
setNotice( null );
try {
await apiFetch( {
path: '/accessibility-checker/v1/fixes/update',
method: 'POST',
data: { [ fixInfo.fix_slug ]: formValues },
} );
setNotice( { status: 'success', message: __( 'Fix settings saved.', 'accessibility-checker' ) } );
setFixInfo( ( prev ) => {
if ( ! prev?.fields ) {
return prev;
}
const nextFields = { ...prev.fields };
Object.keys( formValues ).forEach( ( key ) => {
if ( nextFields[ key ] ) {
nextFields[ key ] = { ...nextFields[ key ], value: formValues[ key ] };
}
} );
return { ...prev, fields: nextFields };
} );
onSave?.();
} catch ( err ) {
const msg = err?.message || __( 'Failed to save fix settings.', 'accessibility-checker' );
setNotice( {
status: 'error',
message: msg,
} );
onError?.( msg );
} finally {
setIsSaving( false );
}
};
const renderField = ( fieldKey, field ) => {
const value = formValues[ fieldKey ];
const decodedLabel = decodeEntities( field.label );
if ( field.type === 'checkbox' ) {
return (
<div key={ fieldKey } className="edac-fix-field edac-fix-field--checkbox">
<ToggleControl
label={ <span dangerouslySetInnerHTML={ { __html: field.label } } /> }
help={ field.description ? <span dangerouslySetInnerHTML={ { __html: field.description } } /> : undefined }
checked={ !! value }
onChange={ ( next ) => handleFieldChange( fieldKey, next ) }
/>
</div>
);
}
if ( field.type === 'textarea' ) {
return (
<div key={ fieldKey } className="edac-fix-field edac-fix-field--textarea">
<label className="edac-fix-field__label" htmlFor={ fieldKey } dangerouslySetInnerHTML={ { __html: decodedLabel } } />
<TextareaControl
id={ fieldKey }
value={ value ?? '' }
onChange={ ( next ) => handleFieldChange( fieldKey, next ) }
/>
{ field.description && (
<p className="edac-fix-field__description" dangerouslySetInnerHTML={ { __html: field.description } } />
) }
</div>
);
}
return (
<div key={ fieldKey } className="edac-fix-field edac-fix-field--text">
<label className="edac-fix-field__label" htmlFor={ fieldKey } dangerouslySetInnerHTML={ { __html: decodedLabel } } />
<TextControl
id={ fieldKey }
value={ value ?? '' }
onChange={ ( next ) => handleFieldChange( fieldKey, next ) }
/>
{ field.description && (
<p className="edac-fix-field__description" dangerouslySetInnerHTML={ { __html: field.description } } />
) }
</div>
);
};
if ( error ) {
return (
<div className="edac-fix-card edac-fix-card--error">
<Notice status="error" isDismissible={ false }>{ error }</Notice>
</div>
);
}
if ( isLoading ) {
return (
<div className="edac-fix-card edac-fix-card--loading">
<Spinner />
<p>{ __( 'Loading fix information...', 'accessibility-checker' ) }</p>
</div>
);
}
if ( ! fixInfo ) {
return null;
}
const statusClass = fixInfo.enabled ? 'edac-fix-card--enabled' : 'edac-fix-card--disabled';
return (
<div className={ `edac-fix-card ${ statusClass }` }>
<form onSubmit={ ( e ) => {
e.preventDefault();
handleSave();
} }>
<div className="edac-fix-card__header">
<h3 className="edac-fix-card__title">{ fixInfo.fix_name }</h3>
</div>
{ Object.keys( fixInfo.fields ).length > 0 && (
<div className="edac-fix-card__fields">
{ Object.entries( fixInfo.fields ).map( ( [ fieldKey, field ] ) => renderField( fieldKey, field ) ) }
</div>
) }
{ notice && (
<Notice
status={ notice.status }
isDismissible={ true }
onRemove={ () => setNotice( null ) }
>
{ notice.message }
</Notice>
) }
<div className="edac-fix-card__actions">
<Button
variant="primary"
type="submit"
disabled={ isSaving }
aria-label={ sprintf( __( 'Save fix for: %s', 'accessibility-checker' ), fixInfo.fix_name ) }
>
{ __( 'Save', 'accessibility-checker' ) }
</Button>
</div>
</form>
</div>
);
};
export default memo( FixCard );