2023-03-28 09:33:17 +03:00

56 lines
1.6 KiB
TypeScript

import Validation from '../../validation';
import type { ValidationParams } from '../../validation/types';
import type * as FinGAP from '@/Components/Calculation/Form/Insurance/FinGAPTable/types';
import type RootStore from '@/stores/root';
import type { IObservableArray } from 'mobx';
import { makeAutoObservable, observable, toJS } from 'mobx';
export default class FinGAPTable {
private root: RootStore;
public validation: Validation;
public risks: IObservableArray<FinGAP.Risk>;
public selectedKeys: Set<string>;
constructor(rootStore: RootStore) {
this.selectedKeys = new Set();
this.risks = observable<FinGAP.Risk>([]);
this.validation = new Validation({
err_key: 'ERR_FINGAP_TABLE',
err_title: 'Таблица рисков Safe Finance',
});
makeAutoObservable(this);
this.root = rootStore;
}
public getSelectedRisks() {
return toJS(this.risks.filter((x) => this.selectedKeys.has(x.key)));
}
public setRisks = (risks: FinGAP.Risk[]) => {
this.risks.replace(risks);
};
public setSelectedKeys = (keys: string[]) => {
this.selectedKeys = new Set(keys);
};
public get totalSum() {
return this.risks
.filter((risk) => this.selectedKeys.has(risk.key))
.reduce((sum, risk) => sum + risk.premium, 0);
}
public setError = (params: ValidationParams) => this.validation.setError(params);
public removeError = (params: Pick<ValidationParams, 'key'>) => {
this.validation.removeError(params);
};
public clear = () => {
this.risks.clear();
this.selectedKeys.clear();
};
}