Skip to content

Commit

Permalink
TNO-2033 Check if update returns error
Browse files Browse the repository at this point in the history
  • Loading branch information
fbarreta committed Sep 4, 2024
1 parent d95c6f1 commit 45bffc5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 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, getContent }] = useContent();
const [, { addContent, updateContentSilent, getContent }] = useContent();
const { values, onNavigate, isSubmitting, setSubmitting, setValues, activeRow, setActiveRow } =
useReportEditContext();

Expand Down Expand Up @@ -89,17 +89,21 @@ export const ContentEditForm = React.forwardRef<HTMLDivElement | null, IContentE
const originalId = content.id;
const err = validate(content);
if (err.hasErrors) return null;
// gets the edited content to check if the version was changed by another user
const updatedContent = await getContent(originalId);
let contentResult: IContentModel | undefined;
// 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);
try {
contentResult = !content.id
? await addContent(newContent)
: await updateContent(newContent);
} else {
contentResult = !content.id ? await addContent(content) : await updateContent(content);
? 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 = {
Expand Down Expand Up @@ -166,16 +170,7 @@ export const ContentEditForm = React.forwardRef<HTMLDivElement | null, IContentE
setSubmitting(false);
}
},
[
addContent,
getContent,
isContentUpdated,
setActiveRow,
setSubmitting,
setValues,
updateContent,
updateReport,
],
[addContent, setActiveRow, setSubmitting, setValues, updateContentSilent, updateReport],

Check warning on line 173 in app/subscriber/src/features/my-reports/edit/content/ContentEditForm.tsx

View workflow job for this annotation

GitHub Actions / build-app-subscriber (18.11.0)

React Hook React.useCallback has missing dependencies: 'getContent' and 'isContentUpdated'. Either include them or remove the dependency array
);

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 45bffc5

Please sign in to comment.