Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import {takeEvery, put, select, call} from 'redux-saga/effects';
- import {
- requestAddToCart,
- setCart,
- setCartError,
- requestCreateCart,
- requestRemoveToCart,
- requestSetGift,
- decriceProductQuantity,
- requestUpdateCart,
- requestAddCouponToProduct,
- requestAddAddressToCart,
- requestAddCardToCart,
- requestResetCart,
- requestCreateCartSuccess,
- successAddAddressToCart,
- successAddCardToCart
- } from './actions';
- import {getCart} from './selectors';
- import {getCustomerEmail} from '../customerProfile/selectors';
- import {alertShow, updateSelectedShippingCompany} from '../../utils/helpers';
- import {checkIncludes} from '../../utils/helpers';
- import * as network from '../../API/APIFunctions';
- import * as path from '../../API/APIConstants';
- import NavigationService from '../../utils/navigation/NavigationService';
- //Adding new product to cart if it is not there already
- export function* addToCart({payload}) {
- const id_product = payload.id_product;
- const id_person = payload.id_person;
- try {
- //getting cart data from store
- const cart = yield select(getCart);
- if (!checkIncludes(cart, payload.id_product, payload.id_person)) {
- //adding new item to cart from the store it it does not exist already, modifying selection
- cart.push({
- ...payload,
- count: 1,
- coupon: '',
- isGift: false,
- carrier: payload.carrier.filter((c) => +c.active).map((item) => ({...item, selected: false}))
- });
- } else {
- //increasing amount if each item by one if it already exists in out cart
- cart.map(item =>
- {if (item.id_person === id_person && item.id_product === id_product) {item.count = ++item.count; }});
- }
- yield put(setCart(cart));
- } catch (error) {
- yield put(setCartError(error.message));
- alertShow(error);
- }
- }
- /*
- clears up cart from removed cart on user click
- */
- function* removeFromCartFlow({payload}) {
- const id_product = payload.id_product;
- try {
- const cart = yield select(getCart);
- //filtering out removed item
- const newCart = cart.filter(item => item.id_product !== id_product);
- yield put(setCart(newCart));
- } catch (error) {
- alertShow(error);
- yield put(setCartError(error.message));
- }
- }
- /*
- user can create coupons for his own products
- so each tapped item is modified to be either with or without coupon
- later in makes two different flows for user to use it
- */
- function* setGiftFlow({payload}) {
- const id_product = payload.id_product;
- const id_person = payload.id_person;
- try {
- const cart = yield select(getCart);
- let withGift = cart.map(item =>
- (item.id_product === id_product && item.id_person === id_person) && ({...item, isGift: !item.isGift}));
- yield put(setCart(withGift));
- } catch (error) {
- yield put(setCartError(error.message));
- }
- }
- //decreasing amount of same items
- export function* updateCartProductQuantity({payload}) {
- const id_product = payload.id_product;
- const id_person = payload.id_person;
- try {
- //getting cart from application's store
- let cart = yield select(getCart);
- //if current item exists in out cart, then we decrease amount of same items by one on each click and replace the object with correct amount of items inside cart
- if (checkIncludes(cart, payload.id_product, payload.id_person)) {
- cart = cart.map((item) => {
- if (item.id_person === id_person && item.id_product === id_product && +item.count > 1) {
- return {
- ...item,
- count: --item.count
- };
- }
- return item;
- });
- }
- yield put(setCart(cart));
- } catch (error) {
- yield put(setCartError(error.message));
- alertShow(error);
- }
- }
- //adding new field to product that will have different purchase option with coupon
- function* addProductCoupon({payload}) {
- const id_product = payload.id_product;
- const id_person = payload.id_person;
- try {
- let cart = yield select(getCart);
- cart = cart.map(item => {
- if (item.id_product === id_product && item.id_person === id_person) {
- return {
- ...item,
- coupon: payload.coupon
- };
- }
- return item;
- });
- yield put(setCart(cart));
- } catch (error) {
- yield put(setCartError(error.message));
- }
- }
- //modifying field of cart
- /*
- or more specifically, we change either seller name, item data (name, description or photos),
- and submit the changes to server, then if request succeeded we receive updated cart with correct items
- and we replace previous one with new
- */
- function* updateCart({payload}) {
- try {
- const cart = yield select(getCart);
- const newCart = yield updateSelectedShippingCompany(cart, payload.productId, payload.item, payload.seller);
- yield put(setCart(newCart));
- } catch (error) {
- alertShow('Something went wrong.\nPlease try again later.');
- yield put(setCartError(error.message));
- }
- }
- /*
- Initiations of new users cart
- Then navigating user to screen where
- he can fill up billing information
- for specific cart
- */
- export function* createACart({payload}) {
- const body = JSON.stringify(payload);
- try {
- const resp = yield call(network.apiPost, path.API_URL + path.CREATE_CART, body);
- if (resp.status === 200) {
- yield put(requestCreateCartSuccess());
- NavigationService.navigate('BuyBillingView');
- } else {
- yield put(setCartError(''));
- alertShow('Something went wrong.\nPlease try again later.');
- }
- } catch (e) {
- console.error('Error', e);
- }
- }
- //Saving user address with cart data, then moving to check out flow
- /*
- Here user initiates process of buying something,
- we save his address to server with current
- stage of payment process
- */
- export function* addAddressToCart({payload}) {
- const body = JSON.stringify(payload);
- try {
- const resp = yield call(network.apiPost, path.API_URL + path.CERT_ADD_ADDRESS, body);
- if (resp.status === 200) {
- yield put(successAddAddressToCart());
- //after request succeeded we move user to other screen to fill his Credit Card credentials
- NavigationService.navigate('CardPaymentDetails');
- } else {
- yield put(setCartError(''));
- alertShow('Something went wrong.\nPlease try again later.');
- }
- } catch (e) {
- console.error('Error', e);
- }
- }
- //adding credit card and proceeding with payment depending on server callback
- /*
- When user fills out credit card credentials
- we make a post request to initiate payment process
- */
- export function* addCardToCart({payload}) {
- const body = JSON.stringify(payload);
- try {
- const userEmail = yield select(getCustomerEmail);
- const resp = yield call(network.apiPostToken, path.CART_ADD_CARD, body);
- if (resp.status === 200) {
- //here we calling another action to save user credit card data inside keychain
- yield put(successAddCardToCart());
- const object = yield resp.json();
- if (object.resultCode === 'Authorised') {
- //payment succeeded, here we reset user cart and greet him on other screen
- yield put(requestResetCart());
- NavigationService.navigate('ThankYou', {email: userEmail});
- } else {
- alertShow(object.refusalReason);
- }
- } else {
- yield put(setCartError(''));
- alertShow('Something went wrong.\nPlease try again later.');
- }
- } catch (e) {
- console.error('Error', e);
- }
- }
- /*
- each saga is watching over specific action
- (or group of actions) with payload to fire up
- */
- export default function* () {
- yield takeEvery(requestAddToCart, addToCart);
- yield takeEvery(requestUpdateCart, updateCart);
- yield takeEvery(requestRemoveToCart, removeFromCartFlow);
- yield takeEvery(requestSetGift, setGiftFlow);
- yield takeEvery(decriceProductQuantity, updateCartProductQuantity);
- yield takeEvery(requestAddCouponToProduct, addProductCoupon);
- yield takeEvery(requestCreateCart, createACart);
- yield takeEvery(requestAddAddressToCart, addAddressToCart);
- yield takeEvery(requestAddCardToCart, addCardToCart);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement