diff --git a/pages/index.tsx b/pages/index.tsx index 9fa2b26..df6928b 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -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({ - query: QUERY_GET_OWNER_DATA, - variables: { - domainname: getDomainName(user), - }, - }); - - // prettier-ignore - const { data: { selectSupplierCurrency } } = await apolloClient.query({ - query: QUERY_GET_TRANSACTION_CURRENCIES, - }); + const { options } = await getCRMData(apolloClient, user); return { props: { + user, calculation: { - options: { - selectLead, - selectOpportunity, - selectSupplierCurrency, - }, + options, }, initialApolloState: apolloClient.cache.extract(), diff --git a/pages/__generated__/GetOwnerData.ts b/process/init/__generated__/GetOwnerData.ts similarity index 100% rename from pages/__generated__/GetOwnerData.ts rename to process/init/__generated__/GetOwnerData.ts diff --git a/pages/__generated__/GetTransactionCurrencies.ts b/process/init/__generated__/GetTransactionCurrencies.ts similarity index 100% rename from pages/__generated__/GetTransactionCurrencies.ts rename to process/init/__generated__/GetTransactionCurrencies.ts diff --git a/process/init/get-data.ts b/process/init/get-data.ts new file mode 100644 index 0000000..16163d2 --- /dev/null +++ b/process/init/get-data.ts @@ -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, user: User) { + const { + data: { selectLead, selectOpportunity }, + } = await apolloClient.query({ + query: QUERY_GET_OWNER_DATA, + variables: { + domainname: getDomainName(user), + }, + }); + + // prettier-ignore + const { data: { selectSupplierCurrency } } = await apolloClient.query({ + query: QUERY_GET_TRANSACTION_CURRENCIES, + }); + + return { + options: { + selectLead, + selectOpportunity, + selectSupplierCurrency, + }, + }; +}