process/init: fix get-data fetched multiple times

This commit is contained in:
Chika 2022-11-07 22:59:25 +03:00
parent 222b0b0e74
commit 6f1cfab8f5
13 changed files with 307 additions and 379 deletions

View File

@ -5,7 +5,7 @@ import * as Calculation from 'Components/Calculation';
import { CRMError } from 'Components/Common/Error';
import Output from 'Components/Output';
import Head from 'next/head';
import { useCRMData, useOwnerData } from 'process/init/get-data/hooks';
import { useInsuranceData, useMainData, useOwnerData } from 'process/init/get-data';
import { useReactions } from 'process/init/inject-reactions/hooks';
import styled from 'styled-components';
import { Box } from 'UIKit/grid';
@ -37,8 +37,9 @@ const Grid = styled(Box)`
function Home({ user }) {
const { error } = useOwnerData(user);
useMainData();
useInsuranceData();
useReactions();
useCRMData(user);
if (error) return <CRMError />;

View File

@ -149,7 +149,7 @@ export default function commonReactions({ store, apolloClient, queryClient }: Re
>({
query: QUERY_GET_FINGAP_ADDPRODUCT_TYPES,
variables: {
currentDate: dayjs().utc().format('YYYY-MM-DD'),
currentDate: dayjs().utc(false).format('YYYY-MM-DD'),
},
});

View File

@ -1,62 +0,0 @@
import type { ApolloClient } from '@apollo/client';
import { gql } from '@apollo/client';
import type { GetAddproductTypesQuery } from 'graphql/crm.types';
const QUERY_GET_ADDPRODUCT_TYPES = gql`
query GetAddproductTypes {
evo_addproduct_types(statecode: 0) {
label: evo_name
value: evo_addproduct_typeid
evo_graph_price
evo_product_type
}
}
`;
export default async function getAddProductTypes(apolloClient: ApolloClient<object>) {
const { data: addproductTypes } = await apolloClient.query<GetAddproductTypesQuery>({
query: QUERY_GET_ADDPRODUCT_TYPES,
});
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
);
return {
options: {
selectRegistration,
selectTechnicalCard,
selectTelematic,
selectTracker,
selectInsNSIB,
},
};
}

View File

@ -1,26 +0,0 @@
import type { ApolloClient } from '@apollo/client';
import { gql } from '@apollo/client';
import type { GetBrandsQuery } from 'graphql/crm.types';
const QUERY_GET_BRANDS = gql`
query GetBrands {
selectBrand: evo_brands(statecode: 0) {
label: evo_name
value: evo_brandid
}
}
`;
export default async function getBrands(apolloClient: ApolloClient<object>) {
const {
data: { selectBrand },
} = await apolloClient.query<GetBrandsQuery>({
query: QUERY_GET_BRANDS,
});
return {
options: {
selectBrand,
},
};
}

View File

@ -1,26 +0,0 @@
import type { ApolloClient } from '@apollo/client';
import { gql } from '@apollo/client';
import type { GetDealersQuery } from 'graphql/crm.types';
const QUERY_GET_DEALERS = gql`
query GetDealers {
selectDealer: accounts(evo_account_type: [100000001], statecode: 0, evo_legal_form: 100000001) {
label: name
value: accountid
}
}
`;
export default async function getDealers(apolloClient: ApolloClient<object>) {
const {
data: { selectDealer },
} = await apolloClient.query<GetDealersQuery>({
query: QUERY_GET_DEALERS,
});
return {
options: {
selectDealer,
},
};
}

View File

@ -0,0 +1,67 @@
/* eslint-disable import/prefer-default-export */
import { gql, useQuery } from '@apollo/client';
import { useStore } from 'stores/hooks';
const QUERY_GET_INSURANCE_DATA = gql`
query GetInsuranceData($evo_account_type: [Int!]) {
osago: accounts(
evo_account_type: $evo_account_type
evo_type_ins_policy: [100000001]
statecode: 0
) {
value: accountid
label: name
}
kasko: accounts(
evo_account_type: $evo_account_type
evo_type_ins_policy: [100000000]
statecode: 0
) {
value: accountid
label: name
}
fingap: accounts(
evo_account_type: $evo_account_type
evo_type_ins_policy: [100000002]
statecode: 0
) {
value: accountid
label: name
}
}
`;
export function useInsuranceData() {
const { $tables } = useStore();
function handleOnCompleted(options) {
Object.keys(options).forEach((key) => {
const rowOptions = options[key];
if (rowOptions !== undefined) {
Object.keys(rowOptions).forEach((valueName) => {
$tables.insurance.row(key).setOptions(valueName, rowOptions[valueName]);
});
}
});
}
useQuery(QUERY_GET_INSURANCE_DATA, {
onCompleted: (insuranceData) => {
const insurance = {
osago: {
insuranceCompany: insuranceData.osago,
},
kasko: {
insuranceCompany: insuranceData.kasko,
},
fingap: {
insuranceCompany: insuranceData.fingap,
},
};
handleOnCompleted(insurance);
},
});
}

View File

@ -1,58 +0,0 @@
import type { ApolloClient } from '@apollo/client';
import { gql } from '@apollo/client';
import type { GetInsuranceDataQuery } from 'graphql/crm.types';
const QUERY_GET_INSURANCE_DATA = gql`
query GetInsuranceData($evo_account_type: [Int!]) {
osago: accounts(
evo_account_type: $evo_account_type
evo_type_ins_policy: [100000001]
statecode: 0
) {
value: accountid
label: name
}
kasko: accounts(
evo_account_type: $evo_account_type
evo_type_ins_policy: [100000000]
statecode: 0
) {
value: accountid
label: name
}
fingap: accounts(
evo_account_type: $evo_account_type
evo_type_ins_policy: [100000002]
statecode: 0
) {
value: accountid
label: name
}
}
`;
export default async function getInsuranceData(apolloClient: ApolloClient<object>) {
const { data: insuranceData } = await apolloClient.query<GetInsuranceDataQuery>({
query: QUERY_GET_INSURANCE_DATA,
});
const insurance = {
osago: {
insuranceCompany: insuranceData.osago,
},
kasko: {
insuranceCompany: insuranceData.kasko,
},
fingap: {
insuranceCompany: insuranceData.fingap,
},
};
return {
tables: {
insurance,
},
};
}

View File

@ -0,0 +1,195 @@
/* eslint-disable import/prefer-default-export */
import { gql, useQuery } from '@apollo/client';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import { useStore } from 'stores/hooks';
dayjs.extend(utc);
const QUERY_GET_MAIN_OPTIONS = gql`
query GetMainOptions($currentDate: DateTime) {
selectSupplierCurrency: transactioncurrencies {
label: currencyname
currencysymbol
value: transactioncurrencyid
}
selectProduct: evo_baseproducts(
statecode: 0
evo_relation: [100000000]
evo_datefrom_param: { lte: $currentDate }
evo_dateto_param: { gte: $currentDate }
) {
label: evo_name
value: evo_baseproductid
}
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_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 {
evo_addproduct_types(statecode: 0) {
label: evo_name
value: evo_addproduct_typeid
evo_graph_price
evo_product_type
}
}
`;
const currentDate = dayjs().utc(false).toISOString();
export function useMainData() {
const { $calculation } = useStore();
function handleOnCompleted(options) {
Object.keys(options).forEach((elementName) => {
const elementOptions = options[elementName];
$calculation.element(elementName).setOptions(elementOptions);
});
}
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,
});
},
});
}

View File

@ -1,112 +0,0 @@
/* eslint-disable import/prefer-default-export */
import type { ApolloClient } from '@apollo/client';
import { gql } from '@apollo/client';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import type * as CRMTypes from 'graphql/crm.types';
dayjs.extend(utc);
const QUERY_GET_MAIN_OPTIONS = gql`
query GetMainOptions($currentDate: DateTime) {
selectSupplierCurrency: transactioncurrencies {
label: currencyname
currencysymbol
value: transactioncurrencyid
}
selectProduct: evo_baseproducts(
statecode: 0
evo_relation: [100000000]
evo_datefrom_param: { lte: $currentDate }
evo_dateto_param: { gte: $currentDate }
) {
label: evo_name
value: evo_baseproductid
}
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_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
}
}
`;
export default async function getMainData(apolloClient: ApolloClient<object>) {
const { data: mainOptions } = await apolloClient.query<
CRMTypes.GetMainOptionsQuery,
CRMTypes.GetMainOptionsQueryVariables
>({
query: QUERY_GET_MAIN_OPTIONS,
variables: {
currentDate: dayjs().utc().toISOString(),
},
});
const { data: subsidies } = await apolloClient.query<
CRMTypes.GetSubsidiesQuery,
CRMTypes.GetSubsidiesQueryVariables
>({
query: QUERY_GET_SUBSIDIES,
variables: {
currentDate: dayjs().utc().toISOString(),
},
});
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)
);
const { data: regions } = await apolloClient.query<
CRMTypes.GetRegionsQuery,
CRMTypes.GetRegionsQueryVariables
>({
query: QUERY_GET_REGIONS,
});
const selectRegionRegistration = regions.evo_regions;
const selectObjectRegionRegistration = regions.evo_regions;
const selectLegalClientRegion = regions.evo_regions;
return {
options: {
...mainOptions,
selectSubsidy,
selectImportProgram,
selectRegionRegistration,
selectObjectRegionRegistration,
selectLegalClientRegion,
},
};
}

View File

@ -1,9 +1,12 @@
import type { ApolloClient, NormalizedCache } from '@apollo/client';
import { gql } from '@apollo/client';
/* eslint-disable import/prefer-default-export */
import { gql, useQuery } from '@apollo/client';
import type { User } from 'api/user/types';
import type { GetOwnerDataQuery, GetOwnerDataQueryVariables } from 'graphql/crm.types';
import { useStore } from 'stores/hooks';
import { normalizeOptions } from 'tools/entity';
export const QUERY_GET_OWNER_DATA = gql`
const QUERY_GET_OWNER_DATA = gql`
query GetOwnerData($domainname: String) {
selectLead: leads(owner_domainname: $domainname) {
label: fullname
@ -16,21 +19,38 @@ export const QUERY_GET_OWNER_DATA = gql`
}
`;
export default async function getOwnerData(
apolloClient: ApolloClient<NormalizedCache>,
user: User
) {
const { data: ownerData } = await apolloClient.query<
GetOwnerDataQuery,
GetOwnerDataQueryVariables
>({
query: QUERY_GET_OWNER_DATA,
variables: {
domainname: user.domainName,
},
});
export function useOwnerData(user: User) {
const { $calculation } = useStore();
function handleOnStartFetch() {
$calculation.$status.setStatus('selectLead', 'Loading');
$calculation.$status.setStatus('selectOpportunity', 'Loading');
}
function handleOnCompleted(data: GetOwnerDataQuery) {
$calculation.element('selectLead').setOptions(normalizeOptions(data?.selectLead));
$calculation.element('selectOpportunity').setOptions(normalizeOptions(data?.selectOpportunity));
$calculation.$status.setStatus('selectLead', 'Default');
$calculation.$status.setStatus('selectOpportunity', 'Default');
}
const { loading, error } = useQuery<GetOwnerDataQuery, GetOwnerDataQueryVariables>(
QUERY_GET_OWNER_DATA,
{
variables: {
domainname: user?.domainName,
},
onCompleted: handleOnCompleted,
}
);
if (loading) {
handleOnStartFetch();
}
return {
options: ownerData,
error,
};
}

View File

@ -1,74 +0,0 @@
/* eslint-disable import/prefer-default-export */
import { useApolloClient, useQuery } from '@apollo/client';
import { useStore } from 'stores/hooks';
import getAddProductTypes from './get-addproduct-types-data';
import getBrands from './get-brands';
import getDealers from './get-dealers';
import getInsuranceData from './get-insurance-data';
import getMainData from './get-main-data';
import { QUERY_GET_OWNER_DATA } from './get-owner-data';
export function useCRMData() {
const store = useStore();
const apolloClient = useApolloClient();
const { $calculation, $tables } = store;
function setManyOptions(options) {
Object.keys(options).forEach((elementName) => {
const elementOptions = options[elementName];
$calculation.element(elementName).setOptions(elementOptions);
});
}
getMainData(apolloClient).then(({ options }) => setManyOptions(options));
getBrands(apolloClient).then(({ options }) => setManyOptions(options));
getDealers(apolloClient).then(({ options }) => setManyOptions(options));
getAddProductTypes(apolloClient).then(({ options }) => setManyOptions(options));
function setManyRowOptions(options) {
Object.keys(options).forEach((key) => {
const rowOptions = options[key];
if (rowOptions !== undefined) {
Object.keys(rowOptions).forEach((valueName) => {
$tables.insurance.row(key).setOptions(valueName, rowOptions[valueName]);
});
}
});
}
getInsuranceData(apolloClient).then(({ tables }) => setManyRowOptions(tables.insurance));
}
export function useOwnerData(user) {
const store = useStore();
const { $calculation } = store;
function handleOnStartFetch() {
$calculation.$status.setStatus('selectLead', 'Loading');
$calculation.$status.setStatus('selectOpportunity', 'Loading');
}
function handleOnCompleted(data) {
$calculation.element('selectLead').setOptions(data?.selectLead);
$calculation.element('selectOpportunity').setOptions(data?.selectOpportunity);
$calculation.$status.setStatus('selectLead', 'Default');
$calculation.$status.setStatus('selectOpportunity', 'Default');
}
const { loading, error } = useQuery(QUERY_GET_OWNER_DATA, {
variables: {
domainname: user?.domainName,
},
onCompleted: handleOnCompleted,
});
if (loading) {
handleOnStartFetch();
}
return {
error,
};
}

View File

@ -0,0 +1,3 @@
export { useInsuranceData } from './get-insurance-data';
export { useMainData } from './get-main-data';
export { useOwnerData } from './get-owner-data';

View File

@ -68,7 +68,7 @@ export default function computedReactions({ store, apolloClient }: ReactionsCont
>({
query: QUERY_GET_EVO_CURRENCY_CHANGES,
variables: {
currentDate: dayjs().utc().format('YYYY-MM-DD'),
currentDate: dayjs().utc(false).format('YYYY-MM-DD'),
},
});