Skip to content

Commit 163014d

Browse files
committed
Starting redux
1 parent c4d5ca0 commit 163014d

15 files changed

+239
-74
lines changed

.editorconfig

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# http://editorconfig.org
2+
root = true
3+
4+
[*]
5+
charset = utf-8
6+
end_of_line = lf
7+
indent_size = 2
8+
indent_style = space
9+
insert_final_newline = true
10+
max_line_length = 120
11+
trim_trailing_whitespace = true
12+
13+
[*.md]
14+
max_line_length = 0
15+
trim_trailing_whitespace = false
16+
17+
[COMMIT_EDITMSG]
18+
max_line_length = 0

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"bootstrap": "^4.1.3",
77
"prop-types": "^15.6.2",
88
"react": "^16.4.2",
9+
"react-bootstrap": "^0.32.3",
910
"react-dom": "^16.4.2",
1011
"react-redux": "^5.0.7",
1112
"react-router-dom": "^4.3.1",

src/App.css

-28
This file was deleted.

src/App.js

-21
This file was deleted.

src/App.test.js

-9
This file was deleted.

src/constants/actionTypes.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export const SIGNUP_REQUEST = 'SIGNUP_REQUEST';
2+
export const SIGNUP_SUCCESS = 'SIGNUP_SUCCESS';
3+
export const SIGNUP_FAILURE = 'SIGNUP_FAILURE';
4+
5+
export const LOGIN_REQUEST = 'LOGIN_REQUEST';
6+
export const LOGIN_SUCCESS = 'LOGIN_SUCCESS';
7+
export const LOGIN_FAILURE = 'LOGIN_FAILURE';
8+
9+
export const LOGOUT_REQUEST = 'LOGOUT_REQUEST';
10+
export const LOGOUT_SUCCESS = 'LOGOUT_SUCCESS';
11+
export const LOGOUT_FAILURE = 'LOGOUT_FAILURE';

src/containers/main/App.js

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import React from 'react';
2+
import {
3+
BrowserRouter as Router,
4+
Route,
5+
Switch,
6+
Redirect,
7+
Link
8+
} from 'react-router-dom';
9+
import { connect } from 'react-redux';
10+
import PropTypes from 'prop-types';
11+
import Login from '../login/Login';
12+
import Signup from '../signup/Signup';
13+
// import PrivateRoute from '../privateRoute/PrivateRoute';
14+
import { logout } from '../../actions/user.actions';
15+
16+
class App extends React.PureComponent {
17+
18+
static propTypes = {
19+
user: PropTypes.shape({}).isRequired,
20+
logout: PropTypes.func.isRequired
21+
}
22+
23+
/**
24+
* Logout Handler
25+
*/
26+
logoutHandler = () => {
27+
const { logout } = this.props;
28+
logout();
29+
};
30+
31+
render() {
32+
const { user } = this.props;
33+
34+
return (
35+
<Router>
36+
<div>
37+
<nav className="navbar navbar-expand-lg navbar-light bg-light">
38+
<div className="navbar-brand">APP</div>
39+
<button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
40+
<span className="navbar-toggler-icon"></span>
41+
</button>
42+
<div className="collapse navbar-collapse" id="navbarNav">
43+
<ul className="navbar-nav">
44+
<li className="nav-item">
45+
<div className="nav-item nav-link">
46+
<Link to="/login">Login</Link>
47+
</div>
48+
</li>
49+
<li className="nav-item">
50+
<div className="nav-item nav-link">
51+
<Link to="/signup">Signup</Link>
52+
</div>
53+
</li>
54+
</ul>
55+
</div>
56+
</nav>
57+
<div className="page-container">
58+
<Switch>
59+
<Route exact path="/login" component={Login} />
60+
<Route path="/signup" component={Signup} />
61+
<Redirect to="/login" />
62+
</Switch>
63+
</div>
64+
</div>
65+
</Router>
66+
);
67+
}
68+
}
69+
70+
const mapStateToProps = (state) => ({
71+
user: state.user
72+
});
73+
74+
const mapDispatchToProps = {
75+
logout
76+
};
77+
78+
export default connect(mapStateToProps, mapDispatchToProps)(App);

