daily pastebin goal
33%
SHARE
TWEET

Untitled

a guest Jan 16th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { pageTypes, AddFiltersError, RemoveFiltersError, SetFilter } from 'actions/page'
  2. import { createDefaultFromDate, createDefaultToDate, validateFromToDate } from 'helpers'
  3. import { filtersErrorsText } from 'constants/filters'
  4.  
  5. const validate = (key, dispatch, filtersErrors, from, to) => {
  6.   const dictionaryKey = `${key}DateFiltersError`
  7.   const errorText = filtersErrorsText[dictionaryKey]
  8.  
  9.   if (!validateFromToDate(from, to)) {
  10.     if (!filtersErrors[key].includes(errorText)) {
  11.       dispatch(
  12.         AddFiltersError(key, errorText)
  13.       )
  14.     }
  15.   } else {
  16.     if (filtersErrors[key].includes(errorText)) {
  17.       dispatch(
  18.         RemoveFiltersError(key, errorText)
  19.       )
  20.     }
  21.   }
  22. }
  23.  
  24. export const filtersDatesValidationMiddleware = ({ dispatch, getState }) => next => action => {
  25.   const { type } = action
  26.   const {
  27.     page: {
  28.       filters,
  29.       filtersErrors
  30.     }
  31.   } = getState()
  32.  
  33.   if (type === pageTypes.SET_FILTER) {
  34.     // Перехватываем установку фильтров. Если даты невалидны, ставим флаг
  35.     const { name, value } = action
  36.     const args = [ dispatch, filtersErrors ]
  37.  
  38.     const fromArgs = [ ...args, value, filters.to ]
  39.     const toArgs = [ ...args, filters.from, value ]
  40.  
  41.     switch (name) {
  42.       case 'from':
  43.         validate('from', ...fromArgs)
  44.         validate('to', ...fromArgs)
  45.         break
  46.  
  47.       case 'to':
  48.         validate('to', ...toArgs)
  49.         validate('from', ...toArgs)
  50.         break
  51.     }
  52.   }
  53.  
  54.   if (type === pageTypes.LOAD_PAGE_DATA) {
  55.     // Перехватываем загрузку отчёта. Если даты невалидны, - устанавливаем даты из initialState
  56.     if (!validateFromToDate(filters.from, filters.to)) {
  57.       dispatch(
  58.         SetFilter('from', createDefaultFromDate())
  59.       )
  60.       dispatch(
  61.         RemoveFiltersError('from', filtersErrorsText.fromDateFiltersError)
  62.       )
  63.       dispatch(
  64.         SetFilter('to', createDefaultToDate())
  65.       )
  66.       dispatch(
  67.         RemoveFiltersError('from', filtersErrorsText.toDateFiltersError)
  68.       )
  69.     }
  70.   }
  71.  
  72.   return next(action)
  73. }
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