pages/index: move GQL queries to process

This commit is contained in:
Chika 2022-07-11 12:45:30 +03:00
parent 6d7dbd42bd
commit f485ba9659
4 changed files with 59 additions and 46 deletions

View File

@ -1,21 +1,19 @@
/* eslint-disable object-curly-newline */
import { gql, useApolloClient } from '@apollo/client';
import { useApolloClient } from '@apollo/client';
import initializeApollo from 'apollo/client';
import * as Calculation from 'Components/Calculation';
import Output from 'Components/Output';
import type { GetServerSideProps } from 'next';
import Head from 'next/head';
import calculateValidationReactions from 'process/calculate/reactions/validation';
import { getCRMData } from 'process/init/get-data';
import leadOpportunityUrlsReactions from 'process/lead-opportunity/reactions/urls';
import paymentsReactions from 'process/payments/reactions';
import { fetchUser } from 'services/user';
import { getDomainName } from 'services/user/tools';
import { useStore } from 'stores/hooks';
import styled from 'styled-components';
import { Box } from 'UIKit/grid';
import { min } from 'UIKit/mq';
import type { GetOwnerData, GetOwnerDataVariables } from './__generated__/GetOwnerData';
import type { GetTransactionCurrencies } from './__generated__/GetTransactionCurrencies';
const Grid = styled(Box)`
display: flex;
@ -64,29 +62,6 @@ function Home() {
);
}
const QUERY_GET_OWNER_DATA = gql`
query GetOwnerData($domainname: String) {
selectLead: leads(owner_domainname: $domainname) {
label: fullname
value: leadid
}
selectOpportunity: opportunities(owner_domainname: $domainname) {
label: name
value: opportunityid
}
}
`;
const QUERY_GET_TRANSACTION_CURRENCIES = gql`
query GetTransactionCurrencies {
selectSupplierCurrency: transactioncurrencies {
label: currencyname
currencysymbol
value: transactioncurrencyid
}
}
`;
export const getServerSideProps: GetServerSideProps = async ({ req }) => {
const { cookie = '' } = req.headers;
@ -97,29 +72,13 @@ export const getServerSideProps: GetServerSideProps = async ({ req }) => {
});
const apolloClient = initializeApollo();
const {
data: { selectLead, selectOpportunity },
} = await apolloClient.query<GetOwnerData, GetOwnerDataVariables>({
query: QUERY_GET_OWNER_DATA,
variables: {
domainname: getDomainName(user),
},
});
// prettier-ignore
const { data: { selectSupplierCurrency } } = await apolloClient.query<GetTransactionCurrencies>({
query: QUERY_GET_TRANSACTION_CURRENCIES,
});
const { options } = await getCRMData(apolloClient, user);
return {
props: {
user,
calculation: {
options: {
selectLead,
selectOpportunity,
selectSupplierCurrency,
},
options,
},
initialApolloState: apolloClient.cache.extract(),

54
process/init/get-data.ts Normal file
View File

@ -0,0 +1,54 @@
/* eslint-disable import/prefer-default-export */
import type { ApolloClient, NormalizedCache } from '@apollo/client';
import { gql } from '@apollo/client';
import { getDomainName } from 'services/user/tools';
import type { User } from 'services/user/types';
import type { GetOwnerData, GetOwnerDataVariables } from './__generated__/GetOwnerData';
import type { GetTransactionCurrencies } from './__generated__/GetTransactionCurrencies';
const QUERY_GET_OWNER_DATA = gql`
query GetOwnerData($domainname: String) {
selectLead: leads(owner_domainname: $domainname) {
label: fullname
value: leadid
}
selectOpportunity: opportunities(owner_domainname: $domainname) {
label: name
value: opportunityid
}
}
`;
const QUERY_GET_TRANSACTION_CURRENCIES = gql`
query GetTransactionCurrencies {
selectSupplierCurrency: transactioncurrencies {
label: currencyname
currencysymbol
value: transactioncurrencyid
}
}
`;
export async function getCRMData(apolloClient: ApolloClient<NormalizedCache>, user: User) {
const {
data: { selectLead, selectOpportunity },
} = await apolloClient.query<GetOwnerData, GetOwnerDataVariables>({
query: QUERY_GET_OWNER_DATA,
variables: {
domainname: getDomainName(user),
},
});
// prettier-ignore
const { data: { selectSupplierCurrency } } = await apolloClient.query<GetTransactionCurrencies>({
query: QUERY_GET_TRANSACTION_CURRENCIES,
});
return {
options: {
selectLead,
selectOpportunity,
selectSupplierCurrency,
},
};
}