From 0f9b3a5722eb29d4f5fbf891a67773a8080600db Mon Sep 17 00:00:00 2001 From: Jakob Schrettenbrunner Date: Fri, 1 Jan 2021 17:21:13 +0000 Subject: [PATCH 1/2] use children in routes instead of component prop --- resources/scripts/TransitionRouter.tsx | 7 +- .../server/schedules/ScheduleContainer.tsx | 7 +- .../schedules/ScheduleEditContainer.tsx | 14 ++-- .../scripts/hoc/RequireServerPermission.tsx | 25 ++++++ .../scripts/hoc/requireServerPermission.tsx | 31 ------- resources/scripts/routers/ServerRouter.tsx | 84 +++++++++---------- 6 files changed, 80 insertions(+), 88 deletions(-) create mode 100644 resources/scripts/hoc/RequireServerPermission.tsx delete mode 100644 resources/scripts/hoc/requireServerPermission.tsx diff --git a/resources/scripts/TransitionRouter.tsx b/resources/scripts/TransitionRouter.tsx index 342e31a7a..5e5777fee 100644 --- a/resources/scripts/TransitionRouter.tsx +++ b/resources/scripts/TransitionRouter.tsx @@ -1,10 +1,9 @@ -import React, { useRef } from 'react'; +import React from 'react'; import { Route } from 'react-router'; import { SwitchTransition } from 'react-transition-group'; import Fade from '@/components/elements/Fade'; import styled from 'styled-components/macro'; import tw from 'twin.macro'; -import v4 from 'uuid/v4'; const StyledSwitchTransition = styled(SwitchTransition)` ${tw`relative`}; @@ -15,13 +14,11 @@ const StyledSwitchTransition = styled(SwitchTransition)` `; const TransitionRouter: React.FC = ({ children }) => { - const uuid = useRef(v4()).current; - return ( ( - +
{children}
diff --git a/resources/scripts/components/server/schedules/ScheduleContainer.tsx b/resources/scripts/components/server/schedules/ScheduleContainer.tsx index 04c302917..ddcd0a865 100644 --- a/resources/scripts/components/server/schedules/ScheduleContainer.tsx +++ b/resources/scripts/components/server/schedules/ScheduleContainer.tsx @@ -2,7 +2,7 @@ import React, { useEffect, useState } from 'react'; import getServerSchedules from '@/api/server/schedules/getServerSchedules'; import { ServerContext } from '@/state/server'; import Spinner from '@/components/elements/Spinner'; -import { RouteComponentProps } from 'react-router-dom'; +import { useHistory, useRouteMatch } from 'react-router-dom'; import FlashMessageRender from '@/components/FlashMessageRender'; import ScheduleRow from '@/components/server/schedules/ScheduleRow'; import { httpErrorToHuman } from '@/api/http'; @@ -14,7 +14,10 @@ import GreyRowBox from '@/components/elements/GreyRowBox'; import Button from '@/components/elements/Button'; import ServerContentBlock from '@/components/elements/ServerContentBlock'; -export default ({ match, history }: RouteComponentProps) => { +export default () => { + const match = useRouteMatch(); + const history = useHistory(); + const uuid = ServerContext.useStoreState(state => state.server.data!.uuid); const { clearFlashes, addError } = useFlash(); const [ loading, setLoading ] = useState(true); diff --git a/resources/scripts/components/server/schedules/ScheduleEditContainer.tsx b/resources/scripts/components/server/schedules/ScheduleEditContainer.tsx index 4e204e1c0..17d58ac4e 100644 --- a/resources/scripts/components/server/schedules/ScheduleEditContainer.tsx +++ b/resources/scripts/components/server/schedules/ScheduleEditContainer.tsx @@ -1,5 +1,5 @@ import React, { useCallback, useEffect, useState } from 'react'; -import { RouteComponentProps } from 'react-router-dom'; +import { useHistory, useLocation, useParams } from 'react-router-dom'; import { Schedule } from '@/api/server/schedules/getServerSchedules'; import getServerSchedule from '@/api/server/schedules/getServerSchedule'; import Spinner from '@/components/elements/Spinner'; @@ -45,7 +45,11 @@ const ActivePill = ({ active }: { active: boolean }) => ( ); -export default ({ match, history, location: { state } }: RouteComponentProps, State>) => { +export default () => { + const params = useParams() as Params; + const history = useHistory(); + const state: State = useLocation().state; + const id = ServerContext.useStoreState(state => state.server.data!.id); const uuid = ServerContext.useStoreState(state => state.server.data!.uuid); @@ -57,20 +61,20 @@ export default ({ match, history, location: { state } }: RouteComponentProps actions.schedules.appendSchedule); useEffect(() => { - if (schedule?.id === Number(match.params.id)) { + if (schedule?.id === Number(params.id)) { setIsLoading(false); return; } clearFlashes('schedules'); - getServerSchedule(uuid, Number(match.params.id)) + getServerSchedule(uuid, Number(params.id)) .then(schedule => appendSchedule(schedule)) .catch(error => { console.error(error); clearAndAddHttpError({ error, key: 'schedules' }); }) .then(() => setIsLoading(false)); - }, [ match ]); + }, [ params ]); const toggleEditModal = useCallback(() => { setShowEditModal(s => !s); diff --git a/resources/scripts/hoc/RequireServerPermission.tsx b/resources/scripts/hoc/RequireServerPermission.tsx new file mode 100644 index 000000000..ba825a59e --- /dev/null +++ b/resources/scripts/hoc/RequireServerPermission.tsx @@ -0,0 +1,25 @@ +import React from 'react'; +import Can from '@/components/elements/Can'; +import ScreenBlock from '@/components/screens/ScreenBlock'; +export interface RequireServerPermissionProps { + permissions: string | string[] +} + +const RequireServerPermission: React.FC = ({ children, permissions }) => { + return ( + + } + > + {children} + + ); +}; + +export default RequireServerPermission; diff --git a/resources/scripts/hoc/requireServerPermission.tsx b/resources/scripts/hoc/requireServerPermission.tsx deleted file mode 100644 index 2cafa8fcb..000000000 --- a/resources/scripts/hoc/requireServerPermission.tsx +++ /dev/null @@ -1,31 +0,0 @@ -import React from 'react'; -import Can from '@/components/elements/Can'; -import ScreenBlock from '@/components/screens/ScreenBlock'; -import isEqual from 'react-fast-compare'; - -const requireServerPermission = (Component: React.ComponentType, permissions: string | string[]) => { - return class extends React.Component { - shouldComponentUpdate (nextProps: Readonly) { - return !isEqual(nextProps, this.props); - } - - render () { - return ( - - } - > - - - ); - } - }; -}; - -export default requireServerPermission; diff --git a/resources/scripts/routers/ServerRouter.tsx b/resources/scripts/routers/ServerRouter.tsx index 533491db6..c917bf590 100644 --- a/resources/scripts/routers/ServerRouter.tsx +++ b/resources/scripts/routers/ServerRouter.tsx @@ -27,10 +27,10 @@ import SubNavigation from '@/components/elements/SubNavigation'; import NetworkContainer from '@/components/server/network/NetworkContainer'; import InstallListener from '@/components/server/InstallListener'; import StartupContainer from '@/components/server/startup/StartupContainer'; -import requireServerPermission from '@/hoc/requireServerPermission'; import ErrorBoundary from '@/components/elements/ErrorBoundary'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faExternalLinkAlt } from '@fortawesome/free-solid-svg-icons'; +import RequireServerPermission from '@/hoc/RequireServerPermission'; const ServerRouter = ({ match, location }: RouteComponentProps<{ id: string }>) => { const rootAdmin = useStoreState(state => state.user.data!.rootAdmin); @@ -142,50 +142,44 @@ const ServerRouter = ({ match, location }: RouteComponentProps<{ id: string }>) - - ( - - - - )} - exact - /> - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 8fb28fdf225bf3db1e2c61657572e9e97190777b Mon Sep 17 00:00:00 2001 From: Jakob Schrettenbrunner Date: Fri, 1 Jan 2021 23:14:29 +0100 Subject: [PATCH 2/2] remove unused import --- resources/scripts/components/server/network/NetworkContainer.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/resources/scripts/components/server/network/NetworkContainer.tsx b/resources/scripts/components/server/network/NetworkContainer.tsx index 236707219..fdc66aa6d 100644 --- a/resources/scripts/components/server/network/NetworkContainer.tsx +++ b/resources/scripts/components/server/network/NetworkContainer.tsx @@ -11,7 +11,6 @@ import Can from '@/components/elements/Can'; import SpinnerOverlay from '@/components/elements/SpinnerOverlay'; import getServerAllocations from '@/api/swr/getServerAllocations'; import isEqual from 'react-fast-compare'; -import { Allocation } from '@/api/server/getServer'; const NetworkContainer = () => { const [ loading, setLoading ] = useState(false);