<commit_before>import request from 'superagent-bluebird-promise';
import {browserHistory} from 'react-router';
import {store} from '../store.js';

import {ACTION_TYPES} from './ActionTypes.js';
import {success, failure, busyToggle} from './appActions.js';
import {buildUrl, catchHandler} from './fetch.js';
import t from '../trans.js';

function cleanViewModel(data) {
  var invoice = Object.assign({}, data);
  Object.keys(invoice).filter(k => k[0] === '_' && k !== '_id').forEach(k => {
    delete invoice[k];
  });
  return invoice;
}


export function createInvoice(data) {
  return dispatch => {
    dispatch(busyToggle());
    request.post(buildUrl('/invoices'))
      .set('Content-Type', 'application/json')
      .set('Accept', 'application/json')
      .send(cleanViewModel(data))
      .then(function(res) {
        dispatch({
          type: ACTION_TYPES.INVOICE_ADDED,
          invoice: res.body
        });

        dispatch(success(t('invoice.createConfirm')));
        browserHistory.push('/invoice/' + res.body.number);
        //browserHistory.push('/');
      }, function(err) {
        if (err.res.text === 'TemplateNotFound') {
          dispatch(failure(t('invoice.pdfTemplateNotFoundTitle'), t('invoice.pdfTemplateNotFound')));
        } else {
          catchHandler(err);
        }
      })
      .catch(catchHandler)
      .then(() => dispatch(busyToggle.off()));
  };
}

function updateInvoiceRequest(data, successMsg, andGoHome) {
  return dispatch => {
    dispatch(busyToggle());
    request.put(buildUrl('/invoices'))
      .set('Content-Type', 'application/json')
      .set('Accept', 'application/json')
      .send(cleanViewModel(data))
      .then(function(res) {
        dispatch({
          type: ACTION_TYPES.INVOICE_UPDATED,
          invoice: res.body
        });

        dispatch(success(successMsg || t('toastrConfirm')));
        if (andGoHome) { // TODO: check: do we always stay on the same page after insert? Then delete this!
          browserHistory.push('/');
        }
      })
      .catch(catchHandler)
      .then(() => dispatch(busyToggle.off()));
  };<commit_msg>TemplateNotFound: don't crash on other errors<commit_after>import request from 'superagent-bluebird-promise';
import {browserHistory} from 'react-router';
import {store} from '../store.js';

import {ACTION_TYPES} from './ActionTypes.js';
import {success, failure, busyToggle} from './appActions.js';
import {buildUrl, catchHandler} from './fetch.js';
import t from '../trans.js';

function cleanViewModel(data) {
  var invoice = Object.assign({}, data);
  Object.keys(invoice).filter(k => k[0] === '_' && k !== '_id').forEach(k => {
    delete invoice[k];
  });
  return invoice;
}


export function createInvoice(data) {
  return dispatch => {
    dispatch(busyToggle());
    request.post(buildUrl('/invoices'))
      .set('Content-Type', 'application/json')
      .set('Accept', 'application/json')
      .send(cleanViewModel(data))
      .then(function(res) {
        dispatch({
          type: ACTION_TYPES.INVOICE_ADDED,
          invoice: res.body
        });

        dispatch(success(t('invoice.createConfirm')));
        browserHistory.push('/invoice/' + res.body.number);
        //browserHistory.push('/');
      }, function(err) {
        if (err.res && err.res.text === 'TemplateNotFound') {
          dispatch(failure(t('invoice.pdfTemplateNotFoundTitle'), t('invoice.pdfTemplateNotFound')));
        } else {
          catchHandler(err);
        }
      })
      .catch(catchHandler)
      .then(() => dispatch(busyToggle.off()));
  };
}

function updateInvoiceRequest(data, successMsg, andGoHome) {
  return dispatch => {
    dispatch(busyToggle());
    request.put(buildUrl('/invoices'))
      .set('Content-Type', 'application/json')
      .set('Accept', 'application/json')
      .send(cleanViewModel(data))
      .then(function(res) {
        dispatch({
          type: ACTION_TYPES.INVOICE_UPDATED,
          invoice: res.body
        });

        dispatch(success(successMsg || t('toastrConfirm')));
        if (andGoHome) { // TODO: check: do we always stay on the same page after insert? Then delete this!
          browserHistory.push('/');
        }
      })
      .catch(catchHandler)
      .then(() => dispatch(busyToggle.off()));
  };
{'fraction_non_alphanumeric': 0.16787298622460892, 'fraction_numerical': 0.00046696240952603317, 'mean_word_length': 2.9629972247918595, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5190965', 'n_tokens_mistral': 1286, 'n_tokens_neox': 1274, 'n_words': 246}