97 lines
2.9 KiB
TypeScript
97 lines
2.9 KiB
TypeScript
/* eslint-disable object-curly-newline */
|
||
/* eslint-disable unicorn/prefer-set-has */
|
||
import type { Elements, ElementsTypes } from 'Components/Calculation/config/map/values';
|
||
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 } 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;
|
||
|
||
constructor(rootStore: RootStore) {
|
||
makeAutoObservable(this);
|
||
this.root = rootStore;
|
||
}
|
||
|
||
hydrate = (initialOptions: CalculationOptions) => {
|
||
this.options = initialOptions;
|
||
};
|
||
|
||
getOptions<T extends Elements>(elementName: T) {
|
||
const options = this.options[elementName];
|
||
|
||
return options;
|
||
}
|
||
|
||
setOptions = <T extends Elements>(elementName: T, options: BaseOption<ElementsTypes[T]>[]) => {
|
||
/**
|
||
* TODO: use T instead of any in BaseOption type
|
||
* at this moment T causes typescript error
|
||
* but infer works just perfect
|
||
*/
|
||
this.options[elementName] = options as BaseOption<any>[];
|
||
|
||
/**
|
||
* Проверяем, что значение есть в новом списке, иначе сбрасываем значение
|
||
*/
|
||
const value = this.root.$calculation.getElementValue(elementName);
|
||
if (!options?.length || options.some((x) => x.value === value)) {
|
||
this.root.$calculation.resetElementValue(elementName);
|
||
}
|
||
|
||
/**
|
||
* Если в новом списке одна запись, то указываем ее
|
||
* (для элементов из списка {@link AUTO_SET_VALUE_ELEMENTS})
|
||
*/
|
||
if (options?.length === 1 && AUTO_SET_VALUE_ELEMENTS.includes(elementName)) {
|
||
const optionValue = options.at(0)?.value;
|
||
|
||
if (optionValue) {
|
||
this.root.$calculation.setElementValue(elementName, optionValue);
|
||
}
|
||
}
|
||
};
|
||
|
||
resetOption = <T extends Elements>(elementName: T) => {
|
||
this.options[elementName] = defaultOptions[elementName];
|
||
};
|
||
|
||
setManyOptions = (
|
||
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) => {
|
||
const elementOptions = options[elementName];
|
||
|
||
if (elementOptions) {
|
||
this.setOptions(elementName, elementOptions);
|
||
}
|
||
});
|
||
};
|
||
}
|