Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { takeEvery, takeLatest, put, select, all, call, throttle } from 'redux-saga/effects';
- import * as actions from '../actions';
- import {
- getToken,
- getNextInvoice,
- getPreviousInvoice,
- getDisplayedAccounts,
- getFilter,
- getSortDirection,
- getSortField,
- getUserId,
- getUser,
- getSelectedInvoice,
- getSelectedInvoiceId,
- } from '../selectors';
- import { fetchInvoices, fetchCompanyForInvoice, getPositionsForInvoice, getCommentsForInvoice } from '../http';
- import { LOCAL_STORAGE_KEYS, INVOICE_STATE } from '../constants';
- import { push } from 'react-router-redux';
- import lockInvoice$ from './invoice-actions/lockInvoice$';
- function* fetchInvoices$() {
- try {
- yield put(actions.startLoading());
- const token = yield select(getToken);
- const state = yield select(getFilter);
- const direction = yield select(getSortDirection);
- const orderBy = yield select(getSortField);
- const assigneeId = yield select(getUserId);
- const invoices = yield call(fetchInvoices, token, { state, assigneeId }, { orderBy, direction });
- yield put(actions.fetchInvoicesOK({ invoices }));
- } catch (_) {
- console.log(_);
- yield put(actions.fireErrorNotification('Can\'t load invoices'));
- } finally {
- yield put(actions.stopLoading());
- }
- }
- function* fetchInvoicesOK$() {
- yield put(actions.setIsInvoiceValid({ isValid: true }));
- }
- function* nextInvoice$() {
- const invoiceId = yield select(getSelectedInvoiceId);
- const nextInvoice = yield select(getNextInvoice);
- if (nextInvoice) {
- yield all([
- put(actions.unlockInvoice({ invoiceId })),
- put(actions.selectInvoice({ invoiceId: nextInvoice.id })),
- ]);
- } else {
- yield put(push('invoices'));
- }
- }
- function* previousInvoice$() {
- const invoiceId = yield select(getSelectedInvoiceId);
- const previousInvoice = yield select(getPreviousInvoice);
- if (previousInvoice) {
- yield all([
- put(actions.unlockInvoice({ invoiceId })),
- put(actions.selectInvoice({ invoiceId: previousInvoice.id })),
- ]);
- } else {
- yield put(push('invoices'));
- }
- }
- function* selectInvoice$() {
- try {
- yield put(actions.startLoadingFields());
- const { lockingUser, state, mandant, lieferantenNr, id } = yield select(getSelectedInvoice);
- const invoice = yield select(getSelectedInvoice);
- const token = yield select(getToken);
- const positions = yield call(getPositionsForInvoice, token, id);
- const comments = yield call(getCommentsForInvoice, token, id);
- yield put(actions.updateInvoice({ invoice: { ...invoice, positions, comments } }));
- if (state !== INVOICE_STATE.ACCOUNTING_NPO && state !== INVOICE_STATE.ACCOUNTING_NONPO) {
- yield put(actions.stopLoadingFields());
- return;
- }
- const user = yield select(getUser);
- if (lockingUser === null) {
- // yield put(actions.lockInvoice());
- yield lockInvoice$();
- } else if (lockingUser.id !== user.id) {
- yield put(actions.fireWarningNotification(
- 'notification.warning.invoice-locked-by-other-user', { user: lockingUser.fullName },
- ));
- }
- const company = yield call(fetchCompanyForInvoice, token, lieferantenNr % 100000, mandant);
- if (company) {
- const { gegenkonto_OHNE_Kat } = company;
- const accounts = yield select(getDisplayedAccounts);
- const account = gegenkonto_OHNE_Kat !== 0
- ? accounts.find(acc => acc.number === gegenkonto_OHNE_Kat).displayName.split(' ')[0]
- : null;
- if (account) {
- const selectedAccount = account ? accounts.find(acc => acc.number === gegenkonto_OHNE_Kat) : null;
- const updates = {
- sachkonto: account,
- ustCode: selectedAccount.ustcode !== 0 ? selectedAccount.ustcode : null,
- ustArt: selectedAccount.ustart,
- };
- yield put(actions.updateAllPositions({ updates }));
- }
- yield put(actions.setSelectedInvoiceCompany({ company }));
- }
- } catch (_) {
- console.log(_);
- yield put(actions.fireErrorNotification('Can\'t load positions!'));
- } finally {
- yield put(actions.stopLoadingFields());
- }
- }
- function* sendItToLocalStorage$() {
- const invoice = yield select(getSelectedInvoice);
- localStorage.setItem(LOCAL_STORAGE_KEYS.INVOICE, JSON.stringify(invoice));
- }
- function* updateDocument$({ payload: { field, value } }) {
- const invoice = yield select(getSelectedInvoice);
- yield put(actions.updateInvoice({ invoice: { ...invoice, [field]: value } }));
- }
- export function* invoicesSaga() {
- yield all([
- takeLatest(actions.fetchInvoices, fetchInvoices$),
- takeLatest(actions.selectFilter, fetchInvoices$),
- takeLatest(actions.selectSortField, fetchInvoices$),
- takeEvery(actions.nextInvoice, nextInvoice$),
- takeEvery(actions.previousInvoice, previousInvoice$),
- takeEvery(actions.selectInvoice, selectInvoice$),
- takeEvery(actions.selectInvoice, sendItToLocalStorage$),
- takeEvery(actions.fetchInvoicesOK, fetchInvoicesOK$),
- throttle(500, actions.updateDocument, updateDocument$),
- ]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement