|
| 1 | +import { get } from 'lodash' |
| 2 | +import { makeAutoObservable } from 'mobx' |
| 3 | + |
| 4 | +import { FilterList } from '@declarations' |
| 5 | +import { ActionFilterEnum } from '@core/enum/action-filter.enum' |
| 6 | +import { t } from '@i18n' |
| 7 | +import datasetStore from '@store/dataset' |
| 8 | +import filterStore from '@store/filter' |
| 9 | +import presetStore from '@store/filterPreset' |
| 10 | +import { compareConditions } from '@utils/filter-refiner/compareConditions' |
| 11 | +import { showToast } from '@utils/notifications/showToast' |
| 12 | +import { validatePresetName } from '@utils/validation/validatePresetName' |
| 13 | + |
| 14 | +class FilterControlRefinerStore { |
| 15 | + constructor() { |
| 16 | + makeAutoObservable(this) |
| 17 | + } |
| 18 | + |
| 19 | + get activePreset(): string { |
| 20 | + return datasetStore.activePreset |
| 21 | + } |
| 22 | + |
| 23 | + get presets(): string[] { |
| 24 | + return get(datasetStore, 'dsStat.filter-list', []) |
| 25 | + .filter(this.isPresetAbleToBeModified) |
| 26 | + .map((preset: FilterList) => preset.name) |
| 27 | + } |
| 28 | + |
| 29 | + public isPresetAbleToBeModified(preset: FilterList): boolean { |
| 30 | + return filterStore.actionName === ActionFilterEnum.Delete || |
| 31 | + filterStore.actionName === ActionFilterEnum.Modify |
| 32 | + ? !preset.standard |
| 33 | + : true |
| 34 | + } |
| 35 | + |
| 36 | + public applyAction = ( |
| 37 | + createPresetName: string, |
| 38 | + isSelectedFiltersEmpty: boolean, |
| 39 | + ): void => { |
| 40 | + if (filterStore.actionName === ActionFilterEnum.Delete) { |
| 41 | + presetStore.deletePreset() |
| 42 | + } |
| 43 | + |
| 44 | + if (filterStore.actionName === ActionFilterEnum.Join) { |
| 45 | + if (!datasetStore.activePreset) { |
| 46 | + showToast(t('error.choosePresetFirst'), 'error') |
| 47 | + |
| 48 | + return |
| 49 | + } |
| 50 | + |
| 51 | + const isConditionsAbleToJoin = compareConditions({ |
| 52 | + currentConditions: datasetStore.conditions, |
| 53 | + startConditions: datasetStore.startPresetConditions, |
| 54 | + currentPreset: datasetStore.activePreset, |
| 55 | + prevPreset: datasetStore.prevPreset, |
| 56 | + }) |
| 57 | + |
| 58 | + if (!isConditionsAbleToJoin) { |
| 59 | + showToast(t('error.cantJoinTheSamePreset'), 'error') |
| 60 | + |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + presetStore.joinPreset(datasetStore.activePreset) |
| 65 | + } |
| 66 | + |
| 67 | + if (filterStore.actionName === ActionFilterEnum.Create) { |
| 68 | + const isPresetNameValid = validatePresetName(createPresetName) |
| 69 | + |
| 70 | + if (!isPresetNameValid) { |
| 71 | + showToast(t('filter.notValidName'), 'error') |
| 72 | + |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + if (isSelectedFiltersEmpty) { |
| 77 | + showToast(t('error.chooseFiltersFirst'), 'error') |
| 78 | + |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + presetStore.createPreset(createPresetName) |
| 83 | + } |
| 84 | + |
| 85 | + if (filterStore.actionName === ActionFilterEnum.Modify) { |
| 86 | + if (!datasetStore.activePreset) { |
| 87 | + showToast(t('error.choosePresetFirst'), 'error') |
| 88 | + |
| 89 | + return |
| 90 | + } |
| 91 | + |
| 92 | + const isConditionsAbleToModify = compareConditions({ |
| 93 | + currentConditions: datasetStore.conditions, |
| 94 | + startConditions: datasetStore.startPresetConditions, |
| 95 | + currentPreset: datasetStore.activePreset, |
| 96 | + prevPreset: datasetStore.prevPreset, |
| 97 | + }) |
| 98 | + |
| 99 | + if (!isConditionsAbleToModify) { |
| 100 | + showToast(t('error.noChangesToModify'), 'error') |
| 101 | + |
| 102 | + return |
| 103 | + } |
| 104 | + |
| 105 | + presetStore.modifyPreset(datasetStore.activePreset) |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +export default new FilterControlRefinerStore() |
0 commit comments