Skip to content

Commit 1566b54

Browse files
authored
Merge pull request #6214 from bcgov/chore/6168
refactor(6153): reorganize cost component structure
2 parents df351f5 + 3154957 commit 1566b54

File tree

16 files changed

+299
-217
lines changed

16 files changed

+299
-217
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import { Select, Switch, Tooltip } from '@mantine/core';
2+
import { MonthPickerInput, YearPickerInput } from '@mantine/dates';
3+
import { useQuery } from '@tanstack/react-query';
4+
import { format } from 'date-fns';
5+
import { useEffect } from 'react';
6+
import { getMonthlyCosts, getQuarterlyCosts, getYearlyCosts } from '@/services/backend/private-cloud/products';
7+
import { PeriodCosts, CostPeriod } from '@/types/private-cloud';
8+
import { formatCurrency } from '@/utils/js';
9+
import { formatAsYearQuarter, getDateFromYyyyMmDd } from '@/utils/js';
10+
import { useCostState } from './state';
11+
12+
const inputClasses = 'border-gray-600 focus:border-gray-800 dark:border-gray-500 dark:focus:border-gray-300';
13+
14+
const switchStyles = (enabled: boolean) => ({
15+
track: {
16+
cursor: 'pointer',
17+
backgroundColor: enabled ? '#0D5EA6' : undefined,
18+
},
19+
});
20+
21+
export default function PeriodSelector({ licencePlate }: { licencePlate: string }) {
22+
const [state, snap] = useCostState();
23+
24+
const { data, isLoading } = useQuery({
25+
queryKey: ['costItems', licencePlate, snap.period, snap.selectedDate],
26+
queryFn: () => {
27+
console.log('here');
28+
if (snap.period === CostPeriod.Monthly) {
29+
return getMonthlyCosts(licencePlate, format(snap.selectedDate, 'yyyy-MM'));
30+
}
31+
32+
if (snap.period === CostPeriod.Quarterly) {
33+
return getQuarterlyCosts(licencePlate, formatAsYearQuarter(snap.selectedDate));
34+
}
35+
36+
return getYearlyCosts(licencePlate, snap.selectedDate.getFullYear().toString());
37+
},
38+
enabled: !!licencePlate && !!snap.selectedDate,
39+
});
40+
41+
useEffect(() => {
42+
if (data) state.data = data;
43+
}, [data]);
44+
45+
const isMonthPicker = snap.period === CostPeriod.Monthly || snap.period === CostPeriod.Quarterly;
46+
47+
const handlePeriodChange = (value: string | null) => (state.period = value as CostPeriod);
48+
const handleDateChange = (value: string | null) =>
49+
(state.selectedDate = value ? getDateFromYyyyMmDd(value) : new Date());
50+
51+
return (
52+
<div className="space-y-4">
53+
<div className="flex px-8 bg-zinc-100 items-center justify-between w-full border-b">
54+
<div className="flex items-center gap-4 py-7">
55+
<Tooltip label="Select Mode">
56+
<Select
57+
placeholder="Select Mode"
58+
value={snap.period}
59+
data={Object.values(CostPeriod)}
60+
onChange={handlePeriodChange}
61+
classNames={{ input: inputClasses }}
62+
/>
63+
</Tooltip>
64+
65+
<Tooltip label={`Select a ${isMonthPicker ? 'month' : 'year'}`}>
66+
{isMonthPicker ? (
67+
<MonthPickerInput
68+
placeholder="Select a month"
69+
maw={200}
70+
value={snap.selectedDate}
71+
onChange={handleDateChange}
72+
classNames={{ input: inputClasses }}
73+
/>
74+
) : (
75+
<YearPickerInput
76+
placeholder="Select a year"
77+
maw={200}
78+
value={snap.selectedDate}
79+
onChange={handleDateChange}
80+
classNames={{ input: inputClasses }}
81+
/>
82+
)}
83+
</Tooltip>
84+
</div>
85+
{/*
86+
<Switch
87+
label="Forecast"
88+
checked={forecastEnabled}
89+
onChange={(event) => onForecastChange(event.currentTarget.checked)}
90+
classNames={{
91+
label: 'cursor-pointer',
92+
thumb: 'cursor-pointer',
93+
}}
94+
styles={switchStyles(forecastEnabled)}
95+
/> */}
96+
</div>
97+
</div>
98+
);
99+
}

