userActions.js 1.41 KB
/**
 * # guideActions.js
 * 
 * App user guide
 * 
 */
'use strict';

import {Actions} from 'react-native-router-flux';
import AppAuthToken from '../../services/AppAuthToken';
import UserService from '../../services/UserService';

const {

	DELETE_TOKEN_REQUEST,
    DELETE_TOKEN_SUCCESS,

    LOGIN_REQUEST,
    LOGIN_SUCCESS,
    LOGIN_FAILURE,

    LOGOUT_REQUEST,
    LOGOUT_SUCCESS,
    LOGOUT_FAILURE,

    SET_SESSION_TOKEN,

    RESET_PASSWORD_REQUEST,
    RESET_PASSWORD_SUCCESS,
    RESET_PASSWORD_FAILURE,

} = require('../../constants/actionTypes').default;


export function loginRequest() {
    return {
        type: LOGIN_REQUEST
    };
}

export function loginSuccess(json) {
    return {
        type: LOGIN_SUCCESS,
        payload: json
    };
}

export function loginFailure(error) {
    return {
        type: LOGIN_FAILURE,
        payload: error
    };
}

export function login(account, password) {
	return dispatch => {
		dispatch(loginRequest());
		return new UserService().login({
			account: account,
			password: password
		})
		.then(function (json) {
            console.log(json);
			return saveSessionToken(json)
				.then(function () {
					dispatch(loginSuccess(json));  
					Actions.Tabbar(); 
				});
		})
		.catch((error) => {
            console.error(error);
			dispatch(loginFailure(error));
		});
	};
}

export function saveSessionToken(json) {
 	return new AppAuthToken().storeSessionToken(json);
}