Created
September 7, 2018 23:28
-
-
Save linux08/e32e26cac1b0cf250678a78b6f7abaad 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 { AsyncStorage } from 'react-native'; | |
export const getToken = (token) => ({ | |
type: 'GET_TOKEN', | |
token, | |
}); | |
export const saveToken = token => ({ | |
type: 'SAVE_TOKEN', | |
token | |
}); | |
export const removeToken = () => ({ | |
type: 'REMOVE_TOKEN', | |
}); | |
export const loading = bool => ({ | |
type: 'LOADING', | |
isLoading: bool, | |
}); | |
export const error = error => ({ | |
type: 'ERROR', | |
error, | |
}); | |
export const getUserToken = () => dispatch => | |
AsyncStorage.getItem('userToken') | |
.then((data) => { | |
dispatch(loading(false)); | |
dispatch(getToken(data)); | |
}) | |
.catch((err) => { | |
dispatch(loading(false)); | |
dispatch(error(err.message || 'ERROR')); | |
}) | |
export const saveUserToken = (data) => dispatch => | |
AsyncStorage.setItem('userToken', 'abc') | |
.then((data) => { | |
dispatch(loading(false)); | |
dispatch(saveToken('token saved')); | |
}) | |
.catch((err) => { | |
dispatch(loading(false)); | |
dispatch(error(err.message || 'ERROR')); | |
}) | |
export const removeUserToken = () => dispatch => | |
AsyncStorage.removeItem('userToken') | |
.then((data) => { | |
dispatch(loading(false)); | |
dispatch(removeToken(data)); | |
}) | |
.catch((err) => { | |
dispatch(loading(false)); | |
dispatch(error(err.message || 'ERROR')); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment