42 lines
1.3 KiB
TypeScript
42 lines
1.3 KiB
TypeScript
import type { CalculationOptions } from './types';
|
|
import type { Elements, ElementsTypes } from '@/Components/Calculation/config/map/values';
|
|
import defaultOptions from '@/config/default-options';
|
|
import type RootStore from '@/stores/root';
|
|
import { makeAutoObservable } from 'mobx';
|
|
import type { BaseOption } from 'ui/elements/types';
|
|
|
|
export default class OptionsStore {
|
|
private root: RootStore;
|
|
private options: CalculationOptions = defaultOptions;
|
|
|
|
constructor(rootStore: RootStore) {
|
|
makeAutoObservable(this);
|
|
this.root = rootStore;
|
|
}
|
|
|
|
public hydrate = (initialOptions: CalculationOptions) => {
|
|
this.options = { ...defaultOptions, ...initialOptions };
|
|
};
|
|
|
|
public getOptions<T extends Elements>(elementName: T) {
|
|
return this.options[elementName];
|
|
}
|
|
|
|
public setOptions = <T extends Elements>(
|
|
elementName: T,
|
|
options: Array<BaseOption<ElementsTypes[T]>>
|
|
) => {
|
|
/**
|
|
* TODO: use T instead of any in BaseOption type
|
|
* at this moment T causes typescript error
|
|
* but infer works just perfect
|
|
*/
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
this.options[elementName] = options as Array<BaseOption<any>>;
|
|
};
|
|
|
|
public resetOptions = <T extends Elements>(elementName: T) => {
|
|
this.setOptions(elementName, defaultOptions[elementName]);
|
|
};
|
|
}
|