Skip to content

Commit 0d28ca3

Browse files
first commit
0 parents  commit 0d28ca3

22 files changed

+13150
-0
lines changed

.eslintrc.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module.exports = {
2+
"parser": "babel-eslint",
3+
"env": {
4+
"browser": true,
5+
"es6": true,
6+
},
7+
"extends": [
8+
"eslint:recommended",
9+
"plugin:react/recommended"
10+
],
11+
"plugins": [
12+
"react"
13+
],
14+
"parserOptions": {
15+
"ecmaFeatures": {
16+
"jsx": true
17+
}
18+
},
19+
"rules": {
20+
"no-console": 0,
21+
"no-unused-vars": 0,
22+
"indent": 0,
23+
"semi": [
24+
"error",
25+
"always"
26+
]
27+
}
28+
};

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

app/index.jsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React from 'react';
2+
import { render } from 'react-dom';
3+
import { BrowserRouter } from 'react-router-dom';
4+
import { Provider } from 'react-redux';
5+
6+
import store from './store/store';
7+
import Layout from './main/layout/Layout';
8+
9+
render(
10+
<Provider store={store}>
11+
<BrowserRouter>
12+
<Layout />
13+
</BrowserRouter>
14+
</Provider>
15+
,document.getElementById('app')
16+
);

app/index.pug

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#app

app/main/footer/Footer.jsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from 'react';
2+
3+
class Footer extends React.Component {
4+
render() {
5+
return (
6+
<footer>
7+
<h3>Footer</h3>
8+
</footer>
9+
);
10+
}
11+
}
12+
13+
export default Footer;

app/main/header/Header.jsx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
import { bindActionCreators } from 'redux';
4+
import { connect } from 'react-redux';
5+
import { Link } from 'react-router-dom';
6+
7+
class Header extends React.Component {
8+
9+
render() {
10+
11+
const { a, b } = this.props;
12+
13+
return (
14+
<header>
15+
<h3>Header A:{a} B:{b}</h3>
16+
<nav>
17+
<ul>
18+
<li><Link to='/'>Home</Link></li>
19+
<li><Link to='/about'>About</Link></li>
20+
</ul>
21+
</nav>
22+
</header>
23+
);
24+
}
25+
}
26+
27+
Header.propTypes = {
28+
a: PropTypes.number,
29+
b: PropTypes.number
30+
};
31+
32+
const putStateToProps = (state) => {
33+
return {
34+
a: state.test.a,
35+
b: state.rest.b
36+
};
37+
};
38+
39+
const putActionsToProps = (dispatch) => {
40+
return {};
41+
};
42+
43+
export default connect(putStateToProps, putActionsToProps)(Header);

app/main/layout/Layout.jsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React from 'react';
2+
3+
import Header from 'app/main/header/Header';
4+
import Footer from 'app/main/footer/Footer';
5+
import Pages from 'app/pages/Pages';
6+
7+
8+
class Layout extends React.Component {
9+
render() {
10+
return (
11+
<React.Fragment>
12+
<Header />
13+
<Pages />
14+
<Footer />
15+
</React.Fragment>
16+
);
17+
}
18+
}
19+
20+
export default Layout;

app/pages/AsyncComponent.jsx

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import React, { PureComponent } from 'react';
2+
import PropTypes from 'prop-types';
3+
4+
export default class AsyncComponent extends PureComponent {
5+
6+
constructor(props) {
7+
super(props);
8+
9+
this.state = {
10+
Component: null
11+
};
12+
}
13+
14+
unCamelCase(s) {
15+
return s.split(/(?=[A-Z])/).join('-').toLowerCase();
16+
}
17+
18+
componentWillMount() {
19+
if (!this.state.Component) {
20+
this.props.moduleProvider().then(( component ) => {
21+
document.body.setAttribute('page', this.unCamelCase(component.default.name));
22+
this.setState({ component: component.default });
23+
});
24+
}
25+
}
26+
27+
componentWillUnmount() {
28+
document.body.removeAttribute('page');
29+
}
30+
31+
render() {
32+
const Component = this.state.component;
33+
return (
34+
<React.Fragment>
35+
{/* {Component ? <Component {...this.props} /> : null} */}
36+
{Component ? <Component /> : null}
37+
</React.Fragment>
38+
);
39+
}
40+
}
41+
42+
AsyncComponent.propTypes = {
43+
moduleProvider: PropTypes.func
44+
};