src/index.css

-5
This file was deleted.

src/index.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
import React from 'react';
2-
import ReactDOM from 'react-dom';
3-
import './index.css';
4-
import App from './App';
2+
import 'bootstrap/dist/css/bootstrap.min.css';
3+
import { render } from 'react-dom';
4+
import { Provider } from 'react-redux';
55
import registerServiceWorker from './registerServiceWorker';
6+
import App from './containers/main/App';
7+
import configureStore from './store/configureStore';
68

7-
ReactDOM.render(<App />, document.getElementById('root'));
9+
const store = configureStore();
10+
11+
render(
12+
<Provider store={store}>
13+
<App />
14+
</Provider>,
15+
document.getElementById('root'),
16+
);
817
registerServiceWorker();

src/logo.svg

-7
This file was deleted.

src/reducers/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { combineReducers } from 'redux';
2+
3+
import user from './user.reducer';
4+
5+
const rootReducer = combineReducers({
6+
user
7+
});
8+
9+
export default rootReducer;

src/reducers/user.reducer.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import {
2+
SIGNUP_REQUEST,
3+
SIGNUP_SUCCESS,
4+
SIGNUP_FAILURE,
5+
LOGIN_REQUEST,
6+
LOGIN_SUCCESS,
7+
LOGIN_FAILURE,
8+
LOGOUT_SUCCESS,
9+
LOGOUT_FAILURE,
10+
} from '../constants/actionTypes';
11+
12+
const user = (state = { isFetching: false }, action) => {
13+
switch (action.type) {
14+
case SIGNUP_REQUEST:
15+
return {
16+
...state,
17+
isFetching: true,
18+
};
19+
case SIGNUP_SUCCESS:
20+
return {
21+
...state,
22+
isFetching: true,
23+
};
24+
case SIGNUP_FAILURE:
25+
return {
26+
...state,
27+
isFetching: false
28+
};
29+
case LOGIN_REQUEST:
30+
return {
31+
...state,
32+
isFetching: true
33+
};
34+
case LOGIN_SUCCESS:
35+
return {
36+
...state,
37+
data: action.data,
38+
isAuth: true,
39+
isFetching: false
40+
};
41+
case LOGIN_FAILURE:
42+
return {
43+
...state,
44+
isAuth: false,
45+
isFetching: false
46+
};
47+
case LOGOUT_SUCCESS:
48+
return {};
49+
case LOGOUT_FAILURE:
50+
return {
51+
...state,
52+
isAuth: true,
53+
isFetching: false,
54+
};
55+
default:
56+
return state;
57+
}
58+
};
59+
60+
export default user;

src/store/configureStore.dev.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { createStore, applyMiddleware } from 'redux';
2+
import thunk from 'redux-thunk';
3+
import { composeWithDevTools } from 'redux-devtools-extension';
4+
import logger from 'redux-logger';
5+
import rootReducer from '../reducers';
6+
7+
const configureStore = (preloadedState) => {
8+
const store = createStore(
9+
rootReducer,
10+
preloadedState,
11+
composeWithDevTools(
12+
applyMiddleware(thunk, logger),
13+
),
14+
);
15+
16+
if (module.hot) {
17+
// Enable Webpack hot module replacement for reducers
18+
module.hot.accept('../reducers', () => {
19+
store.replaceReducer(rootReducer);
20+
});
21+
}
22+
23+
return store;
24+
};
25+
26+
export default configureStore;

src/store/configureStore.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
if (process.env.NODE_ENV === 'production') {
2+
module.exports = require('./configureStore.prod');
3+
} else {
4+
module.exports = require('./configureStore.dev');
5+
}

src/store/configureStore.prod.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { createStore, applyMiddleware, compose } from 'redux';
2+
import thunk from 'redux-thunk';
3+
import rootReducer from '../reducers';
4+
5+
const configureStore = (preloadedState) => {
6+
const store = createStore(
7+
rootReducer,
8+
preloadedState,
9+
compose(
10+
applyMiddleware(thunk),
11+
),
12+
);
13+
14+
15+
return store;
16+
};
17+
18+
export default configureStore;

0 commit comments

Comments
 (0)