Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Commit

Permalink
style: add revenue monthly by year chart dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
foxminchan committed Jun 5, 2024
1 parent 6bcce94 commit e39fab0
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,23 @@ public sealed class RevenueYearHandler(IDatabaseFactory factory)
const string sql = $"""
WITH monthly_revenue AS (
SELECT
to_char(o.created_date, 'Mon') AS month,
to_char(o.created_date, 'Mon') AS month_name,
SUM(od.price * od.quantity) AS revenue
FROM orders o
JOIN order_details od ON o.id = od.order_id
WHERE EXTRACT(YEAR FROM o.created_date) = @year
GROUP BY to_char(o.created_date, 'Mon')
WHERE EXTRACT(YEAR FROM o.created_date) = @Year
GROUP BY to_char(o.created_date, 'Mon')
)
SELECT
month_names.month AS {nameof(RevenueYearDto.Month)},
COALESCE(monthly_revenue.revenue, 0) AS {nameof(RevenueYearDto.Revenue)}
FROM (
VALUES ('Jan'), ('Feb'), ('Mar'), ('Apr'), ('May'), ('Jun'),
('Jul'), ('Aug'), ('Sep'), ('Oct'), ('Nov'), ('Dec')
) AS month_names(month)
LEFT JOIN monthly_revenue ON month_names.month = monthly_revenue.month
ORDER BY month_names.month;
VALUES
(1,'Jan'), (2,'Feb'), (3,'Mar'), (4,'Apr'), (5,'May'), (6,'Jun'),
(7,'Jul'), (8,'Aug'), (9,'Sep'), (10,'Oct'), (11,'Nov'), (12,'Dec')
) AS month_names(month_number, month)
LEFT JOIN monthly_revenue ON month_names.month = monthly_revenue.month_name
ORDER BY month_names.month_number;
""";

using var conn = factory.GetOpenConnection();
Expand Down
4 changes: 2 additions & 2 deletions ui/backoffice/app/(dashboard)/dashboard/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import {
} from "@/components/ui/card"
import { ScrollArea } from "@/components/ui/scroll-area"
import { Tabs, TabsContent } from "@/components/ui/tabs"
import { BestSeller } from "@/components/custom/best-seller"
import BestSeller from "@/components/custom/best-seller"
import { Icons } from "@/components/custom/icons"
import { Overview } from "@/components/custom/overview"
import Overview from "@/components/custom/overview"

