From 9ce07ed3c2bd7297017f11a93b1365fc13ecff2a Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Fri, 26 Jun 2026 16:05:11 +0330 Subject: [PATCH] up --- src/pages/dashboard/hooks/useDashboardData.ts | 12 ++++++++++++ src/pages/dashboard/service/DashboardService.ts | 14 ++++++++++++++ src/shared/Sidebar.tsx | 6 ++++++ src/shared/SidebarItem.tsx | 11 ++++++++--- 4 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 src/pages/dashboard/hooks/useDashboardData.ts create mode 100644 src/pages/dashboard/service/DashboardService.ts diff --git a/src/pages/dashboard/hooks/useDashboardData.ts b/src/pages/dashboard/hooks/useDashboardData.ts new file mode 100644 index 0000000..80e9fc5 --- /dev/null +++ b/src/pages/dashboard/hooks/useDashboardData.ts @@ -0,0 +1,12 @@ +import { useQuery } from '@tanstack/react-query'; +import * as api from '../service/DashboardService'; + +const DASHBOARD_COUNTS_REFETCH_MS = 2 * 60 * 1000; + +export const useGetDashboardCounts = () => { + return useQuery({ + queryKey: ['dashboard-counts'], + queryFn: () => api.getDashboardCounts(), + refetchInterval: DASHBOARD_COUNTS_REFETCH_MS, + }); +}; diff --git a/src/pages/dashboard/service/DashboardService.ts b/src/pages/dashboard/service/DashboardService.ts new file mode 100644 index 0000000..6a63060 --- /dev/null +++ b/src/pages/dashboard/service/DashboardService.ts @@ -0,0 +1,14 @@ +import axios from '@/config/axios'; +import type { BaseResponse } from '@/shared/types/Types'; + +export type DashboardCountsType = { + requestsCount: number; + invoicesCount: number; + ordersCount: number; + paymentsCount: number; +}; + +export const getDashboardCounts = async (): Promise> => { + const { data } = await axios.get>('/public/dashboard/counts'); + return data; +}; diff --git a/src/shared/Sidebar.tsx b/src/shared/Sidebar.tsx index fb22af8..0c775b6 100644 --- a/src/shared/Sidebar.tsx +++ b/src/shared/Sidebar.tsx @@ -7,11 +7,14 @@ import { Paths } from '../config/Paths' import { useSharedStore } from './store/useSharedStore' import { clx } from '../helpers/utils' import { t } from '../locale' +import { useGetDashboardCounts } from '@/pages/dashboard/hooks/useDashboardData' const SideBar: FC = () => { const { openSidebar, setOpenSidebar } = useSharedStore() const location = useLocation() + const { data: dashboardCounts } = useGetDashboardCounts() + const counts = dashboardCounts?.data const isActive = (name: string) => { return location.pathname.includes(name) @@ -56,6 +59,7 @@ const SideBar: FC = () => { title={t('sidebar.myRequests')} isActive={isActive('request')} link={Paths.request.myRequest} + badge={counts?.requestsCount} /> { title={t('sidebar.preFactors')} isActive={isActive('/proforma-invoice')} link={Paths.proformaInvoice} + badge={counts?.invoicesCount} /> { title={t('sidebar.myOrders')} isActive={isActive('order')} link={Paths.order.myOrders} + badge={counts?.ordersCount} /> diff --git a/src/shared/SidebarItem.tsx b/src/shared/SidebarItem.tsx index b91ffbb..f5b4f84 100644 --- a/src/shared/SidebarItem.tsx +++ b/src/shared/SidebarItem.tsx @@ -12,7 +12,8 @@ type Props = { isActive: boolean, link: string, isLogout?: boolean, - isWithoutLine?: boolean + isWithoutLine?: boolean, + badge?: number, } const SideBarItem: FC = (props: Props) => { @@ -28,7 +29,7 @@ const SideBarItem: FC = (props: Props) => { return (
@@ -37,7 +38,11 @@ const SideBarItem: FC = (props: Props) => { {props.title}
- + {props.badge != null && props.badge > 0 && ( + + {props.badge > 99 ? '99+' : props.badge} + + )} ) }