Skip to content

Commit

Permalink
Refactoring thresholdConfig => expirationWarningDaysBeforeConfig
Browse files Browse the repository at this point in the history
And introducing the useLoanThresholds hook to centralise the concept.

Also:
The overloading of the useConfig types needed to support that you do not
use any options/parsers, in case you want a single value unprocessed.
  • Loading branch information
spaceo committed Nov 10, 2023
1 parent 67257ce commit 16a8b3d
Show file tree
Hide file tree
Showing 17 changed files with 60 additions and 70 deletions.
6 changes: 2 additions & 4 deletions src/apps/dashboard/dashboard.dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,8 @@ export default {
defaultValue: "Borrow before @date",
control: { type: "text" }
},
// Config
thresholdConfig: {
defaultValue:
'{\n "colorThresholds":{\n "danger":"0",\n "warning":"6"\n }\n }',
expirationWarningDaysBeforeConfig: {
defaultValue: "6",
control: { type: "text" }
}
},
Expand Down
2 changes: 1 addition & 1 deletion src/apps/dashboard/dashboard.entry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface DashBoardProps {
// Config
blacklistedPickupBranchesConfig: string;
branchesConfig: string;
thresholdConfig: string;
expirationWarningDaysBeforeConfig: string;
// Texts
yourProfileText: string;
feesText: string;
Expand Down
5 changes: 2 additions & 3 deletions src/apps/fee-list/FeeList.dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,8 @@ export default {
defaultValue: "@amount,-",
control: { type: "text" }
},
thresholdConfig: {
defaultValue:
'{\n "colorThresholds":{\n "danger":"0",\n "warning":"6"\n }\n }',
expirationWarningDaysBeforeConfig: {
defaultValue: "6",
control: { type: "text" }
},
iAcceptText: {
Expand Down
2 changes: 1 addition & 1 deletion src/apps/fee-list/FeeList.entry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import GlobalUrlEntryPropsInterface from "../../core/utils/types/global-url-prop
import { withConfig } from "../../core/utils/config";

export interface IntermedateListEntryConfigProps {
thresholdConfig: string;
expirationWarningDaysBeforeConfig: string;
}

export interface FeeListProps {
Expand Down
5 changes: 2 additions & 3 deletions src/apps/loan-list/list/loan-list.dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ export default {
control: { type: "number" }
},
// Config
thresholdConfig: {
defaultValue:
'{\n "colorThresholds":{\n "danger":"0",\n "warning":"6"\n }\n }',
expirationWarningDaysBeforeConfig: {
defaultValue: "6",
control: { type: "text" }
},
// Texts
Expand Down
4 changes: 2 additions & 2 deletions src/apps/loan-list/list/loan-list.entry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import { MaterialDetailsModalProps } from "../../../core/storybook/materialDetai
import { AcceptFeesModalEntryTextProps } from "../../../core/storybook/acceptFeesModalArgs";

export interface LoanListEntryConfigProps {
thresholdConfig: string;
expirationWarningDaysBeforeConfig: string;
}
export interface LoanListEntryUrlProps {
materialOverdueUrl: string;
thresholdConfig: string;
expirationWarningDaysBeforeConfig: string;
}

interface LoanListEntryTextProps {
Expand Down
23 changes: 7 additions & 16 deletions src/apps/loan-list/materials/utils/status-badge.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { FC } from "react";
import { ThresholdType } from "../../../../core/utils/types/threshold-type";
import { useConfig } from "../../../../core/utils/config";
import { daysBetweenTodayAndDate } from "../../../../core/utils/helpers/general";
import useLoanThresholds from "../../../../core/utils/useLoanThresholds";

interface StatusBadgeProps {
badgeDate?: string | null;
Expand All @@ -20,26 +19,18 @@ const StatusBadge: FC<StatusBadgeProps> = ({
infoText,
neutralText
}) => {
const config = useConfig();
const threshold = useLoanThresholds();
const daysBetweenTodayAndDue = badgeDate
? daysBetweenTodayAndDate(badgeDate)
: 0;

const {
colorThresholds: { danger, warning }
} = config<ThresholdType>("thresholdConfig", {
transformer: "jsonParse"
});

let daysBetweenTodayAndDue = 0;
if (badgeDate) {
daysBetweenTodayAndDue = daysBetweenTodayAndDate(badgeDate);
}

if (daysBetweenTodayAndDue < Number(danger) && dangerText) {
if (daysBetweenTodayAndDue < threshold.danger && dangerText) {
return (
<div className="status-label status-label--danger">{dangerText}</div>
);
}

if (daysBetweenTodayAndDue <= Number(warning) && warningText) {
if (daysBetweenTodayAndDue <= threshold.warning && warningText) {
return (
<div className="status-label status-label--warning">{warningText}</div>
);
Expand Down
15 changes: 5 additions & 10 deletions src/apps/loan-list/materials/utils/status-circle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import {
daysBetweenDates
} from "../../../../core/utils/helpers/general";
import { useText } from "../../../../core/utils/text";
import { useConfig } from "../../../../core/utils/config";
import { ThresholdType } from "../../../../core/utils/types/threshold-type";
import useLoanThresholds from "../../../../core/utils/useLoanThresholds";

interface StatusCircleProps {
dueDate?: string | null;
Expand All @@ -17,13 +16,9 @@ interface StatusCircleProps {

const StatusCircle: FC<StatusCircleProps> = ({ loanDate, dueDate }) => {
const t = useText();
const config = useConfig();
const colors = getColors();
const {
colorThresholds: { danger, warning }
} = config<ThresholdType>("thresholdConfig", {
transformer: "jsonParse"
});
const threshold = useLoanThresholds();

let color = colors.default;
let percent = 100;
let daysBetweenTodayAndDue = null;
Expand All @@ -36,9 +31,9 @@ const StatusCircle: FC<StatusCircleProps> = ({ loanDate, dueDate }) => {
if (percent < 0) {
percent = 100;
}
if (daysBetweenTodayAndDue < danger) {
if (daysBetweenTodayAndDue < threshold.danger) {
color = colors.danger;
} else if (daysBetweenTodayAndDue <= warning) {
} else if (daysBetweenTodayAndDue <= threshold.warning) {
color = colors.warning;
}
} else {
Expand Down
5 changes: 2 additions & 3 deletions src/apps/menu/menu.dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,8 @@ export default {
defaultValue: "/Signup",
control: { type: "text" }
},
thresholdConfig: {
defaultValue:
'{\n "colorThresholds":{\n "danger":"0",\n "warning":"6"\n }\n }',
expirationWarningDaysBeforeConfig: {
defaultValue: "6",
control: { type: "text" }
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/apps/menu/menu.entry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface MenuProps {
loansSoonOverdueText: string;
loansOverdueText: string;
logoutUrl: string;
thresholdConfig: string;
expirationWarningDaysBeforeConfig: string;
feeListDaysText: string;
menuLoginText: string;
menuLoginUrl: string;
Expand Down
5 changes: 2 additions & 3 deletions src/apps/reservation-list/list/reservation-list.dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ export default {
defaultValue: "FBS-751032,FBS-751031,FBS-751009,FBS-751027,FBS-751024",
control: { type: "text" }
},
thresholdConfig: {
defaultValue:
'{\n "colorThresholds":{\n "danger":"0",\n "warning":"6"\n }\n }',
expirationWarningDaysBeforeConfig: {
defaultValue: "6",
control: { type: "text" }
},
pauseReservationStartDateConfig: {
Expand Down
4 changes: 2 additions & 2 deletions src/apps/reservation-list/list/reservation-list.entry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import { ReservationMaterialDetailsProps } from "../../../core/storybook/reserva
import { DeleteReservationModalArgs } from "../../../core/storybook/deleteReservationModalArgs";

export interface ReservationListUrlProps {
thresholdConfig: string;
expirationWarningDaysBeforeConfig: string;
ereolenMyPageUrl: string;
pauseReservationInfoUrl: string;
}

export interface ReservationListConfigProps {
thresholdConfig: string;
expirationWarningDaysBeforeConfig: string;
pauseReservationStartDateConfig: string;
blacklistedPickupBranchesConfig: string;
branchesConfig: string;
Expand Down
5 changes: 2 additions & 3 deletions src/core/storybook/reservationListArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ export default {
defaultValue: "FBS-751032,FBS-751031,FBS-751009,FBS-751027,FBS-751024",
control: { type: "text" }
},
thresholdConfig: {
defaultValue:
'{\n "colorThresholds":{\n "danger":"0",\n "warning":"6"\n }\n }',
expirationWarningDaysBeforeConfig: {
defaultValue: "6",
control: { type: "text" }
},
pauseReservationStartDateConfig: {
Expand Down
3 changes: 3 additions & 0 deletions src/core/utils/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ function config(
transformer: "stringToArray";
}
): string[];

function config<T>(key: string): T;

function config<T>(): T | string | string[] {
return [];
}
Expand Down
6 changes: 0 additions & 6 deletions src/core/utils/types/threshold-type.ts

This file was deleted.

14 changes: 14 additions & 0 deletions src/core/utils/useLoanThresholds.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useConfig } from "./config";

type Days = number;

const useLoanThresholds = (): { warning: Days; danger: Days } => {
const config = useConfig();

return {
warning: Number(config<`${number}`>("expirationWarningDaysBeforeConfig")),
danger: 0
};
};

export default useLoanThresholds;
24 changes: 12 additions & 12 deletions src/core/utils/useLoans.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { useGetLoansV2 } from "../fbs/fbs";
import { useGetV1UserLoans } from "../publizon/publizon";
import { useConfig } from "./config";
import { daysBetweenTodayAndDate, materialIsOverdue } from "./helpers/general";
import {
mapFBSLoanToLoanType,
mapPublizonLoanToLoanType
} from "./helpers/list-mapper";
import { LoanType } from "./types/loan-type";
import { ThresholdType } from "./types/threshold-type";
import useLoanThresholds from "./useLoanThresholds";

// Loans with more than warning-threshold days until due
const filterLoansNotOverdue = (loans: LoanType[], warning: number) => {
Expand Down Expand Up @@ -75,7 +74,6 @@ type UseLoansType = {
type UseLoans = () => UseLoansType;

const useLoans: UseLoans = () => {
const config = useConfig();
const {
data: loansFbs,
isLoading: isLoadingFbs,
Expand All @@ -86,12 +84,8 @@ const useLoans: UseLoans = () => {
isLoading: isLoadingPublizon,
isError: isErrorPublizon
} = useGetV1UserLoans();
const {
colorThresholds: { warning }
} = config<ThresholdType>("thresholdConfig", {
transformer: "jsonParse"
});

const threshold = useLoanThresholds();
const loansIsLoading = isLoadingFbs || isLoadingPublizon;
const loansIsError = isErrorFbs || isErrorPublizon;

Expand All @@ -116,21 +110,27 @@ const useLoans: UseLoans = () => {
]);

// combine "soon overdue" loans from both FBS and Publizon
const loansSoonOverdueFBS = filterLoansSoonOverdue(mappedLoansFbs, warning);
const loansSoonOverdueFBS = filterLoansSoonOverdue(
mappedLoansFbs,
threshold.warning
);
const loansSoonOverduePublizon = filterLoansSoonOverdue(
mappedLoansPublizon,
warning
threshold.warning
);
const loansSoonOverdue = sortByDueDate([
...loansSoonOverdueFBS,
...loansSoonOverduePublizon
]);

// combine "far from overdue" loans from both FBS and Publizon
const loansFarFromOverdueFBS = filterLoansNotOverdue(mappedLoansFbs, warning);
const loansFarFromOverdueFBS = filterLoansNotOverdue(
mappedLoansFbs,
threshold.warning
);
const loansFarFromOverduePublizon = filterLoansNotOverdue(
mappedLoansPublizon,
warning
threshold.warning
);
const loansFarFromOverdue = sortByDueDate([
...loansFarFromOverdueFBS,
Expand Down

0 comments on commit 16a8b3d

Please sign in to comment.