2023-01-30 15:41:33 +03:00

254 lines
5.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* eslint-disable import/prefer-default-export */
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);
const QUERY_GET_MAIN_OPTIONS = gql`
query GetMainOptions {
selectSupplierCurrency: transactioncurrencies {
label: currencyname
currencysymbol
value: transactioncurrencyid
}
selectLeaseObjectType: evo_leasingobject_types(statecode: 0) {
label: evo_name
value: evo_leasingobject_typeid
}
selectGPSBrand: evo_gps_brands(statecode: 0) {
label: evo_name
value: evo_gps_brandid
}
}
`;
const QUERY_GET_PRODUCTS = gql`
query GetProducts($currentDate: DateTime) {
selectProduct: evo_baseproducts(
statecode: 0
evo_relation: [100000000]
evo_datefrom_param: { lte: $currentDate }
evo_dateto_param: { gte: $currentDate }
) {
label: evo_name
value: evo_baseproductid
}
}
`;
const QUERY_GET_SUBSIDIES = gql`
query GetSubsidies($currentDate: DateTime) {
evo_subsidies(
statecode: 0
evo_datefrom_param: { lte: $currentDate }
evo_dateto_param: { gte: $currentDate }
) {
label: evo_name
value: evo_subsidyid
evo_subsidy_type
}
}
`;
const QUERY_GET_REGIONS = gql`
query GetRegions {
evo_regions {
label: evo_name
value: evo_regionid
}
}
`;
const QUERY_GET_BRANDS = gql`
query GetBrands {
selectBrand: evo_brands(statecode: 0) {
label: evo_name
value: evo_brandid
}
}
`;
const QUERY_GET_DEALERS = gql`
query GetDealers {
selectDealer: accounts(evo_account_type: [100000001], statecode: 0, evo_legal_form: 100000001) {
label: name
value: accountid
}
}
`;
const QUERY_GET_ADDPRODUCT_TYPES = gql`
query GetAddproductTypes($currentDate: DateTime) {
evo_addproduct_types(
statecode: 0
evo_datefrom_param: { lte: $currentDate }
evo_dateto_param: { gte: $currentDate }
) {
label: evo_name
value: evo_addproduct_typeid
evo_graph_price
evo_product_type
}
}
`;
const QUERY_GET_TARIFS = gql`
query GetTarifs($currentDate: DateTime) {
selectTarif: evo_tarifs(
statecode: 0
evo_datefrom_param: { lte: $currentDate }
evo_dateto_param: { gte: $currentDate }
) {
label: evo_name
value: evo_tarifid
}
}
`;
const currentDate = dayjs().utc(false).toISOString();
function getMainData(query, onCompleted) {
query({
query: QUERY_GET_MAIN_OPTIONS,
}).then(({ data }) => {
onCompleted(data);
});
query({
query: QUERY_GET_PRODUCTS,
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,
variables: {
currentDate,
},
}).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
);
const selectLeasingWithoutKasko = data.evo_addproduct_types?.filter(
(x) => x?.evo_product_type === 100_000_007
);
onCompleted({
selectRegistration,
selectTechnicalCard,
selectTelematic,
selectTracker,
selectInsNSIB,
selectLeasingWithoutKasko,
});
});
query({
query: QUERY_GET_TARIFS,
variables: {
currentDate,
},
}).then(({ data }) => {
onCompleted(data);
});
}
export function useMainData() {
const { $calculation } = useStore();
const { query } = useApolloClient();
function handleOnCompleted(options) {
Object.keys(options).forEach((elementName) => {
const elementOptions = options[elementName];
$calculation.element(elementName).setOptions(elementOptions);
});
}
useEffect(() => {
getMainData(query, handleOnCompleted);
}, []);
}