store/calculation: remove filters store

This commit is contained in:
Chika 2022-07-07 20:41:25 +03:00
parent 2bc2c3bc99
commit 51bf308c5b
6 changed files with 21 additions and 64 deletions

View File

@ -1,6 +0,0 @@
/* eslint-disable object-curly-newline */
import type { CalculationFilters } from 'stores/calculation/options/types';
const defaultFilters: CalculationFilters = {};
export default defaultFilters;

View File

@ -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);
};
}

View File

@ -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<CalculationFilters> = 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];
};
}

View File

@ -2,6 +2,3 @@ import type { Elements } from 'Components/Calculation/config/map/values';
import type { BaseOption } from 'Elements/types';
export type CalculationOptions = Record<Elements, BaseOption[]>;
export type Filter = (options: BaseOption[]) => BaseOption[];
export type CalculationFilters = Partial<Record<Elements, Filter>>;

View File

@ -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({

View File

@ -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<CalculationFilters>;
};
tables?: {
insurance: InsuranceTableData;