Skip to content
This repository has been archived by the owner on Feb 10, 2025. It is now read-only.

Commit

Permalink
fix: check working memo mutation errors
Browse files Browse the repository at this point in the history
Wasn't checking GraphQL field errors at all so any request that was
valid GQL was reported successful to the user, regardless if it was
successful in the backend.
  • Loading branch information
joonatank committed Nov 30, 2023
1 parent ec12055 commit e5ab99f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 28 deletions.
86 changes: 58 additions & 28 deletions apps/admin-ui/src/component/WorkingMemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,61 @@ const ButtonContainer = styled.div`
margin-top: var(--spacing-m);
`;

// TODO split into two variations with different mutations
// one for application and other for reservation
function WorkingMemo({
initialValue,
onMutate,
onSuccess,
}: {
initialValue: string;
onMutate: (memo: string) => Promise<FetchResult<Mutation>>;
onSuccess: () => void;
}) {
const [workingMemo, setWorkingMemo] = useState<string>(initialValue);
const { notifyError } = useNotification();
const { notifyError, notifySuccess } = useNotification();
const { t } = useTranslation();

const handleSave = async () => {
// TODO awful error handling code, real problem is the lack of error design
// compounded with using two separate mutations here
try {
await onMutate(workingMemo);
const res = await onMutate(workingMemo);
if (res.errors != null) {
throw new Error(res.errors[0].message);
}
const { data } = res;
if (
data?.updateReservationWorkingMemo == null &&
data?.updateApplication == null
) {
throw new Error("No data returned");
}
const { updateReservationWorkingMemo, updateApplication } = data;
if (updateReservationWorkingMemo != null) {
const { errors: errs } = updateReservationWorkingMemo;
if (errs != null) {
throw new Error(errs[0]?.messages.find((m) => m != null));
}
}
if (updateApplication != null) {
const { errors: errs } = updateApplication;
if (errs != null) {
throw new Error(errs[0]?.messages.find((m) => m != null));
}
}
notifySuccess(t("RequestedReservation.savedWorkingMemo"));
onSuccess();
} catch (ex) {
if (ex instanceof Error) {
const { message } = ex;
if (message === "No permission to mutate.") {
notifyError(t("errors.noPermission"));
return;
}
if (message === "No data returned") {
notifyError(t("errors.mutationNoDataReturned"));
return;
}
}
notifyError(t("RequestedReservation.errorSavingWorkingMemo"));
}
};
Expand Down Expand Up @@ -77,27 +115,23 @@ export function ReservationWorkingMemo({
refetch: () => void;
initialValue: string;
}) {
const { t } = useTranslation();
const { notifyError, notifySuccess } = useNotification();
const [updateWorkingMemo] = useMutation<
Mutation,
ReservationWorkingMemoMutationInput
>(UPDATE_RESERVATION_WORKING_MEMO, {
onCompleted: () => {
refetch();
notifySuccess(t("RequestedReservation.savedWorkingMemo"));
},
onError: () => {
notifyError(t("RequestedReservation.errorSavingWorkingMemo"));
},
});
>(UPDATE_RESERVATION_WORKING_MEMO);

const updateMemo = (memo: string) =>
updateWorkingMemo({
variables: { pk: reservationPk, workingMemo: memo },
});

return <WorkingMemo onMutate={updateMemo} initialValue={initialValue} />;
return (
<WorkingMemo
onMutate={updateMemo}
initialValue={initialValue}
onSuccess={refetch}
/>
);
}

export function ApplicationWorkingMemo({
Expand All @@ -109,25 +143,21 @@ export function ApplicationWorkingMemo({
refetch: () => void;
initialValue: string;
}) {
const { t } = useTranslation();
const { notifyError, notifySuccess } = useNotification();
const [updateWorkingMemo] = useMutation<
Mutation,
ApplicationUpdateMutationInput
>(UPDATE_APPLICATION_WORKING_MEMO, {
onCompleted: () => {
refetch();
notifySuccess(t("RequestedReservation.savedWorkingMemo"));
},
onError: () => {
notifyError(t("RequestedReservation.errorSavingWorkingMemo"));
},
});
>(UPDATE_APPLICATION_WORKING_MEMO);

const updateMemo = (memo: string) =>
updateWorkingMemo({
variables: { pk: applicationPk, workingMemo: memo },
});

return <WorkingMemo onMutate={updateMemo} initialValue={initialValue} />;
return (
<WorkingMemo
onMutate={updateMemo}
initialValue={initialValue}
onSuccess={refetch}
/>
);
}
1 change: 1 addition & 0 deletions apps/admin-ui/src/i18n/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ const translations: ITranslations = {
"Virhe. Varaus tehty, mutta sen näyttäminen epäonnistui.",
],
noPermission: ["Sinulla ei ole käyttöoikeutta."],
mutationNoDataReturned: ["Odottamaton vastaus."],
descriptive: {
"Reservation overlaps with reservation before due to buffer time.": [
"Varaus menee päällekkäin edellisen varauksen kanssa tauon takia.",
Expand Down

0 comments on commit e5ab99f

Please sign in to comment.