From 894e135be7c4a328408333655e5192c1ea7f64e9 Mon Sep 17 00:00:00 2001 From: admin Date: Sun, 8 Dec 2024 23:23:34 +0300 Subject: [PATCH] feat: open deal by id --- .../LeadsPage/contexts/DealPageContext.tsx | 19 +++++-- src/pages/LeadsPage/ui/LeadsPage.tsx | 11 ++-- src/routeTree.gen.ts | 51 ++++++++++++++++--- src/routes/leads.$dealId.tsx | 6 +++ 4 files changed, 73 insertions(+), 14 deletions(-) create mode 100644 src/routes/leads.$dealId.tsx diff --git a/src/pages/LeadsPage/contexts/DealPageContext.tsx b/src/pages/LeadsPage/contexts/DealPageContext.tsx index 6ab133c..13cd2c9 100644 --- a/src/pages/LeadsPage/contexts/DealPageContext.tsx +++ b/src/pages/LeadsPage/contexts/DealPageContext.tsx @@ -1,5 +1,5 @@ -import { createContext, FC, useContext, useState } from "react"; -import { DealSchema } from "../../../client"; +import { createContext, FC, useContext, useEffect, useState } from "react"; +import { DealSchema, DealService } from "../../../client"; type DealPageContextState = { selectedDeal?: DealSchema; @@ -13,14 +13,23 @@ const DealPageContext = createContext( type DealPageContextStateProps = { refetchDeals: () => Promise; + defaultDealId?: number; } const useDealPageContextState = (props: DealPageContextStateProps) => { - const { refetchDeals } = props; + const { refetchDeals, defaultDealId } = props; const [selectedDeal, setSelectedDeal] = useState( undefined, ); + useEffect(() => { + if (!defaultDealId) + return; + DealService.getDealById({ dealId: defaultDealId }).then(deal => { + setSelectedDeal(deal); + + }); + }, []); return { selectedDeal, setSelectedDeal, @@ -31,13 +40,15 @@ const useDealPageContextState = (props: DealPageContextStateProps) => { type DealPageContextProviderProps = { children: React.ReactNode; refetchDeals: () => Promise; + defaultDealId?: number; }; export const DealPageContextProvider: FC = ({ children, refetchDeals, + defaultDealId, }) => { - const state = useDealPageContextState({ refetchDeals }); + const state = useDealPageContextState({ refetchDeals, defaultDealId }); return ( {children} diff --git a/src/pages/LeadsPage/ui/LeadsPage.tsx b/src/pages/LeadsPage/ui/LeadsPage.tsx index 212b5e7..e6188ca 100644 --- a/src/pages/LeadsPage/ui/LeadsPage.tsx +++ b/src/pages/LeadsPage/ui/LeadsPage.tsx @@ -22,6 +22,7 @@ import { motion } from "framer-motion"; import { dateWithoutTimezone } from "../../../shared/lib/date.ts"; import DealPrefillDrawer from "../drawers/DealPrefillDrawer/DealPrefillDrawer.tsx"; import { PrefillDealContextProvider } from "../contexts/PrefillDealContext.tsx"; +import { useParams } from "@tanstack/react-router"; enum DisplayMode { BOARD, @@ -30,7 +31,7 @@ enum DisplayMode { export const LeadsPage: FC = () => { const { data, form } = useDealsPageState(); - + const { dealId } = useParams({ strict: false }); const { summariesRaw, refetch } = useDealSummaries(); const [summaries, setSummaries] = useState(summariesRaw); const [displayMode, setDisplayMode] = useState( @@ -394,9 +395,11 @@ export const LeadsPage: FC = () => { backgroundColor: "transparent", boxShadow: "none", }}> - { - await refetch(); - }}> + { + await refetch(); + }}> rootRoute, } as any).lazy(() => import('./routes/index.lazy').then((d) => d.Route)) +const LeadsDealIdRoute = LeadsDealIdImport.update({ + id: '/$dealId', + path: '/$dealId', + getParentRoute: () => LeadsLazyRoute, +} as any) + const DealsDealIdRoute = DealsDealIdImport.update({ id: '/deals/$dealId', path: '/deals/$dealId', @@ -207,17 +214,36 @@ declare module '@tanstack/react-router' { preLoaderRoute: typeof DealsDealIdImport parentRoute: typeof rootRoute } + '/leads/$dealId': { + id: '/leads/$dealId' + path: '/$dealId' + fullPath: '/leads/$dealId' + preLoaderRoute: typeof LeadsDealIdImport + parentRoute: typeof LeadsLazyImport + } } } // Create and export the route tree +interface LeadsLazyRouteChildren { + LeadsDealIdRoute: typeof LeadsDealIdRoute +} + +const LeadsLazyRouteChildren: LeadsLazyRouteChildren = { + LeadsDealIdRoute: LeadsDealIdRoute, +} + +const LeadsLazyRouteWithChildren = LeadsLazyRoute._addFileChildren( + LeadsLazyRouteChildren, +) + export interface FileRoutesByFullPath { '/': typeof IndexLazyRoute '/admin': typeof AdminLazyRoute '/barcode': typeof BarcodeLazyRoute '/clients': typeof ClientsLazyRoute - '/leads': typeof LeadsLazyRoute + '/leads': typeof LeadsLazyRouteWithChildren '/login': typeof LoginLazyRoute '/marketplaces': typeof MarketplacesLazyRoute '/products': typeof ProductsLazyRoute @@ -226,6 +252,7 @@ export interface FileRoutesByFullPath { '/statistics': typeof StatisticsLazyRoute '/test': typeof TestLazyRoute '/deals/$dealId': typeof DealsDealIdRoute + '/leads/$dealId': typeof LeadsDealIdRoute } export interface FileRoutesByTo { @@ -233,7 +260,7 @@ export interface FileRoutesByTo { '/admin': typeof AdminLazyRoute '/barcode': typeof BarcodeLazyRoute '/clients': typeof ClientsLazyRoute - '/leads': typeof LeadsLazyRoute + '/leads': typeof LeadsLazyRouteWithChildren '/login': typeof LoginLazyRoute '/marketplaces': typeof MarketplacesLazyRoute '/products': typeof ProductsLazyRoute @@ -242,6 +269,7 @@ export interface FileRoutesByTo { '/statistics': typeof StatisticsLazyRoute '/test': typeof TestLazyRoute '/deals/$dealId': typeof DealsDealIdRoute + '/leads/$dealId': typeof LeadsDealIdRoute } export interface FileRoutesById { @@ -250,7 +278,7 @@ export interface FileRoutesById { '/admin': typeof AdminLazyRoute '/barcode': typeof BarcodeLazyRoute '/clients': typeof ClientsLazyRoute - '/leads': typeof LeadsLazyRoute + '/leads': typeof LeadsLazyRouteWithChildren '/login': typeof LoginLazyRoute '/marketplaces': typeof MarketplacesLazyRoute '/products': typeof ProductsLazyRoute @@ -259,6 +287,7 @@ export interface FileRoutesById { '/statistics': typeof StatisticsLazyRoute '/test': typeof TestLazyRoute '/deals/$dealId': typeof DealsDealIdRoute + '/leads/$dealId': typeof LeadsDealIdRoute } export interface FileRouteTypes { @@ -277,6 +306,7 @@ export interface FileRouteTypes { | '/statistics' | '/test' | '/deals/$dealId' + | '/leads/$dealId' fileRoutesByTo: FileRoutesByTo to: | '/' @@ -292,6 +322,7 @@ export interface FileRouteTypes { | '/statistics' | '/test' | '/deals/$dealId' + | '/leads/$dealId' id: | '__root__' | '/' @@ -307,6 +338,7 @@ export interface FileRouteTypes { | '/statistics' | '/test' | '/deals/$dealId' + | '/leads/$dealId' fileRoutesById: FileRoutesById } @@ -315,7 +347,7 @@ export interface RootRouteChildren { AdminLazyRoute: typeof AdminLazyRoute BarcodeLazyRoute: typeof BarcodeLazyRoute ClientsLazyRoute: typeof ClientsLazyRoute - LeadsLazyRoute: typeof LeadsLazyRoute + LeadsLazyRoute: typeof LeadsLazyRouteWithChildren LoginLazyRoute: typeof LoginLazyRoute MarketplacesLazyRoute: typeof MarketplacesLazyRoute ProductsLazyRoute: typeof ProductsLazyRoute @@ -331,7 +363,7 @@ const rootRouteChildren: RootRouteChildren = { AdminLazyRoute: AdminLazyRoute, BarcodeLazyRoute: BarcodeLazyRoute, ClientsLazyRoute: ClientsLazyRoute, - LeadsLazyRoute: LeadsLazyRoute, + LeadsLazyRoute: LeadsLazyRouteWithChildren, LoginLazyRoute: LoginLazyRoute, MarketplacesLazyRoute: MarketplacesLazyRoute, ProductsLazyRoute: ProductsLazyRoute, @@ -380,7 +412,10 @@ export const routeTree = rootRoute "filePath": "clients.lazy.tsx" }, "/leads": { - "filePath": "leads.lazy.tsx" + "filePath": "leads.lazy.tsx", + "children": [ + "/leads/$dealId" + ] }, "/login": { "filePath": "login.lazy.tsx" @@ -405,6 +440,10 @@ export const routeTree = rootRoute }, "/deals/$dealId": { "filePath": "deals.$dealId.tsx" + }, + "/leads/$dealId": { + "filePath": "leads.$dealId.tsx", + "parent": "/leads" } } } diff --git a/src/routes/leads.$dealId.tsx b/src/routes/leads.$dealId.tsx new file mode 100644 index 0000000..ba7215d --- /dev/null +++ b/src/routes/leads.$dealId.tsx @@ -0,0 +1,6 @@ +import { createFileRoute } from "@tanstack/react-router"; +import { LeadsPage } from "../pages/LeadsPage"; + +export const Route = createFileRoute("/leads/$dealId")({ + component: LeadsPage, +}); \ No newline at end of file