Skip to content

Commit

Permalink
Refactor saveReservation (openOrder)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
kasperbirch1 committed Dec 22, 2023
1 parent 862f61f commit f41d7d9
Showing 1 changed file with 29 additions and 33 deletions.
62 changes: 29 additions & 33 deletions src/components/reservation/ReservationModalBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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: {
Expand All @@ -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;
Expand Down

0 comments on commit f41d7d9

Please sign in to comment.