Skip to content

Commit

Permalink
TNO-2033 Merge user versions on content (#2256)
Browse files Browse the repository at this point in the history
* TNO-2033 Merge user versions on content

* TNO-2033 Check if update returns error

* TNO-2033 Lint errors fix
  • Loading branch information
fbarreta authored Sep 4, 2024
1 parent 0edccfe commit 9d93e4b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const ContentEditForm = React.forwardRef<HTMLDivElement | null, IContentE
({ disabled }, ref) => {
const [{ userInfo }] = useApp();
const [, { updateReport }] = useReports();
const [, { addContent, updateContent }] = useContent();
const [, { addContent, updateContentSilent, getContent }] = useContent();
const { values, onNavigate, isSubmitting, setSubmitting, setValues, activeRow, setActiveRow } =
useReportEditContext();

Expand Down Expand Up @@ -60,18 +60,51 @@ export const ContentEditForm = React.forwardRef<HTMLDivElement | null, IContentE
return err;
};

const isContentUpdated = React.useCallback(
async (userContent: IContentModel, updatedContent: IContentModel) => {
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,
Expand Down Expand Up @@ -124,7 +157,7 @@ export const ContentEditForm = React.forwardRef<HTMLDivElement | null, IContentE
? {
...instance,
content: instance.content.map((c) =>
c.contentId === contentResult.id ? { ...c, content: contentResult } : c,
c.contentId === contentResult?.id ? { ...c, content: contentResult } : c,
),
}
: instance,
Expand All @@ -137,7 +170,16 @@ export const ContentEditForm = React.forwardRef<HTMLDivElement | null, IContentE
setSubmitting(false);
}
},
[addContent, setActiveRow, setSubmitting, setValues, updateContent, updateReport],
[
addContent,
getContent,
isContentUpdated,
setActiveRow,
setSubmitting,
setValues,
updateContentSilent,
updateReport,
],
);

const reportContent: { label: string; url: string; section: string }[] = values.instances.length
Expand Down
11 changes: 11 additions & 0 deletions app/subscriber/src/store/hooks/subscriber/useContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface IContentController {
stream: (path: string) => Promise<string>;
addContent: (content: IContentModel) => Promise<IContentModel | undefined>;
updateContent: (content: IContentModel) => Promise<IContentModel | undefined>;
updateContentSilent: (content: IContentModel) => Promise<IContentModel | undefined>;
deleteContent: (content: IContentModel) => Promise<IContentModel | undefined>;
}

Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit 9d93e4b

Please sign in to comment.