fix(ctb): refetch data after save or update (#19844)

This commit is contained in:
Bassel Kanso 2024-03-19 16:23:27 +02:00 committed by GitHub
parent 426ddd2ec2
commit 67a503d9cc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 14 additions and 3 deletions

View File

@ -42,6 +42,7 @@ import {
REMOVE_FIELD_FROM_DISPLAYED_COMPONENT,
SET_MODIFIED_DATA,
UPDATE_SCHEMA,
UPDATE_INITIAL_STATE,
} from './constants';
import { makeSelectDataManagerProvider } from './selectors';
import { formatMainDataType, getComponentsToPost, sortContentType } from './utils/cleanData';
@ -552,7 +553,7 @@ const DataManagerProvider = ({ children }: DataManagerProviderProps) => {
await serverRestartWatcher(true);
// Unlock the app
await unlockAppWithAutoreload?.();
unlockAppWithAutoreload?.();
if (
isCreating &&
@ -576,6 +577,10 @@ const DataManagerProvider = ({ children }: DataManagerProviderProps) => {
trackUsage('didSaveComponent');
}
// refetch and update initial state after the data has been saved
await getDataRef.current();
dispatch({ type: UPDATE_INITIAL_STATE });
// Update the app's permissions
await updatePermissions();
} catch (err: any) {

View File

@ -21,3 +21,4 @@ export const REMOVE_COMPONENT_FROM_DYNAMIC_ZONE =
export const REMOVE_FIELD = 'ContentTypeBuilder/DataManagerProvider/REMOVE_FIELD';
export const SET_MODIFIED_DATA = 'ContentTypeBuilder/DataManagerProvider/SET_MODIFIED_DATA';
export const UPDATE_SCHEMA = 'ContentTypeBuilder/DataManagerProvider/UPDATE_SCHEMA';
export const UPDATE_INITIAL_STATE = 'ContentTypeBuilder/DataManagerProvider/UPDATE_INITIAL_STATE';

View File

@ -545,6 +545,11 @@ const reducer = (state = initialState, action: Action) =>
break;
}
case actions.UPDATE_INITIAL_STATE: {
draftState.initialData = draftState.modifiedData;
break;
}
case actions.DELETE_NOT_SAVED_TYPE: {
// Doing so will also reset the modified and the initial data
draftState.contentTypes = state.initialContentTypes;

View File

@ -41,7 +41,7 @@ export interface DataManagerContextValue {
removeComponentFromDynamicZone: (dzName: string, componentToRemoveIndex: number) => void;
setModifiedData: () => void;
sortedContentTypesList: any[]; // Define the actual type
submitData: (additionalContentTypeData?: Record<string, any>) => void;
submitData: (additionalContentTypeData?: Record<string, any>) => Promise<void>;
updateSchema: (data: Record<string, any>, schemaType: SchemaType, componentUID: UID.Any) => void;
components: Record<UID.Component, Component>;
componentsGroupedByCategory: Record<string, Component[]>;

View File

@ -139,7 +139,7 @@ const ListView = () => {
)}
<Button
startIcon={<Check />}
onClick={() => submitData()}
onClick={async () => await submitData()}
type="submit"
disabled={isEqual(modifiedData, initialData)}
>