From 51bf308c5ba582e456bcda50328330db9aae25ed Mon Sep 17 00:00:00 2001 From: Chika Date: Thu, 7 Jul 2022 20:41:25 +0300 Subject: [PATCH] store/calculation: remove filters store --- config/default-filters.ts | 6 --- stores/calculation/index.ts | 1 - stores/calculation/options/index.ts | 71 ++++++++--------------------- stores/calculation/options/types.ts | 3 -- stores/index.js | 1 - types/page.ts | 3 +- 6 files changed, 21 insertions(+), 64 deletions(-) delete mode 100644 config/default-filters.ts diff --git a/config/default-filters.ts b/config/default-filters.ts deleted file mode 100644 index b4cda81..0000000 --- a/config/default-filters.ts +++ /dev/null @@ -1,6 +0,0 @@ -/* eslint-disable object-curly-newline */ -import type { CalculationFilters } from 'stores/calculation/options/types'; - -const defaultFilters: CalculationFilters = {}; - -export default defaultFilters; diff --git a/stores/calculation/index.ts b/stores/calculation/index.ts index e153ad8..9f461f2 100644 --- a/stores/calculation/index.ts +++ b/stores/calculation/index.ts @@ -22,7 +22,6 @@ export default class CalculationStore { resetElement = (elementName: Elements) => { this.$values.resetElementValue(elementName); this.$options.resetOption(elementName); - this.$options.resetFilter(elementName); this.$validation.clearErrors(elementName); }; } diff --git a/stores/calculation/options/index.ts b/stores/calculation/options/index.ts index 6275be7..34206f5 100644 --- a/stores/calculation/options/index.ts +++ b/stores/calculation/options/index.ts @@ -1,12 +1,11 @@ /* eslint-disable object-curly-newline */ /* eslint-disable unicorn/prefer-set-has */ import type { Elements } from 'Components/Calculation/config/map/values'; -import defaultFilters from 'config/default-filters'; import defaultOptions from 'config/default-options'; import type { BaseOption } from 'Elements/types'; import { makeAutoObservable } from 'mobx'; import type RootStore from 'stores/root'; -import type { CalculationFilters, CalculationOptions, Filter } from './types'; +import type { CalculationOptions } from './types'; const AUTO_SET_VALUE_ELEMENTS: Elements[] = [ 'selectProduct', @@ -25,42 +24,46 @@ const AUTO_SET_VALUE_ELEMENTS: Elements[] = [ export default class OptionsStore { root: RootStore; options: CalculationOptions = defaultOptions; - filters: Partial = defaultFilters; constructor(rootStore: RootStore) { makeAutoObservable(this); this.root = rootStore; } - hydrate = (initialOptions: CalculationOptions, initialFilters: CalculationFilters) => { + hydrate = (initialOptions: CalculationOptions) => { this.options = initialOptions; - this.filters = initialFilters; }; - /** **************** OPTIONS **************** */ getOption(elementName: Elements) { const value = this.root.$calculation.$values.getElementValue(elementName); return this.options[elementName]?.find((x) => x.value === value); } - getOptions( - elementName: Elements, - settings?: { - filtered: true; - } - ) { + getOptions(elementName: Elements) { const options = this.options[elementName]; - if (!settings?.filtered) return options; - - const filter = this.filters[elementName]; - - return filter ? options && filter(options) : options; + return options; } setElementOptions = (elementName: Elements, options: BaseOption[]) => { this.options[elementName] = options; + + /** + * Проверяем, что значение есть в новом списке, иначе сбрасываем значение + */ + const value = this.root.$calculation.$values.getElementValue(elementName); + if (options.length === 0 || options.some((x) => x.value === value)) { + this.root.$calculation.$values.resetElementValue(elementName); + } + + /** + * Если в новом списке одна запись, то указываем ее + * (для элементов из списка {@link AUTO_SET_VALUE_ELEMENTS}) + */ + if (options?.length === 1 && AUTO_SET_VALUE_ELEMENTS.includes(elementName)) { + this.root.$calculation.$values.setElementValue(elementName, options.at(0)?.value); + } }; resetOption = (elementName: Elements) => { @@ -83,38 +86,4 @@ export default class OptionsStore { this.setElementOptions(elementName, options[elementName]!); }); }; - - /** **************** FILTERS **************** */ - getFilter(elementName: Elements) { - return this.filters[elementName]; - } - - setFilter = (elementName: Elements, filter: Filter) => { - this.filters[elementName] = filter; - - /** - * Проверяем, что значение есть в отфильтрованном списке, иначе сбрасываем значение - */ - const filteredOptons = this.getOptions(elementName); - - const elementValue = this.root.$calculation.$values.getElementValue(elementName); - - if (!filteredOptons?.length || !filteredOptons.some((x) => x.value === elementValue)) { - this.root.$calculation.$values.resetElementValue(elementName); - - return; - } - - /** - * Если после фильтрации остается одна запись, то указываем ее - * (для элементов из списка {@link AUTO_SET_VALUE_ELEMENTS}) - */ - if (filteredOptons?.length === 1 && AUTO_SET_VALUE_ELEMENTS.includes(elementName)) { - this.root.$calculation.$values.setElementValue(elementName, filteredOptons[0].value); - } - }; - - resetFilter = (elementName: Elements) => { - this.filters[elementName] = defaultFilters[elementName]; - }; } diff --git a/stores/calculation/options/types.ts b/stores/calculation/options/types.ts index 361dd58..f0caf3c 100644 --- a/stores/calculation/options/types.ts +++ b/stores/calculation/options/types.ts @@ -2,6 +2,3 @@ import type { Elements } from 'Components/Calculation/config/map/values'; import type { BaseOption } from 'Elements/types'; export type CalculationOptions = Record; - -export type Filter = (options: BaseOption[]) => BaseOption[]; -export type CalculationFilters = Partial>; diff --git a/stores/index.js b/stores/index.js index 34f51f7..0cbe3ae 100644 --- a/stores/index.js +++ b/stores/index.js @@ -18,7 +18,6 @@ export function initializeStore(initialData) { if (calculation?.values) _store.$calculation.$values.hydrate(calculation.values); if (calculation?.statuses) _store.$calculation.$status.hydrate(calculation.statuses); if (calculation?.options) _store.$calculation.$options.hydrate(calculation.options); - if (calculation?.filters) _store.$calculation.$options.hydrate(calculation.filters); if (tables?.insurance) { _store.$tables.insurance.hydrate({ diff --git a/types/page.ts b/types/page.ts index 88d041d..707f2eb 100644 --- a/types/page.ts +++ b/types/page.ts @@ -1,6 +1,6 @@ import type { NormalizedCacheObject } from '@apollo/client'; import type { User } from 'services/user/types'; -import type { CalculationFilters, CalculationOptions } from 'stores/calculation/options/types'; +import type { CalculationOptions } from 'stores/calculation/options/types'; import type { CalculationStatuses } from 'stores/calculation/statuses/types'; import type { CalculationValues } from 'stores/calculation/values/types'; import type { InsuranceTableData } from 'stores/tables/insurance'; @@ -12,7 +12,6 @@ export interface BasePageProps { values: CalculationValues; statuses: CalculationStatuses; options: CalculationOptions; - filters: Partial; }; tables?: { insurance: InsuranceTableData;