Skip to content

Commit

Permalink
Refactor return structure for useReservations for improved readability
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperbirch1 committed Nov 6, 2023
1 parent 6ef8e08 commit f314c6e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ const DashboardNotificationList: FC<DashboardNotificationListProps> = ({
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<boolean>(false);
Expand Down
15 changes: 5 additions & 10 deletions src/apps/dashboard/modal/ReservationsGroupModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@ const ReservationGroupModal: FC<ReservationGroupModalProps> = ({
setReservationsToDelete,
openDetailsModal
}) => {
const {
reservationsReadyToLoanFBS,
reservationsReadyToLoanPublizon,
reservationsQueuedFBS,
reservationsQueuedPublizon
} = useReservations();
const { fbs, publizon } = useReservations();
const t = useText();
const { reservationsReady, reservationsQueued } = getModalIds();
const [materialsToDelete, setMaterialsToDelete] = useState<string[]>([]);
Expand All @@ -38,13 +33,13 @@ const ReservationGroupModal: FC<ReservationGroupModalProps> = ({
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(() => {
Expand Down
4 changes: 3 additions & 1 deletion src/apps/menu/menu-logged-in/MenuLoggedInContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ interface MenuLoggedInContentProps {
}

const MenuLoggedInContent: FC<MenuLoggedInContentProps> = ({ pageSize }) => {
const { reservations } = useReservations();
const {
all: { reservations }
} = useReservations();
const { loans, loansOverdue, loansSoonOverdue } = useLoans();
const { data: patronData } = usePatronData();
const { data: fbsFees } = useGetFeesV2();
Expand Down
45 changes: 35 additions & 10 deletions src/core/utils/useReservations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
};
};

Expand Down

0 comments on commit f314c6e

Please sign in to comment.