2023-03-16 15:35:44 +03:00

113 lines
3.3 KiB
TypeScript

import type { ValidationParams } from '../validation/types';
import OptionsStore from './options';
import StatusStore from './statuses';
import ValuesStore from './values';
import titles from '@/Components/Calculation/config/elements-titles';
import type * as Values from '@/Components/Calculation/config/map/values';
import { getValueName } from '@/Components/Calculation/config/map/values';
import type RootStore from '@/stores/root';
import Validation from '@/stores/validation';
import { observable } from 'mobx';
import type { BaseOption } from 'ui/elements/types';
export default class CalculationStore {
public $values: ValuesStore;
public $status: StatusStore;
public $options: OptionsStore;
public $validation: Partial<Record<Values.Elements, Validation>>;
constructor(rootStore: RootStore) {
this.$values = new ValuesStore(rootStore);
this.$status = new StatusStore(rootStore);
this.$options = new OptionsStore(rootStore);
this.$validation = observable.object({});
}
private createElementValidation = <E extends Values.Elements>(elementName: E) => {
this.$validation[elementName] = new Validation({
err_key: elementName,
err_title: titles[elementName],
});
};
public element = <E extends Values.Elements>(elementName: E) => ({
block: () => {
this.$status.setStatus(elementName, 'Disabled');
return this.element(elementName);
},
cleanErrors: () => {
this.$validation[elementName]?.clearErrors();
},
getOption: () => {
const valueName = getValueName(elementName);
const value = this.$values.getValue(valueName) as Values.ElementsTypes[E];
return this.$options.getOptions(elementName)?.find((x) => x.value === value);
},
getOptions: () => this.$options.getOptions(elementName),
getValue: () => {
const valueName = getValueName(elementName);
return this.$values.getValue(valueName) as Values.ElementsTypes[E];
},
removeError: (params: Pick<ValidationParams, 'key'>) => {
this.$validation[elementName]?.removeError(params);
},
reset: () => {
const valueName = getValueName(elementName);
this.$values.resetValue(valueName);
this.$options.resetOptions(elementName);
this.$status.resetStatus(elementName);
this.$validation[elementName]?.clearErrors();
return this.element(elementName);
},
resetOptions: () => {
this.$options.resetOptions(elementName);
return this.element(elementName);
},
resetValue: () => {
const valueName = getValueName(elementName);
this.$values.resetValue(valueName);
return this.element(elementName);
},
setError: (params: ValidationParams) => {
if (!this.$validation[elementName]) this.createElementValidation(elementName);
return this.$validation[elementName]?.setError(params);
},
setOptions: (options: Array<BaseOption<Values.ElementsTypes[E]>>) => {
this.$options.setOptions(elementName, options);
return this.element(elementName);
},
setValue: (value: Values.ElementsTypes[E]) => {
const valueName = getValueName(elementName);
this.$values.setValue(valueName, value);
return this.element(elementName);
},
unblock: () => {
this.$status.setStatus(elementName, 'Default');
return this.element(elementName);
},
});
}