Skip to content

Commit

Permalink
v0.2.0: + user data
Browse files Browse the repository at this point in the history
  • Loading branch information
RubaXa committed Jun 16, 2016
1 parent b02c3e0 commit 12f6fcf
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redux-octavius",
"version": "0.1.0",
"version": "0.2.0",
"private": true,
"description": "Боль и унижение!",
"main": "index.js",
Expand Down
17 changes: 12 additions & 5 deletions src/actions/auth.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import {USER_LOGIN_MEETHOD} from '../constants/api';
import {LOGIN_REQUEST, LOGIN_SUCCESS, LOGIN_FAIL} from '../constants/auth';
import {fetchUser} from './user';

export const login = (email, password) => ({
api: USER_LOGIN_MEETHOD,
data: {email, password},
types: [LOGIN_REQUEST, LOGIN_SUCCESS, LOGIN_FAIL],
});
export const login = (email, password) => async (dispatch) => {
const response = await dispatch({
api: USER_LOGIN_MEETHOD,
data: {email, password},
types: [LOGIN_REQUEST, LOGIN_SUCCESS, LOGIN_FAIL],
});

if (!response.error) {
return dispatch(fetchUser(email));
}
};
8 changes: 8 additions & 0 deletions src/actions/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {USER_SHORT_METHOD} from '../constants/api';
import {USER_FETCH, USER_FETCH_FAIL, USER_FETCH_SUCCESS} from '../constants/user';

export const fetchUser = (email) => ({
api: USER_SHORT_METHOD,
data: {email},
types: [USER_FETCH, USER_FETCH_SUCCESS, USER_FETCH_FAIL]
});
2 changes: 1 addition & 1 deletion src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ import AuthForm from './AuthForm';
export default class App extends Component {
render() {
const {auth} = this.props;
return !auth.state ? <AuthForm/> : <h1>Привет!</h1>;
return !auth.state ? <AuthForm/> : <h1>Привет, {auth.email}!</h1>;
}
}
2 changes: 2 additions & 0 deletions src/constants/api.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export const USER_LOGIN_MEETHOD = 'user/login';
export const USER_SHORT_METHOD = 'user/short';

3 changes: 3 additions & 0 deletions src/constants/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const USER_FETCH = 'USER_FETCH';
export const USER_FETCH_SUCCESS = 'USER_FETCH_SUCCESS';
export const USER_FETCH_FAIL = 'USER_FETCH_FAIL';
9 changes: 6 additions & 3 deletions src/reducers/auth.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {LOGIN_REQUEST, LOGIN_FAIL, LOGIN_SUCCESS} from '../constants/auth';
import {LOGIN_REQUEST, LOGIN_FAIL} from '../constants/auth';
import {USER_FETCH_SUCCESS, USER_FETCH_FAIL} from '../constants/user';

const initialState = {
state: false,
Expand All @@ -12,10 +13,12 @@ export default (state = initialState, action) => {
case LOGIN_REQUEST:
return {...state, busy: true};

case LOGIN_SUCCESS:
return {...state, state: true, busy: false};
case USER_FETCH_SUCCESS:
const email = `${action.result.login}@${action.result.domain}`;
return {...state, ...action.result, email, state: true, busy: false};

case LOGIN_FAIL:
case USER_FETCH_FAIL:
return {...state, error: true, busy: false};

default:
Expand Down

0 comments on commit 12f6fcf

Please sign in to comment.