45 lines
1.3 KiB
TypeScript
45 lines
1.3 KiB
TypeScript
import type RootStore from '../../root';
|
|
import type { CalculationValues, Values } from './types';
|
|
import defaultValues from '@/config/default-values';
|
|
import { makeAutoObservable, toJS } from 'mobx';
|
|
import { pick } from 'radash';
|
|
|
|
export default class ValuesStore {
|
|
private root: RootStore;
|
|
private values = defaultValues;
|
|
|
|
constructor(rootStore: RootStore) {
|
|
makeAutoObservable(this);
|
|
this.root = rootStore;
|
|
}
|
|
|
|
public hydrate = (initialValues: CalculationValues) => {
|
|
this.values = { ...defaultValues, ...initialValues };
|
|
};
|
|
|
|
public getValues = <K extends keyof CalculationValues>(keys?: K[]) => {
|
|
if (keys) return pick(this.values, keys);
|
|
|
|
return toJS(this.values);
|
|
};
|
|
|
|
public setValues = (values: Partial<CalculationValues>) => {
|
|
(Object.keys(values) as Array<keyof CalculationValues>).forEach((valueName) => {
|
|
const value = values[valueName];
|
|
if (value) this.setValue(valueName, value);
|
|
});
|
|
};
|
|
|
|
public getValue<V extends Values>(valueName: V) {
|
|
return this.values[valueName];
|
|
}
|
|
|
|
public setValue = <V extends Values>(valueName: V, value: CalculationValues[V]) => {
|
|
this.values[valueName] = value;
|
|
};
|
|
|
|
public resetValue = (valueName: Values) => {
|
|
this.setValue(valueName, defaultValues[valueName]);
|
|
};
|
|
}
|