From 3c1dfb88df8651b26cb1dbc102a34cd0aad722bc Mon Sep 17 00:00:00 2001 From: James Date: Wed, 20 Apr 2022 17:20:05 -0400 Subject: [PATCH] fix: intermittent blocks UI issue --- .../forms/field-types/Blocks/Blocks.tsx | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/admin/components/forms/field-types/Blocks/Blocks.tsx b/src/admin/components/forms/field-types/Blocks/Blocks.tsx index 2d7d5b60e8..b09f695923 100644 --- a/src/admin/components/forms/field-types/Blocks/Blocks.tsx +++ b/src/admin/components/forms/field-types/Blocks/Blocks.tsx @@ -104,7 +104,7 @@ const Blocks: React.FC = (props) => { }, [dispatchRows, dispatchFields, path]); const setCollapse = useCallback(async (rowID: string, collapsed: boolean) => { - dispatchRows({ type: 'SET_COLLAPSE', rowID, collapsed }); + dispatchRows({ type: 'SET_COLLAPSE', id: rowID, collapsed }); if (preferencesKey) { const preferences: DocumentPreferences = await getPreference(preferencesKey); @@ -142,13 +142,16 @@ const Blocks: React.FC = (props) => { // Get preferences, and once retrieved, // Reset rows with preferences included useEffect(() => { - const fetchPreferences = async () => { - const preferences = preferencesKey ? await getPreference(preferencesKey) : undefined; - const data = formContext.getDataByPath(path); - dispatchRows({ type: 'SET_ALL', data: data || [], collapsedState: preferences?.fields?.[path]?.collapsed }); - }; + const data = formContext.getDataByPath(path); - fetchPreferences(); + if (Array.isArray(data)) { + const fetchPreferences = async () => { + const preferences = preferencesKey ? await getPreference(preferencesKey) : undefined; + dispatchRows({ type: 'SET_ALL', data: data || [], collapsedState: preferences?.fields?.[path]?.collapsed }); + }; + + fetchPreferences(); + } }, [formContext, path, preferencesKey, getPreference]); // Set row count on mount and when form context is reset