Skip to content

Commit b950344

Browse files
committed
❇️ [refactor][frontend] date range picker state should be props
1 parent 15a7515 commit b950344

3 files changed

Lines changed: 32 additions & 19 deletions

File tree

frontend/src/components/date-range-picker.tsx

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { addDays, format } from 'date-fns'
1+
import { format } from 'date-fns'
22
import { CalendarIcon } from 'lucide-react'
3-
import * as React from 'react'
43
import { type DateRange } from 'react-day-picker'
54

65
import { Button } from '@/components/ui/button'
@@ -12,12 +11,15 @@ import {
1211
PopoverTrigger
1312
} from '@/components/ui/popover'
1413

15-
export function DatePickerWithRange() {
16-
const [date, setDate] = React.useState<DateRange | undefined>({
17-
from: new Date(new Date().getFullYear(), 0, 20),
18-
to: addDays(new Date(new Date().getFullYear(), 0, 20), 20)
19-
})
14+
interface DateRangePickerProps {
15+
dateRange: DateRange | undefined
16+
setDateRange: (date: DateRange | undefined) => void
17+
}
2018

19+
export function DatePickerWithRange({
20+
dateRange,
21+
setDateRange
22+
}: DateRangePickerProps) {
2123
return (
2224
<Field className="w-60 shrink-0">
2325
<FieldLabel htmlFor="date-picker-range">Date Range</FieldLabel>
@@ -29,14 +31,14 @@ export function DatePickerWithRange() {
2931
className="justify-start px-2.5 font-normal"
3032
>
3133
<CalendarIcon />
32-
{date?.from ? (
33-
date.to ? (
34+
{dateRange?.from ? (
35+
dateRange.to ? (
3436
<>
35-
{format(date.from, 'LLL dd, y')} -{' '}
36-
{format(date.to, 'LLL dd, y')}
37+
{format(dateRange.from, 'LLL dd, y')} -{' '}
38+
{format(dateRange.to, 'LLL dd, y')}
3739
</>
3840
) : (
39-
format(date.from, 'LLL dd, y')
41+
format(dateRange.from, 'LLL dd, y')
4042
)
4143
) : (
4244
<span>Pick a date</span>
@@ -46,9 +48,9 @@ export function DatePickerWithRange() {
4648
<PopoverContent className="w-auto p-0" align="start">
4749
<Calendar
4850
mode="range"
49-
defaultMonth={date?.from}
50-
selected={date}
51-
onSelect={setDate}
51+
defaultMonth={dateRange?.from}
52+
selected={dateRange}
53+
onSelect={setDateRange}
5254
numberOfMonths={2}
5355
/>
5456
</PopoverContent>

frontend/src/routes/_auth/account/$id/transaction.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useQuery } from '@tanstack/react-query'
22
import { createFileRoute } from '@tanstack/react-router'
3-
import { Fragment, useEffect } from 'react'
3+
import { Fragment, useEffect, useState } from 'react'
4+
import { type DateRange } from 'react-day-picker'
45
import { toast } from 'sonner'
56

67
import { Amount } from '@/components/amount'
@@ -375,6 +376,8 @@ function AccountTransactionPage() {
375376
const { id } = Route.useParams()
376377
const accountId = Number(id)
377378

379+
const [dateRange, setDateRange] = useState<DateRange | undefined>()
380+
378381
const {
379382
isError,
380383
data: account,
@@ -406,7 +409,10 @@ function AccountTransactionPage() {
406409
{/* Date Picker */}
407410
<div className="bg-background sticky top-0 z-10 pt-4">
408411
<div className="flex items-end gap-3 overflow-x-auto">
409-
<DatePickerWithRange />
412+
<DatePickerWithRange
413+
dateRange={dateRange}
414+
setDateRange={setDateRange}
415+
/>
410416
</div>
411417

412418
<Separator className="my-4 bg-foreground/30" />

frontend/src/routes/_auth/category.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useQuery } from '@tanstack/react-query'
22
import { createFileRoute } from '@tanstack/react-router'
3-
import { Fragment, useEffect } from 'react'
3+
import { Fragment, useEffect, useState } from 'react'
4+
import { type DateRange } from 'react-day-picker'
45
import { toast } from 'sonner'
56

67
import { DatePickerWithRange } from '@/components/date-range-picker'
@@ -21,6 +22,7 @@ const transactions = [
2122

2223
function CategoryPage() {
2324
const { client } = Route.useRouteContext()
25+
const [dateRange, setDateRange] = useState<DateRange | undefined>()
2426

2527
const {
2628
isError,
@@ -58,7 +60,10 @@ function CategoryPage() {
5860

5961
<div className="flex-1 p-4">
6062
<ScrollArea className="h-full w-full rounded-md">
61-
<DatePickerWithRange />
63+
<DatePickerWithRange
64+
dateRange={dateRange}
65+
setDateRange={setDateRange}
66+
/>
6267
{transactions.map((transaction) => (
6368
<Fragment key={transaction.title}>
6469
<div className="text-sm">

0 commit comments

Comments
 (0)