From 3258781d5f03c269844d59ed5df62ebc8c39199a Mon Sep 17 00:00:00 2001 From: yassinedorbozgithub Date: Thu, 3 Oct 2024 16:35:40 +0100 Subject: [PATCH] refactor(frontend): content logic --- .../src/components/contents/ContentDialog.tsx | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/frontend/src/components/contents/ContentDialog.tsx b/frontend/src/components/contents/ContentDialog.tsx index 22c7b3d..f71d179 100644 --- a/frontend/src/components/contents/ContentDialog.tsx +++ b/frontend/src/components/contents/ContentDialog.tsx @@ -119,6 +119,20 @@ const ContentFieldInput: React.FC = ({ return ; } }; +const INITIAL_FIELDS = ["title", "status"]; +const buildDynamicFields = ( + content: IContentAttributes, + contentType?: IContentType, +) => ({ + title: content.title, + entity: content.entity, + status: content.status, + dynamicFields: { + ...contentType?.fields + ?.filter(({ name }) => !INITIAL_FIELDS.includes(name)) + .reduce((acc, { name }) => ({ ...acc, [name]: content[name] }), {}), + }, +}); export type ContentDialogProps = DialogControlProps<{ content?: IContent; @@ -163,7 +177,7 @@ export const ContentDialog: FC = ({ const onSubmitForm = async (params: IContentAttributes) => { if (content) { updateContent( - { id: content.id, params }, + { id: content.id, params: buildDynamicFields(params, contentType) }, { onError: () => { toast.error(t("message.internal_server_error")); @@ -176,7 +190,7 @@ export const ContentDialog: FC = ({ ); } else if (contentType) { createContent( - { ...params, entity: contentType.id }, + { ...buildDynamicFields(params, contentType), entity: contentType.id }, { onError: (error) => { toast.error(error); @@ -198,11 +212,7 @@ export const ContentDialog: FC = ({ useEffect(() => { if (content) { - reset({ - entity: content.entity, - status: content.status, - title: content.title, - }); + reset(content); } else { reset(); } @@ -221,7 +231,11 @@ export const ContentDialog: FC = ({