stores/tables: refactor insurance methods
This commit is contained in:
parent
391f9039f7
commit
21c97ece2b
@ -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<T>(
|
||||
) {
|
||||
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 (
|
||||
<Component
|
||||
value={value}
|
||||
options={options[valueName]}
|
||||
setValue={setValue}
|
||||
status={statuses[valueName]}
|
||||
{...props}
|
||||
/>
|
||||
<Component value={value} options={options} setValue={setValue} status={statuses} {...props} />
|
||||
);
|
||||
});
|
||||
}
|
||||
@ -33,8 +28,9 @@ export function buildValueComponent<T>(
|
||||
) {
|
||||
return observer((props: T) => {
|
||||
const [value, setValue] = useInsuranceValue(key, valueName);
|
||||
const statuses = useRowStatuses(key);
|
||||
const { getStatus } = useRow(key);
|
||||
const statuses = getStatus(valueName);
|
||||
|
||||
return <Component value={value} setValue={setValue} status={statuses[valueName]} {...props} />;
|
||||
return <Component value={value} setValue={setValue} status={statuses} {...props} />;
|
||||
});
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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');
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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]);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -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<Insurance.RowValues>) => {
|
||||
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<Insurance.RowOptions>) => {
|
||||
this.options[key] = { ...this.options[key], ...rowOptions };
|
||||
};
|
||||
|
||||
getRowStatuses(key: Insurance.Keys) {
|
||||
return this.statuses[key];
|
||||
}
|
||||
|
||||
setRowStatuses = (key: Insurance.Keys, rowStatuses: Partial<Insurance.RowStatuses>) => {
|
||||
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 = <K extends Insurance.Keys>(key: K) => ({
|
||||
getValue: <V extends Insurance.Values>(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: <V extends Insurance.Values>(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);
|
||||
},
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user