import Validation from '../validation'; 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 { observable } from 'mobx'; import type { BaseOption } from 'ui/elements/types'; export default class CalculationStore { private root: RootStore; public $values: ValuesStore; public $status: StatusStore; public $options: OptionsStore; public $validation: Partial>; public get hasErrors() { return (Object.keys(this.$validation) as Values.Elements[]).some( (elementName) => this.$validation[elementName]?.hasErrors ); } constructor(rootStore: RootStore) { this.root = rootStore; this.$values = new ValuesStore(rootStore); this.$status = new StatusStore(rootStore); this.$options = new OptionsStore(rootStore); this.$validation = observable.object({}); } private createElementValidation = (elementName: E) => { this.$validation[elementName] = new Validation( { err_key: elementName, err_title: titles[elementName], }, this.root ); }; public element = (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) => { 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>) => { 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); }, }); }