81 lines
2.1 KiB
TypeScript
81 lines
2.1 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 type { IObservableArray } from 'mobx';
|
|
import { makeAutoObservable, observable, reaction, toJS } from 'mobx';
|
|
import { notification } from 'ui/elements';
|
|
|
|
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;
|
|
|
|
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,
|
|
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 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();
|
|
};
|
|
}
|