From 61ed3c3859bacf4585cb74730ebb1aeda355c0c9 Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Mon, 8 May 2023 20:10:21 +0300 Subject: [PATCH] fix infinite fetching pass network errors to message --- .../Form/ELT/Components/PolicyTable.tsx | 5 +- .../Components/Calculation/Form/ELT/Kasko.tsx | 105 ++++++++++-------- .../Components/Calculation/Form/ELT/Osago.tsx | 82 ++++++++------ 3 files changed, 110 insertions(+), 82 deletions(-) diff --git a/apps/web/Components/Calculation/Form/ELT/Components/PolicyTable.tsx b/apps/web/Components/Calculation/Form/ELT/Components/PolicyTable.tsx index 05c137d..a4b828e 100644 --- a/apps/web/Components/Calculation/Form/ELT/Components/PolicyTable.tsx +++ b/apps/web/Components/Calculation/Form/ELT/Components/PolicyTable.tsx @@ -26,7 +26,10 @@ export const PolicyTable = observer( x: true, }} rowSelection={{ - getCheckboxProps: (record) => ({ disabled: !record.sum || record.status !== null }), + getCheckboxProps: (record) => ({ + disabled: + !record.sum || record.status !== null || getRows.some((x) => x.status === 'fetching'), + }), hideSelectAll: true, onSelect: (record) => { if (record.sum > 0) { diff --git a/apps/web/Components/Calculation/Form/ELT/Kasko.tsx b/apps/web/Components/Calculation/Form/ELT/Kasko.tsx index 8731764..77583cc 100644 --- a/apps/web/Components/Calculation/Form/ELT/Kasko.tsx +++ b/apps/web/Components/Calculation/Form/ELT/Kasko.tsx @@ -23,8 +23,9 @@ export function Kasko() { const apolloClient = useApolloClient(); const queries = useQueries({ - queries: rows.map(({ key, id }) => ({ + queries: rows.map(({ key, id, ...row }) => ({ enabled: false, + initialData: { id, key, ...row, error: '', kaskoSum: 0, sum: 0 }, queryFn: async (context: QueryFunctionContext) => { const payload = await makeEltKaskoRequest({ apolloClient, store }, id); const res = await getEltKasko(payload, context); @@ -42,53 +43,65 @@ export function Kasko() { const fetchingRows = rows.map((x) => ({ ...x, status: 'fetching', sum: 0 })); $tables.elt.kasko.setRows(fetchingRows); - queries.forEach(({ refetch }) => { - refetch().then((res) => { - if (res.data) { - const { key, kaskoSum, message, skCalcId, totalFranchise } = res.data; - let { error } = res.data; + queries.forEach(({ refetch, data }) => { + refetch() + .then((res) => { + if (res.data) { + const { key, kaskoSum, message, skCalcId, totalFranchise } = res.data; + let { error } = res.data; - if (totalFranchise > MAX_FRANCHISE) { - error ||= `Франшиза по страховке превышает максимально допустимое значение: ${Intl.NumberFormat( - 'ru', - { - currency: 'RUB', - style: 'currency', - } - ).format(MAX_FRANCHISE)}`; + 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, + skCalcId, + status: error ? 'error' : null, + sum: kaskoSum, + 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, - skCalcId, - status: error ? 'error' : null, - sum: kaskoSum, - totalFranchise, - }); - } - }); + }) + .catch((error) => { + if (data?.key) + $tables.elt.kasko.setRow({ + key: data?.key, + message: error, + numCalc: 0, + status: 'error', + sum: 0, + totalFranchise: 0, + }); + }); }); } diff --git a/apps/web/Components/Calculation/Form/ELT/Osago.tsx b/apps/web/Components/Calculation/Form/ELT/Osago.tsx index bfda9a7..54a27a0 100644 --- a/apps/web/Components/Calculation/Form/ELT/Osago.tsx +++ b/apps/web/Components/Calculation/Form/ELT/Osago.tsx @@ -23,8 +23,9 @@ export function Osago() { const apolloClient = useApolloClient(); const queries = useQueries({ - queries: rows.map(({ key, id }) => ({ + queries: rows.map(({ key, id, ...row }) => ({ enabled: false, + initialData: { id, key, ...row, error: '', premiumSum: 0 }, queryFn: async (context: QueryFunctionContext) => { const payload = await makeEltOsagoRequest({ apolloClient, store }, id); const res = await getEltOsago(payload, context); @@ -42,42 +43,53 @@ export function Osago() { const fetchingRows = rows.map((x) => ({ ...x, status: 'fetching', sum: 0 })); $tables.elt.osago.setRows(fetchingRows); - queries.forEach(({ refetch }) => { - refetch().then((res) => { - if (res.data) { - const { key, numCalc, premiumSum, message, skCalcId } = res.data; - let { error } = res.data; + queries.forEach(({ refetch, data }) => { + refetch() + .then((res) => { + if (res.data) { + const { key, numCalc, premiumSum, 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 > 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, + status: 'error', + sum: 0, + }); + }); }); }