From 9d93e4b474df4350678724fd938f5c48b4232795 Mon Sep 17 00:00:00 2001 From: Felipe Barreta Date: Wed, 4 Sep 2024 15:29:55 -0700 Subject: [PATCH] TNO-2033 Merge user versions on content (#2256) * TNO-2033 Merge user versions on content * TNO-2033 Check if update returns error * TNO-2033 Lint errors fix --- .../edit/content/ContentEditForm.tsx | 58 ++++++++++++++++--- .../src/store/hooks/subscriber/useContent.ts | 11 ++++ 2 files changed, 61 insertions(+), 8 deletions(-) diff --git a/app/subscriber/src/features/my-reports/edit/content/ContentEditForm.tsx b/app/subscriber/src/features/my-reports/edit/content/ContentEditForm.tsx index 4c8e0fa3b..57970da20 100644 --- a/app/subscriber/src/features/my-reports/edit/content/ContentEditForm.tsx +++ b/app/subscriber/src/features/my-reports/edit/content/ContentEditForm.tsx @@ -28,7 +28,7 @@ export const ContentEditForm = React.forwardRef { const [{ userInfo }] = useApp(); const [, { updateReport }] = useReports(); - const [, { addContent, updateContent }] = useContent(); + const [, { addContent, updateContentSilent, getContent }] = useContent(); const { values, onNavigate, isSubmitting, setSubmitting, setValues, activeRow, setActiveRow } = useReportEditContext(); @@ -60,18 +60,51 @@ export const ContentEditForm = React.forwardRef { + if ( + updatedContent.version && + userContent.version && + updatedContent.version > userContent.version + ) { + const key: number = userId; + const userVersions = userContent.versions[userId]; + const newVersions = userContent.versions; + newVersions[key] = userVersions; + + return { ...updatedContent, versions: newVersions }; + } else { + return userContent; + } + }, + [userId], + ); + const handleAddUpdateContent = React.useCallback( async (values: IReportForm, row: IReportInstanceContentForm) => { try { setSubmitting(true); const content = row.content; - if (!content) return null; + if (!content || content === undefined) return null; + const originalId = content.id; const err = validate(content); if (err.hasErrors) return null; - const originalId = content.id; - const contentResult = !content.id - ? await addContent(content) - : await updateContent(content); + let contentResult: IContentModel | undefined; + try { + contentResult = !content.id + ? await addContent(content) + : await updateContentSilent(content); + } catch (err) { + // gets the edited content to check if the version was changed by another user + const updatedContent = await getContent(originalId); + // if the content was updated, set the changes to the user version and update the content version avoing data loss and concurrency errors + if (updatedContent) { + const newContent = await isContentUpdated(content, updatedContent); + contentResult = !content.id + ? await addContent(newContent) + : await updateContentSilent(newContent); + } + } if (contentResult) { const instanceContent: IReportInstanceContentForm = { contentId: contentResult.id, @@ -124,7 +157,7 @@ export const ContentEditForm = React.forwardRef - c.contentId === contentResult.id ? { ...c, content: contentResult } : c, + c.contentId === contentResult?.id ? { ...c, content: contentResult } : c, ), } : instance, @@ -137,7 +170,16 @@ export const ContentEditForm = React.forwardRef Promise; addContent: (content: IContentModel) => Promise; updateContent: (content: IContentModel) => Promise; + updateContentSilent: (content: IContentModel) => Promise; deleteContent: (content: IContentModel) => Promise; } @@ -114,6 +115,16 @@ export const useContent = (props?: IContentProps): [IContentState, IContentContr ); return response.data; }, + updateContentSilent: async (content: IContentModel) => { + const response = await dispatch( + 'update-content', + () => api.updateContent(content), + 'content', + true, + true, + ); + return response.data; + }, deleteContent: async (content: IContentModel) => { const response = await dispatch( 'delete-content',