userActions.js 3.19 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';
import {Linking} from 'react-native';

const {

	DELETE_TOKEN_REQUEST,
    DELETE_TOKEN_SUCCESS,

	ACCOUNT_CHANGE,
	ACCOUNT_CLEAN,
	PWD_CHANGE,
	PWD_DISPLAY,
    PWD_MODIFY,
    PWD_MODIFY_OLD,
    PWD_MODIFY_NEW,
    PWD_MODIFY_REPEAT,
    PWD_MODIFY_SUBMIT,

    LOGIN_REQUEST,
    LOGIN_SUCCESS,
    LOGIN_FAILURE,

    LOGOUT_REQUEST,
    LOGOUT_SUCCESS,
    LOGOUT_FAILURE,

    SET_SESSION_TOKEN,

    RESET_PASSWORD_REQUEST,
    RESET_PASSWORD_SUCCESS,
    RESET_PASSWORD_FAILURE,


	PHONE_CALL,
	ABOUT_US,
	USER_ITEMS_PRESSED,

	CHECK_UPDATE,
	UPDATE_VERSION,

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

export function userItemsPressed(item) {
	switch (item.index) {
		case 0:
            Linking.openURL('tel:025-87781000');
			return {
				type: PHONE_CALL,

			};
		case 1:
			Actions.AboutUs();
			return {
				type: ABOUT_US,
			};

	}
}

export function checkUpdate(currentVersion) {

	// todo

	return {
		type: CHECK_UPDATE,
		isShowUpdate: true,
	};
}

export function updateVersion() {

	// todo

	return {
		type: UPDATE_VERSION,
	}
}

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 accountChange(account) {
    return {
        type: ACCOUNT_CHANGE,
				payload: account
    };
}

export function accountClean() {
    return {
        type: ACCOUNT_CLEAN,
    };
}

export function pwdChange(pwd) {
    return {
        type: PWD_CHANGE,
		payload: pwd
    };
}
export function pwdDisplay(ishow) {
    return {
        type: PWD_DISPLAY,
		payload: ishow
    };
}

export function logout() {
    new AppAuthToken().deleteSessionToken();
    Actions.pop();
    Actions.Login();
    return {
        type: LOGOUT_REQUEST
    };
}

export function pwdModify() {
    Actions.Modify();
    return {
        type: PWD_MODIFY
    };
}

export function pwdModifyOld(pwd) {
    
    return {
        type: PWD_MODIFY_OLD,
        payload: pwd
    };
}

export function pwdModifyNew(pwd) {
       return {
        type: PWD_MODIFY_NEW,
        payload: pwd
    };
}

export function pwdModifyRepeat(pwd) {
    
    return {
        type: PWD_MODIFY_REPEAT,
        payload: pwd
    };
}

export function pwdModifySubmit() {
    
    return {
        type: PWD_MODIFY_SUBMIT
    };
}


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

}

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