app/app/private-cloud/products/(product)/[licencePlate]/costs/DateSelector.tsx renamed to app/app/private-cloud/products/(product)/[licencePlate]/costs/PeriodSelector_tobedeleted.tsx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Select, Switch, Tooltip } from '@mantine/core';
22
import { MonthPickerInput, YearPickerInput } from '@mantine/dates';
33
import { format } from 'date-fns';
4-
import { PeriodCosts, TimeView } from '@/types/private-cloud';
4+
import { PeriodCosts, CostPeriod } from '@/types/private-cloud';
55
import { formatCurrency } from '@/utils/js';
66

77
const inputClasses = 'border-gray-600 focus:border-gray-800 dark:border-gray-500 dark:focus:border-gray-300';
@@ -13,7 +13,7 @@ const switchStyles = (enabled: boolean) => ({
1313
},
1414
});
1515

16-
export default function DateSelector({
16+
export default function PeriodSelector({
1717
selectedDate,
1818
handleDateChange,
1919
viewMode,
@@ -26,21 +26,21 @@ export default function DateSelector({
2626
}: {
2727
selectedDate: Date;
2828
handleDateChange: (date: string | null) => void;
29-
viewMode: TimeView;
30-
onModeChange: (mode: TimeView) => void;
29+
viewMode: CostPeriod;
30+
onModeChange: (mode: CostPeriod) => void;
3131
onForecastChange: (enabled: boolean) => void;
3232
forecastEnabled: boolean;
3333
showForecastSwitch?: boolean;
3434
showTable?: boolean;
3535
data: PeriodCosts;
3636
}) {
3737
const handleModeChange = (value: string | null) => {
38-
if (value && Object.values(TimeView).includes(value as TimeView)) {
39-
onModeChange(value as TimeView);
38+
if (value && Object.values(CostPeriod).includes(value as CostPeriod)) {
39+
onModeChange(value as CostPeriod);
4040
}
4141
};
4242

43-
const isMonthPicker = viewMode === TimeView.Monthly || viewMode === TimeView.Quarterly;
43+
const isMonthPicker = viewMode === CostPeriod.Monthly || viewMode === CostPeriod.Quarterly;
4444
const currentMonth = format(selectedDate, 'MMMM');
4545

4646
return (
@@ -51,7 +51,7 @@ export default function DateSelector({
5151
<Select
5252
placeholder="Select Mode"
5353
value={viewMode}
54-
data={Object.values(TimeView)}
54+
data={Object.values(CostPeriod)}
5555
onChange={handleModeChange}
5656
classNames={{ input: inputClasses }}
5757
/>
@@ -100,23 +100,23 @@ export default function DateSelector({
100100
condition: data.currentTotal !== -1,
101101
value: data.currentTotal,
102102
label:
103-
viewMode === TimeView.Monthly
103+
viewMode === CostPeriod.Monthly
104104
? `Current total cost for ${currentMonth}`
105105
: `Current total cost for ${data.billingPeriod}`,
106106
},
107107
{
108108
condition: data.grandTotal !== -1,
109109
value: data.grandTotal,
110110
label:
111-
viewMode === TimeView.Monthly
111+
viewMode === CostPeriod.Monthly
112112
? `Grand total cost for ${currentMonth}`
113113
: `Grand total cost for ${data.billingPeriod}`,
114114
},
115115
{
116116
condition: data.estimatedGrandTotal !== -1 && forecastEnabled,
117117
value: data.estimatedGrandTotal,
118118
label:
119-
viewMode === TimeView.Monthly
119+
viewMode === CostPeriod.Monthly
120120
? `Estimated grand total cost for ${currentMonth}`
121121
: `Estimated grand total cost for ${data.billingPeriod}`,
122122
},

0 commit comments

Comments
 (0)