Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(app-headless-cms): do not set dirty model unless it changed #4207

Merged
merged 2 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import {
UpdateCmsModelMutationVariables
} from "~/admin/graphql/contentModels";
import { LIST_MENU_CONTENT_GROUPS_MODELS } from "~/admin/viewsGraphql";
import { CmsModelField, CmsModel } from "~/types";
import { CmsModel, CmsModelField } from "~/types";
import { FetchResult } from "apollo-link";
import { ModelProvider } from "~/admin/components/ModelProvider";
import { createHashing } from "@webiny/app/utils";

export interface ContentModelEditorProviderContext {
apolloClient: ApolloClient<any>;
Expand Down Expand Up @@ -65,6 +66,8 @@ export const contentModelEditorReducer: Reducer = (prev: State, action: Action):
}
};

const hashModel = createHashing("SHA-256");

/**
* Cleanup is required because backend always expects string value in predefined values entries
*/
Expand Down Expand Up @@ -176,8 +179,13 @@ export const ContentModelEditorProvider = ({
* Return new data once finished.
*/
const setData = async (setter: (value: any) => any, saveModel = false): Promise<void> => {
setPristine(false);
const data = setter(state.data);
const existingHash = await hashModel(state.data);
const newHash = await hashModel(data);
if (existingHash === newHash) {
return;
}
setPristine(false);
dispatch({ type: "data", data });
if (!saveModel) {
return;
Expand All @@ -204,6 +212,7 @@ export const ContentModelEditorProvider = ({
}

await setData(() => data, false);

setPristine(true);
return response;
};
Expand Down
18 changes: 18 additions & 0 deletions packages/app/src/utils/createHashing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export type IHashingAlgorithm = "SHA-1" | "SHA-256" | "SHA-512";

export const createHashing = (algorithm: IHashingAlgorithm) => {
return async (value: unknown) => {
const data = JSON.stringify(value);
return new Promise<string>(resolve => {
window.crypto.subtle.digest(algorithm, new TextEncoder().encode(data)).then(encoded => {
const hexes: string[] = [];
const view = new DataView(encoded);
for (let i = 0; i < view.byteLength; i += 4) {
hexes.push(("00000000" + view.getUint32(i).toString(16)).slice(-8));
}
const hash = hexes.join("");
resolve(hash);
});
});
};
};
1 change: 1 addition & 0 deletions packages/app/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./createHashing";
export * from "./getApiUrl";
export * from "./getGqlApiUrl";
export * from "./getHeadlessCmsGqlApiUrl";
Expand Down