app/pages/Pages.jsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import React from 'react';
2+
import { Switch, Route } from 'react-router-dom';
3+
import AsyncComponent from './AsyncComponent';
4+
5+
const Home = () => import('./home/Home');
6+
const About = () => import('./about/About');
7+
8+
class Pages extends React.Component {
9+
render() {
10+
return (
11+
<main>
12+
<Switch>
13+
<Route exact path='/' component={() => <AsyncComponent moduleProvider={Home} />} />
14+
<Route path='/about' component={() => <AsyncComponent moduleProvider={About} />} />
15+
</Switch>
16+
</main>
17+
);
18+
}
19+
}
20+
21+
export default Pages;

app/pages/about/About.jsx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
import { bindActionCreators } from 'redux';
4+
import { connect } from 'react-redux';
5+
6+
7+
class About extends React.Component {
8+
render() {
9+
10+
const { a, b } = this.props;
11+
12+
return (
13+
<h1>About A:{a} B:{b}</h1>
14+
);
15+
}
16+
}
17+
18+
About.propTypes = {
19+
a: PropTypes.number,
20+
b: PropTypes.number
21+
};
22+
23+
const putStateToProps = (state) => {
24+
return {
25+
a: state.test.a,
26+
b: state.rest.b
27+
};
28+
};
29+
30+
const putActionsToProps = (dispatch) => {
31+
return {
32+
};
33+
};
34+
35+
export default connect(putStateToProps, putActionsToProps)(About);

app/pages/about/about.styl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[page*="about"]
2+
// background: #CCC

app/pages/home/Home.jsx

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
import { bindActionCreators } from 'redux';
4+
import { connect } from 'react-redux';
5+
6+
import { changeA } from 'store/actions/test';
7+
import { changeB } from 'store/actions/rest';
8+
9+
class Home extends React.Component {
10+
11+
constructor(props) {
12+
super(props);
13+
}
14+
15+
clickA(i,e) {
16+
e.preventDefault();
17+
const { changeA } = this.props;
18+
changeA(i);
19+
}
20+
21+
clickB(i, e) {
22+
e.preventDefault();
23+
const { changeB } = this.props;
24+
changeB(i);
25+
}
26+
27+
28+
render() {
29+
30+
const { a, b } = this.props;
31+
32+
return (
33+
<React.Fragment>
34+
<h1>Home A:{a} B:{b}</h1>
35+
<p><a href="" onClick={(e) => this.clickA(1,e) }>Change A to 1</a></p>
36+
<p><a href="" onClick={(e) => this.clickA(2, e)}>Change A to 2</a></p>
37+
<p><a href="" onClick={(e) => this.clickB(1, e)}>Change B to 1</a></p>
38+
<p><a href="" onClick={(e) => this.clickB(2, e)}>Change B to 2</a></p>
39+
</React.Fragment>
40+
);
41+
}
42+
}
43+
44+
Home.propTypes = {
45+
a: PropTypes.number,
46+
b: PropTypes.number,
47+
changeA: PropTypes.func,
48+
changeB: PropTypes.func,
49+
};
50+
51+
const putStateToProps = (state) => {
52+
return {
53+
a: state.test.a,
54+
b: state.rest.b
55+
};
56+
};
57+
58+
const putActionsToProps = (dispatch) => {
59+
return {
60+
changeA: bindActionCreators(changeA, dispatch),
61+
changeB: bindActionCreators(changeB, dispatch)
62+
};
63+
};
64+
65+
export default connect(putStateToProps, putActionsToProps)(Home);

app/pages/home/home.styl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[page*="home"]
2+
// background: red

app/store/actions/rest.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
import { B } from '../constants/actionTypes';
3+
4+
export const changeB = (newTest) => {
5+
6+
return {
7+
type: B,
8+
payload: newTest
9+
};
10+
};

app/store/actions/test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
import { A } from '../constants/actionTypes';
3+
4+
5+
export const changeA = (newTest) => {
6+
return {
7+
type: A,
8+
payload: newTest
9+
};
10+
};

app/store/constants/actionTypes.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
export const A = 'A';
3+
export const B = 'B';

app/store/reducers/rest.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
3+
const initialState = {
4+
b: 1
5+
};
6+
7+
const rest = (state = initialState, action) => {
8+
9+
const TYPE = action.type || '';
10+
const PAYLOAD = action.payload || null;
11+
12+
const log = false;
13+
let newState;
14+
15+
if (log){
16+
console.log(`[old state] TYPE: ${TYPE} | PAYLOAD:`, PAYLOAD, '| STATE', state);
17+
}
18+
19+
switch (TYPE) {
20+
case 'B':
21+
newState = { ...state, b: PAYLOAD };
22+
break;
23+
default:
24+
newState = state;
25+
break;
26+
}
27+
28+
if (log) {
29+
console.log(`[new state] TYPE: ${TYPE} | PAYLOAD:`, PAYLOAD, '| STATE', newState);
30+
}
31+
32+
return newState;
33+
};
34+
35+
export default rest;

0 commit comments

Comments
 (0)