Created
November 18, 2019 08:08
-
-
Save minhajul-islam/cfac0f4f247a8bb3e3badb33fcfbdaf8 to your computer and use it in GitHub Desktop.
redux/actions/io.js
This file contains hidden or 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 { | |
| LOADING_START, | |
| LOADING_END, | |
| REDIRECT_TO_MANAGE_PAGE, | |
| GET_TOKEN, | |
| SAVE_TOKEN, | |
| REMOVE_TOKEN, | |
| ERROR, | |
| } from '../constent/io'; | |
| import asyncStore from 'react-native-simple-store'; | |
| import {TOKEN_KEY} from '../../api/Constants'; | |
| export function loadingStart() { | |
| return { | |
| type: LOADING_START, | |
| }; | |
| } | |
| export function loadingEnd() { | |
| return { | |
| type: LOADING_END, | |
| }; | |
| } | |
| export function setShouldRedirectedToManagePage(payload) { | |
| return { | |
| type: REDIRECT_TO_MANAGE_PAGE, | |
| payload, | |
| }; | |
| } | |
| export const getToken = (payload) => ({ | |
| type: GET_TOKEN, | |
| payload, | |
| }); | |
| export const saveToken = (payload) => ({ | |
| type: SAVE_TOKEN, | |
| payload, | |
| }); | |
| export const removeToken = () => ({ | |
| type: REMOVE_TOKEN, | |
| }); | |
| export const error = error => ({ | |
| type: ERROR, | |
| error, | |
| }); | |
| export const getUserToken = (callback) => dispatch => { | |
| asyncStore.get(TOKEN_KEY) | |
| .then(value => { | |
| // Alert.alert('get', value); | |
| dispatch(getToken(value)); | |
| dispatch(callback(value)); | |
| return Promise.resolve(); | |
| }) | |
| .catch(error => { | |
| dispatch(error(error.message || 'ERROR')); | |
| return Promise.resolve(); | |
| }); | |
| }; | |
| export const saveUserToken = (token) => dispatch => { | |
| asyncStore | |
| .save(TOKEN_KEY, token).then(value => { | |
| dispatch(saveToken(token.token)); | |
| }) | |
| .catch(error => { | |
| dispatch(error(error.message || 'ERROR')); | |
| }); | |
| }; | |
| export const removeUserToken = (callBack) => dispatch => { | |
| asyncStore | |
| .update(TOKEN_KEY, {}).then(value => { | |
| dispatch(saveToken("")); | |
| dispatch(callBack); | |
| }) | |
| .catch(error => { | |
| dispatch(error(error.message || 'ERROR')); | |
| }); | |
| }; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment