From f314c6e3ec582ad0a6ec86c042e9ed5af88ca9e9 Mon Sep 17 00:00:00 2001 From: Kasper Birch Date: Mon, 6 Nov 2023 15:02:29 +0100 Subject: [PATCH] Refactor return structure for `useReservations` for improved readability --- .../dashboard-notification-list.tsx | 5 ++- .../modal/ReservationsGroupModal.tsx | 15 +++---- .../menu-logged-in/MenuLoggedInContent.tsx | 4 +- src/core/utils/useReservations.tsx | 45 ++++++++++++++----- 4 files changed, 46 insertions(+), 23 deletions(-) diff --git a/src/apps/dashboard/dashboard-notification-list/dashboard-notification-list.tsx b/src/apps/dashboard/dashboard-notification-list/dashboard-notification-list.tsx index f62c4ce13e..65781b7d9f 100644 --- a/src/apps/dashboard/dashboard-notification-list/dashboard-notification-list.tsx +++ b/src/apps/dashboard/dashboard-notification-list/dashboard-notification-list.tsx @@ -33,8 +33,9 @@ const DashboardNotificationList: FC = ({ columns }) => { const t = useText(); - const { reservations, reservationsReadyToLoan, reservationsQueued } = - useReservations(); + const { + all: { reservations, reservationsReadyToLoan, reservationsQueued } + } = useReservations(); const { loans, loansOverdue, loansSoonOverdue, loansFarFromOverdue } = useLoans(); const [accepted, setAccepted] = useState(false); diff --git a/src/apps/dashboard/modal/ReservationsGroupModal.tsx b/src/apps/dashboard/modal/ReservationsGroupModal.tsx index a4cd1b7efc..c262347065 100644 --- a/src/apps/dashboard/modal/ReservationsGroupModal.tsx +++ b/src/apps/dashboard/modal/ReservationsGroupModal.tsx @@ -24,12 +24,7 @@ const ReservationGroupModal: FC = ({ setReservationsToDelete, openDetailsModal }) => { - const { - reservationsReadyToLoanFBS, - reservationsReadyToLoanPublizon, - reservationsQueuedFBS, - reservationsQueuedPublizon - } = useReservations(); + const { fbs, publizon } = useReservations(); const t = useText(); const { reservationsReady, reservationsQueued } = getModalIds(); const [materialsToDelete, setMaterialsToDelete] = useState([]); @@ -38,13 +33,13 @@ const ReservationGroupModal: FC = ({ let digitalReservations: ReservationType[] = []; if (modalId === reservationsReady) { - physicalReservations = reservationsReadyToLoanFBS; - digitalReservations = reservationsReadyToLoanPublizon; + physicalReservations = fbs.readyToLoan; + digitalReservations = publizon.readyToLoan; } if (modalId === reservationsQueued) { - physicalReservations = reservationsQueuedFBS; - digitalReservations = reservationsQueuedPublizon; + physicalReservations = fbs.queued; + digitalReservations = publizon.queued; } useEffect(() => { diff --git a/src/apps/menu/menu-logged-in/MenuLoggedInContent.tsx b/src/apps/menu/menu-logged-in/MenuLoggedInContent.tsx index cddb6efce7..8cc5d5e8a8 100644 --- a/src/apps/menu/menu-logged-in/MenuLoggedInContent.tsx +++ b/src/apps/menu/menu-logged-in/MenuLoggedInContent.tsx @@ -19,7 +19,9 @@ interface MenuLoggedInContentProps { } const MenuLoggedInContent: FC = ({ pageSize }) => { - const { reservations } = useReservations(); + const { + all: { reservations } + } = useReservations(); const { loans, loansOverdue, loansSoonOverdue } = useLoans(); const { data: patronData } = usePatronData(); const { data: fbsFees } = useGetFeesV2(); diff --git a/src/core/utils/useReservations.tsx b/src/core/utils/useReservations.tsx index 99cf186a12..767a658a54 100644 --- a/src/core/utils/useReservations.tsx +++ b/src/core/utils/useReservations.tsx @@ -6,8 +6,29 @@ import { mapPublizonReservationToReservationType } from "./helpers/list-mapper"; import { getReadyForPickup } from "../../apps/reservation-list/utils/helpers"; +import { ReservationType } from "./types/reservation-type"; -const useReservations = () => { +type ProviderReservations = { + readyToLoan: ReservationType[]; + queued: ReservationType[]; + isLoading: boolean; +}; + +type UseReservationsType = { + all: { + reservations: ReservationType[]; + reservationsReadyToLoan: ReservationType[]; + reservationsQueued: ReservationType[]; + }; + fbs: ProviderReservations; + publizon: ProviderReservations; + isLoading: boolean; + isError: boolean; +}; + +type UseReservations = () => UseReservationsType; + +const useReservations: UseReservations = () => { const { data: reservationsFbs, isLoading: isLoadingFbs, @@ -57,15 +78,19 @@ const useReservations = () => { ]; return { - reservations, - reservationsReadyToLoanFBS, - reservationsReadyToLoanPublizon, - reservationsReadyToLoan, - reservationsQueued, - reservationsQueuedFBS, - reservationsQueuedPublizon, - reservationsIsLoading, - reservationsIsError + all: { reservations, reservationsReadyToLoan, reservationsQueued }, + fbs: { + readyToLoan: reservationsReadyToLoanFBS, + queued: reservationsQueuedFBS, + isLoading: isLoadingFbs + }, + publizon: { + readyToLoan: reservationsReadyToLoanPublizon, + queued: reservationsQueuedPublizon, + isLoading: isLoadingPublizon + }, + isLoading: reservationsIsLoading, + isError: reservationsIsError }; };