Skip to content

Commit b6482e0

Browse files
committed
feat: dashboard chart labels
1 parent 6ad68fc commit b6482e0

File tree

5 files changed

+101
-10
lines changed

5 files changed

+101
-10
lines changed

components/dashboard/DateRangeSelector.tsx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
'use client'
22

33
import * as React from 'react'
4-
import { format, subMonths, startOfMonth, endOfMonth } from 'date-fns'
4+
import {
5+
format,
6+
subMonths,
7+
startOfMonth,
8+
endOfMonth,
9+
startOfWeek,
10+
endOfWeek,
11+
} from 'date-fns'
512
import { Calendar as CalendarIcon } from 'lucide-react'
613
import { DateRange } from 'react-day-picker'
714

@@ -101,6 +108,20 @@ export function DateRangeSelector({
101108
}
102109

103110
const rangePresets: { label: string; date: DateRange }[] = [
111+
{
112+
label: 'This Week',
113+
date: {
114+
from: startOfWeek(new Date()),
115+
to: endOfWeek(new Date()),
116+
},
117+
},
118+
{
119+
label: 'This Month',
120+
date: {
121+
from: startOfMonth(new Date()),
122+
to: endOfMonth(new Date()),
123+
},
124+
},
104125
{
105126
label: 'Last Month',
106127
date: {

components/dashboard/MonthlyRevenueChart.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ import {
1919
} from '@/components/ui/chart'
2020
import { Analysis } from '@/lib/types/analysis'
2121
import { formatDate } from '@/lib/utils'
22+
import { useSearchParams } from 'next/navigation'
23+
import { format, parse } from 'date-fns'
24+
import { useMemo } from 'react'
2225

2326
const chartConfig = {
2427
subscription: {
@@ -32,12 +35,25 @@ const chartConfig = {
3235
} satisfies ChartConfig
3336

3437
export function MonthlyRevenueChart({ data }: { data: Analysis['revenue'] }) {
38+
const params = useSearchParams()
39+
const paramFrom = params.get('from') as string
40+
const paramTo = params.get('to') as string
41+
42+
const [from, to] = useMemo(() => {
43+
const from = format(
44+
parse(paramFrom, 'dd-MM-yyyy', new Date()),
45+
'LLL dd, yyyy'
46+
)
47+
const to = format(parse(paramTo, 'dd-MM-yyyy', new Date()), 'LLL dd, yyyy')
48+
return [from, to]
49+
}, [paramFrom, paramTo])
50+
3551
return (
3652
<Card>
3753
<CardHeader>
3854
<CardTitle>Monthly Revenue</CardTitle>
3955
<CardDescription>
40-
Showing total revenue from subscriptions and fines
56+
{from} - {to}
4157
</CardDescription>
4258
</CardHeader>
4359
<CardContent>

components/dashboard/MontlyBorrowChart.tsx

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ import {
1919
} from '@/components/ui/chart'
2020
import { Analysis } from '@/lib/types/analysis'
2121
import { formatDate } from '@/lib/utils'
22+
import { useSearchParams } from 'next/navigation'
23+
import { useMemo } from 'react'
24+
import { format, parse } from 'date-fns'
2225

2326
const chartConfig = {
2427
count: {
@@ -28,11 +31,26 @@ const chartConfig = {
2831
} satisfies ChartConfig
2932

3033
export function MontlyBorrowChart({ data }: { data: Analysis['borrowing'] }) {
34+
const params = useSearchParams()
35+
const paramFrom = params.get('from') as string
36+
const paramTo = params.get('to') as string
37+
38+
const [from, to] = useMemo(() => {
39+
const from = format(
40+
parse(paramFrom, 'dd-MM-yyyy', new Date()),
41+
'LLL dd, yyyy'
42+
)
43+
const to = format(parse(paramTo, 'dd-MM-yyyy', new Date()), 'LLL dd, yyyy')
44+
return [from, to]
45+
}, [paramFrom, paramTo])
46+
3147
return (
3248
<Card>
3349
<CardHeader>
3450
<CardTitle>Monthly Borrows</CardTitle>
35-
<CardDescription>January - June 2024</CardDescription>
51+
<CardDescription>
52+
{from} - {to}
53+
</CardDescription>
3654
</CardHeader>
3755
<CardContent>
3856
<ChartContainer config={chartConfig}>
@@ -76,7 +94,7 @@ export function MontlyBorrowChart({ data }: { data: Analysis['borrowing'] }) {
7694
Trending up by 5.2% this month <TrendingUp className="h-4 w-4" />
7795
</div> */}
7896
<div className="leading-none text-muted-foreground">
79-
Showing total borrows per month
97+
Showing total borrows
8098
</div>
8199
</CardFooter>
82100
</Card>

components/dashboard/MostBorrowedBookChart.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ import {
1818
ChartTooltipContent,
1919
} from '@/components/ui/chart'
2020
import { Analysis } from '@/lib/types/analysis'
21+
import { useSearchParams } from 'next/navigation'
22+
import { useMemo } from 'react'
23+
import { format, parse } from 'date-fns'
2124

2225
export function MostBorrowedBookChart({ data }: { data: Analysis['book'] }) {
2326
const chartConfig = data.reduce((acc, { title }, index) => {
@@ -33,11 +36,26 @@ export function MostBorrowedBookChart({ data }: { data: Analysis['book'] }) {
3336
fill: chartConfig[title].color,
3437
}))
3538

39+
const params = useSearchParams()
40+
const paramFrom = params.get('from') as string
41+
const paramTo = params.get('to') as string
42+
43+
const [from, to] = useMemo(() => {
44+
const from = format(
45+
parse(paramFrom, 'dd-MM-yyyy', new Date()),
46+
'LLL dd, yyyy'
47+
)
48+
const to = format(parse(paramTo, 'dd-MM-yyyy', new Date()), 'LLL dd, yyyy')
49+
return [from, to]
50+
}, [paramFrom, paramTo])
51+
3652
return (
3753
<Card>
3854
<CardHeader>
3955
<CardTitle>Most Borrowed Books</CardTitle>
40-
<CardDescription>January - June 2024</CardDescription>
56+
<CardDescription>
57+
{from} - {to}
58+
</CardDescription>
4159
</CardHeader>
4260
<CardContent>
4361
<ChartContainer config={chartConfig}>

components/dashboard/TopMembershipChart.tsx

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ import {
1818
ChartTooltipContent,
1919
} from '@/components/ui/chart'
2020
import { Analysis } from '@/lib/types/analysis'
21+
import { useSearchParams } from 'next/navigation'
22+
import { useMemo } from 'react'
23+
import { format, parse } from 'date-fns'
2124

2225
export function TopMembershipChart({ data }: { data: Analysis['membership'] }) {
2326
const chartConfig = data.reduce((acc, { name }, index) => {
@@ -34,13 +37,28 @@ export function TopMembershipChart({ data }: { data: Analysis['membership'] }) {
3437
fill: chartConfig[name].color,
3538
}))
3639

40+
const params = useSearchParams()
41+
const paramFrom = params.get('from') as string
42+
const paramTo = params.get('to') as string
43+
44+
const [from, to] = useMemo(() => {
45+
const from = format(
46+
parse(paramFrom, 'dd-MM-yyyy', new Date()),
47+
'LLL dd, yyyy'
48+
)
49+
const to = format(parse(paramTo, 'dd-MM-yyyy', new Date()), 'LLL dd, yyyy')
50+
return [from, to]
51+
}, [paramFrom, paramTo])
52+
3753
return (
3854
<Card className="flex flex-col">
39-
<CardHeader className="items-center pb-0">
55+
<CardHeader>
4056
<CardTitle>Top Purchased Membership</CardTitle>
41-
<CardDescription>January - June 2024</CardDescription>
57+
<CardDescription>
58+
{from} - {to}
59+
</CardDescription>
4260
</CardHeader>
43-
<CardContent className="flex-1 pb-0">
61+
<CardContent className="flex-1">
4462
<ChartContainer
4563
config={chartConfig}
4664
className="mx-auto aspect-square max-h-[250px] pb-0 [&_.recharts-pie-label-text]:fill-foreground"
@@ -51,12 +69,12 @@ export function TopMembershipChart({ data }: { data: Analysis['membership'] }) {
5169
</PieChart>
5270
</ChartContainer>
5371
</CardContent>
54-
<CardFooter className="flex-col gap-2 text-sm">
72+
<CardFooter className="flex-col items-start gap-2 text-sm">
5573
{/* <div className="flex items-center gap-2 font-medium leading-none">
5674
Trending up by 5.2% this month <TrendingUp className="h-4 w-4" />
5775
</div> */}
5876
<div className="leading-none text-muted-foreground">
59-
Showing top most purchased membership
77+
Showing top purchased membership
6078
</div>
6179
</CardFooter>
6280
</Card>

0 commit comments

Comments
 (0)