feat: open deal by id

This commit is contained in:
2024-12-08 23:23:34 +03:00
parent 886eef4c0d
commit 894e135be7
4 changed files with 73 additions and 14 deletions

View File

@@ -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<DealPageContextState | undefined>(
type DealPageContextStateProps = {
refetchDeals: () => Promise<void>;
defaultDealId?: number;
}
const useDealPageContextState = (props: DealPageContextStateProps) => {
const { refetchDeals } = props;
const { refetchDeals, defaultDealId } = props;
const [selectedDeal, setSelectedDeal] = useState<DealSchema | undefined>(
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<void>;
defaultDealId?: number;
};
export const DealPageContextProvider: FC<DealPageContextProviderProps> = ({
children,
refetchDeals,
defaultDealId,
}) => {
const state = useDealPageContextState({ refetchDeals });
const state = useDealPageContextState({ refetchDeals, defaultDealId });
return (
<DealPageContext.Provider value={state}>
{children}

View File

@@ -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<DisplayMode>(
@@ -394,9 +395,11 @@ export const LeadsPage: FC = () => {
backgroundColor: "transparent",
boxShadow: "none",
}}>
<DealPageContextProvider refetchDeals={async () => {
await refetch();
}}>
<DealPageContextProvider
defaultDealId={(dealId && parseInt(dealId)) || undefined}
refetchDeals={async () => {
await refetch();
}}>
<PrefillDealContextProvider>
<PageBlock style={{ flex: 0 }}>
<Flex

View File

@@ -13,6 +13,7 @@ import { createFileRoute } from '@tanstack/react-router'
// Import Routes
import { Route as rootRoute } from './routes/__root'
import { Route as LeadsDealIdImport } from './routes/leads.$dealId'
import { Route as DealsDealIdImport } from './routes/deals.$dealId'
// Create Virtual Routes
@@ -106,6 +107,12 @@ const IndexLazyRoute = IndexLazyImport.update({
getParentRoute: () => 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"
}
}
}

View File

@@ -0,0 +1,6 @@
import { createFileRoute } from "@tanstack/react-router";
import { LeadsPage } from "../pages/LeadsPage";
export const Route = createFileRoute("/leads/$dealId")({
component: LeadsPage,
});