Skip to content

Commit e11d135

Browse files
committed
Fix React-Router4
1 parent f1d6ed5 commit e11d135

File tree

22 files changed

+153
-9039
lines changed

22 files changed

+153
-9039
lines changed

build/asset-manifest.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

build/favicon.ico

-24.3 KB
Binary file not shown.

build/index.html

Lines changed: 0 additions & 1 deletion
This file was deleted.

build/main.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

build/manifest.json

Lines changed: 0 additions & 15 deletions
This file was deleted.

build/service-worker.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

build/static/media/index.948fa350.scss

Lines changed: 0 additions & 4 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,12 @@
1313
"dependencies": {
1414
"antd": "^3.4.0",
1515
"babel-plugin-import": "^1.1.1",
16-
"bizcharts": "^3.1.10",
17-
"classnames": "^2.2.5",
18-
"constant-mirror": "^1.1.1",
1916
"js-cookie": "^2.1.4",
2017
"react": "^16.6.3",
21-
"react-copy-to-clipboard": "^5.0.0",
2218
"react-dom": "^16.6.3",
2319
"react-redux": "^5.0.6",
2420
"react-router": "^3.0.2",
21+
"react-router-dom": "^4.3.1",
2522
"react-router-redux": "^4.0.8",
2623
"redux": "^3.7.2",
2724
"redux-actions": "^2.2.1",

src/app/App.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import React from "react";
2+
import { Link } from "react-router-dom";
3+
import { Route, Switch, Redirect } from "react-router-dom";
4+
import { Layout, Menu, Dropdown, Icon } from "antd";
5+
6+
import App from "./containers/index";
7+
import HomePage from "./containers/homePage/index.tsx";
8+
import SubPage from "./containers/subPage/index";
9+
import Login from "./containers/login/index";
10+
11+
const { Header, Content, Footer } = Layout;
12+
13+
export default class EnrtyRouter extends React.Component {
14+
constructor(props) {
15+
super(props);
16+
}
17+
18+
handleMenuClick = e => {
19+
if (e.key === "exit") {
20+
sessionStorage.removeItem("access_token");
21+
window.location.hash = "login";
22+
}
23+
};
24+
25+
renderMainPage() {
26+
let selectMenu = window.location.hash.split("/")[1]
27+
? window.location.hash.split("/")[1]
28+
: "homePage";
29+
30+
const menu = (
31+
<Menu onClick={this.handleMenuClick}>
32+
<Menu.Item key="exit">exit</Menu.Item>
33+
</Menu>
34+
);
35+
36+
return (
37+
<React.Fragment>
38+
{/* Header */}
39+
<Header className="header">
40+
<Menu
41+
theme="dark"
42+
mode="horizontal"
43+
defaultSelectedKeys={["homePage"]}
44+
style={{ lineHeight: "64px" }}
45+
selectedKeys={[selectMenu]}
46+
>
47+
<Menu.Item key="homePage">
48+
<Link to="/">HomePage</Link>
49+
</Menu.Item>
50+
<Menu.Item key="subPage">
51+
<Link to="/subPage">SubPage</Link>
52+
</Menu.Item>
53+
54+
<Menu.Item key="user" style={{ float: "right" }}>
55+
<Dropdown overlay={menu} onClick={this.handleMenuClick}>
56+
<a className="ant-dropdown-link">
57+
Guest<Icon type="down" />
58+
</a>
59+
</Dropdown>
60+
</Menu.Item>
61+
</Menu>
62+
</Header>
63+
64+
{/* main content */}
65+
<Switch>
66+
<Route
67+
exact
68+
path="/"
69+
component={() => {
70+
if (!sessionStorage.getItem("access_token")) {
71+
return <Redirect to="/login" />;
72+
} else {
73+
return <HomePage />;
74+
}
75+
}}
76+
/>
77+
<Route path="/subPage" component={SubPage} />
78+
<Route path="/login" component={Login} />
79+
</Switch>
80+
81+
</React.Fragment>
82+
);
83+
}
84+
85+
render() {
86+
const isAuthenticated = sessionStorage.getItem("access_token");
87+
return (
88+
<React.Fragment>
89+
{isAuthenticated ? this.renderMainPage() : <Login />}
90+
</React.Fragment>
91+
);
92+
}
93+
}

src/app/components/NotFoundPage/index.jsx

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/app/components/NotFoundPage/index.scss

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/app/containers/homePage/action.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
/**
2-
* Created by Allan on 06/24/2018
2+
* 有关于 mock 数据!!!
3+
* 接口请求地址必须与 ./scripts/server.js 中接口地址保持一致
34
*/
45
import cFetch from "../../utils/cFetch";
56
import { GET_V1_SONG } from "./actionTypes";
67

78
export const fetchList = () => (dispatch:any) => {
8-
// 此处接口地址必须与 scripts/server.js mock中地址一致
99
return new Promise((resolve, reject)=>{
1010
cFetch("/v1/main/list", { method: "POST" })
1111
.then((result: any) => {
1212
// store the result to redux
1313
dispatch({ type: GET_V1_SONG, result });
14+
// export result to outside
1415
resolve(result||{success: "ok"})
1516
})
1617
.catch(()=>{

src/app/containers/homePage/index.tsx

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,28 @@
44
import React from "react";
55
import { bindActionCreators } from "redux";
66
import { connect } from "react-redux";
7-
import { withRouter } from "react-router";
87
import { Layout } from "antd";
98
import { Props, State } from "./type";
10-
import styles from "./index.style";
11-
12-
import { fetchList } from "./action"
9+
import styles from "./index.style"; // css modules
10+
import { fetchList } from "./action";
1311

1412
class App extends React.Component<Props, State> {
1513
constructor(props: Props) {
1614
super(props);
1715
this.state = {
18-
msg: "This is a TypeScript Example!",
19-
num: 1,
20-
// a: 11 // it will be wrong if it not be defined in typs.ts
16+
welcomeText: "Welcome to React!",
17+
num: 1
2118
};
2219
}
2320

24-
componentDidMount(){
25-
const { fetchList } =this.props.actions;
26-
fetchList().then((res:any)=>{
27-
console.log(res)
21+
componentDidMount() {
22+
this.props.fetchList().then((res: any) => {
23+
console.log("接口返回数据:", res);
2824
});
2925
}
3026

3127
render() {
32-
console.log(this.props, "res")
33-
const { homeData={} } = this.props;
34-
if (!homeData.result) {
35-
return null;
36-
}
37-
28+
const { homeData = {} } = this.props;
3829
return (
3930
<Layout>
4031
<div className={styles.homePage}>
@@ -48,26 +39,28 @@ class App extends React.Component<Props, State> {
4839
alt="logo"
4940
/>
5041
</a>
51-
<h1>{this.state.msg}</h1>
52-
<p className={styles.mp3Name}>{homeData.result.name} is from Redux's store!</p>
53-
<audio controls="controls" src={homeData.result.url}></audio>
42+
<h1>{this.state.welcomeText}</h1>
43+
<p className={styles.mp3Name}>
44+
{homeData.name} is from Redux's store!
45+
</p>
46+
<audio controls src={homeData.url} />
5447
</div>
5548
</Layout>
5649
);
5750
}
5851
}
5952

60-
const mapStateToProps = (state:any) => ({
53+
const mapStateToProps = (state: any) => ({
6154
homeData: state.homeData
6255
});
6356

64-
const mapDispatchToProps = (dispatch:any) => ({
65-
actions: bindActionCreators(
57+
const mapDispatchToProps = (dispatch: any) => {
58+
return bindActionCreators(
6659
{
6760
fetchList
6861
},
6962
dispatch
70-
)
71-
});
63+
);
64+
};
7265

7366
export default connect(mapStateToProps, mapDispatchToProps)(App);

src/app/containers/homePage/reducer.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,12 @@
22
* Created by Allan on 06/24/2018
33
*/
44
import { GET_V1_SONG } from "./actionTypes"
5-
import { HomeState } from "./type"
65

7-
// 初始化所有数据
8-
function setInitState() {
9-
const data = JSON.parse(JSON.stringify({
10-
msg: "test"
11-
}));
12-
return data;
13-
}
146

15-
export function homeData(state: HomeState = setInitState(), action: any) {
7+
export function homeData(state={}, action: any) {
168
switch (action.type) {
179
case GET_V1_SONG:
18-
return action.result;
10+
return action.result.result;
1911
default:
2012
return state;
2113
}

src/app/containers/homePage/type.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@
55
export interface Props {
66
homeData: any;
77
actions: any;
8+
fetchList: Function
89
}
910

1011
export interface State {
11-
msg: string;
12+
welcomeText: string;
1213
num: Number;
1314
}
1415

1516
/**
1617
* For reducer
1718
*/
1819
export interface HomeState {
19-
msg: string;
20+
welcomeText: string;
2021
num: Number;
2122
}
2223

src/app/containers/index.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Created by Allan on 2017/09/13.
33
*/
44
import React from "react";
5-
import { Link } from "react-router";
5+
import { Link } from "react-router-dom";
66
import { Layout, Menu, Dropdown, Icon } from "antd";
77
import Login from "../containers/login/index";
88
import "./index.less";
@@ -61,7 +61,7 @@ class App extends React.Component {
6161
</Menu.Item>
6262
</Menu>
6363
</Header>
64-
64+
6565
{/* main content */}
6666
<Content className="main-layout-content">{this.props.children}</Content>
6767

src/app/containers/index.less

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
.footer {
2-
position: fixed;
3-
width: 100%;
4-
bottom: 0;
5-
}
1+
// .footer {
2+
// position: fixed;
3+
// width: 100%;
4+
// bottom: 0;
5+
// }

src/app/containers/subPage/index.jsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,18 @@ export class SubPage extends React.Component {
3737
};
3838

3939
render() {
40-
const { img, visible } = this.state;
4140
const { homeData={} } = this.props;
42-
if (!homeData.result) {
43-
return null;
44-
}
41+
const { img, visible } = this.state;
42+
4543
return (
4644
<div className="sub-page">
4745
<img src={img} alt="" />
4846
<Button onClick={this.showModal}>Click to open a modal</Button>
4947

5048
<p className="mp3-title">
51-
{homeData.result.name} is from Redux's store!
49+
{homeData.name} is from Redux's store!
5250
</p>
53-
<audio controls="controls" src={homeData.result.url}>
51+
<audio controls="controls" src={homeData.url}>
5452
Your browser does not support the audio tag.
5553
</audio>
5654

0 commit comments

Comments
 (0)