Skip to content

Commit

Permalink
Client:
Browse files Browse the repository at this point in the history
* Return success/failure when updating data in detail views
* Handle update failures by providing an error and not publishing
  • Loading branch information
jahjedtieson committed Dec 8, 2021
1 parent ea994c3 commit 6288712
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ interface ObjectDetailsProps {
onRetiredUpdate?: (event: React.ChangeEvent<HTMLInputElement>, checked: boolean) => void;
onLicenseUpdate?: (event) => void;
path?: RepositoryPath[][] | null;
updateData?: () => Promise<void>;
updateData?: () => Promise<boolean>;
idSystemObject: number;
license?: number;
licenseInheritance?: number | null;
Expand Down Expand Up @@ -187,8 +187,13 @@ function ObjectDetails(props: ObjectDetailsProps): React.ReactElement {
setLoading(true);

// if we're attempting to publish a subject, call the passed in update method first to persist metadata edits
if (objectType === eSystemObjectType.eSubject && updateData !== undefined)
await updateData();
if (objectType === eSystemObjectType.eSubject && updateData !== undefined) {
if (!await updateData()) {
toast.error(`${action} failed while updating object`);
setLoading(false);
return;
}
}

const { data } = await publish(idSystemObject, eState);
if (data?.publish?.success)
Expand Down
15 changes: 8 additions & 7 deletions client/src/pages/Repository/components/DetailsView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -322,14 +322,14 @@ function DetailsView(): React.ReactElement {
setUpdatedData(updatedDataFields);
};

const updateData = async (): Promise<void> => {
const updateData = async (): Promise<boolean> => {
toast.dismiss();
setIsUpdatingData(true);
const identifierCheck = checkIdentifiersBeforeUpdate();
if (identifierCheck.length) {
identifierCheck.forEach(error => toast.error(error));
setIsUpdatingData(false);
return;
return false;
}

const stateIdentifiersWithIdSystemObject: UpdateIdentifier[] = stateIdentifiers.map(({ id, identifier, identifierType, idIdentifier, preferred }) => {
Expand All @@ -350,15 +350,15 @@ function DetailsView(): React.ReactElement {
const invalidMetadata = validateMetadataFields();
if (invalidMetadata.length) {
invalidMetadata.forEach(message => toast.error(message, { autoClose: false }));
return;
return false;
}

// Create another validation here to make sure that the appropriate SO types are being checked
const errors = await getDetailsViewFieldErrors(updatedData, objectType);
if (errors.length) {
errors.forEach(error => toast.error(`${error}`, { autoClose: false }));
setIsUpdatingData(false);
return;
return false;
}

try {
Expand Down Expand Up @@ -455,18 +455,19 @@ function DetailsView(): React.ReactElement {
}

const metadata = getAllMetadataEntries().filter(entry => entry.Name);
console.log('metadata', metadata);
// console.log('metadata', metadata);
updatedData.Metadata = metadata;

const { data } = await updateDetailsTabData(idSystemObject, idObject, objectType, updatedData);
if (data?.updateObjectDetails?.success) {
toast.success('Data saved successfully');
} else {
return true;
} else
throw new Error(data?.updateObjectDetails?.message ?? '');
}
} catch (error) {
if (error instanceof Error)
toast.error(error.toString() || 'Failed to save updated data');
return false;
} finally {
setIsUpdatingData(false);
}
Expand Down

0 comments on commit 6288712

Please sign in to comment.