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 { observer } from 'mobx-react-lite';
|
||||||
import type { ComponentType } from 'react';
|
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 { useInsuranceValue } from './hooks';
|
||||||
import type { Values } from './types';
|
import type { Values } from './types';
|
||||||
|
|
||||||
@ -11,17 +11,12 @@ export function buildOptionComponent<T>(
|
|||||||
) {
|
) {
|
||||||
return observer((props: T) => {
|
return observer((props: T) => {
|
||||||
const [value, setValue] = useInsuranceValue(key, valueName);
|
const [value, setValue] = useInsuranceValue(key, valueName);
|
||||||
const options = useRowOptions(key);
|
const { getOptions, getStatus } = useRow(key);
|
||||||
const statuses = useRowStatuses(key);
|
const options = getOptions(valueName);
|
||||||
|
const statuses = getStatus(valueName);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Component
|
<Component value={value} options={options} setValue={setValue} status={statuses} {...props} />
|
||||||
value={value}
|
|
||||||
options={options[valueName]}
|
|
||||||
setValue={setValue}
|
|
||||||
status={statuses[valueName]}
|
|
||||||
{...props}
|
|
||||||
/>
|
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -33,8 +28,9 @@ export function buildValueComponent<T>(
|
|||||||
) {
|
) {
|
||||||
return observer((props: T) => {
|
return observer((props: T) => {
|
||||||
const [value, setValue] = useInsuranceValue(key, valueName);
|
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 */
|
/* eslint-disable import/prefer-default-export */
|
||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import { useRowValue } from 'stores/tables/insurance/hooks';
|
import { useRow } from 'stores/tables/insurance/hooks';
|
||||||
import { useDebouncedCallback } from 'use-debounce';
|
import { useDebouncedCallback } from 'use-debounce';
|
||||||
|
|
||||||
export function useInsuranceValue(key, valueName) {
|
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);
|
const [value, setValue] = useState(storeValue);
|
||||||
|
|
||||||
// eslint-disable-next-line object-curly-newline
|
// eslint-disable-next-line object-curly-newline
|
||||||
|
|||||||
@ -27,9 +27,7 @@ export default function commonReactions(
|
|||||||
reaction(
|
reaction(
|
||||||
() => $tables.fingap.totalSum,
|
() => $tables.fingap.totalSum,
|
||||||
(totalSum) => {
|
(totalSum) => {
|
||||||
$tables.insurance.setRowValues('fingap', {
|
$tables.insurance.row('fingap').setValue('insCost', totalSum);
|
||||||
insCost: totalSum,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -52,7 +50,7 @@ export default function commonReactions(
|
|||||||
*/
|
*/
|
||||||
reaction(
|
reaction(
|
||||||
() => {
|
() => {
|
||||||
const finGAPInsuranceCompany = $tables.insurance.getRowValue('fingap', 'insuranceCompany');
|
const finGAPInsuranceCompany = $tables.insurance.row('fingap').getValue('insuranceCompany');
|
||||||
const leasingPeriod = $calculation.element('tbxLeasingPeriod').getValue();
|
const leasingPeriod = $calculation.element('tbxLeasingPeriod').getValue();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -62,27 +60,26 @@ export default function commonReactions(
|
|||||||
},
|
},
|
||||||
({ finGAPInsuranceCompany, leasingPeriod }) => {
|
({ finGAPInsuranceCompany, leasingPeriod }) => {
|
||||||
if (!finGAPInsuranceCompany) {
|
if (!finGAPInsuranceCompany) {
|
||||||
$tables.insurance.setRowValues('fingap', {
|
$tables.insurance
|
||||||
insured: 100_000_000,
|
.row('fingap')
|
||||||
insCost: 0,
|
.setValue('insured', 100_000_000)
|
||||||
insTerm: 100_000_000,
|
.setValue('insCost', 0)
|
||||||
});
|
.setValue('insTerm', 100_000_000)
|
||||||
|
.block('insured')
|
||||||
$tables.insurance.setRowStatuses('fingap', {
|
.block('insTerm');
|
||||||
insured: 'Disabled',
|
|
||||||
insTerm: 'Disabled',
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
$tables.insurance.setRowValues('fingap', {
|
$tables.insurance
|
||||||
insured: 100_000_000,
|
.row('fingap')
|
||||||
insCost: 0,
|
.setValue('insured', 100_000_000)
|
||||||
insTerm: leasingPeriod < 13 ? 100_000_000 : undefined,
|
.setValue('insCost', 0)
|
||||||
});
|
.setValue('insTerm', leasingPeriod < 13 ? 100_000_000 : null)
|
||||||
|
.unblock('insured');
|
||||||
|
|
||||||
$tables.insurance.setRowStatuses('fingap', {
|
if (leasingPeriod < 13) {
|
||||||
insured: 'Default',
|
$tables.insurance.row('fingap').block('insTerm');
|
||||||
insTerm: leasingPeriod < 13 ? 'Disabled' : 'Default',
|
} else {
|
||||||
});
|
$tables.insurance.row('fingap').unblock('insTerm');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -92,7 +89,7 @@ export default function commonReactions(
|
|||||||
|
|
||||||
// Очищаем таблицу ФинГАП, если не выбрана страховая компания
|
// Очищаем таблицу ФинГАП, если не выбрана страховая компания
|
||||||
reaction(
|
reaction(
|
||||||
() => $tables.insurance.getRowValue('fingap', 'insuranceCompany'),
|
() => $tables.insurance.row('fingap').getValue('insuranceCompany'),
|
||||||
(finGAPInsuranceCompany) => {
|
(finGAPInsuranceCompany) => {
|
||||||
if (!finGAPInsuranceCompany) {
|
if (!finGAPInsuranceCompany) {
|
||||||
$tables.fingap.clear();
|
$tables.fingap.clear();
|
||||||
@ -123,7 +120,7 @@ export default function commonReactions(
|
|||||||
|
|
||||||
reaction(
|
reaction(
|
||||||
() => {
|
() => {
|
||||||
const finGAPInsuranceCompany = $tables.insurance.getRowValue('fingap', 'insuranceCompany');
|
const finGAPInsuranceCompany = $tables.insurance.row('fingap').getValue('insuranceCompany');
|
||||||
const paymentsValues = toJS($tables.payments.values);
|
const paymentsValues = toJS($tables.payments.values);
|
||||||
const plPriceRub = $calculation.$values.getValue('plPriceRub');
|
const plPriceRub = $calculation.$values.getValue('plPriceRub');
|
||||||
const discountRub = $calculation.$values.getValue('discountRub');
|
const discountRub = $calculation.$values.getValue('discountRub');
|
||||||
|
|||||||
@ -13,7 +13,7 @@ export default function validationReactions(
|
|||||||
reaction(
|
reaction(
|
||||||
() => {
|
() => {
|
||||||
const hasPaymentsErrors = $tables.payments.validation.hasErrors;
|
const hasPaymentsErrors = $tables.payments.validation.hasErrors;
|
||||||
const finGAPInsuranceCompany = $tables.insurance.getRowValue('fingap', 'insuranceCompany');
|
const finGAPInsuranceCompany = $tables.insurance.row('fingap').getValue('insuranceCompany');
|
||||||
|
|
||||||
return {
|
return {
|
||||||
hasPaymentsErrors,
|
hasPaymentsErrors,
|
||||||
|
|||||||
@ -22,7 +22,11 @@ export default function getData(apolloClient, store) {
|
|||||||
function setManyRowOptions(options) {
|
function setManyRowOptions(options) {
|
||||||
Object.keys(options).forEach((key) => {
|
Object.keys(options).forEach((key) => {
|
||||||
const rowOptions = options[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('stores/tables/insurance', () => {
|
||||||
describe('[method] setRowValues', () => {
|
describe('[method] setValue', () => {
|
||||||
it('should set only insuranceCompany value', () => {
|
test('should set only insuranceCompany value', () => {
|
||||||
const rootStore = new RootStore();
|
const rootStore = new RootStore();
|
||||||
const { insurance } = rootStore.$tables;
|
const { insurance } = rootStore.$tables;
|
||||||
|
|
||||||
const TEST_VALUE = 'TEST_VALUE';
|
const TEST_VALUE = 'TEST_VALUE';
|
||||||
|
|
||||||
insurance.setRowValues('kasko', {
|
insurance.row('kasko').setValue('insuranceCompany', TEST_VALUE);
|
||||||
insuranceCompany: TEST_VALUE,
|
|
||||||
});
|
|
||||||
|
|
||||||
const KASKO_VALUES = toJS(insurance.values.find((x) => x.key === 'kasko'));
|
const KASKO_VALUES = toJS(insurance.values.find((x) => x.key === 'kasko'));
|
||||||
const DEFAULT_KASKO_VALUES = defaultValues.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', () => {
|
describe('[method] setOptions', () => {
|
||||||
it('should replace only insuranceCompany options', () => {
|
test('should replace only insuranceCompany options', () => {
|
||||||
const rootStore = new RootStore();
|
const rootStore = new RootStore();
|
||||||
const { insurance } = rootStore.$tables;
|
const { insurance } = rootStore.$tables;
|
||||||
|
|
||||||
insurance.setRowOptions('kasko', {
|
insurance.row('kasko').setOptions('insuranceCompany', INSURANCE_COMPANIES_OPTIONS);
|
||||||
insuranceCompany: INSURANCE_COMPANIES_OPTIONS,
|
|
||||||
});
|
|
||||||
|
|
||||||
const KASKO = toJS(insurance.getRowOptions('kasko'));
|
const { getOptions } = insurance.row('kasko');
|
||||||
|
expect(getOptions('insuranceCompany')).toEqual(INSURANCE_COMPANIES_OPTIONS);
|
||||||
expect(KASKO).toEqual({
|
expect(getOptions('insured')).toEqual(defaultOptions.kasko.insured);
|
||||||
insuranceCompany: INSURANCE_COMPANIES_OPTIONS,
|
expect(getOptions('insTerm')).toEqual(defaultOptions.kasko.insTerm);
|
||||||
insured: defaultOptions.kasko.insured,
|
|
||||||
insTerm: defaultOptions.kasko.insTerm,
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should replace insuranceCompany and insured options', () => {
|
test('should replace insuranceCompany and insured options', () => {
|
||||||
const rootStore = new RootStore();
|
const rootStore = new RootStore();
|
||||||
const { insurance } = rootStore.$tables;
|
const { insurance } = rootStore.$tables;
|
||||||
|
|
||||||
insurance.setRowOptions('kasko', {
|
insurance
|
||||||
insuranceCompany: INSURANCE_COMPANIES_OPTIONS,
|
.row('kasko')
|
||||||
insured: INSURED_OPTIONS,
|
.setOptions('insuranceCompany', INSURANCE_COMPANIES_OPTIONS)
|
||||||
});
|
.setOptions('insured', INSURED_OPTIONS);
|
||||||
|
|
||||||
const KASKO = toJS(insurance.getRowOptions('kasko'));
|
const { getOptions } = insurance.row('kasko');
|
||||||
|
expect(getOptions('insuranceCompany')).toEqual(INSURANCE_COMPANIES_OPTIONS);
|
||||||
expect(KASKO).toEqual({
|
expect(getOptions('insured')).toEqual(INSURED_OPTIONS);
|
||||||
insuranceCompany: INSURANCE_COMPANIES_OPTIONS,
|
expect(getOptions('insTerm')).toEqual(defaultOptions.kasko.insTerm);
|
||||||
insured: INSURED_OPTIONS,
|
|
||||||
insTerm: defaultOptions.kasko.insTerm,
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('[method] setRowStatuses', () => {
|
describe('[method] block', () => {
|
||||||
it('should replace only insuranceCompany and insured statuses', () => {
|
test('should replace only insuranceCompany and insured statuses', () => {
|
||||||
const rootStore = new RootStore();
|
const rootStore = new RootStore();
|
||||||
const { insurance } = rootStore.$tables;
|
const { insurance } = rootStore.$tables;
|
||||||
|
|
||||||
insurance.setRowStatuses('kasko', {
|
insurance.row('kasko').block('insuranceCompany').block('insured');
|
||||||
insuranceCompany: 'Disabled',
|
|
||||||
insured: 'Disabled',
|
|
||||||
});
|
|
||||||
|
|
||||||
const KASKO = toJS(insurance.getRowStatuses('kasko'));
|
const { getStatus } = insurance.row('kasko');
|
||||||
|
|
||||||
expect(KASKO).toEqual({
|
expect(getStatus('insTerm')).toEqual(defaultStatuses.kasko.insTerm);
|
||||||
insTerm: defaultStatuses.kasko.insTerm,
|
expect(getStatus('insCost')).toEqual(defaultStatuses.kasko.insCost);
|
||||||
insCost: defaultStatuses.kasko.insCost,
|
expect(getStatus('insuranceCompany')).toEqual('Disabled');
|
||||||
insuranceCompany: 'Disabled',
|
expect(getStatus('insured')).toEqual('Disabled');
|
||||||
insured: 'Disabled',
|
expect(getStatus('policyType')).toEqual(defaultStatuses.kasko.policyType);
|
||||||
policyType: defaultStatuses.kasko.policyType,
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,31 +1,8 @@
|
|||||||
|
/* eslint-disable import/prefer-default-export */
|
||||||
import { useStore } from 'stores/hooks';
|
import { useStore } from 'stores/hooks';
|
||||||
|
|
||||||
export function useRowValue(key, valueName) {
|
export function useRow(key) {
|
||||||
const { $tables } = useStore();
|
const { $tables } = useStore();
|
||||||
|
|
||||||
const storeValue = $tables.insurance.getRowValue(key, valueName);
|
return $tables.insurance.row(key);
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -39,40 +39,6 @@ export default class InsuranceTable {
|
|||||||
if (initialStatuses) this.statuses = initialStatuses;
|
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) => {
|
validate = ({ invalid, message }: ValidationParams) => {
|
||||||
if (invalid) {
|
if (invalid) {
|
||||||
this.validation?.addError(message);
|
this.validation?.addError(message);
|
||||||
@ -81,7 +47,7 @@ export default class InsuranceTable {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
resetTable = () => {
|
reset = () => {
|
||||||
this.values = insuranceTableConfig.defaultValues;
|
this.values = insuranceTableConfig.defaultValues;
|
||||||
this.options = insuranceTableConfig.defaultOptions;
|
this.options = insuranceTableConfig.defaultOptions;
|
||||||
this.statuses = insuranceTableConfig.defaultStatuses;
|
this.statuses = insuranceTableConfig.defaultStatuses;
|
||||||
@ -89,12 +55,20 @@ export default class InsuranceTable {
|
|||||||
};
|
};
|
||||||
|
|
||||||
row = <K extends Insurance.Keys>(key: K) => ({
|
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]) => {
|
setValue: <V extends Insurance.Values>(valueName: V, value: Insurance.RowValues[V]) => {
|
||||||
const rowIndex = this.values.findIndex((x) => x.key === key);
|
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);
|
return this.row(key);
|
||||||
},
|
},
|
||||||
@ -120,10 +94,8 @@ export default class InsuranceTable {
|
|||||||
resetValue: (valueName: Insurance.Values) => {
|
resetValue: (valueName: Insurance.Values) => {
|
||||||
const rowIndex = this.values.findIndex((x) => x.key === key);
|
const rowIndex = this.values.findIndex((x) => x.key === key);
|
||||||
|
|
||||||
if (rowIndex >= 0) {
|
const defaultValue = insuranceTableConfig.defaultValues[rowIndex][valueName];
|
||||||
const defaultValue = insuranceTableConfig.defaultValues[rowIndex][valueName];
|
this.values[rowIndex] = { ...this.values[rowIndex], [valueName]: defaultValue };
|
||||||
this.values[rowIndex] = { ...this.values[rowIndex], [valueName]: defaultValue };
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.row(key);
|
return this.row(key);
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user