80 lines
2.4 KiB
TypeScript
80 lines
2.4 KiB
TypeScript
/* eslint-disable object-curly-newline */
|
|
import type * as Insurance from 'Components/Calculation/Form/Insurance/InsuranceTable/types';
|
|
import * as insuranceTableConfig from 'config/tables/insurance-table';
|
|
import { mergeWith } from 'lodash';
|
|
import { makeAutoObservable } from 'mobx';
|
|
import type RootStore from 'stores/root';
|
|
import Validation from '../validation';
|
|
|
|
export interface InsuranceTableData {
|
|
values: Insurance.RowValues[];
|
|
options: Insurance.RowOptions[];
|
|
statuses: Insurance.RowStatuses[];
|
|
}
|
|
|
|
export default class InsuranceTable {
|
|
root: RootStore;
|
|
validation: Validation;
|
|
values: Insurance.RowValues[] = insuranceTableConfig.defaultValues;
|
|
options: Insurance.RowOptions[] = insuranceTableConfig.defaultOptions;
|
|
statuses: Insurance.RowStatuses[] = insuranceTableConfig.defaultStatuses;
|
|
|
|
constructor(rootStore: RootStore) {
|
|
this.root = rootStore;
|
|
this.validation = new Validation();
|
|
makeAutoObservable(this);
|
|
}
|
|
|
|
hydrate = ({
|
|
values: initialValues,
|
|
options: initialOptions,
|
|
statuses: initialStatuses,
|
|
}: InsuranceTableData) => {
|
|
this.values = initialValues;
|
|
this.options = initialOptions;
|
|
this.statuses = initialStatuses;
|
|
};
|
|
|
|
getRowValue(key: Insurance.Keys, valueName: Insurance.Values) {
|
|
const rowIndex = this.values.findIndex((x) => x.key === key);
|
|
|
|
return this.values[rowIndex][valueName];
|
|
}
|
|
|
|
setRowValues = (key: Insurance.Keys, rowValues: Partial<Insurance.RowValues>) => {
|
|
const rowIndex = this.values.findIndex((x) => x.key === key);
|
|
|
|
if (rowIndex >= 0) {
|
|
mergeWith(this.values[rowIndex], rowValues);
|
|
}
|
|
};
|
|
|
|
getRowOptions(key: Insurance.Keys) {
|
|
const rowIndex = this.options.findIndex((x) => x.key === key);
|
|
|
|
return this.options[rowIndex];
|
|
}
|
|
|
|
setRowOptions = (key: Insurance.Keys, rowOptions: Partial<Insurance.RowOptions>) => {
|
|
const rowIndex = this.options.findIndex((x) => x.key === key);
|
|
|
|
if (rowIndex >= 0) {
|
|
mergeWith(this.options[rowIndex], rowOptions);
|
|
}
|
|
};
|
|
|
|
getRowStatuses(key: Insurance.Keys) {
|
|
const rowIndex = this.values.findIndex((x) => x.key === key);
|
|
|
|
return this.statuses[rowIndex];
|
|
}
|
|
|
|
setRowStatuses = (key: Insurance.Keys, rowStatuses: Insurance.RowStatuses) => {
|
|
const rowIndex = this.values.findIndex((x) => x.key === key);
|
|
|
|
if (rowIndex >= 0) {
|
|
mergeWith(this.statuses[rowIndex], rowStatuses);
|
|
}
|
|
};
|
|
}
|