From 615a59806af7ccf028f11cbb0232c581956dfc99 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sun, 17 Oct 2021 01:27:35 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20#71?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/inspectionProfiles/Project_Default.xml | 7 --- .../src/components/routing/ViewSetRouter.tsx | 63 ++++++++++--------- frontend/src/hooks/useManagedViewSet.ts | 29 +++++---- frontend/src/hooks/useViewSet.ts | 14 +++-- 4 files changed, 62 insertions(+), 51 deletions(-) diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml index bfc35fd..4d74dbe 100644 --- a/.idea/inspectionProfiles/Project_Default.xml +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -40,13 +40,6 @@ - - - diff --git a/frontend/src/components/routing/ViewSetRouter.tsx b/frontend/src/components/routing/ViewSetRouter.tsx index b8b9b04..d6cf085 100644 --- a/frontend/src/components/routing/ViewSetRouter.tsx +++ b/frontend/src/components/routing/ViewSetRouter.tsx @@ -24,37 +24,44 @@ export function ViewSetRouter({viewSet, unselec const path = useSophonPath() const pk = path?.[pathSegment] - if(viewSet === undefined) { - return ( - - - - ) - } + return React.useMemo( + () => { + if(viewSet === undefined) { + return ( + + + + ) + } - // If an error happens, display it in a ErrorBox - if(viewSet.error) { - return ( - - ) - } + // If an error happens, display it in a ErrorBox + if(viewSet.error) { + return ( + + ) + } - // If the viewset is still loading, display a loading message - if(viewSet.resources === null) { - return ( - - - - ) - } + // If the viewset is still loading, display a loading message + if(viewSet.resources === null) { + return ( + + + + ) + } - const selection = pk ? viewSet.resources?.find(res => res.value[pkKey] === pk) : undefined + const selection = pk ? viewSet.resources?.find(res => res.value[pkKey] === pk) : undefined - return ( - } - selectedRoute={(props) => } - /> + return ( + } + selectedRoute={(props) => } + /> + ) + }, + [viewSet, UnselectedRoute, SelectedRoute, pk, pkKey], ) + + } diff --git a/frontend/src/hooks/useManagedViewSet.ts b/frontend/src/hooks/useManagedViewSet.ts index dbc282c..8a76f7f 100644 --- a/frontend/src/hooks/useManagedViewSet.ts +++ b/frontend/src/hooks/useManagedViewSet.ts @@ -617,17 +617,22 @@ export function useManagedViewSet(baseRoute: st [refresh, state, refreshOnMount, viewset, baseRoute], ) - if(!viewset) { - return undefined - } + return React.useMemo( + () => { + if(!viewset) { + return undefined + } - return { - busy: state.busy, - error: state.error, - operationError: state.operationError, - resources, - refresh, - create, - command, - } + return { + busy: state.busy, + error: state.error, + operationError: state.operationError, + resources, + refresh, + create, + command, + } + }, + [state, resources, refresh, create, command] + ) } diff --git a/frontend/src/hooks/useViewSet.ts b/frontend/src/hooks/useViewSet.ts index 14d5f6b..12baa28 100644 --- a/frontend/src/hooks/useViewSet.ts +++ b/frontend/src/hooks/useViewSet.ts @@ -149,9 +149,15 @@ export function useViewSet(baseRoute: string): ViewSet | und [action, baseRoute], ) - if(!api) { - return undefined - } - return {command, action, list, retrieve, create, update, destroy} + return React.useMemo( + () => { + if(!api) { + return undefined + } + + return {command, action, list, retrieve, create, update, destroy} + }, + [command, action, list, retrieve, create, update, destroy], + ) }