Sagas - gabrielrangel95/react-native-folder-structure GitHub Wiki
Ex: <user.ts>
import { takeEvery, put, call } from 'redux-saga/effects';
import { Types } from '@redux/types';
import { ITryLoginRequest } from '@interfaces/user';
import { UserActions } from '@redux/actions/user';
export function* watchTryLogin() {
yield takeEvery(Types.USER_TRY_LOGIN_REQUEST, handleTryLogin);
}
function* handleTryLogin(action: ITryLoginRequest) {
try {
const { user } = action.payload;
const opts = { email: user.email, password: user.password }
const response = yield call('http://my-api.com', opts);
yield put(Actions.tryLoginSuccess(response));
} catch (err) {
console.log({ LOGIN_ERROR: err});
const { status, response } = err;
if(status) {
if(status === 403) {
const { body } = response;
if (body && body.message ) {
yield put(Actions.tryLoginFailure(body.message));
} else {
yield put(Actions.tryLoginFailure('Invalid username / password'));
}
} else {
yield put(Actions.tryLoginFailure('System error'));
}
} else {
yield put(Actions.tryLoginFailure('System error'));
}
}
}
Ex: <index.ts>
import { all } from 'redux-saga/effects';
import {
watchTryLogin,
} from './user';
export default function* rootSaga(){
return yield all([
// user
watchTryLogin(),
])
}