process/init: fix GetMainData query fetched twice

This commit is contained in:
Chika 2022-11-08 09:18:38 +03:00
parent 6f1cfab8f5
commit 857a40af49
2 changed files with 133 additions and 111 deletions

View File

@ -1,5 +1,6 @@
/* eslint-disable import/prefer-default-export */
import { gql, useQuery } from '@apollo/client';
import { gql, useApolloClient } from '@apollo/client';
import { useEffect } from 'react';
import { useStore } from 'stores/hooks';
const QUERY_GET_INSURANCE_DATA = gql`
@ -33,8 +34,29 @@ const QUERY_GET_INSURANCE_DATA = gql`
}
`;
function getInsuranceData(query, handleOnCompleted) {
query({
query: QUERY_GET_INSURANCE_DATA,
}).then(({ data }) => {
const insurance = {
osago: {
insuranceCompany: data.osago,
},
kasko: {
insuranceCompany: data.kasko,
},
fingap: {
insuranceCompany: data.fingap,
},
};
handleOnCompleted(insurance);
});
}
export function useInsuranceData() {
const { $tables } = useStore();
const { query } = useApolloClient();
function handleOnCompleted(options) {
Object.keys(options).forEach((key) => {
@ -47,21 +69,7 @@ export function useInsuranceData() {
});
}
useQuery(QUERY_GET_INSURANCE_DATA, {
onCompleted: (insuranceData) => {
const insurance = {
osago: {
insuranceCompany: insuranceData.osago,
},
kasko: {
insuranceCompany: insuranceData.kasko,
},
fingap: {
insuranceCompany: insuranceData.fingap,
},
};
handleOnCompleted(insurance);
},
});
useEffect(() => {
getInsuranceData(query, handleOnCompleted);
}, []);
}

View File

@ -1,7 +1,8 @@
/* eslint-disable import/prefer-default-export */
import { gql, useQuery } from '@apollo/client';
import { gql, useApolloClient } from '@apollo/client';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import { useEffect } from 'react';
import { useStore } from 'stores/hooks';
dayjs.extend(utc);
@ -90,8 +91,110 @@ const QUERY_GET_ADDPRODUCT_TYPES = gql`
const currentDate = dayjs().utc(false).toISOString();
function getMainData(query, onCompleted) {
query({
query: QUERY_GET_MAIN_OPTIONS,
variables: {
currentDate,
},
}).then(({ data }) => {
onCompleted(data);
});
query({
query: QUERY_GET_SUBSIDIES,
variables: {
currentDate,
},
}).then(({ data }) => {
const selectSubsidy = data?.evo_subsidies?.filter(
(x) => x?.evo_subsidy_type && [100_000_000, 100_000_001].includes(x?.evo_subsidy_type)
);
const selectImportProgram = data?.evo_subsidies?.filter(
(x) => x?.evo_subsidy_type && [100_000_002].includes(x?.evo_subsidy_type)
);
onCompleted({
selectSubsidy,
selectImportProgram,
});
});
query({
query: QUERY_GET_REGIONS,
}).then(({ data }) => {
const selectRegionRegistration = data?.evo_regions;
const selectObjectRegionRegistration = data?.evo_regions;
const selectLegalClientRegion = data?.evo_regions;
onCompleted({
selectRegionRegistration,
selectObjectRegionRegistration,
selectLegalClientRegion,
});
});
query({
query: QUERY_GET_BRANDS,
}).then(({ data }) => {
onCompleted(data);
});
query({
query: QUERY_GET_DEALERS,
}).then(({ data }) => {
onCompleted(data);
});
query({
query: QUERY_GET_ADDPRODUCT_TYPES,
}).then(({ data }) => {
const selectRegistration = data.evo_addproduct_types
?.filter((x) => x?.evo_product_type === 100_000_001)
.map((x) => ({
...x,
label: `${x?.label} (${x?.evo_graph_price} руб.)`,
}));
const selectTechnicalCard = data.evo_addproduct_types
?.filter((x) => x?.evo_product_type === 100_000_000)
.map((x) => ({
...x,
label: `${x?.label} (${x?.evo_graph_price} руб.)`,
}));
const selectTelematic = data.evo_addproduct_types
?.filter((x) => x?.evo_product_type === 100_000_004)
.map((x) => ({
...x,
label: `${x?.label} (${x?.evo_graph_price} руб.)`,
}));
const selectTracker = data.evo_addproduct_types
?.filter((x) => x?.evo_product_type === 100_000_003)
.map((x) => ({
...x,
label: `${x?.label} (${x?.evo_graph_price} руб.)`,
}));
const selectInsNSIB = data.evo_addproduct_types?.filter(
(x) => x?.evo_product_type === 100_000_002
);
onCompleted({
selectRegistration,
selectTechnicalCard,
selectTelematic,
selectTracker,
selectInsNSIB,
});
});
}
export function useMainData() {
const { $calculation } = useStore();
const { query } = useApolloClient();
function handleOnCompleted(options) {
Object.keys(options).forEach((elementName) => {
@ -100,96 +203,7 @@ export function useMainData() {
});
}
useQuery(QUERY_GET_MAIN_OPTIONS, {
variables: {
currentDate,
},
onCompleted: handleOnCompleted,
});
useQuery(QUERY_GET_SUBSIDIES, {
variables: {
currentDate,
},
onCompleted: (subsidies) => {
const selectSubsidy = subsidies?.evo_subsidies?.filter(
(x) => x?.evo_subsidy_type && [100_000_000, 100_000_001].includes(x?.evo_subsidy_type)
);
const selectImportProgram = subsidies?.evo_subsidies?.filter(
(x) => x?.evo_subsidy_type && [100_000_002].includes(x?.evo_subsidy_type)
);
handleOnCompleted({
selectSubsidy,
selectImportProgram,
});
},
});
useQuery(QUERY_GET_REGIONS, {
onCompleted: (regions) => {
const selectRegionRegistration = regions?.evo_regions;
const selectObjectRegionRegistration = regions?.evo_regions;
const selectLegalClientRegion = regions?.evo_regions;
handleOnCompleted({
selectRegionRegistration,
selectObjectRegionRegistration,
selectLegalClientRegion,
});
},
});
useQuery(QUERY_GET_BRANDS, {
onCompleted: handleOnCompleted,
});
useQuery(QUERY_GET_DEALERS, {
onCompleted: handleOnCompleted,
});
useQuery(QUERY_GET_ADDPRODUCT_TYPES, {
onCompleted: (addproductTypes) => {
const selectRegistration = addproductTypes.evo_addproduct_types
?.filter((x) => x?.evo_product_type === 100_000_001)
.map((x) => ({
...x,
label: `${x?.label} (${x?.evo_graph_price} руб.)`,
}));
const selectTechnicalCard = addproductTypes.evo_addproduct_types
?.filter((x) => x?.evo_product_type === 100_000_000)
.map((x) => ({
...x,
label: `${x?.label} (${x?.evo_graph_price} руб.)`,
}));
const selectTelematic = addproductTypes.evo_addproduct_types
?.filter((x) => x?.evo_product_type === 100_000_004)
.map((x) => ({
...x,
label: `${x?.label} (${x?.evo_graph_price} руб.)`,
}));
const selectTracker = addproductTypes.evo_addproduct_types
?.filter((x) => x?.evo_product_type === 100_000_003)
.map((x) => ({
...x,
label: `${x?.label} (${x?.evo_graph_price} руб.)`,
}));
const selectInsNSIB = addproductTypes.evo_addproduct_types?.filter(
(x) => x?.evo_product_type === 100_000_002
);
handleOnCompleted({
selectRegistration,
selectTechnicalCard,
selectTelematic,
selectTracker,
selectInsNSIB,
});
},
});
useEffect(() => {
getMainData(query, handleOnCompleted);
}, []);
}