Advertisement
Guest User

New Epic

a guest
Nov 14th, 2019
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { combineEpics } from 'redux-observable';
  2. import { ofType } from 'redux-observable';
  3. import { mergeMap } from 'rxjs/operators';
  4. import * as actionTypes from './actionTypes';
  5. import auth, {
  6.   loginWithApi,
  7.   getProfile,
  8.   updateProfile,
  9.   updatePhotoUrl,
  10.   changePassword,
  11. } from './action';
  12. import { $axios } from '../../utils/api';
  13. import translate from '../../utils/translate';
  14. import moment from 'moment';
  15. import { persistor } from '../store';
  16.  
  17. export const loginEpic = action$ => {
  18.   return action$.pipe(
  19.     ofType(actionTypes.LOGIN),
  20.     mergeMap(act => {
  21.       return loginWithApi(
  22.         act.payload.email,
  23.         act.payload.password,
  24.         act.payload.signalID,
  25.       )
  26.         .then(res => {
  27.           return auth.ok({ token: res.data.data.token, time: moment().unix() });
  28.         })
  29.         .catch(err => {
  30.           return auth.fail(err.response.data);
  31.         });
  32.     }),
  33.   );
  34. };
  35.  
  36. //update profile epic
  37. export const updateProfileEpic = action$ => {
  38.   return action$.pipe(
  39.     ofType(actionTypes.UPDATE),
  40.     mergeMap(act => {
  41.       return updateProfile(act.payload.user)
  42.         .then(res => {
  43.           return auth.update_ok({ code: 200 });
  44.         })
  45.         .catch(err => {
  46.           return auth.update_fail(err.response.data);
  47.         });
  48.     }),
  49.   );
  50. };
  51.  
  52. //update photo url ok epic
  53. export const updatePhotoUrlOKEpic = action$ => {
  54.   return action$.pipe(
  55.     ofType(actionTypes.UPDATE),
  56.     mergeMap(act => {
  57.       return updatePhotoUrl(act.payload.formData)
  58.         .then(res => {
  59.           return auth.update_ok({ code: 200 });
  60.         })
  61.         .catch(err => auth.update_photo_url_fail(err.response.data));
  62.     }),
  63.   );
  64. };
  65.  
  66. //change password epic
  67. export const changePasswordEpic = action$ => {
  68.   return action$.pipe(
  69.     ofType(actionTypes.CHANGE_PASSWORD),
  70.     mergeMap(act => {
  71.       return changePassword(act.payload.user)
  72.         .then(res => {
  73.           return auth.change_password_ok({ code: 200, message: res.data.message });
  74.         })
  75.         .catch(err => {
  76.           let payload = { message: err.response.data.message, code: err.response.data.code };
  77.           return auth.change_password_fail(payload);
  78.         });
  79.     }),
  80.   );
  81. };
  82.  
  83.  
  84. //update ok epic
  85. export const UpdateOKEpic = action$ => {
  86.   return action$.pipe(
  87.     ofType(actionTypes.UPDATE_OK),
  88.     mergeMap(act => {
  89.       return getProfile(act.payload.user)
  90.         .then(res => {
  91.           return auth.profile({ user: res.data.data });
  92.         })
  93.         .catch(err => auth.fail(err.response.data));
  94.     }),
  95.   );
  96. };
  97.  
  98. //login ok epic
  99. export const loginOKEpic = action$ => {
  100.   return action$.pipe(
  101.     ofType(actionTypes.OK),
  102.     mergeMap(act => {
  103.       return getProfile(act.payload.token)
  104.         .then(res => {
  105.           return auth.profile({ user: res.data.data });
  106.         })
  107.         .catch(err => auth.fail(err.response.data));
  108.     }),
  109.   );
  110. };
  111.  
  112. export const loginDistributorEpic = action$ => {
  113.   return action$.pipe(
  114.     ofType(actionTypes.OK),
  115.     mergeMap(act => {
  116.       return $axios
  117.         .get('user/distributors', {
  118.           headers: {
  119.             Authorization: `Bearer ${act.payload.token}`,
  120.           },
  121.         })
  122.         .then(res => {
  123.           let distributors = res.data.data.map(m => {
  124.             return { ...m, value: translate(m.name) };
  125.           });
  126.           return auth.ok_distributor({ distributors });
  127.         })
  128.         .catch(err => {
  129.           return auth.fail_distributor(err.response.data);
  130.         });
  131.     }),
  132.   );
  133. };
  134.  
  135. export const salesEpic = action$ => {
  136.   return action$.pipe(
  137.     ofType(actionTypes.OK),
  138.     mergeMap(act => {
  139.       return $axios
  140.         .get('user/guests', {
  141.           headers: {
  142.             Authorization: `Bearer ${act.payload.token}`,
  143.           },
  144.         })
  145.         .then(res => {
  146.           let sales = res.data.data.map(m => {
  147.             return { ...m, value: translate(m.name), filter: m.email + m.name };
  148.           });
  149.           return auth.fetch_sales_OK({ sales });
  150.         })
  151.         .catch(err => {
  152.           return auth.fetch_sales_BAD(err.response.data);
  153.         });
  154.     }),
  155.   );
  156. };
  157.  
  158. export const authLogoutEpic = action$ => {
  159.   return action$.pipe(
  160.     ofType(actionTypes.LOGOUT),
  161.     mergeMap(act => {
  162.       return $axios
  163.         .get('user/logout')
  164.         .then(res => {
  165.           return auth.ok({ token: '' });
  166.         })
  167.         .catch(err => {
  168.           return auth.fail(err.response.data);
  169.         })
  170.         .finally(() => persistor.purge());
  171.     }),
  172.   );
  173. };
  174.  
  175. const authEpics = combineEpics(
  176.   loginEpic,
  177.   updateProfileEpic,
  178.   loginOKEpic,
  179.   loginDistributorEpic,
  180.   salesEpic,
  181.   authLogoutEpic,
  182.   UpdateOKEpic,
  183.   updatePhotoUrlOKEpic,
  184.   changePasswordEpic,
  185.   // eventAfterLogin
  186. );
  187.  
  188. export default authEpics;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement