Chika b7c02370ba stores: add defaultFilters
add resetElement
beautify stores code
2022-06-01 18:09:03 +03:00

119 lines
3.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* 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 { CalculationOptions, Filter } from './types';
const AUTO_SET_VALUE_ELEMENTS: Elements[] = [
'selectProduct',
'selectLeaseObjectType',
'selectBrand',
'selectModel',
'selectConfiguration',
'selectTracker',
'selectTelematic',
'selectTechnicalCard',
'selectFuelCard',
'selectRegistration',
'selectTownRegistration',
];
export default class OptionsStore {
root: RootStore;
options = defaultOptions;
filters = defaultFilters;
constructor(rootStore: RootStore) {
makeAutoObservable(this);
this.root = rootStore;
}
hydrate = (initialOptions: CalculationOptions) => {
this.options = initialOptions;
};
/** **************** 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;
}
) {
const options = this.options[elementName];
if (!settings?.filtered) return options;
const filter = this.filters[elementName];
return filter ? options && filter(options) : options;
}
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) {
(Object.keys(defaultOptions) as Elements[])
.filter((elementName) => !settings?.exclude.includes(elementName))
.forEach((elementName) => {
this.resetOption(elementName);
});
}
(Object.keys(options) as Elements[]).forEach((elementName) => {
this.setOption(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];
};
}