Created
November 13, 2018 19:46
-
-
Save idfunctor/39fad5bf97ec8f8a7b6c69066e587d62 to your computer and use it in GitHub Desktop.
authSaga.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 { takeEvery, takeLatest } from 'redux-saga'; | |
| import { call, put, fork, select } from 'redux-saga/effects'; | |
| import { push } from 'react-router-redux'; | |
| import { toastr } from 'react-redux-toastr'; | |
| import { destroy } from 'redux-form'; | |
| import * as groupsApi from 'groupsModule/api'; | |
| import * as selectors from 'authModule/redux/selectors'; | |
| import * as layoutActions from 'layoutModule/redux/actions'; | |
| import * as actions from 'authModule/redux/actions'; | |
| import * as api from 'authModule/api'; | |
| import * as constants from 'authModule/constants'; | |
| /** | |
| * Verification Resend | |
| **/ | |
| function* userResendVerifyRequest() { | |
| try { | |
| const id = yield select(selectors.userLoginUnverifiedId); | |
| yield call(api.userResendVerifyRequest.bind(null, id)); | |
| yield put(actions.userResendVerifySuccess({})); | |
| } catch (e) { | |
| yield put(actions.userResendVerifyError(e.data)); | |
| } | |
| } | |
| function* acceptInviteRequest(req) { | |
| const { payload } = req; | |
| try { | |
| const data = yield call(api.acceptInviteRequest.bind(null, payload)); | |
| yield put(actions.acceptInviteSuccess(data)); | |
| } catch (e) { | |
| yield put(actions.acceptInviteError(e.data)); | |
| } | |
| } | |
| function* _acceptInviteRequest(req) { | |
| yield* acceptInviteRequest(req); | |
| yield put(layoutActions.addHomeMessage(['You have sucessfully accepted invitation'])); | |
| const userLogin = yield select(selectors.userLogin); | |
| if (userLogin.data.invites && userLogin.data.invites.frontendUrl && userLogin.data.invites.frontendUrl.length > 0) { | |
| yield put(push(userLogin.data.invites.frontendUrl)); | |
| } else { | |
| yield put(push('/')); | |
| } | |
| } | |
| function* rejectInviteRequest(req) { | |
| const { payload } = req; | |
| try { | |
| const data = yield call(api.rejectInviteRequest.bind(null, payload)); | |
| yield put(actions.rejectInviteSuccess(data)); | |
| } catch (e) { | |
| yield put(actions.rejectInviteError(e.data)); | |
| } | |
| } | |
| function* _rejectInviteRequest(req) { | |
| yield* rejectInviteRequest(req); | |
| yield put(layoutActions.addHomeMessage(['Invitation request has been rejected'])); | |
| yield put(push('/')); | |
| } | |
| function* guestInviteRequest(req) { | |
| const { payload } = req; | |
| try { | |
| const data = yield call(api.guestInviteRequest.bind(null, payload)); | |
| yield put(actions.guestInviteSuccess(data)); | |
| } catch (e) { | |
| yield put(actions.guestInviteError(e.data)); | |
| } | |
| } | |
| /** | |
| * Signin | |
| */ | |
| function* validateUserLoginRequest(req) { | |
| const { payload } = req; | |
| try { | |
| const data = yield call(api.validateUserLoginRequest.bind(null, payload)); | |
| yield put(actions.validateUserLoginSuccess(data)); | |
| } catch (e) { | |
| yield put(actions.validateUserLoginError(e.data)); | |
| } | |
| } | |
| /** | |
| * Signin | |
| */ | |
| function* userLoginRequest(req) { | |
| const { payload } = req; | |
| try { | |
| const data = yield call(api.userLoginRequest.bind(null, payload)); | |
| yield put(actions.userLoginSuccess(data)); | |
| yield put(destroy('user-login')); | |
| } catch (e) { | |
| yield put(actions.userLoginError(e.data)); | |
| } | |
| } | |
| /** | |
| * Signout | |
| */ | |
| function* userLogoutRequest(req) { | |
| const { payload } = req; | |
| try { | |
| const data = yield call(api.userLogoutRequest.bind(null, payload)); | |
| window.localStorage.removeItem('currentGroupData'); | |
| yield put(actions.userLogoutSuccess(data)); | |
| yield put(push('/')); | |
| } catch (e) { | |
| yield put(actions.userLogoutError(e.data)); | |
| } | |
| } | |
| /** | |
| * Signup | |
| */ | |
| function* userSignupRequest(req) { | |
| const { payload } = req; | |
| try { | |
| const data = yield call(api.userSignupRequest, payload); | |
| yield put(actions.userSignupSuccess(data)); | |
| yield put(destroy('user-signup')); | |
| yield call(toastr.success, 'Sign up successful', `Verification email has been sent to ${data.email}`); | |
| yield put(push('/')); | |
| } catch (e) { | |
| yield put(actions.userSignupError(e.data)); | |
| yield call(toastr.error, 'Sign up failed', e.data.message); | |
| } | |
| } | |
| function* groupSelectedSpaceRequest(req) { | |
| const { payload } = req; | |
| try { | |
| const data = yield call(groupsApi.groupGetRequest.bind(null, payload)); | |
| window.localStorage.setItem('currentGroupData', JSON.stringify(data)); | |
| yield put(actions.groupSelectedSpaceSuccess(data)); | |
| yield put(push(payload.pathname)); | |
| } catch (e) { | |
| yield put(actions.groupSelectedSpaceError(e.data)); | |
| } | |
| } | |
| function* groupSelectSuggestedRequest(req) { | |
| const { payload: { userId, ...formData } } = req; | |
| try { | |
| const data = yield call(groupsApi.groupsAllListRequest.bind(null, { userId }, formData)); | |
| yield put(actions.groupSelectSuggestedSuccess(data)); | |
| } catch (e) { | |
| yield put(actions.groupSelectSuggestedError(e.data)); | |
| } | |
| } | |
| function* groupSelectSearchRequest(req) { | |
| const { payload } = req; | |
| try { | |
| const data = yield call(groupsApi.groupListRequest.bind(null, payload)); | |
| yield put(actions.groupSelectSearchSuccess(data)); | |
| } catch (e) { | |
| yield put(actions.groupSelectSearchError(e.data)); | |
| } | |
| } | |
| function* userInviteRequest() { | |
| const userLogin = yield select(selectors.userLogin); | |
| const data = yield call(api.userPendingInvites.bind(null, userLogin.data.id)); | |
| if (data.guid) { | |
| yield put(push(`invite/${data.guid}`)); | |
| } | |
| } | |
| function* _userLoginRequest(req) { | |
| if (req.type === constants.USER_LOGIN_REQUEST) { | |
| yield* userLoginRequest(req); | |
| } else { | |
| yield* validateUserLoginRequest(req); | |
| } | |
| const userLogin = yield select(selectors.userLogin); | |
| if (!userLogin.isSuccess) { | |
| return; | |
| } | |
| if (!userLogin.data.id) { | |
| yield put(actions.userLoginError({ message: userLogin.errors })); | |
| return; | |
| } | |
| if (!userLogin.data.PersonalGroup || userLogin.data.PersonalGroup === null) { | |
| yield put(actions.userLoginError({ message: 'No Personal Group Found!!! Data Corrupt' })); | |
| return; | |
| } | |
| yield* groupSelectSuggestedRequest({ payload: { userId: userLogin.data.id, limit: 5 } }); | |
| yield* groupSelectedSpaceRequest({ payload: { groupId: userLogin.data.PersonalGroup.id, pathname: '/' } }); | |
| yield [ | |
| call(groupSelectSuggestedRequest, { payload: { userId: userLogin.data.id, limit: 5 } }), | |
| call(groupSelectedSpaceRequest, { payload: { groupId: userLogin.data.PersonalGroup.id, pathname: '/' } }) | |
| ]; | |
| if (userLogin.data.invites && userLogin.data.invites.guid) { | |
| yield put(push(`invite/${userLogin.data.invites.guid}`)); | |
| } else { | |
| const location = yield select(state => state.routing.locationBeforeTransitions); | |
| const next = location.query.next || '/'; | |
| yield put(push(next)); | |
| } | |
| } | |
| function* userVerifyRequest(req) { | |
| const { payload } = req; | |
| try { | |
| const data = yield call(api.userVerifyRequest.bind(null, payload)); | |
| yield put(actions.userVerifySuccess(data)); | |
| } catch (e) { | |
| yield put(actions.userVerifyError(e.data)); | |
| } | |
| } | |
| export default [ | |
| fork(takeEvery, constants.USER_INVITE_REQUEST, userInviteRequest), | |
| fork(takeEvery, constants.USER_RESEND_VERIFY_REQUEST, userResendVerifyRequest), | |
| fork(takeEvery, constants.VALIDATE_USER_LOGIN_REQUEST, _userLoginRequest), | |
| fork(takeLatest, constants.USER_LOGIN_REQUEST, _userLoginRequest), | |
| fork(takeLatest, constants.USER_LOGOUT_REQUEST, userLogoutRequest), | |
| fork(takeEvery, constants.USER_SIGNUP_REQUEST, userSignupRequest), | |
| fork(takeLatest, constants.GROUP_SELECTED_SPACE_REQUEST, groupSelectedSpaceRequest), | |
| fork(takeEvery, constants.GROUP_SELECT_SUGGESTED_REQUEST, groupSelectSuggestedRequest), | |
| fork(takeLatest, constants.GROUP_SELECT_SEARCH_REQUEST, groupSelectSearchRequest), | |
| fork(takeEvery, constants.USER_VERIFY_REQUEST, userVerifyRequest), | |
| fork(takeEvery, constants.GUEST_INVITE_REQUEST, guestInviteRequest), | |
| fork(takeEvery, constants.ACCEPT_INVITE_REQUEST, _acceptInviteRequest), | |
| fork(takeEvery, constants.REJECT_INVITE_REQUEST, _rejectInviteRequest) | |
| ]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment