67 lines
1.9 KiB
TypeScript
67 lines
1.9 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';
|
|
|
|
export interface InsuranceTableData {
|
|
values: Insurance.Row[];
|
|
options: Insurance.Options;
|
|
statuses: Insurance.RowStatuses[];
|
|
}
|
|
|
|
export default class InsuranceTable {
|
|
root: RootStore;
|
|
values: Insurance.Row[] = insuranceTableConfig.defaultRows;
|
|
options: Insurance.Options = insuranceTableConfig.defaultOptions;
|
|
statuses: Insurance.RowStatuses[] = insuranceTableConfig.defaultStatuses;
|
|
|
|
constructor(rootStore: RootStore) {
|
|
this.root = rootStore;
|
|
makeAutoObservable(this);
|
|
}
|
|
|
|
hydrate = ({
|
|
values: initialValues,
|
|
options: initialOptions,
|
|
statuses: initialStatuses,
|
|
}: InsuranceTableData) => {
|
|
this.values = initialValues;
|
|
this.options = initialOptions;
|
|
this.statuses = initialStatuses;
|
|
};
|
|
|
|
getRowValue(key: string, valueName: Insurance.Keys) {
|
|
const rowIndex = this.values.findIndex((x) => x.key === key);
|
|
|
|
return this.values[rowIndex][valueName];
|
|
}
|
|
|
|
setRowValues = (key: string, row: Partial<Insurance.Row>) => {
|
|
const rowIndex = this.values.findIndex((x) => x.key === key);
|
|
|
|
if (rowIndex >= 0) {
|
|
mergeWith(this.values[rowIndex], row);
|
|
}
|
|
};
|
|
|
|
getRowOptions(valueName: Insurance.Keys) {
|
|
return this.options[valueName];
|
|
}
|
|
|
|
getRowStatuses(key: string) {
|
|
const rowIndex = this.values.findIndex((x) => x.key === key);
|
|
|
|
return this.statuses[rowIndex];
|
|
}
|
|
|
|
setRowStatuses = (key: string, rowStatuses: Insurance.RowStatuses) => {
|
|
const rowIndex = this.values.findIndex((x) => x.key === key);
|
|
|
|
if (rowIndex >= 0) {
|
|
mergeWith(this.statuses[rowIndex], rowStatuses);
|
|
}
|
|
};
|
|
}
|