Skip to content

Commit 51dd71c

Browse files
committed
Update behaviour so that each guideline can be saved separately
1 parent 96c5c23 commit 51dd71c

3 files changed

Lines changed: 24 additions & 28 deletions

File tree

‎routes/content-guidelines/api.ts‎

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,22 @@ export async function fetchContentGuidelines(): Promise< RestGuidelinesResponse
4949
}
5050

5151
export async function saveContentGuidelines(): Promise< RestGuidelinesResponse > {
52-
const { setFromResponse } = dispatch( STORE_NAME ) as {
53-
setFromResponse: ( response: RestGuidelinesResponse ) => void;
54-
};
52+
// @ts-ignore
53+
const { setFromResponse } = dispatch( STORE_NAME );
5554

5655
const guidelinesStore = select( STORE_NAME ) as {
5756
getId: () => number | null;
5857
getStatus: () => string | null;
5958
getAllGuidelines: () => Partial< Record< string, string > >;
59+
getGuideline: ( category: string ) => string;
6060
};
6161

6262
const id = guidelinesStore.getId();
6363
const status = guidelinesStore.getStatus() || 'draft';
6464
const categories = guidelinesStore.getAllGuidelines();
6565

66-
const data: {
67-
id?: number;
68-
status: string;
69-
guideline_categories: RestGuidelinesResponse[ 'guideline_categories' ];
70-
} = {
66+
const data = {
67+
id: id && id > 0 ? id : undefined,
7168
status,
7269
guideline_categories: {
7370
site: {
@@ -85,10 +82,6 @@ export async function saveContentGuidelines(): Promise< RestGuidelinesResponse >
8582
},
8683
};
8784

88-
if ( id && id > 0 ) {
89-
data.id = id;
90-
}
91-
9285
const { createSuccessNotice, createErrorNotice } = dispatch( noticesStore );
9386

9487
try {

‎routes/content-guidelines/components/guideline-accordion.tsx‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export default function GuidelineAccordion( {
3939
const headingId = useId();
4040
const descriptionId = useId();
4141

42+
// @ts-ignore
4243
const { setGuideline } = useDispatch( STORE_NAME );
4344

4445
const { value } = useSelect(
@@ -49,9 +50,12 @@ export default function GuidelineAccordion( {
4950
[ slug ]
5051
);
5152

52-
const handleSave = ( event: React.FormEvent< HTMLFormElement > ) => {
53+
const [ draft, setDraft ] = useState( value );
54+
55+
const handleSave = ( event: SubmitEvent ) => {
5356
event.preventDefault();
54-
void saveContentGuidelines();
57+
setGuideline( slug, draft );
58+
saveContentGuidelines();
5559
};
5660

5761
return (
@@ -116,10 +120,8 @@ export default function GuidelineAccordion( {
116120
<TextareaControl
117121
label={ __( 'Copy guidelines' ) }
118122
hideLabelFromVision
119-
value={ value }
120-
onChange={ ( newValue: string ) => {
121-
setGuideline( slug, newValue );
122-
} }
123+
value={ draft }
124+
onChange={ setDraft }
123125
/>
124126
<Button
125127
variant="primary"

‎routes/content-guidelines/store.ts‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const STORE_NAME = 'core/content-guidelines';
88
export interface ContentGuidelinesState {
99
id: number | null;
1010
status: string | null;
11-
categories: Partial< Record< string, string > >;
11+
categories: Record< string, string >;
1212
}
1313

1414
const DEFAULT_STATE: ContentGuidelinesState = {
@@ -17,6 +17,8 @@ const DEFAULT_STATE: ContentGuidelinesState = {
1717
categories: {},
1818
};
1919

20+
const CATEGORIES = [ 'site', 'copy', 'images', 'additional' ];
21+
2022
const actions = {
2123
setFromResponse( response: unknown ) {
2224
return {
@@ -48,17 +50,16 @@ function parseResponse( response: unknown ): Partial< ContentGuidelinesState > {
4850

4951
const categoriesFromResponse = data.guideline_categories ?? {};
5052

51-
const result: Partial< ContentGuidelinesState > = {
52-
id: typeof data.id === 'number' ? data.id : null,
53-
status: typeof data.status === 'string' ? data.status : null,
53+
const result = {
54+
id: data.id as number | null,
55+
status: data.status as string | null,
5456
categories: {},
5557
};
5658

57-
[ 'site', 'copy', 'images', 'additional' ].forEach( ( slug ) => {
58-
const guidelines = categoriesFromResponse?.[ slug ]?.guidelines;
59+
CATEGORIES.forEach( ( category ) => {
60+
const guidelines = categoriesFromResponse?.[ category ]?.guidelines;
5961
if ( typeof guidelines === 'string' ) {
60-
( result.categories as Record< string, string > )[ slug ] =
61-
guidelines;
62+
result.categories[ category ] = guidelines;
6263
}
6364
} );
6465

@@ -89,8 +90,8 @@ function reducer(
8990
}
9091

9192
const selectors = {
92-
getGuideline( state: ContentGuidelinesState, slug: string ): string {
93-
return state.categories[ slug ] ?? '';
93+
getGuideline( state: ContentGuidelinesState, category: string ): string {
94+
return state.categories[ category ] ?? '';
9495
},
9596
getAllGuidelines(
9697
state: ContentGuidelinesState

0 commit comments

Comments
 (0)