Skip to content

Commit 3984955

Browse files
authored
Merge pull request #1396 from chhsiao1981/refactor-routes
refactor routes
2 parents f09789c + f95eb04 commit 3984955

File tree

4 files changed

+26
-42
lines changed

4 files changed

+26
-42
lines changed
Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,44 @@
11
import React from "react";
22

3-
interface RouterContextProps<S, A = any> {
3+
type RouterContextProps<S, A = any> = {
44
state: S;
55
actions?: A;
6-
}
6+
};
77

88
type RouterContextType<S, A = any> = React.Context<RouterObjectType<S, A>>;
9+
910
type RouterObjectType<S, A = any> = {
1011
state: S;
1112
actions: A;
1213
};
1314

14-
export function RouterContext<S, A = any>({
15-
state,
16-
actions,
17-
}: RouterContextProps<S, A>): [S, RouterContextType<S, A>] {
15+
export const RouterContext = <S, A = any>(
16+
props: RouterContextProps<S, A>,
17+
): [S, RouterContextType<S, A>] => {
18+
const { state, actions } = props;
1819
return [
1920
state,
2021
React.createContext<RouterObjectType<S, A>>({
2122
actions: actions ? actions : ({} as A),
2223
state,
2324
}),
2425
];
25-
}
26+
};
2627

27-
interface RouterProviderProps<S = any, A = any> {
28+
type RouterProviderProps<S = any, A = any> = {
2829
context: RouterContextType<S>;
2930
state: S;
3031
actions: A;
3132
children: React.ReactNode;
32-
}
33+
};
3334

34-
export function RouterProvider({
35-
context,
36-
actions,
37-
state,
38-
children,
39-
}: RouterProviderProps) {
40-
const props = {
41-
context,
42-
actions,
43-
state,
44-
children,
45-
};
46-
return <RouterComponent propsElement={props} />;
47-
}
35+
export const RouterProvider = (props: RouterProviderProps) => {
36+
return <RouterComponent {...props} />;
37+
};
38+
39+
const RouterComponent = (props: RouterProviderProps) => {
40+
const { context, state, actions, children } = props;
4841

49-
const RouterComponent = ({
50-
propsElement: { context, actions, state, children },
51-
}: {
52-
propsElement: RouterProviderProps;
53-
}) => {
5442
return (
5543
<context.Provider
5644
value={{
@@ -62,5 +50,3 @@ const RouterComponent = ({
6250
</context.Provider>
6351
);
6452
};
65-
66-
export default RouterContext;

src/components/Wrapper/Sidebar.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ export default (props: Props) => {
150150
const uploadDataColor =
151151
sidebarActiveItem === "uploadData" ? "#ffffff" : "#aaaaaa";
152152

153+
// only the admin can import package.
153154
const classNameImportPackage = role === Role.Admin ? undefined : styles.hide;
155+
// only the clinician cannot compose package.
154156
const classNameComposePackage =
155157
role === Role.Clinician ? styles.hide : undefined;
156158

src/components/Wrapper/index.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Page } from "@patternfly/react-core";
2-
import type * as React from "react";
2+
import type { FormEvent, KeyboardEvent, ReactElement } from "react";
33
import { useAppDispatch, useAppSelector } from "../../store/hooks";
44
import AnonSidebar from "./AnonSidebar";
55
import Header from "./Header";
@@ -13,8 +13,8 @@ import {
1313
import { OperationsProvider } from "../NewLibrary/context";
1414

1515
type Props = {
16-
children: React.ReactElement[] | React.ReactElement;
17-
titleComponent?: React.ReactElement;
16+
children: ReactElement[] | ReactElement;
17+
titleComponent?: ReactElement;
1818
};
1919

2020
export default (props: Props) => {
@@ -27,14 +27,14 @@ export default (props: Props) => {
2727
const onNavToggle = () => {
2828
dispatch(setIsNavOpen(!isNavOpen));
2929
};
30-
const onTagToggle = (e: React.FormEvent) => {
30+
const onTagToggle = (e: FormEvent) => {
3131
dispatch(setIsTagExpanded(!isTagExpanded));
3232
};
33-
const onPackageTagToggle = (e: React.FormEvent) => {
33+
const onPackageTagToggle = (e: FormEvent) => {
3434
dispatch(setIsPackageTagExpanded(!isPackageTagExpanded));
3535
};
3636
const onPageResize = (
37-
_event: MouseEvent | TouchEvent | React.KeyboardEvent<Element>,
37+
_event: MouseEvent | TouchEvent | KeyboardEvent<Element>,
3838
data: { mobileView: boolean; windowSize: number },
3939
) => {
4040
if (data.mobileView) {

src/routes.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export const [State, MainRouterContext] = RouterContext<IState, IActions>({
4545
},
4646
});
4747

48-
export const MainRouter = () => {
48+
export default () => {
4949
const location = useLocation();
5050
const dispatch = useAppDispatch();
5151
const [state, setState] = useState(State);
@@ -124,7 +124,7 @@ export const MainRouter = () => {
124124
);
125125
}, [location.pathname, dispatch]);
126126

127-
const element = useRoutes([
127+
return useRoutes([
128128
{
129129
path: "/",
130130
element: <Dashboard />,
@@ -242,8 +242,4 @@ export const MainRouter = () => {
242242
element: <PluginInstall />,
243243
},
244244
]);
245-
246-
return element;
247245
};
248-
249-
export default MainRouter;

0 commit comments

Comments
 (0)