From f03ba9d97977b603207e89b4fb9980984e0bc89a Mon Sep 17 00:00:00 2001 From: Chika Date: Tue, 8 Nov 2022 12:04:37 +0300 Subject: [PATCH] pages/index: revert getOwnerData back to server --- pages/index.jsx | 31 +++++++++++------ process/init/get-data/get-owner-data.ts | 46 +++++-------------------- process/init/get-data/index.js | 2 +- 3 files changed, 30 insertions(+), 49 deletions(-) diff --git a/pages/index.jsx b/pages/index.jsx index 05269eb..4c951b5 100644 --- a/pages/index.jsx +++ b/pages/index.jsx @@ -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 { useInsuranceData, useMainData, useOwnerData } from 'process/init/get-data'; +import { getOwnerData, useInsuranceData, useMainData } from 'process/init/get-data'; import { useReactions } from 'process/init/inject-reactions/hooks'; import styled from 'styled-components'; import { Box } from 'UIKit/grid'; @@ -35,8 +35,7 @@ const Grid = styled(Box)` } `; -function Home({ user }) { - const { error } = useOwnerData(user); +function Home({ error }) { useMainData(); useInsuranceData(); useReactions(); @@ -68,15 +67,25 @@ export const getServerSideProps = async ({ req }) => { const user = await queryClient.fetchQuery(['user'], queryGetUser); const apolloClient = initializeApollo(); + try { + const { data } = await getOwnerData(apolloClient, user); - return { - props: { - user, - calculation: null, - initialApolloState: apolloClient.cache.extract(), - initialQueryState: dehydrate(queryClient), - }, - }; + return { + props: { + calculation: { + options: data, + }, + initialApolloState: apolloClient.cache.extract(), + initialQueryState: dehydrate(queryClient), + }, + }; + } catch (error) { + return { + props: { + error: JSON.stringify(error), + }, + }; + } }; export default Home; diff --git a/process/init/get-data/get-owner-data.ts b/process/init/get-data/get-owner-data.ts index 24b2cc8..90ec863 100644 --- a/process/init/get-data/get-owner-data.ts +++ b/process/init/get-data/get-owner-data.ts @@ -1,10 +1,9 @@ /* eslint-disable import/prefer-default-export */ -import { gql, useQuery } from '@apollo/client'; +import type { ApolloClient } from '@apollo/client'; +import { gql } 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'; const QUERY_GET_OWNER_DATA = gql` query GetOwnerData($domainname: String) { @@ -19,38 +18,11 @@ const QUERY_GET_OWNER_DATA = gql` } `; -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( - QUERY_GET_OWNER_DATA, - { - variables: { - domainname: user?.domainName, - }, - - onCompleted: handleOnCompleted, - } - ); - - if (loading) { - handleOnStartFetch(); - } - - return { - error, - }; +export async function getOwnerData({ query }: ApolloClient, user: User) { + return query({ + query: QUERY_GET_OWNER_DATA, + variables: { + domainname: user?.domainName, + }, + }); } diff --git a/process/init/get-data/index.js b/process/init/get-data/index.js index 6d65323..0dbd66f 100644 --- a/process/init/get-data/index.js +++ b/process/init/get-data/index.js @@ -1,3 +1,3 @@ export { useInsuranceData } from './get-insurance-data'; export { useMainData } from './get-main-data'; -export { useOwnerData } from './get-owner-data'; +export { getOwnerData } from './get-owner-data';