Skip to content

Commit 8635478

Browse files
committed
feat: dashboard with actual data
1 parent 0dc9725 commit 8635478

File tree

6 files changed

+87
-126
lines changed

6 files changed

+87
-126
lines changed

app/(protected)/dashboard/page.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import {
77
BreadcrumbSeparator,
88
} from '@/components/ui/breadcrumb'
99

10-
import { Component as AC } from '@/components/dashboard/AreaChart'
11-
import { Component as BC } from '@/components/dashboard/BarChart'
12-
import { Component as LC } from '@/components/dashboard/LineChart'
13-
import { Component as PC } from '@/components/dashboard/PineChart'
10+
import { MonthlyRevenueChart } from '@/components/dashboard/MonthlyRevenueChart'
11+
import { MostBorrowedBookChart } from '@/components/dashboard/MostBorrowedBookChart'
12+
import { MontlyBorrowChart } from '@/components/dashboard/MontlyBorrowChart'
13+
import { TopMembershipChart } from '@/components/dashboard/TopMembershipChart'
1414
import { getAnalysis } from '@/lib/api/analysis'
1515
import { SITE_NAME } from '@/lib/consts'
1616
import type { Metadata } from 'next'
@@ -22,7 +22,7 @@ export const metadata: Metadata = {
2222

2323
export default async function Dashboard() {
2424
const res = await getAnalysis({
25-
limit: 5,
25+
limit: 50,
2626
skip: 0,
2727
from: '2024-01-05T17:00:47.680Z',
2828
to: '2025-06-05T17:00:47.680Z',
@@ -54,10 +54,10 @@ export default async function Dashboard() {
5454
</Breadcrumb>
5555

5656
<div className="grid my-4 grid-cols-1 gap-4 md:grid-cols-2">
57-
<LC />
58-
<AC />
59-
<BC />
60-
<PC />
57+
<MontlyBorrowChart data={res.data.borrowing} />
58+
<MonthlyRevenueChart data={res.data.revenue} />
59+
<MostBorrowedBookChart data={res.data.book} />
60+
<TopMembershipChart data={res.data.membership} />
6161
</div>
6262
</div>
6363
)

components/dashboard/AreaChart.tsx renamed to components/dashboard/MonthlyRevenueChart.tsx

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,94 +17,88 @@ import {
1717
ChartTooltip,
1818
ChartTooltipContent,
1919
} from '@/components/ui/chart'
20-
const chartData = [
21-
{ month: 'January', desktop: 186, mobile: 80 },
22-
{ month: 'February', desktop: 305, mobile: 200 },
23-
{ month: 'March', desktop: 237, mobile: 120 },
24-
{ month: 'April', desktop: 73, mobile: 190 },
25-
{ month: 'May', desktop: 209, mobile: 130 },
26-
{ month: 'June', desktop: 214, mobile: 140 },
27-
]
20+
import { Analysis } from '@/lib/types/analysis'
21+
import { formatDate } from '@/lib/utils'
2822

2923
const chartConfig = {
30-
desktop: {
31-
label: 'Desktop',
24+
subscription: {
25+
label: 'Subscription',
3226
color: 'hsl(var(--chart-1))',
3327
},
34-
mobile: {
35-
label: 'Mobile',
28+
fine: {
29+
label: 'Fine',
3630
color: 'hsl(var(--chart-2))',
3731
},
3832
} satisfies ChartConfig
3933

40-
export function Component() {
34+
export function MonthlyRevenueChart({ data }: { data: Analysis['revenue'] }) {
4135
return (
4236
<Card>
4337
<CardHeader>
4438
<CardTitle>Monthly Revenue</CardTitle>
4539
<CardDescription>
46-
Showing total visitors for the last 6 months
40+
Showing total revenue from subscriptions and fines
4741
</CardDescription>
4842
</CardHeader>
4943
<CardContent>
5044
<ChartContainer config={chartConfig}>
5145
<AreaChart
5246
accessibilityLayer
53-
data={chartData}
47+
data={data}
5448
margin={{
5549
left: 12,
5650
right: 12,
5751
}}
5852
>
5953
<CartesianGrid vertical={false} />
6054
<XAxis
61-
dataKey="month"
55+
dataKey="timestamp"
6256
tickLine={false}
6357
axisLine={false}
6458
tickMargin={8}
65-
tickFormatter={(value) => value.slice(0, 3)}
59+
tickFormatter={formatDate}
6660
/>
6761
<ChartTooltip cursor={false} content={<ChartTooltipContent />} />
6862
<defs>
69-
<linearGradient id="fillDesktop" x1="0" y1="0" x2="0" y2="1">
63+
<linearGradient id="fillSubscription" x1="0" y1="0" x2="0" y2="1">
7064
<stop
7165
offset="5%"
72-
stopColor="var(--color-desktop)"
66+
stopColor="var(--color-subscription)"
7367
stopOpacity={0.8}
7468
/>
7569
<stop
7670
offset="95%"
77-
stopColor="var(--color-desktop)"
71+
stopColor="var(--color-subscription)"
7872
stopOpacity={0.1}
7973
/>
8074
</linearGradient>
81-
<linearGradient id="fillMobile" x1="0" y1="0" x2="0" y2="1">
75+
<linearGradient id="fillFine" x1="0" y1="0" x2="0" y2="1">
8276
<stop
8377
offset="5%"
84-
stopColor="var(--color-mobile)"
78+
stopColor="var(--color-fine)"
8579
stopOpacity={0.8}
8680
/>
8781
<stop
8882
offset="95%"
89-
stopColor="var(--color-mobile)"
83+
stopColor="var(--color-fine)"
9084
stopOpacity={0.1}
9185
/>
9286
</linearGradient>
9387
</defs>
9488
<Area
95-
dataKey="mobile"
89+
dataKey="fine"
9690
type="natural"
97-
fill="url(#fillMobile)"
91+
fill="url(#fillFine)"
9892
fillOpacity={0.4}
99-
stroke="var(--color-mobile)"
93+
stroke="var(--color-fine)"
10094
stackId="a"
10195
/>
10296
<Area
103-
dataKey="desktop"
97+
dataKey="subscription"
10498
type="natural"
105-
fill="url(#fillDesktop)"
99+
fill="url(#fillSubscription)"
106100
fillOpacity={0.4}
107-
stroke="var(--color-desktop)"
101+
stroke="var(--color-subscription)"
108102
stackId="a"
109103
/>
110104
</AreaChart>

components/dashboard/LineChart.tsx renamed to components/dashboard/MontlyBorrowChart.tsx

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client'
22

3-
import { TrendingUp } from 'lucide-react'
3+
// import { TrendingUp } from 'lucide-react'
44
import { CartesianGrid, Line, LineChart, XAxis } from 'recharts'
55

66
import {
@@ -17,27 +17,17 @@ import {
1717
ChartTooltip,
1818
ChartTooltipContent,
1919
} from '@/components/ui/chart'
20-
const chartData = [
21-
{ month: 'January', desktop: 186, mobile: 80 },
22-
{ month: 'February', desktop: 305, mobile: 200 },
23-
{ month: 'March', desktop: 237, mobile: 120 },
24-
{ month: 'April', desktop: 73, mobile: 190 },
25-
{ month: 'May', desktop: 209, mobile: 130 },
26-
{ month: 'June', desktop: 214, mobile: 140 },
27-
]
20+
import { Analysis } from '@/lib/types/analysis'
21+
import { formatDate } from '@/lib/utils'
2822

2923
const chartConfig = {
30-
desktop: {
31-
label: 'Desktop',
24+
count: {
25+
label: 'Borrowing',
3226
color: 'hsl(var(--chart-1))',
3327
},
34-
mobile: {
35-
label: 'Mobile',
36-
color: 'hsl(var(--chart-2))',
37-
},
3828
} satisfies ChartConfig
3929

40-
export function Component() {
30+
export function MontlyBorrowChart({ data }: { data: Analysis['borrowing'] }) {
4131
return (
4232
<Card>
4333
<CardHeader>
@@ -48,31 +38,31 @@ export function Component() {
4838
<ChartContainer config={chartConfig}>
4939
<LineChart
5040
accessibilityLayer
51-
data={chartData}
41+
data={data}
5242
margin={{
5343
left: 12,
5444
right: 12,
5545
}}
5646
>
5747
<CartesianGrid vertical={false} />
5848
<XAxis
59-
dataKey="month"
49+
dataKey="timestamp"
6050
tickLine={false}
6151
axisLine={false}
6252
tickMargin={8}
63-
tickFormatter={(value) => value.slice(0, 3)}
53+
tickFormatter={formatDate}
6454
/>
6555
<ChartTooltip
6656
cursor={false}
6757
content={<ChartTooltipContent hideLabel />}
6858
/>
6959
<Line
70-
dataKey="desktop"
60+
dataKey="count"
7161
type="natural"
72-
stroke="var(--color-desktop)"
62+
stroke="var(--color-count)"
7363
strokeWidth={2}
7464
dot={{
75-
fill: 'var(--color-desktop)',
65+
fill: 'var(--color-count)',
7666
}}
7767
activeDot={{
7868
r: 6,
@@ -82,11 +72,11 @@ export function Component() {
8272
</ChartContainer>
8373
</CardContent>
8474
<CardFooter className="flex-col items-start gap-2 text-sm">
85-
<div className="flex gap-2 font-medium leading-none">
75+
{/* <div className="flex gap-2 font-medium leading-none">
8676
Trending up by 5.2% this month <TrendingUp className="h-4 w-4" />
87-
</div>
77+
</div> */}
8878
<div className="leading-none text-muted-foreground">
89-
Showing total visitors for the last 6 months
79+
Showing total borrows per month
9080
</div>
9181
</CardFooter>
9282
</Card>

components/dashboard/BarChart.tsx renamed to components/dashboard/MostBorrowedBookChart.tsx

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

33
import { TrendingUp } from 'lucide-react'
4-
import { Bar, BarChart, CartesianGrid, LabelList, XAxis } from 'recharts'
4+
import { Bar, BarChart, XAxis, YAxis } from 'recharts'
55

66
import {
77
Card,
@@ -17,23 +17,22 @@ import {
1717
ChartTooltip,
1818
ChartTooltipContent,
1919
} from '@/components/ui/chart'
20-
const chartData = [
21-
{ month: 'January', desktop: 186 },
22-
{ month: 'February', desktop: 305 },
23-
{ month: 'March', desktop: 237 },
24-
{ month: 'April', desktop: 73 },
25-
{ month: 'May', desktop: 209 },
26-
{ month: 'June', desktop: 214 },
27-
]
20+
import { Analysis } from '@/lib/types/analysis'
2821

29-
const chartConfig = {
30-
desktop: {
31-
label: 'Desktop',
32-
color: 'hsl(var(--chart-1))',
33-
},
34-
} satisfies ChartConfig
22+
export function MostBorrowedBookChart({ data }: { data: Analysis['book'] }) {
23+
const chartConfig = data.reduce((acc, { title }, index) => {
24+
acc[title] = {
25+
label: title,
26+
color: `hsl(var(--chart-${index + 1}))`,
27+
}
28+
return acc
29+
}, {} as ChartConfig)
30+
const chartData = data.map(({ title, count }) => ({
31+
browser: title,
32+
total: count,
33+
fill: chartConfig[title].color,
34+
}))
3535

36-
export function Component() {
3736
return (
3837
<Card>
3938
<CardHeader>
@@ -45,30 +44,27 @@ export function Component() {
4544
<BarChart
4645
accessibilityLayer
4746
data={chartData}
47+
layout="vertical"
4848
margin={{
49-
top: 20,
49+
left: 0,
5050
}}
5151
>
52-
<CartesianGrid vertical={false} />
53-
<XAxis
54-
dataKey="month"
52+
<YAxis
53+
dataKey="browser"
54+
type="category"
5555
tickLine={false}
5656
tickMargin={10}
5757
axisLine={false}
58-
tickFormatter={(value) => value.slice(0, 3)}
58+
tickFormatter={(value) =>
59+
chartConfig[value as keyof typeof chartConfig].label as string
60+
}
5961
/>
62+
<XAxis dataKey="total" type="number" hide />
6063
<ChartTooltip
6164
cursor={false}
6265
content={<ChartTooltipContent hideLabel />}
6366
/>
64-
<Bar dataKey="desktop" fill="var(--color-desktop)" radius={8}>
65-
<LabelList
66-
position="top"
67-
offset={12}
68-
className="fill-foreground"
69-
fontSize={12}
70-
/>
71-
</Bar>
67+
<Bar dataKey="total" layout="vertical" radius={5} />
7268
</BarChart>
7369
</ChartContainer>
7470
</CardContent>

components/dashboard/PineChart.tsx renamed to components/dashboard/TopMembershipChart.tsx

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,23 @@ import {
1717
ChartTooltip,
1818
ChartTooltipContent,
1919
} from '@/components/ui/chart'
20-
const chartData = [
21-
{ browser: 'chrome', visitors: 275, fill: 'var(--color-chrome)' },
22-
{ browser: 'safari', visitors: 200, fill: 'var(--color-safari)' },
23-
{ browser: 'firefox', visitors: 187, fill: 'var(--color-firefox)' },
24-
{ browser: 'edge', visitors: 173, fill: 'var(--color-edge)' },
25-
{ browser: 'other', visitors: 90, fill: 'var(--color-other)' },
26-
]
20+
import { Analysis } from '@/lib/types/analysis'
2721

28-
const chartConfig = {
29-
visitors: {
30-
label: 'Visitors',
31-
},
32-
chrome: {
33-
label: 'Chrome',
34-
color: 'hsl(var(--chart-1))',
35-
},
36-
safari: {
37-
label: 'Safari',
38-
color: 'hsl(var(--chart-2))',
39-
},
40-
firefox: {
41-
label: 'Firefox',
42-
color: 'hsl(var(--chart-3))',
43-
},
44-
edge: {
45-
label: 'Edge',
46-
color: 'hsl(var(--chart-4))',
47-
},
48-
other: {
49-
label: 'Other',
50-
color: 'hsl(var(--chart-5))',
51-
},
52-
} satisfies ChartConfig
22+
export function TopMembershipChart({ data }: { data: Analysis['membership'] }) {
23+
const chartConfig = data.reduce((acc, { name }, index) => {
24+
acc[name] = {
25+
label: name,
26+
color: `hsl(var(--chart-${index + 1}))`,
27+
}
28+
return acc
29+
}, {} as ChartConfig)
30+
31+
const chartData = data.map(({ name, count }) => ({
32+
browser: name,
33+
visitors: count,
34+
fill: chartConfig[name].color,
35+
}))
5336

54-
export function Component() {
5537
return (
5638
<Card className="flex flex-col">
5739
<CardHeader className="items-center pb-0">

0 commit comments

Comments
 (0)