Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { combineEpics } from 'redux-observable';
- import { ofType } from 'redux-observable';
- import { mergeMap } from 'rxjs/operators';
- import * as actionTypes from './actionTypes';
- import auth, {
- loginWithApi,
- getProfile,
- updateProfile,
- updatePhotoUrl,
- changePassword,
- } from './action';
- import { $axios } from '../../utils/api';
- import translate from '../../utils/translate';
- import moment from 'moment';
- import { persistor } from '../store';
- export const loginEpic = action$ => {
- return action$.pipe(
- ofType(actionTypes.LOGIN),
- mergeMap(act => {
- return loginWithApi(
- act.payload.email,
- act.payload.password,
- act.payload.signalID,
- )
- .then(res => {
- return auth.ok({ token: res.data.data.token, time: moment().unix() });
- })
- .catch(err => {
- return auth.fail(err.response.data);
- });
- }),
- );
- };
- //update profile epic
- export const updateProfileEpic = action$ => {
- return action$.pipe(
- ofType(actionTypes.UPDATE),
- mergeMap(act => {
- return updateProfile(act.payload.user)
- .then(res => {
- return auth.update_ok({ code: 200 });
- })
- .catch(err => {
- return auth.update_fail(err.response.data);
- });
- }),
- );
- };
- //update photo url ok epic
- export const updatePhotoUrlOKEpic = action$ => {
- return action$.pipe(
- ofType(actionTypes.UPDATE),
- mergeMap(act => {
- return updatePhotoUrl(act.payload.formData)
- .then(res => {
- return auth.update_ok({ code: 200 });
- })
- .catch(err => auth.update_photo_url_fail(err.response.data));
- }),
- );
- };
- //change password epic
- export const changePasswordEpic = action$ => {
- return action$.pipe(
- ofType(actionTypes.CHANGE_PASSWORD),
- mergeMap(act => {
- return changePassword(act.payload.user)
- .then(res => {
- return auth.change_password_ok({ code: 200, message: res.data.message });
- })
- .catch(err => {
- let payload = { message: err.response.data.message, code: err.response.data.code };
- return auth.change_password_fail(payload);
- });
- }),
- );
- };
- //update ok epic
- export const UpdateOKEpic = action$ => {
- return action$.pipe(
- ofType(actionTypes.UPDATE_OK),
- mergeMap(act => {
- return getProfile(act.payload.user)
- .then(res => {
- return auth.profile({ user: res.data.data });
- })
- .catch(err => auth.fail(err.response.data));
- }),
- );
- };
- //login ok epic
- export const loginOKEpic = action$ => {
- return action$.pipe(
- ofType(actionTypes.OK),
- mergeMap(act => {
- return getProfile(act.payload.token)
- .then(res => {
- return auth.profile({ user: res.data.data });
- })
- .catch(err => auth.fail(err.response.data));
- }),
- );
- };
- export const loginDistributorEpic = action$ => {
- return action$.pipe(
- ofType(actionTypes.OK),
- mergeMap(act => {
- return $axios
- .get('user/distributors', {
- headers: {
- Authorization: `Bearer ${act.payload.token}`,
- },
- })
- .then(res => {
- let distributors = res.data.data.map(m => {
- return { ...m, value: translate(m.name) };
- });
- return auth.ok_distributor({ distributors });
- })
- .catch(err => {
- return auth.fail_distributor(err.response.data);
- });
- }),
- );
- };
- export const salesEpic = action$ => {
- return action$.pipe(
- ofType(actionTypes.OK),
- mergeMap(act => {
- return $axios
- .get('user/guests', {
- headers: {
- Authorization: `Bearer ${act.payload.token}`,
- },
- })
- .then(res => {
- let sales = res.data.data.map(m => {
- return { ...m, value: translate(m.name), filter: m.email + m.name };
- });
- return auth.fetch_sales_OK({ sales });
- })
- .catch(err => {
- return auth.fetch_sales_BAD(err.response.data);
- });
- }),
- );
- };
- export const authLogoutEpic = action$ => {
- return action$.pipe(
- ofType(actionTypes.LOGOUT),
- mergeMap(act => {
- return $axios
- .get('user/logout')
- .then(res => {
- return auth.ok({ token: '' });
- })
- .catch(err => {
- return auth.fail(err.response.data);
- })
- .finally(() => persistor.purge());
- }),
- );
- };
- const authEpics = combineEpics(
- loginEpic,
- updateProfileEpic,
- loginOKEpic,
- loginDistributorEpic,
- salesEpic,
- authLogoutEpic,
- UpdateOKEpic,
- updatePhotoUrlOKEpic,
- changePasswordEpic,
- // eventAfterLogin
- );
- export default authEpics;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement