Skip to content

Commit 8d54ba9

Browse files
committed
Chenge layout -> drawer access with localStateRedux
replace some default exports with const
1 parent f2d44a4 commit 8d54ba9

File tree

8 files changed

+108
-37
lines changed

8 files changed

+108
-37
lines changed

ClientApp/Views/Dashboard/Components/Home.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from "react";
22
import { RouteComponentProps } from "react-router-dom";
33

4-
export default class Home extends React.PureComponent<RouteComponentProps<any> | undefined> {
4+
export class Home extends React.PureComponent<RouteComponentProps<any> | undefined> {
55
render() {
66
return (
77
<div>

ClientApp/Views/Dashboard/Components/Lecturers/Lecturers.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type LecturersProps =
1616
& typeof lecturersActions.mapCreators.lecturers
1717
& RouteComponentProps<any>;
1818

19-
class Lecturers extends React.PureComponent<LecturersProps> {
19+
class LecturersClass extends React.PureComponent<LecturersProps> {
2020
render() {
2121
return (
2222
<Grid>
@@ -58,7 +58,7 @@ export const FormProgress = connect(
5858
() => ({}),
5959
)(LinearProgress);
6060

61-
export default withRouter<RouteComponentProps<any> | undefined>(connect(
61+
export const Lecturers = withRouter<RouteComponentProps<any> | undefined>(connect(
6262
null,
6363
lecturersActions.mapDispatch.lecturers,
64-
)(Lecturers as any) as any);
64+
)(LecturersClass as any) as any);
Lines changed: 77 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import * as React from "react";
2+
import { matchPath, RouteComponentProps, withRouter } from "react-router";
3+
import { Link } from "react-router-dom";
24
import {
35
Drawer,
46
DrawerContent,
@@ -11,19 +13,49 @@ import {
1113
ListItemText,
1214
} from "rmwc/List";
1315

16+
import { connectState, connectWithComponentId, IComponentId, ILocalReducer } from "@app/middlewares/redux-subreducer";
17+
import { compose } from "redux";
18+
import { lecturersActions } from "../../actions";
19+
import Module from "../../module";
20+
1421
interface IProps {
15-
open?: boolean;
1622
onClose?: () => void;
1723
onOpen?: () => void;
1824
}
1925

20-
export default class Counter extends React.PureComponent<IProps, { open: boolean }> {
21-
constructor(props: IProps) {
26+
interface IState {
27+
open: boolean;
28+
}
29+
30+
type Props = IProps;
31+
32+
interface IMenuLink {
33+
key: number;
34+
title: string;
35+
url: string;
36+
path: string;
37+
exact?: boolean;
38+
strict?: boolean;
39+
}
40+
41+
const initState: IState = {
42+
open: true,
43+
};
44+
45+
export class DrawerWraperClass extends React.Component<IProps & RouteComponentProps<{}>, IState> {
46+
links: IMenuLink[];
47+
48+
constructor(props: IProps & RouteComponentProps<{}>) {
2249
super(props);
50+
this.state = initState;
2351

24-
this.state = {
25-
open: props.open || true,
26-
};
52+
this.links = Module.navigation.map<IMenuLink>(({ title, url, path, exact }, key) => ({
53+
key,
54+
title: title || "?",
55+
url,
56+
path: path || url,
57+
exact,
58+
}));
2759
}
2860
switchDrawer = () => {
2961
this.setState({
@@ -44,20 +76,47 @@ export default class Counter extends React.PureComponent<IProps, { open: boolean
4476
return (
4577
<Drawer persistent={true} open={this.state.open} onClose={this.closeDrawer} onOpen={this.openDrawer}>
4678
<DrawerContent>
47-
<ListItem>
48-
<ListItemGraphic>star_border</ListItemGraphic>
49-
<ListItemText>Counter</ListItemText>
50-
</ListItem>
51-
<ListItem>
52-
<ListItemGraphic>star_border</ListItemGraphic>
53-
<ListItemText>Pizza</ListItemText>
54-
</ListItem>
55-
<ListItem>
56-
<ListItemGraphic>star_border</ListItemGraphic>
57-
<ListItemText>Icecream</ListItemText>
58-
</ListItem>
79+
{this.getElements()}
5980
</DrawerContent>
6081
</Drawer>
6182
);
6283
}
84+
85+
private getElements = () =>
86+
this.links.map(({ title, url }, index) => (
87+
<ListItem key={title} wrap={false}>
88+
{/* <Link to={url}> */}
89+
<ListItemGraphic>star_border</ListItemGraphic>
90+
<ListItemText>{title}</ListItemText>
91+
{/* </Link> */}
92+
</ListItem>
93+
))
94+
95+
private getActive = () => {
96+
let pathname = "/";
97+
if (!!this.props.location) {
98+
pathname = this.props.location.pathname;
99+
}
100+
const match = this.links.find(link => !!matchPath(pathname, link));
101+
if (match) {
102+
return match.key;
103+
}
104+
return 0;
105+
}
63106
}
107+
108+
const switchDrawer = (props, prevState) => ({ open: !prevState.open });
109+
110+
const stateReducer = (reducer: ILocalReducer<Props & IComponentId, IState>) => reducer
111+
.on(lecturersActions.actions.ui.switchDrawer, switchDrawer);
112+
113+
const enhance = compose<React.ComponentClass>(
114+
withRouter,
115+
connectState<Props, IState>(
116+
initState,
117+
stateReducer,
118+
"drawer",
119+
),
120+
);
121+
122+
export const DrawerWrapper = enhance(DrawerWraperClass);
Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,35 @@
11
import * as React from "react";
2+
import { connect } from "react-redux";
3+
import { RouteComponentProps, withRouter } from "react-router";
4+
import { compose } from "redux";
25
import {
36
ToolbarFixedAdjust,
47
} from "rmwc/Toolbar";
5-
import Drawer from "./Drawer";
8+
import { DrawerWrapper } from "./Drawer";
69
import NavMenu from "./NavMenu";
710

8-
export class Layout extends React.Component {
9-
Drawer?: Drawer | null;
10-
switchDrawer = () => {
11-
if (!!this.Drawer) {
12-
this.Drawer.switchDrawer();
13-
}
14-
}
11+
import { lecturersActions } from "../../actions";
12+
13+
export class LayoutClass extends React.Component<typeof lecturersActions.mapCreators.ui & RouteComponentProps<{}>> {
1514
render() {
1615
return [
17-
<NavMenu switchDrawer={this.switchDrawer} key={"drawer"} />,
16+
<NavMenu switchDrawer={this.props.actions.switchDrawer} key={"drawer"} />,
1817
(
1918
<ToolbarFixedAdjust key={"toolbar"} className={"dashboard"}>
20-
<Drawer ref={ref => { this.Drawer = ref; }} />
19+
<DrawerWrapper />
2120
<main>{this.props.children}</main>
2221
</ToolbarFixedAdjust>
2322
),
2423
];
2524
}
2625
}
26+
27+
const enhance = compose<React.ComponentClass>(
28+
withRouter,
29+
connect(
30+
null,
31+
lecturersActions.mapDispatch.ui,
32+
),
33+
);
34+
35+
export const Layout = enhance(LayoutClass);

ClientApp/Views/Dashboard/Components/Shared/NavMenu.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
ToolbarTitle,
99
} from "rmwc/Toolbar";
1010

11-
import TabList from "./TabList";
11+
import { TabList } from "./TabList";
1212

1313
interface IProps {
1414
switchDrawer: () => void;

ClientApp/Views/Dashboard/Components/Shared/TabList.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ interface IMenuLink {
2828

2929
type Props = RouteComponentProps<{}>;
3030

31-
class TabList extends React.PureComponent<Props, {}> {
31+
class TabListClass extends React.PureComponent<Props, {}> {
3232
links: IMenuLink[];
3333

3434
constructor(props: Props) {
@@ -75,4 +75,4 @@ class TabList extends React.PureComponent<Props, {}> {
7575
}
7676
}
7777

78-
export default withRouter<{}>(TabList);
78+
export const TabList = withRouter<{}>(TabListClass);
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export { default as Home } from "./Home";
1+
export { Home } from "./Home";
22
export { Layout } from "./Shared/Layout";
3-
export { default as Lecturers } from "./Lecturers/Lecturers";
3+
export { Lecturers } from "./Lecturers/Lecturers";

ClientApp/Views/Dashboard/actions/lecturers.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import { ILecturer } from "@app/store/database";
88
const from = "Views/Dashboard";
99

1010
export const { actions, creators, mapDispatch, mapCreators } = prepareActions({
11+
ui: {
12+
switchDrawer: createAction("Switch Drawer", from),
13+
},
1114
lecturers: {
1215
setLoading: createAction("Lecturers loading...", from),
1316
setLoaded: createAction("Lecturers loaded", from),

0 commit comments

Comments
 (0)