Skip to content

Commit d07d692

Browse files
committed
Refactor form value use with new hook for consistency.
1 parent a9df9e6 commit d07d692

3 files changed

Lines changed: 17 additions & 20 deletions

File tree

assets/js/components/pdf-generation/PDFSectionsSelectionPanel/PanelContent.tsx

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ import { Fragment, useCallback } from '@wordpress/element';
3030
/**
3131
* Internal dependencies
3232
*/
33-
import { useDispatch } from 'googlesitekit-data';
34-
import { CORE_FORMS } from '@/js/googlesitekit/datastore/forms/constants';
3533
import { SelectionPanelContent } from '@/js/components/SelectionPanel';
3634
import SelectionPanelNotice from '@/js/components/SelectionPanel/SelectionPanelNotice';
3735
import { NOTICE_TYPES } from '@/js/components/Notice/constants';
@@ -51,27 +49,23 @@ interface PanelContentProps {
5149
}
5250

5351
const PanelContent: FC< PanelContentProps > = ( { closePanel } ) => {
54-
const [ selectedSectionsValue ] = useFormValue(
52+
const [ selectedSectionsValue, setSelectedSections ] = useFormValue(
5553
FORM_PDF_DOWNLOAD,
5654
FORM_PDF_DOWNLOAD_SELECTED_SECTIONS
5755
);
5856
const selectedSections =
5957
( selectedSectionsValue as string[] | undefined ) ??
6058
DEFAULT_SELECTED_SECTIONS;
6159

62-
const { setValues } = useDispatch( CORE_FORMS );
63-
6460
const toggleSection = useCallback(
6561
( slug: string ) => {
6662
const nextSelection = selectedSections.includes( slug )
6763
? selectedSections.filter( ( item ) => item !== slug )
6864
: [ ...selectedSections, slug ];
6965

70-
setValues( FORM_PDF_DOWNLOAD, {
71-
[ FORM_PDF_DOWNLOAD_SELECTED_SECTIONS ]: nextSelection,
72-
} );
66+
setSelectedSections( nextSelection );
7367
},
74-
[ selectedSections, setValues ]
68+
[ selectedSections, setSelectedSections ]
7569
);
7670

7771
const hasSelection = selectedSections.length > 0;

assets/js/components/pdf-generation/PDFSectionsSelectionPanel/index.stories.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { useDispatch } from 'googlesitekit-data';
3333
import WithRegistrySetup from '../../../../../tests/js/WithRegistrySetup';
3434
import { CORE_UI } from '@/js/googlesitekit/datastore/ui/constants';
3535
import { CORE_FORMS } from '@/js/googlesitekit/datastore/forms/constants';
36+
import useFormValue from '@/js/hooks/useFormValue';
3637
import {
3738
FORM_PDF_DOWNLOAD,
3839
FORM_PDF_DOWNLOAD_SELECTED_SECTIONS,
@@ -50,17 +51,18 @@ function DefaultTemplate() {
5051
}
5152

5253
function EmptyTemplate() {
53-
const { setValues } = useDispatch( CORE_FORMS );
54+
const [ , setSelectedSections ] = useFormValue(
55+
FORM_PDF_DOWNLOAD,
56+
FORM_PDF_DOWNLOAD_SELECTED_SECTIONS
57+
);
5458

5559
// The panel's `onSideSheetOpen` resets the form to default selection on
5660
// every mount. Re-applying the empty selection from this Template's
5761
// `useEffect` runs after the panel's reset (effects fire bottom-up),
5862
// so the story renders with no sections selected.
5963
useEffect( () => {
60-
setValues( FORM_PDF_DOWNLOAD, {
61-
[ FORM_PDF_DOWNLOAD_SELECTED_SECTIONS ]: [],
62-
} );
63-
}, [ setValues ] );
64+
setSelectedSections( [] );
65+
}, [ setSelectedSections ] );
6466

6567
return <PDFSectionsSelectionPanel />;
6668
}

assets/js/components/pdf-generation/PDFSectionsSelectionPanel/index.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ import { useCallback } from '@wordpress/element';
3131
*/
3232
import { useSelect, useDispatch, type Select } from 'googlesitekit-data';
3333
import { CORE_UI } from '@/js/googlesitekit/datastore/ui/constants';
34-
import { CORE_FORMS } from '@/js/googlesitekit/datastore/forms/constants';
3534
import {
3635
DEFAULT_SELECTED_SECTIONS,
3736
FORM_PDF_DOWNLOAD,
3837
FORM_PDF_DOWNLOAD_SELECTED_SECTIONS,
3938
PDF_DOWNLOAD_PANEL_OPENED_KEY,
4039
PDF_GENERATING_KEY,
4140
} from '@/js/components/pdf-generation/constants';
41+
import useFormValue from '@/js/hooks/useFormValue';
4242
import InViewProvider from '@/js/components/InViewProvider';
4343
import SelectionPanel from '@/js/components/SelectionPanel';
4444
import PanelContent from './PanelContent';
@@ -51,7 +51,10 @@ const PDFSectionsSelectionPanel: FC = () => {
5151
);
5252

5353
const { setValue } = useDispatch( CORE_UI );
54-
const { setValues } = useDispatch( CORE_FORMS );
54+
const [ , setSelectedSections ] = useFormValue(
55+
FORM_PDF_DOWNLOAD,
56+
FORM_PDF_DOWNLOAD_SELECTED_SECTIONS
57+
);
5558

5659
const closePanel = useCallback( () => {
5760
if ( isOpen ) {
@@ -60,12 +63,10 @@ const PDFSectionsSelectionPanel: FC = () => {
6063
}, [ isOpen, setValue ] );
6164

6265
const onSideSheetOpen = useCallback( () => {
63-
setValues( FORM_PDF_DOWNLOAD, {
64-
[ FORM_PDF_DOWNLOAD_SELECTED_SECTIONS ]: DEFAULT_SELECTED_SECTIONS,
65-
} );
66+
setSelectedSections( DEFAULT_SELECTED_SECTIONS );
6667
// Reset any stale "generating" state left over from a previous session.
6768
setValue( PDF_GENERATING_KEY, false );
68-
}, [ setValues, setValue ] );
69+
}, [ setSelectedSections, setValue ] );
6970

7071
return (
7172
<InViewProvider

0 commit comments

Comments
 (0)