2022-09-17 20:21:50 +03:00

80 lines
2.5 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';
export default class OptionsStore {
root: RootStore;
options: CalculationOptions = defaultOptions;
constructor(rootStore: RootStore) {
makeAutoObservable(this);
this.root = rootStore;
}
hydrate = (initialOptions: CalculationOptions) => {
this.options = { ...defaultOptions, ...initialOptions };
};
getOptions<T extends Elements>(elementName: T) {
const options = this.options[elementName];
return options;
}
private checkValueInOptions = (elementName: Elements) => {
/**
* Проверяем, что значение есть в новом списке, иначе сбрасываем значение
*/
const value = this.root.$calculation.getElementValue(elementName);
if (
// eslint-disable-next-line operator-linebreak
!this.options[elementName]?.length ||
this.options[elementName].some((x) => x.value === value)
) {
this.root.$calculation.resetElementValue(elementName);
}
};
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>[];
this.checkValueInOptions(elementName);
};
resetOptions = <T extends Elements>(elementName: T) => {
this.options[elementName] = defaultOptions[elementName];
this.checkValueInOptions(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.resetOptions(elementName);
});
}
(Object.keys(options) as Elements[]).forEach((elementName) => {
const elementOptions = options[elementName];
if (elementOptions) {
this.setOptions(elementName, elementOptions);
}
});
};
}