export default function Dashboard() {
const { data: DiffRevenueByMonth } = useGetDiffRevenueByMonth({
Expand Down
2 changes: 1 addition & 1 deletion ui/backoffice/components/custom/best-seller.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import useGetBestSellerProducts from "@/features/report/useGetBestSellerProducts

import { Avatar, AvatarFallback } from "@/components/ui/avatar"

export function BestSeller() {
export default function BestSeller() {
const { data } = useGetBestSellerProducts({ top: 5 })
return (
<div className="space-y-8">
Expand Down
63 changes: 8 additions & 55 deletions ui/backoffice/components/custom/overview.tsx
Original file line number Diff line number Diff line change
@@ -1,64 +1,17 @@
"use client"

import useGetRevenueByYear from "@/features/report/useGetRevenueByYear"
import { Bar, BarChart, ResponsiveContainer, XAxis, YAxis } from "recharts"

const data = [
{
name: "Jan",
total: Math.floor(Math.random() * 5000) + 1000,
},
{
name: "Feb",
total: Math.floor(Math.random() * 5000) + 1000,
},
{
name: "Mar",
total: Math.floor(Math.random() * 5000) + 1000,
},
{
name: "Apr",
total: Math.floor(Math.random() * 5000) + 1000,
},
{
name: "May",
total: Math.floor(Math.random() * 5000) + 1000,
},
{
name: "Jun",
total: Math.floor(Math.random() * 5000) + 1000,
},
{
name: "Jul",
total: Math.floor(Math.random() * 5000) + 1000,
},
{
name: "Aug",
total: Math.floor(Math.random() * 5000) + 1000,
},
{
name: "Sep",
total: Math.floor(Math.random() * 5000) + 1000,
},
{
name: "Oct",
total: Math.floor(Math.random() * 5000) + 1000,
},
{
name: "Nov",
total: Math.floor(Math.random() * 5000) + 1000,
},
{
name: "Dec",
total: Math.floor(Math.random() * 5000) + 1000,
},
]

export function Overview() {
export default function Overview() {
const { data: RevenueByYear } = useGetRevenueByYear({
year: new Date().getFullYear(),
})
return (
<ResponsiveContainer width="100%" height={350}>
<BarChart data={data}>
<BarChart data={RevenueByYear}>
<XAxis
dataKey="name"
dataKey="month"
stroke="#888888"
fontSize={12}
tickLine={false}
Expand All @@ -71,7 +24,7 @@ export function Overview() {
axisLine={false}
tickFormatter={(value) => `$${value}`}
/>
<Bar dataKey="total" fill="#adfa1d" radius={[4, 4, 0, 0]} />
<Bar dataKey="revenue" fill="#adfa1d" radius={[4, 4, 0, 0]} />
</BarChart>
</ResponsiveContainer>
)
Expand Down
8 changes: 8 additions & 0 deletions ui/backoffice/features/report/report.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
GrownCustomer,
GrownCustomerParams,
OrderGrownByDay,
RevenueByYear,
RevenueByYearParams,
TodayRevenue,
} from "./report.type"

Expand Down Expand Up @@ -48,6 +50,12 @@ class ReportService extends HttpService {
getOrderGrownByDay(): Promise<OrderGrownByDay> {
return this.get("/reports/orders-grown-by-day")
}

getRevenueByYear(
options: Partial<RevenueByYearParams>
): Promise<RevenueByYear[]> {
return this.get(`/reports/revenue-year?${buildQueryString(options)}`)
}
}

export default new ReportService()
11 changes: 11 additions & 0 deletions ui/backoffice/features/report/report.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,14 @@ export type OrderGrownByDay = {
growthPercentage: number
currentDate: Date
}

// -- Revenue By Year Report

export type RevenueByYear = {
month: string
revenue: number
}

export type RevenueByYearParams = {
year: number
}
3 changes: 2 additions & 1 deletion ui/backoffice/features/report/useGetDiffRevenueByMonth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useQuery } from "@tanstack/react-query"
import { keepPreviousData, useQuery } from "@tanstack/react-query"

import reportService from "./report.service"
import { DiffRevenueByMonthParams } from "./report.type"
Expand All @@ -9,5 +9,6 @@ export default function useGetDiffRevenueByMonth(
return useQuery({
queryKey: ["diff-revenue-by-month"],
queryFn: () => reportService.getDiffRevenueByMonth(options),
placeholderData: keepPreviousData,
})
}
3 changes: 2 additions & 1 deletion ui/backoffice/features/report/useGetGrownCustomer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useQuery } from "@tanstack/react-query"
import { keepPreviousData, useQuery } from "@tanstack/react-query"

import reportService from "./report.service"
import { GrownCustomerParams } from "./report.type"
Expand All @@ -9,5 +9,6 @@ export default function useGetGrownCustomer(
return useQuery({
queryKey: ["grown-customer"],
queryFn: () => reportService.getGrownCustomer(options),
placeholderData: keepPreviousData,
})
}
3 changes: 2 additions & 1 deletion ui/backoffice/features/report/useGetOrderGrownByDay.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { useQuery } from "@tanstack/react-query"
import { keepPreviousData, useQuery } from "@tanstack/react-query"

import reportService from "./report.service"

export default function useGetOrderGrownByDay() {
return useQuery({
queryKey: ["order-grown-by-day"],
queryFn: () => reportService.getOrderGrownByDay(),
placeholderData: keepPreviousData,
})
}
13 changes: 13 additions & 0 deletions ui/backoffice/features/report/useGetRevenueByYear.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { useQuery } from "@tanstack/react-query"

import reportService from "./report.service"
import { RevenueByYearParams } from "./report.type"

export default function useGetRevenueByYear(
options: Partial<RevenueByYearParams>
) {
return useQuery({
queryKey: ["revenue-by-year", options],
queryFn: () => reportService.getRevenueByYear(options),
})
}
3 changes: 2 additions & 1 deletion ui/backoffice/features/report/useGetTodayRevenue.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { useQuery } from "@tanstack/react-query"
import { keepPreviousData, useQuery } from "@tanstack/react-query"

import reportService from "./report.service"

export default function useGetTodayRevenue() {
return useQuery({
queryKey: ["total-revenue-by-day"],
queryFn: () => reportService.getTodayRevenue(),
placeholderData: keepPreviousData,
})
}

0 comments on commit e39fab0

Please sign in to comment.