From e66060aaf16762fd836b5aadad0c774f37436b06 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 30 Sep 2021 01:30:38 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Complete=20the=20`LocationViewSetRo?= =?UTF-8?q?uter`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../routing/LocationViewSetRouter.tsx | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/frontend/src/components/routing/LocationViewSetRouter.tsx b/frontend/src/components/routing/LocationViewSetRouter.tsx index f195f92..6fa00b1 100644 --- a/frontend/src/components/routing/LocationViewSetRouter.tsx +++ b/frontend/src/components/routing/LocationViewSetRouter.tsx @@ -2,19 +2,35 @@ import * as React from "react" import * as ReactDOM from "react-dom" import {ViewSetRouter, ViewSetRouterProps} from "./ViewSetRouter"; import {useLocation} from "@reach/router"; +import {SplitPath, splitPath} from "../../utils/PathSplitter"; +import {Detail} from "../../utils/DjangoTypes"; -interface LocationViewSetRouterProps extends ViewSetRouterProps { - +interface LocationViewSetRouterProps extends ViewSetRouterProps { + pkKey: keyof Resource, + splitPathKey: keyof SplitPath, } -export function LocationViewSetRouter({...props}: LocationViewSetRouterProps): JSX.Element { +export function LocationViewSetRouter({pkKey, splitPathKey, viewSet, ...props}: LocationViewSetRouterProps): JSX.Element { + // Get the current page location const location = useLocation() + // Split the path into multiple segments + const expectedPk = + React.useMemo( + () => splitPath(location.pathname)[splitPathKey], + [location] + ) + // Find the ManagedResource matching the expectedPk + const selection = + React.useMemo( + () => viewSet.resources?.filter(res => res.value[pkKey] === expectedPk)[0], + [viewSet, expectedPk] + ) return ( - + ) }