fix infinite fetching

pass network errors to message
This commit is contained in:
vchikalkin 2023-05-08 20:10:21 +03:00
parent 77f55f5108
commit 61ed3c3859
3 changed files with 110 additions and 82 deletions

View File

@ -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) {

View File

@ -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,
});
});
});
}

View File

@ -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,
});
});
});
}