Skip to content

Commit

Permalink
Change returns from useReservations to useLoans
Browse files Browse the repository at this point in the history
Now we return loans/reservations for both FBS and Publizon.
  • Loading branch information
kasperbirch1 committed Nov 7, 2023
1 parent de7f314 commit fb15984
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 47 deletions.
63 changes: 25 additions & 38 deletions src/apps/loan-list/list/loan-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,44 +58,32 @@ const LoanList: FC<LoanListProps> = ({ pageSize }) => {
const [modalDetailsId, setModalDetailsId] = useState<string | null>(null);
const {
fbs: {
sortedByDate: loansSortedByDateFbs,
loans: loansPhysical,
stackedMaterialsDueDates: stackedMaterialsDueDatesFbs
},
publizon: { sortedByDate: loansSortedByDatePublizon }
publizon: { loans: loansDigital }
} = useLoans();

useEffect(() => {
// If modalLoan is already set it should not be set again, because it will cause an infinite loop
if (modalLoan) {
return;
}
let loanForModal = null;
if (loansSortedByDateFbs && modalDetailsId) {
loanForModal = getFromListByKey(
loansSortedByDateFbs,
"loanId",
modalDetailsId
);
if (loansPhysical && modalDetailsId) {
loanForModal = getFromListByKey(loansPhysical, "loanId", modalDetailsId);
}
if (
loanForModal?.length === 0 &&
loansSortedByDatePublizon &&
modalDetailsId
) {
if (loanForModal?.length === 0 && loansDigital && modalDetailsId) {
loanForModal = getFromListByKey(
loansSortedByDatePublizon,
loansDigital,
"identifier",
modalDetailsId
);
}
if (loanForModal && loanForModal.length > 0) {
setModalLoan(loanForModal[0]);
}
}, [
modalDetailsId,
modalLoan,
loansSortedByDateFbs,
loansSortedByDatePublizon
]);
}, [modalDetailsId, modalLoan, loansPhysical, loansDigital]);

const openAcceptModal = useCallback(() => {
open(`${acceptModal}`);
Expand Down Expand Up @@ -144,9 +132,8 @@ const LoanList: FC<LoanListProps> = ({ pageSize }) => {
}, [allLoansId, loanDetails, openDueDateModal]);

const listContainsLoans =
(Array.isArray(loansSortedByDateFbs) && loansSortedByDateFbs.length > 0) ||
(Array.isArray(loansSortedByDatePublizon) &&
loansSortedByDatePublizon.length > 0);
(Array.isArray(loansPhysical) && loansPhysical.length > 0) ||
(Array.isArray(loansDigital) && loansDigital.length > 0);

const resetAccepted = () => {
setAccepted(false);
Expand All @@ -158,55 +145,55 @@ const LoanList: FC<LoanListProps> = ({ pageSize }) => {
<h1 className="text-header-h1 my-32">{t("loanListTitleText")}</h1>
{listContainsLoans && (
<>
{loansSortedByDateFbs && (
{loansPhysical && (
<List
pageSize={pageSize}
emptyListLabel={t("loanListPhysicalLoansEmptyListText")}
loans={loansSortedByDateFbs}
loans={loansPhysical}
dueDates={stackedMaterialsDueDatesFbs}
view={view}
openLoanDetailsModal={openLoanDetailsModal}
openDueDateModal={openDueDateModal}
>
<ListHeader
header={t("loanListPhysicalLoansTitleText")}
amount={loansSortedByDateFbs.length}
amount={loansPhysical.length}
>
<ToggleListViewButtons
disableRenewLoansButton={
getAmountOfRenewableLoans(loansSortedByDateFbs) === 0
getAmountOfRenewableLoans(loansPhysical) === 0
}
view={view}
setView={setView}
loans={loansSortedByDateFbs}
loans={loansPhysical}
pageSize={pageSize}
openRenewLoansModal={openRenewLoansModal}
/>
</ListHeader>
</List>
)}
{loansSortedByDatePublizon && (
{loansDigital && (
<List
pageSize={pageSize}
emptyListLabel={t("loanListDigitalLoansEmptyListText")}
loans={loansSortedByDatePublizon}
loans={loansDigital}
view="list"
openLoanDetailsModal={openLoanDetailsModal}
openDueDateModal={openDueDateModal}
>
<ListHeader
header={t("loanListDigitalLoansTitleText")}
amount={loansSortedByDatePublizon.length}
amount={loansDigital.length}
/>
</List>
)}
</>
)}

{loansSortedByDateFbs &&
loansSortedByDatePublizon &&
loansAreEmpty(loansSortedByDateFbs) &&
loansAreEmpty(loansSortedByDatePublizon) && (
{loansPhysical &&
loansDigital &&
loansAreEmpty(loansPhysical) &&
loansAreEmpty(loansDigital) && (
<EmptyList
classNames="mt-24"
emptyListText={t("loanListDigitalPhysicalLoansEmptyListText")}
Expand All @@ -227,7 +214,7 @@ const LoanList: FC<LoanListProps> = ({ pageSize }) => {
modalId={constructMaterialDetailsModalId(loanDetails, modalDetailsId)}
/>
</MaterialDetailsModal>
{loansSortedByDateFbs && (
{loansPhysical && (
<LoansGroupModal
accepted={accepted}
resetAccepted={() => resetAccepted()}
Expand All @@ -237,8 +224,8 @@ const LoanList: FC<LoanListProps> = ({ pageSize }) => {
openAcceptModal={openAcceptModal}
loansModal={
dueDate
? removeLoansWithDuplicateDueDate(dueDate, loansSortedByDateFbs)
: loansSortedByDateFbs
? removeLoansWithDuplicateDueDate(dueDate, loansPhysical)
: loansPhysical
}
>
{dueDate && (
Expand Down
10 changes: 4 additions & 6 deletions src/core/utils/useLoans.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,17 @@ const sortByDueDate = (list: LoanType[]) => {
};

type Loans = {
loans: LoanType[];
overdue: LoanType[];
soonOverdue: LoanType[];
farFromOverdue: LoanType[];
isLoading: boolean;
isError: boolean;
sortedByDate?: LoanType[];
stackedMaterialsDueDates?: string[];
};

type UseLoansType = {
all: Loans & {
loans: LoanType[];
};
all: Loans;
fbs: Loans;
publizon: Loans;
};
Expand Down Expand Up @@ -157,19 +155,19 @@ const useLoans: UseLoans = () => {
isError: loansIsError
},
fbs: {
loans: loansSortedByDateFbs,
overdue: loansOverdueFBS,
soonOverdue: loansSoonOverdueFBS,
farFromOverdue: loansFarFromOverdueFBS,
sortedByDate: loansSortedByDateFbs,
stackedMaterialsDueDates: stackedMaterialsDueDatesFbs,
isLoading: isLoadingFbs,
isError: isErrorFbs
},
publizon: {
loans: loansSortedByDatePublizon,
overdue: LoansOverduePublizon,
soonOverdue: loansSoonOverduePublizon,
farFromOverdue: loansFarFromOverduePublizon,
sortedByDate: loansSortedByDatePublizon,
isLoading: isLoadingPublizon,
isError: isErrorPublizon
}
Expand Down
7 changes: 4 additions & 3 deletions src/core/utils/useReservations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@ const getQueuedReservations = (list: ReservationType[]) => {
};

type Reservations = {
reservations: ReservationType[];
readyToLoan: ReservationType[];
queued: ReservationType[];
isLoading: boolean;
isError: boolean;
};

type UseReservationsType = {
all: Reservations & {
reservations: ReservationType[];
};
all: Reservations;
fbs: Reservations;
publizon: Reservations;
};
Expand Down Expand Up @@ -89,12 +88,14 @@ const useReservations: UseReservations = () => {
isError: reservationsIsError
},
fbs: {
reservations: mappedReservationsFbs,
readyToLoan: reservationsReadyToLoanFBS,
queued: reservationsQueuedFBS,
isLoading: isLoadingFbs,
isError: isErrorFbs
},
publizon: {
reservations: mappedReservationsPublizon,
readyToLoan: reservationsReadyToLoanPublizon,
queued: reservationsQueuedPublizon,
isLoading: isLoadingPublizon,
Expand Down

0 comments on commit fb15984

Please sign in to comment.