Advertisement
Guest User

Untitled

a guest
Jan 25th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { takeEvery, takeLatest, put, select, all, call, throttle } from 'redux-saga/effects';
  2. import * as actions from '../actions';
  3. import {
  4.     getToken,
  5.     getNextInvoice,
  6.     getPreviousInvoice,
  7.     getDisplayedAccounts,
  8.     getFilter,
  9.     getSortDirection,
  10.     getSortField,
  11.     getUserId,
  12.     getUser,
  13.     getSelectedInvoice,
  14.     getSelectedInvoiceId,
  15. } from '../selectors';
  16. import { fetchInvoices, fetchCompanyForInvoice, getPositionsForInvoice, getCommentsForInvoice } from '../http';
  17. import { LOCAL_STORAGE_KEYS, INVOICE_STATE } from '../constants';
  18. import { push } from 'react-router-redux';
  19. import lockInvoice$ from './invoice-actions/lockInvoice$';
  20.  
  21. function* fetchInvoices$() {
  22.     try {
  23.         yield put(actions.startLoading());
  24.  
  25.         const token = yield select(getToken);
  26.         const state = yield select(getFilter);
  27.         const direction = yield select(getSortDirection);
  28.         const orderBy = yield select(getSortField);
  29.         const assigneeId = yield select(getUserId);
  30.  
  31.         const invoices = yield call(fetchInvoices, token, { state, assigneeId }, { orderBy, direction });
  32.  
  33.         yield put(actions.fetchInvoicesOK({ invoices }));
  34.     } catch (_) {
  35.         console.log(_);
  36.         yield put(actions.fireErrorNotification('Can\'t load invoices'));
  37.     } finally {
  38.         yield put(actions.stopLoading());
  39.     }
  40. }
  41.  
  42. function* fetchInvoicesOK$() {
  43.     yield put(actions.setIsInvoiceValid({ isValid: true }));
  44. }
  45.  
  46. function* nextInvoice$() {
  47.     const invoiceId = yield select(getSelectedInvoiceId);
  48.     const nextInvoice = yield select(getNextInvoice);
  49.  
  50.     if (nextInvoice) {
  51.         yield all([
  52.             put(actions.unlockInvoice({ invoiceId })),
  53.             put(actions.selectInvoice({ invoiceId: nextInvoice.id })),
  54.         ]);
  55.     } else {
  56.         yield put(push('invoices'));
  57.     }
  58. }
  59.  
  60. function* previousInvoice$() {
  61.     const invoiceId = yield select(getSelectedInvoiceId);
  62.     const previousInvoice = yield select(getPreviousInvoice);
  63.  
  64.     if (previousInvoice) {
  65.         yield all([
  66.             put(actions.unlockInvoice({ invoiceId })),
  67.             put(actions.selectInvoice({ invoiceId: previousInvoice.id })),
  68.         ]);
  69.     } else {
  70.         yield put(push('invoices'));
  71.     }
  72. }
  73.  
  74. function* selectInvoice$() {
  75.     try {
  76.         yield put(actions.startLoadingFields());
  77.        
  78.         const { lockingUser, state, mandant, lieferantenNr, id } = yield select(getSelectedInvoice);
  79.         const invoice = yield select(getSelectedInvoice);
  80.         const token = yield select(getToken);
  81.            
  82.         const positions = yield call(getPositionsForInvoice, token, id);
  83.         const comments = yield call(getCommentsForInvoice, token, id);
  84.  
  85.         yield put(actions.updateInvoice({ invoice: { ...invoice, positions, comments } }));
  86.        
  87.         if (state !== INVOICE_STATE.ACCOUNTING_NPO && state !== INVOICE_STATE.ACCOUNTING_NONPO) {
  88.             yield put(actions.stopLoadingFields());
  89.  
  90.             return;
  91.         }
  92.  
  93.         const user = yield select(getUser);
  94.  
  95.         if (lockingUser === null) {
  96.             // yield put(actions.lockInvoice());
  97.             yield lockInvoice$();
  98.         } else if (lockingUser.id !== user.id) {
  99.             yield put(actions.fireWarningNotification(
  100.                 'notification.warning.invoice-locked-by-other-user', { user: lockingUser.fullName },
  101.             ));
  102.         }
  103.  
  104.         const company = yield call(fetchCompanyForInvoice, token, lieferantenNr % 100000, mandant);
  105.  
  106.         if (company) {
  107.             const { gegenkonto_OHNE_Kat } = company;
  108.             const accounts = yield select(getDisplayedAccounts);
  109.  
  110.             const account = gegenkonto_OHNE_Kat !== 0
  111.                 ? accounts.find(acc => acc.number === gegenkonto_OHNE_Kat).displayName.split(' ')[0]
  112.                 : null;
  113.  
  114.             if (account) {
  115.                 const selectedAccount = account ? accounts.find(acc => acc.number === gegenkonto_OHNE_Kat) : null;
  116.                 const updates = {
  117.                     sachkonto: account,
  118.                     ustCode: selectedAccount.ustcode !== 0 ? selectedAccount.ustcode : null,
  119.                     ustArt: selectedAccount.ustart,
  120.                 };
  121.  
  122.                 yield put(actions.updateAllPositions({ updates }));
  123.             }
  124.  
  125.             yield put(actions.setSelectedInvoiceCompany({ company }));
  126.         }
  127.     } catch (_) {
  128.         console.log(_);
  129.        
  130.         yield put(actions.fireErrorNotification('Can\'t load positions!'));
  131.     } finally {
  132.         yield put(actions.stopLoadingFields());
  133.     }
  134. }
  135.  
  136. function* sendItToLocalStorage$() {
  137.     const invoice = yield select(getSelectedInvoice);
  138.  
  139.     localStorage.setItem(LOCAL_STORAGE_KEYS.INVOICE, JSON.stringify(invoice));
  140. }
  141.  
  142. function* updateDocument$({ payload: { field, value } }) {
  143.     const invoice = yield select(getSelectedInvoice);
  144.  
  145.     yield put(actions.updateInvoice({ invoice: { ...invoice, [field]: value } }));
  146. }
  147.  
  148. export function* invoicesSaga() {
  149.     yield all([
  150.         takeLatest(actions.fetchInvoices, fetchInvoices$),
  151.         takeLatest(actions.selectFilter, fetchInvoices$),
  152.         takeLatest(actions.selectSortField, fetchInvoices$),
  153.  
  154.         takeEvery(actions.nextInvoice, nextInvoice$),
  155.         takeEvery(actions.previousInvoice, previousInvoice$),
  156.         takeEvery(actions.selectInvoice, selectInvoice$),
  157.         takeEvery(actions.selectInvoice, sendItToLocalStorage$),
  158.         takeEvery(actions.fetchInvoicesOK, fetchInvoicesOK$),
  159.         throttle(500, actions.updateDocument, updateDocument$),
  160.     ]);
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement