diff --git a/apps/web/Components/Calculation/Form/ELT/Kasko.tsx b/apps/web/Components/Calculation/Form/ELT/Kasko.tsx index 5a90a3b..c704b82 100644 --- a/apps/web/Components/Calculation/Form/ELT/Kasko.tsx +++ b/apps/web/Components/Calculation/Form/ELT/Kasko.tsx @@ -6,6 +6,7 @@ import type { Row, StoreSelector } from './types'; import { getEltKasko } from '@/api/elt/query'; import { STALE_TIME } from '@/constants/request'; import { MAX_FRANCHISE, MAX_INSURANCE, MIN_INSURANCE } from '@/constants/values'; +import helper from '@/process/elt/lib/helper'; import { useStore } from '@/stores/hooks'; import { useApolloClient } from '@apollo/client'; import type { QueryFunctionContext } from '@tanstack/react-query'; @@ -20,11 +21,14 @@ export function Kasko() { const apolloClient = useApolloClient(); + const { init } = helper({ apolloClient, store }); + const queries = useQueries({ queries: $tables.elt.kasko.getRows.map((row) => { const { id, key } = row; return { + cacheTime: 0, enabled: false, initialData: { ...row, @@ -52,85 +56,90 @@ export function Kasko() { }); async function handleOnClick() { + const { kasko } = await init(); + $tables.elt.kasko.setRows(kasko); + + const kaskoCompanyIds = $tables.insurance + .row('kasko') + .getOptions('insuranceCompany') + .map((x) => x.value); const values = $calculation.$values.getValues(); - const fetchingRows = $tables.elt.kasko.getRows.map((x) => ({ - ...x, - status: 'fetching', - sum: 0, - })); - $tables.elt.kasko.setRows(fetchingRows); - queries.forEach(({ refetch, data }) => { - refetch() - .then((res) => { - if (res.data) { - const { - key, - kaskoSum = 0, - message, - skCalcId, - totalFranchise = 0, - requestId, - paymentPeriods, - } = res.data; - let { error } = res.data; + queries + .filter(({ data }) => data?.key && kaskoCompanyIds.includes(data.key)) + .forEach(({ refetch, data }) => { + if (data?.key) $tables.elt.kasko.setRow({ key: data?.key, status: 'fetching' }); - if (totalFranchise > MAX_FRANCHISE) { - error ||= `Франшиза по страховке превышает максимально допустимое значение: ${Intl.NumberFormat( - 'ru', - { - currency: 'RUB', - style: 'currency', - } - ).format(MAX_FRANCHISE)}`; + refetch() + .then((res) => { + if (res.data) { + const { + key, + kaskoSum = 0, + message, + skCalcId, + totalFranchise = 0, + requestId, + paymentPeriods, + } = res.data; + let { error } = res.data; + + if (totalFranchise > MAX_FRANCHISE) { + error ||= `Франшиза по страховке превышает максимально допустимое значение: ${Intl.NumberFormat( + 'ru', + { + currency: 'RUB', + style: 'currency', + } + ).format(MAX_FRANCHISE)}`; + } + + if (kaskoSum > MAX_INSURANCE) { + error ||= `Сумма по страховке превышает максимально допустимое значение по стоимости КАСКО: ${Intl.NumberFormat( + 'ru', + { + currency: 'RUB', + style: 'currency', + } + ).format(MAX_INSURANCE)}`; + } + + if (kaskoSum < MIN_INSURANCE) { + error ||= `Сумма по страховке не должна быть меньше допустимого значения по стоимости КАСКО: ${Intl.NumberFormat( + 'ru', + { + currency: 'RUB', + style: 'currency', + } + ).format(MIN_INSURANCE)}`; + } + + $tables.elt.kasko.setRow({ + key, + message: error || message, + numCalc: 0, + requestId, + skCalcId, + status: error ? 'error' : null, + sum: values.leasingPeriod <= 16 ? kaskoSum : paymentPeriods.at(0)?.kaskoSum || 0, + totalFranchise, + }); } - - if (kaskoSum > MAX_INSURANCE) { - error ||= `Сумма по страховке превышает максимально допустимое значение по стоимости КАСКО: ${Intl.NumberFormat( - 'ru', - { - currency: 'RUB', - style: 'currency', - } - ).format(MAX_INSURANCE)}`; - } - - if (kaskoSum < MIN_INSURANCE) { - error ||= `Сумма по страховке не должна быть меньше допустимого значения по стоимости КАСКО: ${Intl.NumberFormat( - 'ru', - { - currency: 'RUB', - style: 'currency', - } - ).format(MIN_INSURANCE)}`; - } - - $tables.elt.kasko.setRow({ - key, - message: error || message, - numCalc: 0, - requestId, - skCalcId, - status: error ? 'error' : null, - sum: values.leasingPeriod <= 16 ? kaskoSum : paymentPeriods.at(0)?.kaskoSum || 0, - totalFranchise, - }); - } - }) - .catch((error) => { - if (data?.key) - $tables.elt.kasko.setRow({ - key: data?.key, - message: error, - numCalc: 0, - requestId: '', - skCalcId: '', - status: 'error', - sum: 0, - totalFranchise: 0, - }); - }); - }); + }) + .catch((error) => { + if (data?.key) + $tables.elt.kasko.setRow({ + key: data?.key, + message: error, + numCalc: 0, + requestId: '', + skCalcId: '', + status: 'error', + sum: 0, + totalFranchise: 0, + }); + }); + }); } function handleOnSelectRow(row: Row) { diff --git a/apps/web/Components/Calculation/Form/ELT/Osago.tsx b/apps/web/Components/Calculation/Form/ELT/Osago.tsx index 29d6b18..3f0ee6e 100644 --- a/apps/web/Components/Calculation/Form/ELT/Osago.tsx +++ b/apps/web/Components/Calculation/Form/ELT/Osago.tsx @@ -6,6 +6,7 @@ import type { Row, StoreSelector } from './types'; import { getEltOsago } from '@/api/elt/query'; import { STALE_TIME } from '@/constants/request'; import { MAX_INSURANCE, MIN_INSURANCE } from '@/constants/values'; +import helper from '@/process/elt/lib/helper'; import { useStore } from '@/stores/hooks'; import { useApolloClient } from '@apollo/client'; import type { QueryFunctionContext } from '@tanstack/react-query'; @@ -20,11 +21,14 @@ export function Osago() { const apolloClient = useApolloClient(); + const { init } = helper({ apolloClient, store }); + const queries = useQueries({ queries: $tables.elt.osago.getRows.map((row) => { const { id, key } = row; return { + cacheTime: 0, enabled: false, initialData: { ...row, error: '', premiumSum: 0 }, queryFn: async (context: QueryFunctionContext) => { @@ -42,62 +46,67 @@ export function Osago() { }); async function handleOnClick() { - const fetchingRows = $tables.elt.osago.getRows.map((x) => ({ - ...x, - status: 'fetching', - sum: 0, - })); - $tables.elt.osago.setRows(fetchingRows); + const { osago } = await init(); + $tables.elt.osago.setRows(osago); - queries.forEach(({ refetch, data }) => { - refetch() - .then((res) => { - if (res.data) { - const { key, numCalc, premiumSum = 0, message, skCalcId } = res.data; - let { error } = res.data; + const osagoCompanyIds = $tables.insurance + .row('osago') + .getOptions('insuranceCompany') + .map((x) => x.value); - if (premiumSum > MAX_INSURANCE) { - error ||= `Сумма по страховке превышает максимально допустимое значение по стоимости ОСАГО: ${Intl.NumberFormat( - 'ru', - { - currency: 'RUB', - style: 'currency', - } - ).format(MAX_INSURANCE)}`; + queries + .filter(({ data }) => data?.key && osagoCompanyIds.includes(data.key)) + .forEach(({ refetch, data }) => { + if (data?.key) $tables.elt.osago.setRow({ key: data?.key, status: 'fetching' }); + + refetch() + .then((res) => { + if (res.data) { + const { key, numCalc, premiumSum = 0, message, skCalcId } = res.data; + let { error } = res.data; + + if (premiumSum > MAX_INSURANCE) { + error ||= `Сумма по страховке превышает максимально допустимое значение по стоимости ОСАГО: ${Intl.NumberFormat( + 'ru', + { + currency: 'RUB', + style: 'currency', + } + ).format(MAX_INSURANCE)}`; + } + + if (premiumSum < MIN_INSURANCE) { + error ||= `Сумма по страховке не должна быть меньше допустимого значения по стоимости ОСАГО: ${Intl.NumberFormat( + 'ru', + { + currency: 'RUB', + style: 'currency', + } + ).format(MIN_INSURANCE)}`; + } + + $tables.elt.osago.setRow({ + key, + message: error || message, + numCalc, + skCalcId, + status: error ? 'error' : null, + sum: premiumSum, + }); } - - if (premiumSum < MIN_INSURANCE) { - error ||= `Сумма по страховке не должна быть меньше допустимого значения по стоимости ОСАГО: ${Intl.NumberFormat( - 'ru', - { - currency: 'RUB', - style: 'currency', - } - ).format(MIN_INSURANCE)}`; - } - - $tables.elt.osago.setRow({ - key, - message: error || message, - numCalc, - skCalcId, - status: error ? 'error' : null, - sum: premiumSum, - }); - } - }) - .catch((error) => { - if (data?.key) - $tables.elt.osago.setRow({ - key: data?.key, - message: error, - numCalc: 0, - skCalcId: '', - status: 'error', - sum: 0, - }); - }); - }); + }) + .catch((error) => { + if (data?.key) + $tables.elt.osago.setRow({ + key: data?.key, + message: error, + numCalc: 0, + skCalcId: '', + status: 'error', + sum: 0, + }); + }); + }); } function handleOnSelectRow(row: Row) { diff --git a/apps/web/process/elt/lib/helper.ts b/apps/web/process/elt/lib/helper.ts index 87922d4..2661274 100644 --- a/apps/web/process/elt/lib/helper.ts +++ b/apps/web/process/elt/lib/helper.ts @@ -6,7 +6,7 @@ export default function helper({ apolloClient, store, }: Pick) { - const { $calculation, $tables } = store; + const { $calculation } = store; return { async init() { @@ -29,16 +29,6 @@ export default function helper({ ({ evo_leasingobject_type } = data); } - const kaskoCompanyIds = $tables.insurance - .row('kasko') - .getOptions('insuranceCompany') - .map((x) => x.value); - - const osagoCompanyIds = $tables.insurance - .row('osago') - .getOptions('insuranceCompany') - .map((x) => x.value); - return { kasko: (accounts ?.filter((x) => @@ -48,7 +38,6 @@ export default function helper({ ? Boolean(x.evo_id_elt_smr) : Boolean(x?.evo_id_elt) ) - .filter((x) => x?.value && kaskoCompanyIds.includes(x?.value)) .map((x) => ({ id: evo_leasingobject_type?.evo_id && @@ -67,7 +56,6 @@ export default function helper({ })) || []) as Row[], osago: (accounts ?.filter((x) => x?.evo_type_ins_policy?.includes(100_000_001) && x?.evo_id_elt_osago) - .filter((x) => x?.value && osagoCompanyIds.includes(x.value)) .map((x) => ({ id: x?.evo_id_elt_osago, key: x?.value, diff --git a/apps/web/process/elt/reactions/common.ts b/apps/web/process/elt/reactions/common.ts index e3e4870..898334e 100644 --- a/apps/web/process/elt/reactions/common.ts +++ b/apps/web/process/elt/reactions/common.ts @@ -12,9 +12,6 @@ export default function reactions(context: ProcessContext) { disposableReaction( () => $process.has('ELT') || $process.has('LoadKP'), () => ({ - kasko: { - options: $tables.insurance.row('kasko').getOptions('insuranceCompany'), - }, values: $calculation.$values.getValues([ 'objectRegistration', 'townRegistration', @@ -52,7 +49,7 @@ export default function reactions(context: ProcessContext) { }, { delay: 10, - equals: comparer.shallow, + equals: comparer.structural, fireImmediately: true, } ); @@ -60,9 +57,6 @@ export default function reactions(context: ProcessContext) { disposableReaction( () => $process.has('ELT') || $process.has('LoadKP'), () => ({ - osago: { - options: $tables.insurance.row('osago').getOptions('insuranceCompany'), - }, values: $calculation.$values.getValues([ 'legalClientRegion', 'legalClientTown', @@ -107,7 +101,7 @@ export default function reactions(context: ProcessContext) { }, { delay: 10, - equals: comparer.shallow, + equals: comparer.structural, fireImmediately: true, } );