From 5d65e85f57371107415a96ac35d8cc98f3e569ff Mon Sep 17 00:00:00 2001 From: Chika Date: Wed, 6 Jul 2022 13:19:05 +0300 Subject: [PATCH] insurance/store: minor refactor names --- .../Insurance/InsuranceTable/builders.tsx | 14 ++++++++++--- .../Form/Insurance/InsuranceTable/types.ts | 20 +++++++++---------- stores/tables/insurance/index.ts | 20 +++++++++++++------ 3 files changed, 35 insertions(+), 19 deletions(-) diff --git a/Components/Calculation/Form/Insurance/InsuranceTable/builders.tsx b/Components/Calculation/Form/Insurance/InsuranceTable/builders.tsx index c0fd444..0f1b35b 100644 --- a/Components/Calculation/Form/Insurance/InsuranceTable/builders.tsx +++ b/Components/Calculation/Form/Insurance/InsuranceTable/builders.tsx @@ -2,9 +2,13 @@ import { observer } from 'mobx-react-lite'; import type { ComponentType } from 'react'; import { useRowOptions, useRowStatuses } from 'stores/tables/insurance/hooks'; import { useInsuranceValue } from './hooks'; -import type { Keys } from './types'; +import type { Values } from './types'; -export function buildOptionComponent(key: string, Component: ComponentType, valueName: Keys) { +export function buildOptionComponent( + key: string, + Component: ComponentType, + valueName: Values +) { return observer((props: T) => { const [value, setValue] = useInsuranceValue(key, valueName); const options = useRowOptions(key); @@ -22,7 +26,11 @@ export function buildOptionComponent(key: string, Component: ComponentType }); } -export function buildValueComponent(key: string, Component: ComponentType, valueName: Keys) { +export function buildValueComponent( + key: string, + Component: ComponentType, + valueName: Values +) { return observer((props: T) => { const [value, setValue] = useInsuranceValue(key, valueName); const statuses = useRowStatuses(key); diff --git a/Components/Calculation/Form/Insurance/InsuranceTable/types.ts b/Components/Calculation/Form/Insurance/InsuranceTable/types.ts index be4df17..98f2771 100644 --- a/Components/Calculation/Form/Insurance/InsuranceTable/types.ts +++ b/Components/Calculation/Form/Insurance/InsuranceTable/types.ts @@ -1,7 +1,9 @@ import type { BaseOption, Status } from 'Elements/types'; +export type Keys = 'osago' | 'kasko' | 'dgo' | 'ns' | 'finGAP'; + export type RowValues = { - key: string; + key: Keys; policyType: string; insuranceCompany: string | null; insured: 100_000_000 | 100_000_001 | null; @@ -9,14 +11,12 @@ export type RowValues = { insTerm: 100_000_000 | 100_000_001 | null; }; -export type Keys = keyof RowValues; +export type Values = Exclude; -type Options = { - [ValueName in keyof RowValues]?: BaseOption[]; -}; - -export type RowOptions = Omit & { key: string }; - -export type RowStatuses = Omit, 'key'> & { - key: string; +export type RowOptions = { + [ValueName in Values]?: BaseOption[]; +} & { key: Keys }; + +export type RowStatuses = Record & { + key: Keys; }; diff --git a/stores/tables/insurance/index.ts b/stores/tables/insurance/index.ts index 4b9125d..c88ee7b 100644 --- a/stores/tables/insurance/index.ts +++ b/stores/tables/insurance/index.ts @@ -35,33 +35,41 @@ export default class InsuranceTable { this.statuses = initialStatuses; }; - getRowValue(key: string, valueName: Insurance.Keys) { + getRowValue(key: Insurance.Keys, valueName: Insurance.Values) { const rowIndex = this.values.findIndex((x) => x.key === key); return this.values[rowIndex][valueName]; } - setRowValues = (key: string, row: Partial) => { + setRowValues = (key: Insurance.Keys, rowValues: Partial) => { const rowIndex = this.values.findIndex((x) => x.key === key); if (rowIndex >= 0) { - mergeWith(this.values[rowIndex], row); + mergeWith(this.values[rowIndex], rowValues); } }; - getRowOptions(key: string) { + getRowOptions(key: Insurance.Keys) { const rowIndex = this.options.findIndex((x) => x.key === key); return this.options[rowIndex]; } - getRowStatuses(key: string) { + setRowOptions = (key: Insurance.Keys, rowOptions: Partial) => { + 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: string, rowStatuses: Insurance.RowStatuses) => { + setRowStatuses = (key: Insurance.Keys, rowStatuses: Insurance.RowStatuses) => { const rowIndex = this.values.findIndex((x) => x.key === key); if (rowIndex >= 0) {