-
-
Save ricardokdz/7f9a8d3089db983a1ee1b8205cabad0e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import apiRegister from '../api/register' | |
import apiLogin from '../api/login' | |
import apiLoginFacebook from '../api/login-facebook' | |
import apiLogout from '../api/logout' | |
import apiGetUser from '../api/get-user' | |
import apiPasswordRecovery from '../api/password-recovery' | |
import apiUpdateUser from '../api/update-user' | |
import apiSaveAvatar from '../api/save-avatar' | |
import store from '../store' | |
var {FBLoginManager} = require('react-native-facebook-login') | |
function fetchStart(){ | |
return { | |
type: 'AUTH_FETCH_START' | |
} | |
} | |
function fetchStop(){ | |
return { | |
type: 'AUTH_FETCH_STOP' | |
} | |
} | |
function register_success(user){ | |
return { | |
type: 'REGISTER_SUCCESS', | |
user | |
} | |
} | |
function register_failure(errorMessage){ | |
return { | |
type: 'REGISTER_FAILURE', | |
errorMessage | |
} | |
} | |
function login_success(access_token) { | |
return { | |
type: 'SESSION_LOGIN_SUCCESS', | |
access_token | |
} | |
} | |
function login_failure(errorMessage) { | |
return { | |
type: 'SESSION_LOGIN_FAILURE', | |
errorMessage | |
} | |
} | |
function logout_success() { | |
return { | |
type: 'SESSION_LOGOUT_SUCCESS' | |
} | |
} | |
function logout_failure() { | |
return { | |
type: 'SESSION_LOGOUT_FAILURE' | |
} | |
} | |
function get_user_success(user){ | |
return { | |
type: 'GET_USER_SUCCESS', | |
user: user | |
} | |
} | |
function get_user_failure(errorMessage){ | |
return { | |
type: 'GET_USER_FAILURE', | |
errorMessage | |
} | |
} | |
function password_recovery_success(){ | |
return { | |
type: 'PASSWORD_RECOVERY_SUCCESS' | |
} | |
} | |
function password_recovery_failure(errorMessage){ | |
return { | |
type: 'PASSWORD_RECOVERY_FAILURE', | |
errorMessage | |
} | |
} | |
function update_user_success(user){ | |
return { | |
type: 'UPDATE_USER_SUCCESS', | |
user | |
} | |
} | |
function update_user_failure(errorMessage){ | |
return { | |
type: 'UPDATE_USER_FAILURE', | |
errorMessage | |
} | |
} | |
function save_avatar_start(){ | |
return { | |
type: 'SAVE_AVATAR_START' | |
} | |
} | |
function save_avatar_stop(){ | |
return { | |
type: 'SAVE_AVATAR_STOP' | |
} | |
} | |
function save_avatar_success(avatar){ | |
return { | |
type: 'SAVE_AVATAR_SUCCESS', | |
avatar | |
} | |
} | |
function save_avatar_failure(errorMessage){ | |
return { | |
type: 'SAVE_AVATAR_FAILURE', | |
errorMessage | |
} | |
} | |
export const register = (name, email) => { | |
return dispatch => { | |
dispatch(fetchStart()) | |
return apiRegister(name, email) | |
.then((response) => dispatch(register_success(response.data))) | |
.catch((error) => dispatch(register_failure(error.response.data))) | |
.then(() => dispatch(fetchStop())) | |
} | |
} | |
export const loginByEmail = (username, password) => { | |
return dispatch => { | |
dispatch(fetchStart()) | |
return apiLogin(username, password) | |
.then((response) => dispatch(login_success(response.data.access_token))) | |
.catch((error) => dispatch(login_failure(error.response.data.error))) | |
.then(() => dispatch(fetchStop())) | |
} | |
} | |
export const loginByFacebook = (facebook_access_token) => { | |
return dispatch => { | |
dispatch(fetchStart()) | |
return apiLoginFacebook(facebook_access_token) | |
.then((response) => dispatch(login_success(response.data.access_token))) | |
.catch((error) => dispatch(login_failure(error.response.data.error))) | |
.then(() => dispatch(fetchStop())) | |
} | |
} | |
export const logout = () => { | |
return dispatch => { | |
return apiLogout() | |
.then(() => { | |
dispatch(logout_success()) | |
FBLoginManager.logout((x) => console.log(x)) | |
//Actions.welcome() | |
}) | |
.catch((error) => console.log(error) + dispatch(logout_failure(error.response.data.error))) | |
} | |
} | |
export const getUser = () => { | |
return dispatch => { | |
return apiGetUser() | |
.then((response) => dispatch(get_user_success(response.data))) | |
.catch((error) => dispatch(get_user_failure(error.response.data.error))) | |
} | |
} | |
export const passwordRecovery = (email) => { | |
return dispatch => { | |
return apiPasswordRecovery(email) | |
.then((response) => dispatch(password_recovery_success())) | |
.catch((error) => dispatch(password_recovery_failure(error.response.data.error))) | |
} | |
} | |
export const updateUser = (obj) => { | |
return dispatch => { | |
return apiUpdateUser(obj) | |
.then((response) => dispatch(update_user_success(response.data))) | |
.catch((error) => dispatch(update_user_failure(error.response.data.error))) | |
} | |
} | |
export const saveAvatar = (avatar) => { | |
return dispatch => { | |
dispatch(save_avatar_start()) | |
return apiSaveAvatar(avatar) | |
.then((response) => dispatch(save_avatar_success(response.data))) | |
.catch((error) => dispatch(save_avatar_failure(error.response.data.error))) | |
.then(() => dispatch(save_avatar_stop())) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment