38 lines
1.0 KiB
TypeScript
38 lines
1.0 KiB
TypeScript
import type RootStore from '../../root';
|
|
import type { CalculationValues, Values } from './types';
|
|
import defaultValues from '@/config/default-values';
|
|
import { makeAutoObservable } from 'mobx';
|
|
import { pick } from 'radash';
|
|
|
|
export default class ValuesStore {
|
|
private root: RootStore;
|
|
public 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[]) => pick(this.values, keys);
|
|
|
|
public setValues = (values: CalculationValues) => {
|
|
this.values = values;
|
|
};
|
|
|
|
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]);
|
|
};
|
|
}
|