Last active
January 17, 2023 13:35
-
-
Save lucianomlima/c8b681b1bddaefdd511851ff380b0be9 to your computer and use it in GitHub Desktop.
Redux-saga Ajax API example
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
// ./app/sagas/index.js | |
import { all, takeLatest } from 'redux-saga/effects'; | |
/* Services */ | |
import api from '../services/api'; | |
/* Redux Actions */ | |
import { InitTypes } from '../redux/init'; | |
import { UserTypes } from '../redux/user'; | |
/* Sagas */ | |
import { initialize } from './init'; | |
import { loginSuccess, changeAvailability } from './user'; | |
export default function* root() { | |
yield all([ | |
takeLatest(InitTypes.INITIALIZE, initialize), | |
takeLatest(UserTypes.USER_LOGIN, loginSuccess, api), | |
takeLatest(UserTypes.USER_TOGGLE_AVAILABILITY, changeAvailability) | |
]); | |
}; |
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
// ./app/sagas/init.js | |
import { AsyncStorage } from 'react-native'; | |
import { call, put } from 'redux-saga/effects'; | |
import { initActions, userActions } from '../redux'; | |
export function* initialize() { | |
const user = yield call(getUser); | |
yield put(userActions.userLogin(user)); | |
yield put(initActions.ready()); | |
} | |
async function getUser() { | |
const data = await AsyncStorage.multiGet(['uid', 'photoURL', 'fullName', 'name', 'lastName', 'telephone', 'available', 'email', 'gender']); | |
if (data[0][1]) { | |
userObj = { | |
uid: data[0][1], | |
photoURL: data[1][1], | |
fullName: data[2][1], | |
name: data[3][1], | |
lastName: data[4][1], | |
telephone: data[5][1] || null, | |
available: data[6][1] || false, | |
email: data[7][1], | |
gender: data[8][1] | |
}; | |
return userObj; | |
} | |
return null; | |
} |
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 { AsyncStorage } from 'react-native'; | |
import { select, put } from 'redux-saga/effects'; | |
import actions, { getUser } from '../redux/user'; | |
export function* loginSuccess(api) { | |
const user = yield select(getUser); | |
if (user !== null) { | |
// do some stuff with api received | |
// API is created with apisauce and use axios for requests. | |
} | |
} | |
export function* changeAvailability() { | |
const availability = yield AsyncStorage.getItem('available'); | |
const available = Boolean(+availability) ? 0 : 1; | |
yield AsyncStorage.setItem('available', `${available}`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this! It really saved me hours!