store(insurance): support options per row

This commit is contained in:
Chika 2022-06-23 17:06:08 +03:00
parent ae713fe6b2
commit 6ef19c826f
5 changed files with 130 additions and 31 deletions

View File

@ -7,13 +7,13 @@ import type { Keys } from './types';
export function buildOptionComponent<T>(key: string, Component: ComponentType<T>, valueName: Keys) {
return observer((props: T) => {
const [value, setValue] = useInsuranceValue(key, valueName);
const options = useRowOptions(valueName);
const options = useRowOptions(key);
const statuses = useRowStatuses(key);
return (
<Component
value={value}
options={options}
options={options[valueName]}
setValue={setValue}
status={statuses[valueName]}
{...props}

View File

@ -11,10 +11,12 @@ export type Row = {
export type Keys = keyof Row;
export type Options = {
type Options = {
[Key in keyof Row]?: BaseOption<Row[Key]>[];
};
export type RowOptions = Omit<Options, 'key'> & { key: string };
export type RowStatuses = Omit<Record<Keys, Status>, 'key'> & {
key: string;
};

View File

@ -1,28 +1,123 @@
/* eslint-disable object-curly-newline */
import type * as Insurance from 'Components/Calculation/Form/Insurance/InsuranceTable/types';
export const defaultOptions: Insurance.Options = {
insured: [
{
label: 'ЛП',
value: 100_000_000,
},
{
label: 'ЛД',
value: 100_000_001,
},
],
insTerm: [
{
label: '12 месяцев',
value: 100_000_000,
},
{
label: 'Срок ДЛ',
value: 100_000_001,
},
],
};
export const defaultOptions: Insurance.RowOptions[] = [
{
key: 'osago',
insured: [
{
label: 'ЛП',
value: 100_000_000,
},
{
label: 'ЛД',
value: 100_000_001,
},
],
insTerm: [
{
label: '12 месяцев',
value: 100_000_000,
},
{
label: 'Срок ДЛ',
value: 100_000_001,
},
],
},
{
key: 'kasko',
insured: [
{
label: 'ЛП',
value: 100_000_000,
},
{
label: 'ЛД',
value: 100_000_001,
},
],
insTerm: [
{
label: '12 месяцев',
value: 100_000_000,
},
{
label: 'Срок ДЛ',
value: 100_000_001,
},
],
},
{
key: 'dgo',
insured: [
{
label: 'ЛП',
value: 100_000_000,
},
{
label: 'ЛД',
value: 100_000_001,
},
],
insTerm: [
{
label: '12 месяцев',
value: 100_000_000,
},
{
label: 'Срок ДЛ',
value: 100_000_001,
},
],
},
{
key: 'ns',
insured: [
{
label: 'ЛП',
value: 100_000_000,
},
{
label: 'ЛД',
value: 100_000_001,
},
],
insTerm: [
{
label: '12 месяцев',
value: 100_000_000,
},
{
label: 'Срок ДЛ',
value: 100_000_001,
},
],
},
{
key: 'finGAP',
insured: [
{
label: 'ЛП',
value: 100_000_000,
},
{
label: 'ЛД',
value: 100_000_001,
},
],
insTerm: [
{
label: '12 месяцев',
value: 100_000_000,
},
{
label: 'Срок ДЛ',
value: 100_000_001,
},
],
},
];
export const defaultRows: Insurance.Row[] = [
{

View File

@ -14,10 +14,10 @@ export function useRowValue(key, valueName) {
return [storeValue, setStoreValue];
}
export function useRowOptions(valueName) {
export function useRowOptions(key) {
const { $tables } = useStore();
const options = $tables.insurance.getRowOptions(valueName);
const options = $tables.insurance.getRowOptions(key);
return options;
}

View File

@ -7,14 +7,14 @@ import type RootStore from 'stores/root';
export interface InsuranceTableData {
values: Insurance.Row[];
options: Insurance.Options;
options: Insurance.RowOptions[];
statuses: Insurance.RowStatuses[];
}
export default class InsuranceTable {
root: RootStore;
values: Insurance.Row[] = insuranceTableConfig.defaultRows;
options: Insurance.Options = insuranceTableConfig.defaultOptions;
options: Insurance.RowOptions[] = insuranceTableConfig.defaultOptions;
statuses: Insurance.RowStatuses[] = insuranceTableConfig.defaultStatuses;
constructor(rootStore: RootStore) {
@ -46,8 +46,10 @@ export default class InsuranceTable {
}
};
getRowOptions(valueName: Insurance.Keys) {
return this.options[valueName];
getRowOptions(key: string) {
const rowIndex = this.options.findIndex((x) => x.key === key);
return this.options[rowIndex];
}
getRowStatuses(key: string) {