121 lines
3.6 KiB
TypeScript
121 lines
3.6 KiB
TypeScript
/* 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';
|
||
|
||
const AUTO_SET_VALUE_ELEMENTS: Elements[] = [
|
||
'selectProduct',
|
||
'selectLeaseObjectType',
|
||
'selectBrand',
|
||
'selectModel',
|
||
'selectConfiguration',
|
||
'selectTracker',
|
||
'selectTelematic',
|
||
'selectTechnicalCard',
|
||
'selectFuelCard',
|
||
'selectRegistration',
|
||
'selectTownRegistration',
|
||
];
|
||
|
||
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) => {
|
||
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;
|
||
}
|
||
) {
|
||
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];
|
||
};
|
||
}
|