mirror of
https://github.com/usatiuk/ustk-todolist.git
synced 2025-10-28 23:57:49 +01:00
move loading todos out of render
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import { API_ROOT, getToken } from './util';
|
import { API_ROOT, getToken } from './util';
|
||||||
|
import { loadLists } from './lists';
|
||||||
|
|
||||||
export const LOGIN_SUCCESS = 'LOGIN_SUCCESS';
|
export const LOGIN_SUCCESS = 'LOGIN_SUCCESS';
|
||||||
export const LOGIN_FAIL = 'LOGIN_FAIL';
|
export const LOGIN_FAIL = 'LOGIN_FAIL';
|
||||||
@@ -40,6 +41,7 @@ export function loadUser() {
|
|||||||
if (json.success) {
|
if (json.success) {
|
||||||
localStorage.setItem('jwt', json.data.jwt);
|
localStorage.setItem('jwt', json.data.jwt);
|
||||||
dispatch(loginSuccess(json.data));
|
dispatch(loginSuccess(json.data));
|
||||||
|
dispatch(loadLists());
|
||||||
} else {
|
} else {
|
||||||
dispatch(loginFail(json.error));
|
dispatch(loginFail(json.error));
|
||||||
}
|
}
|
||||||
@@ -63,6 +65,7 @@ export function login(user) {
|
|||||||
if (json.success) {
|
if (json.success) {
|
||||||
localStorage.setItem('jwt', json.data.jwt);
|
localStorage.setItem('jwt', json.data.jwt);
|
||||||
dispatch(loginSuccess(json.data));
|
dispatch(loginSuccess(json.data));
|
||||||
|
dispatch(loadLists());
|
||||||
} else {
|
} else {
|
||||||
dispatch(loginFail(json.error));
|
dispatch(loginFail(json.error));
|
||||||
}
|
}
|
||||||
@@ -91,6 +94,7 @@ export function signup(user) {
|
|||||||
if (json.success) {
|
if (json.success) {
|
||||||
localStorage.setItem('jwt', json.data.jwt);
|
localStorage.setItem('jwt', json.data.jwt);
|
||||||
dispatch(signupSuccess(json.data));
|
dispatch(signupSuccess(json.data));
|
||||||
|
dispatch(loadLists());
|
||||||
} else {
|
} else {
|
||||||
dispatch(signupFail(json.error));
|
dispatch(signupFail(json.error));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,23 +5,24 @@ import InputContainer from '../containers/InputContainer';
|
|||||||
import TodoListContainer from '../containers/TodoListContainer';
|
import TodoListContainer from '../containers/TodoListContainer';
|
||||||
import Header from './Header';
|
import Header from './Header';
|
||||||
|
|
||||||
export default function Todos({ user, loadLists, history }) {
|
export default class Todos extends React.Component {
|
||||||
if (user.user) {
|
componentDidUpdate() {
|
||||||
loadLists();
|
if (!this.props.user.user && !this.props.user.dirty) {
|
||||||
} else if (!user.dirty) {
|
this.props.history.push('/login');
|
||||||
history.push('/login');
|
}
|
||||||
|
}
|
||||||
|
render() {
|
||||||
|
return (
|
||||||
|
<div id="todos">
|
||||||
|
<Header />
|
||||||
|
<InputContainer />
|
||||||
|
<TodoListContainer />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return (
|
|
||||||
<div id="todos">
|
|
||||||
<Header />
|
|
||||||
<InputContainer />
|
|
||||||
<TodoListContainer />
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Todos.propTypes = {
|
Todos.propTypes = {
|
||||||
loadLists: PropTypes.func.isRequired,
|
|
||||||
history: PropTypes.object.isRequired,
|
history: PropTypes.object.isRequired,
|
||||||
user: PropTypes.object.isRequired,
|
user: PropTypes.object.isRequired,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -3,23 +3,10 @@ import { withRouter } from 'react-router-dom';
|
|||||||
|
|
||||||
import Todos from '../components/Todos';
|
import Todos from '../components/Todos';
|
||||||
|
|
||||||
import { loadLists } from '../actions/lists';
|
|
||||||
|
|
||||||
function mapStateToProps(state) {
|
function mapStateToProps(state) {
|
||||||
return {
|
return {
|
||||||
user: state.user,
|
user: state.user,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function mapDispatchToProps(dispatch) {
|
export default withRouter(connect(mapStateToProps)(Todos));
|
||||||
return {
|
|
||||||
loadLists: () => dispatch(loadLists()),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export default withRouter(
|
|
||||||
connect(
|
|
||||||
mapStateToProps,
|
|
||||||
mapDispatchToProps,
|
|
||||||
)(Todos),
|
|
||||||
);
|
|
||||||
|
|||||||
Reference in New Issue
Block a user