/* eslint-disable canonical/sort-keys */ import * as CRMTypes from '@/graphql/crm.types'; import { useStore } from '@/stores/hooks'; import { getCurrentISODate } from '@/utils/date'; import { normalizeOptions } from '@/utils/entity'; import { useApolloClient } from '@apollo/client'; import { useEffect } from 'react'; const currentDate = getCurrentISODate(); /** * * @param {import('@apollo/client').ApolloClient} apolloClient * @param {*} onCompleted */ function getMainData({ query }, onCompleted) { // query({ // query: CRMTypes.GetLeaseObjectTypesDocument, // }).then(({ data }) => { // onCompleted({ // selectLeaseObjectType: data?.evo_leasingobject_types, // }); // }); query({ query: CRMTypes.GetGpsBrandsDocument, }).then(({ data }) => { onCompleted({ selectGPSBrand: data?.evo_gps_brands, }); }); query({ query: CRMTypes.GetSubsidiesDocument, 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: CRMTypes.GetRegionsDocument, }).then(({ data }) => { const selectRegionRegistration = data?.evo_regions; const selectObjectRegionRegistration = data?.evo_regions; const selectLegalClientRegion = data?.evo_regions; onCompleted({ selectRegionRegistration, selectObjectRegionRegistration, selectLegalClientRegion, }); }); // query({ // query: CRMTypes.GetBrandsDocument, // }).then(({ data }) => { // onCompleted({ // selectBrand: data?.evo_brands, // }); // }); // query({ // query: CRMTypes.GetDealersDocument, // }).then(({ data }) => { // onCompleted({ // selectDealer: data?.dealers, // }); // }); query({ query: CRMTypes.GetTelematicTypesDocument, variables: { currentDate, }, }).then(({ data }) => { onCompleted({ selectTelematic: data?.evo_addproduct_types }); }); query({ query: CRMTypes.GetTrackerTypesDocument, variables: { currentDate, }, }).then(({ data }) => { onCompleted({ selectTracker: data?.evo_addproduct_types }); }); query({ query: CRMTypes.GetInsNsibTypesDocument, variables: { currentDate, }, context: { disableModify: true, }, }).then(({ data }) => { onCompleted({ selectInsNSIB: data?.evo_addproduct_types }); }); // query({ // query: CRMTypes.GetTarifsDocument, // variables: { // currentDate, // }, // }).then(({ data }) => { // onCompleted({ // selectTarif: data?.evo_tarifs, // }); // }); } export function useMainData() { const { $calculation } = useStore(); const apolloClient = useApolloClient(); function handleOnCompleted(options) { Object.keys(options).forEach((elementName) => { const elementOptions = options[elementName]; $calculation.element(elementName).setOptions(normalizeOptions(elementOptions)); }); } useEffect(() => { getMainData(apolloClient, handleOnCompleted); }, []); }