From 857a40af494ed56234a35e1b9460c0555fa3937d Mon Sep 17 00:00:00 2001 From: Chika Date: Tue, 8 Nov 2022 09:18:38 +0300 Subject: [PATCH] process/init: fix GetMainData query fetched twice --- process/init/get-data/get-insurance-data.js | 44 +++-- process/init/get-data/get-main-data.js | 200 +++++++++++--------- 2 files changed, 133 insertions(+), 111 deletions(-) diff --git a/process/init/get-data/get-insurance-data.js b/process/init/get-data/get-insurance-data.js index 0c71fb8..b5a4a12 100644 --- a/process/init/get-data/get-insurance-data.js +++ b/process/init/get-data/get-insurance-data.js @@ -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); + }, []); } diff --git a/process/init/get-data/get-main-data.js b/process/init/get-data/get-main-data.js index f57c1b0..cb2d1e2 100644 --- a/process/init/get-data/get-main-data.js +++ b/process/init/get-data/get-main-data.js @@ -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); + }, []); }