stores: add defaultFilters
add resetElement beautify stores code
This commit is contained in:
parent
63b9a93a16
commit
b7c02370ba
5
config/default-filters.ts
Normal file
5
config/default-filters.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import type { CalculationFilters } from 'stores/calculation/options/types';
|
||||
|
||||
const defaultFilters: Partial<CalculationFilters> = {};
|
||||
|
||||
export default defaultFilters;
|
||||
@ -1,4 +1,5 @@
|
||||
/* eslint-disable import/no-cycle */
|
||||
import type { Elements } from 'Components/Calculation/config/map/values';
|
||||
import type RootStore from 'stores/root';
|
||||
import OptionsStore from './options';
|
||||
import StatusStore from './statuses';
|
||||
@ -17,4 +18,10 @@ export default class CalculationStore {
|
||||
this.$options = new OptionsStore(rootStore);
|
||||
this.$validation = new ValidationStore(rootStore);
|
||||
}
|
||||
|
||||
resetElement = (elementName: Elements) => {
|
||||
this.$values.resetElementValue(elementName);
|
||||
this.$options.resetOption(elementName);
|
||||
this.$options.resetFilter(elementName);
|
||||
};
|
||||
}
|
||||
|
||||
@ -1,17 +1,11 @@
|
||||
/* eslint-disable unicorn/prefer-set-has */
|
||||
/* eslint-disable function-paren-newline */
|
||||
/* eslint-disable implicit-arrow-linebreak */
|
||||
/* eslint-disable no-confusing-arrow */
|
||||
/* eslint-disable object-curly-newline */
|
||||
/* eslint-disable import/no-cycle */
|
||||
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 { CalculationOptions, Filter, OptionsFilters } from './types';
|
||||
|
||||
const EXCLUDE_RESET_ELEMENTS: Elements[] = ['selectTechnicalCard', 'selectTownRegistration'];
|
||||
import type { CalculationOptions, Filter } from './types';
|
||||
|
||||
const AUTO_SET_VALUE_ELEMENTS: Elements[] = [
|
||||
'selectProduct',
|
||||
@ -30,7 +24,7 @@ const AUTO_SET_VALUE_ELEMENTS: Elements[] = [
|
||||
export default class OptionsStore {
|
||||
root: RootStore;
|
||||
options = defaultOptions;
|
||||
filters: Partial<OptionsFilters> = {};
|
||||
filters = defaultFilters;
|
||||
|
||||
constructor(rootStore: RootStore) {
|
||||
makeAutoObservable(this);
|
||||
@ -43,7 +37,7 @@ export default class OptionsStore {
|
||||
|
||||
/** **************** OPTIONS **************** */
|
||||
getOption(elementName: Elements) {
|
||||
const value = this.root.$calculation.$values.getValueByElement(elementName);
|
||||
const value = this.root.$calculation.$values.getElementValue(elementName);
|
||||
|
||||
return this.options[elementName]?.find((x) => x.value === value);
|
||||
}
|
||||
@ -63,26 +57,24 @@ export default class OptionsStore {
|
||||
return filter ? options && filter(options) : options;
|
||||
}
|
||||
|
||||
resetOptions = (elementsNames: readonly Elements[]) => {
|
||||
elementsNames.forEach((elementName) => {
|
||||
this.setOption(elementName, defaultOptions[elementName]!);
|
||||
});
|
||||
};
|
||||
|
||||
setOption = (elementName: Elements, option: BaseOption[]) => {
|
||||
this.options[elementName] = option;
|
||||
};
|
||||
|
||||
resetOption = (elementName: Elements) => {
|
||||
this.options[elementName] = defaultOptions[elementName];
|
||||
};
|
||||
|
||||
setOptions = (
|
||||
options: Partial<CalculationOptions>,
|
||||
settings?: { reset: boolean; exclude: Elements[] }
|
||||
) => {
|
||||
if (settings?.reset) {
|
||||
this.resetOptions(
|
||||
(Object.keys(defaultOptions) as Elements[]).filter(
|
||||
(elementName) => !settings?.exclude.includes(elementName)
|
||||
)
|
||||
);
|
||||
(Object.keys(defaultOptions) as Elements[])
|
||||
.filter((elementName) => !settings?.exclude.includes(elementName))
|
||||
.forEach((elementName) => {
|
||||
this.resetOption(elementName);
|
||||
});
|
||||
}
|
||||
|
||||
(Object.keys(options) as Elements[]).forEach((elementName) => {
|
||||
@ -90,12 +82,6 @@ export default class OptionsStore {
|
||||
});
|
||||
};
|
||||
|
||||
clearOptions = (elementName: Elements) => {
|
||||
this.options[elementName] = [];
|
||||
delete this.filters[elementName];
|
||||
this.root.$calculation.$values.clearValueOfElement(elementName);
|
||||
};
|
||||
|
||||
/** **************** FILTERS **************** */
|
||||
getFilter(elementName: Elements) {
|
||||
return this.filters[elementName];
|
||||
@ -105,18 +91,14 @@ export default class OptionsStore {
|
||||
this.filters[elementName] = filter;
|
||||
|
||||
/**
|
||||
* Проверяем, что значение есть в отфильтрованном списке,
|
||||
* иначе сбрасываем значение
|
||||
* (кроме исключений {@link EXCLUDE_RESET_ELEMENTS})
|
||||
* Проверяем, что значение есть в отфильтрованном списке, иначе сбрасываем значение
|
||||
*/
|
||||
const filteredOptons = this.getOptions(elementName);
|
||||
|
||||
if (!EXCLUDE_RESET_ELEMENTS.includes(elementName)) {
|
||||
const elementValue = this.root.$calculation.$values.getValueByElement(elementName);
|
||||
const elementValue = this.root.$calculation.$values.getElementValue(elementName);
|
||||
|
||||
if (!filteredOptons?.length || !filteredOptons.some((x) => x.value === elementValue)) {
|
||||
this.root.$calculation.$values.clearValueOfElement(elementName);
|
||||
}
|
||||
if (!filteredOptons?.length || !filteredOptons.some((x) => x.value === elementValue)) {
|
||||
this.root.$calculation.$values.resetElementValue(elementName);
|
||||
|
||||
return;
|
||||
}
|
||||
@ -126,7 +108,11 @@ export default class OptionsStore {
|
||||
* (для элементов из списка {@link AUTO_SET_VALUE_ELEMENTS})
|
||||
*/
|
||||
if (filteredOptons?.length === 1 && AUTO_SET_VALUE_ELEMENTS.includes(elementName)) {
|
||||
this.root.$calculation.$values.setValueOfElement(elementName, filteredOptons[0].value);
|
||||
this.root.$calculation.$values.setElementValue(elementName, filteredOptons[0].value);
|
||||
}
|
||||
};
|
||||
|
||||
resetFilter = (elementName: Elements) => {
|
||||
this.filters[elementName] = defaultFilters[elementName];
|
||||
};
|
||||
}
|
||||
|
||||
@ -4,4 +4,4 @@ import type { BaseOption } from 'Elements/types';
|
||||
export type CalculationOptions = Record<Elements, BaseOption[]>;
|
||||
|
||||
export type Filter = (options: BaseOption[]) => BaseOption[];
|
||||
export type OptionsFilters = Record<Elements, Filter>;
|
||||
export type CalculationFilters = Record<Elements, Filter>;
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
/* eslint-disable object-curly-newline */
|
||||
/* eslint-disable import/no-cycle */
|
||||
import type { Elements } from 'Components/Calculation/config/map/values';
|
||||
import defaultStatuses from 'config/default-statuses';
|
||||
import type { Status } from 'Elements/types';
|
||||
|
||||
@ -1,5 +1,3 @@
|
||||
/* eslint-disable import/no-cycle */
|
||||
/* eslint-disable implicit-arrow-linebreak */
|
||||
/* eslint-disable object-curly-newline */
|
||||
import type { Elements } from 'Components/Calculation/config/map/values';
|
||||
import { makeAutoObservable, observable } from 'mobx';
|
||||
|
||||
@ -1,6 +1,4 @@
|
||||
/* eslint-disable implicit-arrow-linebreak */
|
||||
/* eslint-disable object-curly-newline */
|
||||
/* eslint-disable import/no-cycle */
|
||||
import type { Elements, ElementsTypes } from 'Components/Calculation/config/map/values';
|
||||
import { getValueName } from 'Components/Calculation/config/map/values';
|
||||
import defaultValues from 'config/default-values';
|
||||
@ -28,7 +26,7 @@ export default class ValuesStore {
|
||||
return this.values[valueName];
|
||||
}
|
||||
|
||||
getValueByElement<E extends Elements>(elementName: E) {
|
||||
getElementValue<E extends Elements>(elementName: E) {
|
||||
const valueName = getValueName(elementName);
|
||||
|
||||
return this.getValue(valueName) as ElementsTypes[E];
|
||||
@ -47,17 +45,6 @@ export default class ValuesStore {
|
||||
this.values[valueName] = value;
|
||||
};
|
||||
|
||||
setValueOfElement = <E extends Elements>(elementName: E, value: ElementsTypes[E]) => {
|
||||
const valueName = getValueName(elementName);
|
||||
this.setValue(valueName, value);
|
||||
};
|
||||
|
||||
resetValues = (valuesNames: readonly Values[]) => {
|
||||
valuesNames.forEach((valueName) => {
|
||||
this.setValue(valueName, defaultValues[valueName]);
|
||||
});
|
||||
};
|
||||
|
||||
setValues = (
|
||||
values: Partial<CalculationValues>,
|
||||
settings?: { reset: boolean; exclude: Values[] }
|
||||
@ -75,12 +62,23 @@ export default class ValuesStore {
|
||||
});
|
||||
};
|
||||
|
||||
clearValue = (valueName: Values) => {
|
||||
this.setValue(valueName, null);
|
||||
setElementValue = <E extends Elements>(elementName: E, value: ElementsTypes[E]) => {
|
||||
const valueName = getValueName(elementName);
|
||||
this.setValue(valueName, value);
|
||||
};
|
||||
|
||||
clearValueOfElement = (elementName: Elements) => {
|
||||
resetValue = (valueName: Values) => {
|
||||
this.setValue(valueName, defaultValues[valueName]);
|
||||
};
|
||||
|
||||
resetElementValue = (elementName: Elements) => {
|
||||
const valueName = getValueName(elementName);
|
||||
this.clearValue(valueName);
|
||||
this.resetValue(valueName);
|
||||
};
|
||||
|
||||
resetValues = (valuesNames: readonly Values[]) => {
|
||||
valuesNames.forEach((valueName) => {
|
||||
this.resetValue(valueName);
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user