2023-03-29 10:07:34 +03:00

157 lines
3.5 KiB
JavaScript

/* eslint-disable canonical/sort-keys */
import * as CRMTypes from '@/graphql/crm.types';
import { useStore } from '@/stores/hooks';
import { useApolloClient } from '@apollo/client';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import { useEffect } from 'react';
import { normalizeOptions } from 'tools';
dayjs.extend(utc);
const currentDate = dayjs().utc(false).format('YYYY-MM-DD');
/**
*
* @param {import('@apollo/client').ApolloClient} apolloClient
* @param {*} onCompleted
*/
function getMainData({ query }, onCompleted) {
query({
query: CRMTypes.GetLeaseObjectTypesDocument,
variables: {
currentDate,
},
}).then(({ data }) => {
onCompleted({
selectLeaseObjectType: data?.evo_leasingobject_types,
});
});
query({
query: CRMTypes.GetGpsBrandsDocument,
}).then(({ data }) => {
onCompleted({
selectGPSBrand: data?.evo_gps_brands,
});
});
query({
query: CRMTypes.GetProductsDocument,
variables: {
currentDate,
},
}).then(({ data }) => {
onCompleted({
selectProduct: data?.evo_baseproducts,
});
});
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);
}, []);
}