From 21c97ece2b56ef4a652df5364c01ec272ed7de89 Mon Sep 17 00:00:00 2001 From: Chika Date: Thu, 3 Nov 2022 11:55:36 +0300 Subject: [PATCH] stores/tables: refactor insurance methods --- .../Insurance/InsuranceTable/builders.tsx | 20 +++--- .../Form/Insurance/InsuranceTable/hooks.js | 11 ++- process/fingap/reactions/common.ts | 47 ++++++------ process/fingap/reactions/validation.ts | 2 +- process/init/get-data/index.js | 6 +- .../tables/insurance/__tests__/index.test.js | 71 ++++++++----------- stores/tables/insurance/hooks.js | 29 +------- stores/tables/insurance/index.ts | 56 ++++----------- 8 files changed, 90 insertions(+), 152 deletions(-) diff --git a/Components/Calculation/Form/Insurance/InsuranceTable/builders.tsx b/Components/Calculation/Form/Insurance/InsuranceTable/builders.tsx index 0f1b35b..53acdc1 100644 --- a/Components/Calculation/Form/Insurance/InsuranceTable/builders.tsx +++ b/Components/Calculation/Form/Insurance/InsuranceTable/builders.tsx @@ -1,6 +1,6 @@ import { observer } from 'mobx-react-lite'; import type { ComponentType } from 'react'; -import { useRowOptions, useRowStatuses } from 'stores/tables/insurance/hooks'; +import { useRow } from 'stores/tables/insurance/hooks'; import { useInsuranceValue } from './hooks'; import type { Values } from './types'; @@ -11,17 +11,12 @@ export function buildOptionComponent( ) { return observer((props: T) => { const [value, setValue] = useInsuranceValue(key, valueName); - const options = useRowOptions(key); - const statuses = useRowStatuses(key); + const { getOptions, getStatus } = useRow(key); + const options = getOptions(valueName); + const statuses = getStatus(valueName); return ( - + ); }); } @@ -33,8 +28,9 @@ export function buildValueComponent( ) { return observer((props: T) => { const [value, setValue] = useInsuranceValue(key, valueName); - const statuses = useRowStatuses(key); + const { getStatus } = useRow(key); + const statuses = getStatus(valueName); - return ; + return ; }); } diff --git a/Components/Calculation/Form/Insurance/InsuranceTable/hooks.js b/Components/Calculation/Form/Insurance/InsuranceTable/hooks.js index 12ffe6c..517a310 100644 --- a/Components/Calculation/Form/Insurance/InsuranceTable/hooks.js +++ b/Components/Calculation/Form/Insurance/InsuranceTable/hooks.js @@ -1,10 +1,17 @@ /* eslint-disable import/prefer-default-export */ import { useEffect, useState } from 'react'; -import { useRowValue } from 'stores/tables/insurance/hooks'; +import { useRow } from 'stores/tables/insurance/hooks'; import { useDebouncedCallback } from 'use-debounce'; export function useInsuranceValue(key, valueName) { - const [storeValue, setStoreValue] = useRowValue(key, valueName); + const row = useRow(key); + + const storeValue = row.getValue(valueName); + + function setStoreValue(value) { + return row.setValue(valueName, value); + } + const [value, setValue] = useState(storeValue); // eslint-disable-next-line object-curly-newline diff --git a/process/fingap/reactions/common.ts b/process/fingap/reactions/common.ts index dffc3b1..311df0e 100644 --- a/process/fingap/reactions/common.ts +++ b/process/fingap/reactions/common.ts @@ -27,9 +27,7 @@ export default function commonReactions( reaction( () => $tables.fingap.totalSum, (totalSum) => { - $tables.insurance.setRowValues('fingap', { - insCost: totalSum, - }); + $tables.insurance.row('fingap').setValue('insCost', totalSum); } ); @@ -52,7 +50,7 @@ export default function commonReactions( */ reaction( () => { - const finGAPInsuranceCompany = $tables.insurance.getRowValue('fingap', 'insuranceCompany'); + const finGAPInsuranceCompany = $tables.insurance.row('fingap').getValue('insuranceCompany'); const leasingPeriod = $calculation.element('tbxLeasingPeriod').getValue(); return { @@ -62,27 +60,26 @@ export default function commonReactions( }, ({ finGAPInsuranceCompany, leasingPeriod }) => { if (!finGAPInsuranceCompany) { - $tables.insurance.setRowValues('fingap', { - insured: 100_000_000, - insCost: 0, - insTerm: 100_000_000, - }); - - $tables.insurance.setRowStatuses('fingap', { - insured: 'Disabled', - insTerm: 'Disabled', - }); + $tables.insurance + .row('fingap') + .setValue('insured', 100_000_000) + .setValue('insCost', 0) + .setValue('insTerm', 100_000_000) + .block('insured') + .block('insTerm'); } else { - $tables.insurance.setRowValues('fingap', { - insured: 100_000_000, - insCost: 0, - insTerm: leasingPeriod < 13 ? 100_000_000 : undefined, - }); + $tables.insurance + .row('fingap') + .setValue('insured', 100_000_000) + .setValue('insCost', 0) + .setValue('insTerm', leasingPeriod < 13 ? 100_000_000 : null) + .unblock('insured'); - $tables.insurance.setRowStatuses('fingap', { - insured: 'Default', - insTerm: leasingPeriod < 13 ? 'Disabled' : 'Default', - }); + if (leasingPeriod < 13) { + $tables.insurance.row('fingap').block('insTerm'); + } else { + $tables.insurance.row('fingap').unblock('insTerm'); + } } }, { @@ -92,7 +89,7 @@ export default function commonReactions( // Очищаем таблицу ФинГАП, если не выбрана страховая компания reaction( - () => $tables.insurance.getRowValue('fingap', 'insuranceCompany'), + () => $tables.insurance.row('fingap').getValue('insuranceCompany'), (finGAPInsuranceCompany) => { if (!finGAPInsuranceCompany) { $tables.fingap.clear(); @@ -123,7 +120,7 @@ export default function commonReactions( reaction( () => { - const finGAPInsuranceCompany = $tables.insurance.getRowValue('fingap', 'insuranceCompany'); + const finGAPInsuranceCompany = $tables.insurance.row('fingap').getValue('insuranceCompany'); const paymentsValues = toJS($tables.payments.values); const plPriceRub = $calculation.$values.getValue('plPriceRub'); const discountRub = $calculation.$values.getValue('discountRub'); diff --git a/process/fingap/reactions/validation.ts b/process/fingap/reactions/validation.ts index 6060397..64eca5a 100644 --- a/process/fingap/reactions/validation.ts +++ b/process/fingap/reactions/validation.ts @@ -13,7 +13,7 @@ export default function validationReactions( reaction( () => { const hasPaymentsErrors = $tables.payments.validation.hasErrors; - const finGAPInsuranceCompany = $tables.insurance.getRowValue('fingap', 'insuranceCompany'); + const finGAPInsuranceCompany = $tables.insurance.row('fingap').getValue('insuranceCompany'); return { hasPaymentsErrors, diff --git a/process/init/get-data/index.js b/process/init/get-data/index.js index ec6fcfd..8ebb35d 100644 --- a/process/init/get-data/index.js +++ b/process/init/get-data/index.js @@ -22,7 +22,11 @@ export default function getData(apolloClient, store) { function setManyRowOptions(options) { Object.keys(options).forEach((key) => { const rowOptions = options[key]; - if (rowOptions !== undefined) $tables.insurance.setRowOptions(key, rowOptions); + if (rowOptions !== undefined) { + Object.keys(rowOptions).forEach((valueName) => { + $tables.insurance.row(key).setOptions(valueName, rowOptions[valueName]); + }); + } }); } diff --git a/stores/tables/insurance/__tests__/index.test.js b/stores/tables/insurance/__tests__/index.test.js index 1fac12c..a809e0b 100644 --- a/stores/tables/insurance/__tests__/index.test.js +++ b/stores/tables/insurance/__tests__/index.test.js @@ -21,16 +21,14 @@ const INSURED_OPTIONS = [ ]; describe('stores/tables/insurance', () => { - describe('[method] setRowValues', () => { - it('should set only insuranceCompany value', () => { + describe('[method] setValue', () => { + test('should set only insuranceCompany value', () => { const rootStore = new RootStore(); const { insurance } = rootStore.$tables; const TEST_VALUE = 'TEST_VALUE'; - insurance.setRowValues('kasko', { - insuranceCompany: TEST_VALUE, - }); + insurance.row('kasko').setValue('insuranceCompany', TEST_VALUE); const KASKO_VALUES = toJS(insurance.values.find((x) => x.key === 'kasko')); const DEFAULT_KASKO_VALUES = defaultValues.find((x) => x.key === 'kasko'); @@ -46,62 +44,49 @@ describe('stores/tables/insurance', () => { }); }); - describe('[method] setRowOptions', () => { - it('should replace only insuranceCompany options', () => { + describe('[method] setOptions', () => { + test('should replace only insuranceCompany options', () => { const rootStore = new RootStore(); const { insurance } = rootStore.$tables; - insurance.setRowOptions('kasko', { - insuranceCompany: INSURANCE_COMPANIES_OPTIONS, - }); + insurance.row('kasko').setOptions('insuranceCompany', INSURANCE_COMPANIES_OPTIONS); - const KASKO = toJS(insurance.getRowOptions('kasko')); - - expect(KASKO).toEqual({ - insuranceCompany: INSURANCE_COMPANIES_OPTIONS, - insured: defaultOptions.kasko.insured, - insTerm: defaultOptions.kasko.insTerm, - }); + const { getOptions } = insurance.row('kasko'); + expect(getOptions('insuranceCompany')).toEqual(INSURANCE_COMPANIES_OPTIONS); + expect(getOptions('insured')).toEqual(defaultOptions.kasko.insured); + expect(getOptions('insTerm')).toEqual(defaultOptions.kasko.insTerm); }); - it('should replace insuranceCompany and insured options', () => { + test('should replace insuranceCompany and insured options', () => { const rootStore = new RootStore(); const { insurance } = rootStore.$tables; - insurance.setRowOptions('kasko', { - insuranceCompany: INSURANCE_COMPANIES_OPTIONS, - insured: INSURED_OPTIONS, - }); + insurance + .row('kasko') + .setOptions('insuranceCompany', INSURANCE_COMPANIES_OPTIONS) + .setOptions('insured', INSURED_OPTIONS); - const KASKO = toJS(insurance.getRowOptions('kasko')); - - expect(KASKO).toEqual({ - insuranceCompany: INSURANCE_COMPANIES_OPTIONS, - insured: INSURED_OPTIONS, - insTerm: defaultOptions.kasko.insTerm, - }); + const { getOptions } = insurance.row('kasko'); + expect(getOptions('insuranceCompany')).toEqual(INSURANCE_COMPANIES_OPTIONS); + expect(getOptions('insured')).toEqual(INSURED_OPTIONS); + expect(getOptions('insTerm')).toEqual(defaultOptions.kasko.insTerm); }); }); - describe('[method] setRowStatuses', () => { - it('should replace only insuranceCompany and insured statuses', () => { + describe('[method] block', () => { + test('should replace only insuranceCompany and insured statuses', () => { const rootStore = new RootStore(); const { insurance } = rootStore.$tables; - insurance.setRowStatuses('kasko', { - insuranceCompany: 'Disabled', - insured: 'Disabled', - }); + insurance.row('kasko').block('insuranceCompany').block('insured'); - const KASKO = toJS(insurance.getRowStatuses('kasko')); + const { getStatus } = insurance.row('kasko'); - expect(KASKO).toEqual({ - insTerm: defaultStatuses.kasko.insTerm, - insCost: defaultStatuses.kasko.insCost, - insuranceCompany: 'Disabled', - insured: 'Disabled', - policyType: defaultStatuses.kasko.policyType, - }); + expect(getStatus('insTerm')).toEqual(defaultStatuses.kasko.insTerm); + expect(getStatus('insCost')).toEqual(defaultStatuses.kasko.insCost); + expect(getStatus('insuranceCompany')).toEqual('Disabled'); + expect(getStatus('insured')).toEqual('Disabled'); + expect(getStatus('policyType')).toEqual(defaultStatuses.kasko.policyType); }); }); }); diff --git a/stores/tables/insurance/hooks.js b/stores/tables/insurance/hooks.js index fe750ab..7fc707b 100644 --- a/stores/tables/insurance/hooks.js +++ b/stores/tables/insurance/hooks.js @@ -1,31 +1,8 @@ +/* eslint-disable import/prefer-default-export */ import { useStore } from 'stores/hooks'; -export function useRowValue(key, valueName) { +export function useRow(key) { const { $tables } = useStore(); - const storeValue = $tables.insurance.getRowValue(key, valueName); - - function setStoreValue(value) { - $tables.insurance.setRowValues(key, { - [valueName]: value, - }); - } - - return [storeValue, setStoreValue]; -} - -export function useRowOptions(key) { - const { $tables } = useStore(); - - const rowOptions = $tables.insurance.getRowOptions(key); - - return rowOptions; -} - -export function useRowStatuses(key) { - const { $tables } = useStore(); - - const rowStatuses = $tables.insurance.getRowStatuses(key); - - return rowStatuses; + return $tables.insurance.row(key); } diff --git a/stores/tables/insurance/index.ts b/stores/tables/insurance/index.ts index ca24cca..bdd8d16 100644 --- a/stores/tables/insurance/index.ts +++ b/stores/tables/insurance/index.ts @@ -39,40 +39,6 @@ export default class InsuranceTable { if (initialStatuses) 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) => { - const rowIndex = this.values.findIndex((x) => x.key === key); - - if (rowIndex >= 0) { - this.values[rowIndex] = { ...this.values[rowIndex], ...rowValues }; - } - - return this; - }; - - getRowOptions(key: Insurance.Keys) { - return this.options[key]; - } - - setRowOptions = (key: Insurance.Keys, rowOptions: Partial) => { - this.options[key] = { ...this.options[key], ...rowOptions }; - }; - - getRowStatuses(key: Insurance.Keys) { - return this.statuses[key]; - } - - setRowStatuses = (key: Insurance.Keys, rowStatuses: Partial) => { - this.statuses[key] = { ...this.statuses[key], ...rowStatuses }; - - return this; - }; - validate = ({ invalid, message }: ValidationParams) => { if (invalid) { this.validation?.addError(message); @@ -81,7 +47,7 @@ export default class InsuranceTable { } }; - resetTable = () => { + reset = () => { this.values = insuranceTableConfig.defaultValues; this.options = insuranceTableConfig.defaultOptions; this.statuses = insuranceTableConfig.defaultStatuses; @@ -89,12 +55,20 @@ export default class InsuranceTable { }; row = (key: K) => ({ + getValue: (valueName: V) => { + const rowIndex = this.values.findIndex((x) => x.key === key); + + return this.values[rowIndex][valueName]; + }, + + getStatus: (valueName: Insurance.Values) => this.statuses[key][valueName], + + getOptions: (valueName: Insurance.Values) => this.options[key][valueName], + setValue: (valueName: V, value: Insurance.RowValues[V]) => { const rowIndex = this.values.findIndex((x) => x.key === key); - if (rowIndex >= 0) { - this.values[rowIndex] = { ...this.values[rowIndex], [valueName]: value }; - } + this.values[rowIndex] = { ...this.values[rowIndex], [valueName]: value }; return this.row(key); }, @@ -120,10 +94,8 @@ export default class InsuranceTable { resetValue: (valueName: Insurance.Values) => { const rowIndex = this.values.findIndex((x) => x.key === key); - if (rowIndex >= 0) { - const defaultValue = insuranceTableConfig.defaultValues[rowIndex][valueName]; - this.values[rowIndex] = { ...this.values[rowIndex], [valueName]: defaultValue }; - } + const defaultValue = insuranceTableConfig.defaultValues[rowIndex][valueName]; + this.values[rowIndex] = { ...this.values[rowIndex], [valueName]: defaultValue }; return this.row(key); },