2023-09-18 10:21:18 +03:00

88 lines
2.3 KiB
TypeScript

import type RootStore from '../../root';
import Validation from '../../validation';
import type { ValidationConfig } from '../../validation/types';
import type * as ELT from '@/Components/Calculation/Form/ELT/types';
import { notification } from '@/Components/Common/Notification';
import type { IObservableArray } from 'mobx';
import { makeAutoObservable, observable, reaction, toJS } from 'mobx';
type ConstructorInput = {
rootStore: RootStore;
validationConfig: ValidationConfig;
};
export default class PolicyStore {
private root: RootStore;
public validation: Validation;
private rows: IObservableArray<ELT.Row>;
private selectedKey: string | null = null;
public abortController = new AbortController();
constructor({ rootStore, validationConfig }: ConstructorInput) {
this.rows = observable<ELT.Row>([]);
makeAutoObservable(this);
this.validation = new Validation(validationConfig, rootStore);
this.root = rootStore;
reaction(
() => toJS(this.rows),
() => {
this.resetSelectedKey();
}
);
reaction(
() => this.selectedKey,
(selectedKey) => {
if (!selectedKey) {
notification.open({
description: 'Выбранный расчет ЭЛТ был сброшен',
key: validationConfig.err_key,
message: validationConfig.err_title,
placement: 'bottomRight',
type: 'warning',
});
}
}
);
}
public setRows = (rows: ELT.Row[]) => {
this.rows.replace(rows);
};
public setRow = (row: Partial<ELT.Row> & Pick<ELT.Row, 'key'>) => {
const index = this.rows.findIndex((x) => x.key === row.key);
if (index >= 0) this.rows[index] = { ...this.rows[index], ...row };
};
public getRow(key: string) {
return this.rows.find((x) => x.key === key);
}
public get getRows() {
return toJS(this.rows);
}
public get hasErrors() {
return this.validation.hasErrors;
}
public setSelectedKey = (key: string) => {
this.selectedKey = key;
};
public resetSelectedKey = () => {
if (this.setSelectedKey !== null) this.selectedKey = null;
};
public get getSelectedRow() {
return this.rows.find((x) => x.key === this.selectedKey);
}
public reset = () => {
this.rows.clear();
};
}