|
| 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 | +} |
0 commit comments