SHARE
TWEET

Shopping Cart Saga

a guest Apr 24th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {takeEvery, put, select, call} from 'redux-saga/effects';
  2.  
  3. import {
  4.   requestAddToCart,
  5.   setCart,
  6.   setCartError,
  7.   requestCreateCart,
  8.   requestRemoveToCart,
  9.   requestSetGift,
  10.   decriceProductQuantity,
  11.   requestUpdateCart,
  12.   requestAddCouponToProduct,
  13.   requestAddAddressToCart,
  14.   requestAddCardToCart,
  15.   requestResetCart,
  16.   requestCreateCartSuccess,
  17.   successAddAddressToCart,
  18.   successAddCardToCart
  19. } from './actions';
  20.  
  21. import {getCart} from './selectors';
  22. import {getCustomerEmail} from '../customerProfile/selectors';
  23.  
  24. import {alertShow, updateSelectedShippingCompany} from '../../utils/helpers';
  25. import {checkIncludes} from '../../utils/helpers';
  26. import * as network from '../../API/APIFunctions';
  27. import * as path from '../../API/APIConstants';
  28. import NavigationService from '../../utils/navigation/NavigationService';
  29.  
  30. export function* addToCart({payload}) {
  31.   const id_product = payload.id_product;
  32.   const id_person = payload.id_person;
  33.   try {
  34.     const cart = yield select(getCart);
  35.  
  36.     if (!checkIncludes(cart, payload.id_product, payload.id_person)) {
  37.       cart.push({
  38.         ...payload,
  39.         count: 1,
  40.         coupon: '',
  41.         isGift: false,
  42.         carrier: payload.carrier.filter((c) => +c.active).map((item) => ({...item, selected: false}))
  43.       });
  44.     } else {
  45.       cart.map(item =>
  46.       {if (item.id_person === id_person && item.id_product === id_product) {item.count = ++item.count; }});
  47.     }
  48.     yield put(setCart(cart));
  49.  
  50.   } catch (error) {
  51.     yield put(setCartError(error.message));
  52.     alertShow(error);
  53.   }
  54. }
  55.  
  56. function* removeFromCartFlow({payload}) {
  57.   const id_product = payload.id_product;
  58.   try {
  59.     const cart = yield select(getCart);
  60.     const newCart = cart.filter(item => item.id_product !== id_product);
  61.  
  62.     yield put(setCart(newCart));
  63.   } catch (error) {
  64.     alertShow(error);
  65.     yield put(setCartError(error.message));
  66.   }
  67. }
  68.  
  69. function* setGiftFlow({payload}) {
  70.   const id_product = payload.id_product;
  71.   const id_person = payload.id_person;
  72.   try {
  73.     const cart = yield select(getCart);
  74.  
  75.     cart.map(item =>
  76.       (item.id_product === id_product && item.id_person === id_person) && ({...item, isGift: !item.isGift}));
  77.  
  78.     yield put(setCart(cart));
  79.   } catch (error) {
  80.     yield put(setCartError(error.message));
  81.   }
  82. }
  83.  
  84. export function* updateCartProductQuantity({payload}) {
  85.   const id_product = payload.id_product;
  86.   const id_person = payload.id_person;
  87.   try {
  88.     let cart = yield select(getCart);
  89.  
  90.     if (checkIncludes(cart, payload.id_product, payload.id_person)) {
  91.       cart = cart.map((item) => {
  92.         if (item.id_person === id_person && item.id_product === id_product && +item.count > 1) {
  93.           return {
  94.             ...item,
  95.             count: --item.count
  96.           };
  97.         }
  98.         return item;
  99.       });
  100.     }
  101.     yield put(setCart(cart));
  102.  
  103.   } catch (error) {
  104.     yield put(setCartError(error.message));
  105.     alertShow(error);
  106.   }
  107. }
  108.  
  109. function* addProductCoupon({payload}) {
  110.   const id_product = payload.id_product;
  111.   const id_person = payload.id_person;
  112.   try {
  113.     let cart = yield select(getCart);
  114.  
  115.     cart = cart.map(item => {
  116.       if (item.id_product === id_product && item.id_person === id_person) {
  117.         return {
  118.           ...item,
  119.           coupon: payload.coupon
  120.         };
  121.       }
  122.       return item;
  123.     });
  124.  
  125.     yield put(setCart(cart));
  126.   } catch (error) {
  127.     yield put(setCartError(error.message));
  128.   }
  129. }
  130.  
  131. function* updateCart({payload}) {
  132.   try {
  133.     const cart = yield select(getCart);
  134.     const newCart = yield updateSelectedShippingCompany(cart, payload.productId, payload.item, payload.seller);
  135.  
  136.     yield put(setCart(newCart));
  137.   } catch (error) {
  138.     alertShow('Something went wrong.\nPlease try again later.');
  139.     yield put(setCartError(error.message));
  140.   }
  141. }
  142.  
  143. export function* createACart({payload}) {
  144.   const body = JSON.stringify(payload);
  145.   try {
  146.     const resp = yield call(network.apiPost, path.API_URL + path.CREATE_CART, body);
  147.  
  148.     if (resp.status === 200) {
  149.       yield put(requestCreateCartSuccess());
  150.       NavigationService.navigate('BuyBillingView');
  151.     } else {
  152.       yield put(setCartError(''));
  153.       alertShow('Something went wrong.\nPlease try again later.');
  154.     }
  155.   } catch (e) {
  156.     console.error('Error', e);
  157.   }
  158. }
  159.  
  160. export function* addAddressToCart({payload}) {
  161.   const body = JSON.stringify(payload);
  162.   try {
  163.     const resp = yield call(network.apiPost, path.API_URL + path.CERT_ADD_ADDRESS, body);
  164.  
  165.     if (resp.status === 200) {
  166.       yield put(successAddAddressToCart());
  167.       NavigationService.navigate('CardDetails');
  168.     } else {
  169.       yield put(setCartError(''));
  170.       alertShow('Something went wrong.\nPlease try again later.');
  171.     }
  172.   } catch (e) {
  173.     console.error('Error', e);
  174.   }
  175. }
  176.  
  177. export function* addCardToCart({payload}) {
  178.   const body = JSON.stringify(payload);
  179.  
  180.   try {
  181.     const userEmail = yield select(getCustomerEmail);
  182.     const resp = yield call(network.apiPostToken, path.CART_ADD_CARD, body);
  183.  
  184.     if (resp.status === 200) {
  185.       yield put(successAddCardToCart());
  186.       const object = yield resp.json();
  187.       if (object.resultCode === 'Authorised') {
  188.         yield put(requestResetCart());
  189.         NavigationService.navigate('ThankYou', {email: userEmail});
  190.       } else {
  191.         alertShow(object.refusalReason);
  192.       }
  193.     } else {
  194.       yield put(setCartError(''));
  195.       alertShow('Something went wrong.\nPlease try again later.');
  196.     }
  197.   } catch (e) {
  198.     console.error('Error', e);
  199.   }
  200. }
  201.  
  202. export default function* () {
  203.   yield takeEvery(requestAddToCart, addToCart);
  204.   yield takeEvery(requestUpdateCart, updateCart);
  205.   yield takeEvery(requestRemoveToCart, removeFromCartFlow);
  206.   yield takeEvery(requestSetGift, setGiftFlow);
  207.   yield takeEvery(decriceProductQuantity, updateCartProductQuantity);
  208.   yield takeEvery(requestAddCouponToProduct, addProductCoupon);
  209.   yield takeEvery(requestCreateCart, createACart);
  210.   yield takeEvery(requestAddAddressToCart, addAddressToCart);
  211.   yield takeEvery(requestAddCardToCart, addCardToCart);
  212. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top