From f41d7d94de34df29b723bda9b2525750e0ba3d28 Mon Sep 17 00:00:00 2001 From: Kasper Birch Date: Fri, 22 Dec 2023 12:00:50 +0100 Subject: [PATCH] Refactor `saveReservation` (openOrder) Due to the `UseReservableManifestations` hook validating whether a manifestation is reservable, we cannot use the `reservableManifestations` variable it returns in the new `useReservableFromAnotherLibrary` hook. Therefore, I have used the `selectedManifestations` variable within `useReservableFromAnotherLibrary`. Additionally, I have implemented minor changes within `saveReservation`. Specifically, I removed the guard check for an early return. Now, it directly checks for the required criteria to determine whether to use `mutateAddReservations` or `mutateOpenOrder`. --- .../reservation/ReservationModalBody.tsx | 62 +++++++++---------- 1 file changed, 29 insertions(+), 33 deletions(-) diff --git a/src/components/reservation/ReservationModalBody.tsx b/src/components/reservation/ReservationModalBody.tsx index 5218022235..5b2db7aff1 100644 --- a/src/components/reservation/ReservationModalBody.tsx +++ b/src/components/reservation/ReservationModalBody.tsx @@ -129,7 +129,7 @@ export const ReservationModalBody = ({ ); const reservablePidsFromAnotherLibrary = useReservableFromAnotherLibrary( - manifestationsToReserve + selectedManifestations ); // If we don't have all data for displaying the view render nothing. @@ -149,13 +149,37 @@ export const ReservationModalBody = ({ : null; const saveReservation = () => { - if (!manifestationsToReserve || manifestationsToReserve.length < 1) { - return; + if (manifestationsToReserve?.length) { + // Save reservation to FBS. + mutateAddReservations( + { + data: constructReservationData({ + manifestations: manifestationsToReserve, + selectedBranch, + expiryDate, + periodical: selectedPeriodical + }) + }, + { + onSuccess: (res) => { + // Track only if the reservation has been successfully saved. + track("click", { + id: statistics.reservation.id, + name: statistics.reservation.name, + trackedData: work.workId + }); + // This state is used to show the success or error modal. + setReservationResponse(res); + // Because after a successful reservation the holdings (reservations) are updated. + queryClient.invalidateQueries(getGetHoldingsV3QueryKey()); + } + } + ); } - if (reservablePidsFromAnotherLibrary.length > 0 && patron) { + if (reservablePidsFromAnotherLibrary?.length && patron) { const { patronId, name, emailAddress, preferredPickupBranch } = patron; - + // Save reservation to open order. mutateOpenOrder( { input: { @@ -179,35 +203,7 @@ export const ReservationModalBody = ({ } } ); - - return; } - - // Save reservation to FBS. - mutateAddReservations( - { - data: constructReservationData({ - manifestations: manifestationsToReserve, - selectedBranch, - expiryDate, - periodical: selectedPeriodical - }) - }, - { - onSuccess: (res) => { - // Track only if the reservation has been successfully saved. - track("click", { - id: statistics.reservation.id, - name: statistics.reservation.name, - trackedData: work.workId - }); - // This state is used to show the success or error modal. - setReservationResponse(res); - // Because after a successful reservation the holdings (reservations) are updated. - queryClient.invalidateQueries(getGetHoldingsV3QueryKey()); - } - } - ); }; const reservationSuccess = reservationResponse?.success || false;