pages/index: hydrate store data

This commit is contained in:
Chika 2022-07-11 12:21:59 +03:00
parent 18f4ee5c0c
commit 6d7dbd42bd
4 changed files with 29 additions and 35 deletions

View File

@ -7,13 +7,13 @@
// GraphQL query operation: GetOwnerData
// ====================================================
export interface GetOwnerData_leads {
export interface GetOwnerData_selectLead {
__typename: "lead";
label: string | null;
value: any | null;
}
export interface GetOwnerData_opportunities {
export interface GetOwnerData_selectOpportunity {
__typename: "opportunity";
label: string | null;
value: any | null;
@ -23,8 +23,8 @@ export interface GetOwnerData {
/**
* Интересы. statecode по умолчанию 0, можно отфильтровать по ownerid и/или domainName
*/
leads: (GetOwnerData_leads | null)[] | null;
opportunities: (GetOwnerData_opportunities | null)[] | null;
selectLead: (GetOwnerData_selectLead | null)[] | null;
selectOpportunity: (GetOwnerData_selectOpportunity | null)[] | null;
}
export interface GetOwnerDataVariables {

View File

@ -7,7 +7,7 @@
// GraphQL query operation: GetTransactionCurrencies
// ====================================================
export interface GetTransactionCurrencies_transactioncurrencies {
export interface GetTransactionCurrencies_selectSupplierCurrency {
__typename: "transactioncurrency";
label: string | null;
currencysymbol: string | null;
@ -18,5 +18,5 @@ export interface GetTransactionCurrencies {
/**
* Валюта. statecode по умолчанию 0
*/
transactioncurrencies: (GetTransactionCurrencies_transactioncurrencies | null)[] | null;
selectSupplierCurrency: (GetTransactionCurrencies_selectSupplierCurrency | null)[] | null;
}

View File

@ -1,8 +1,8 @@
/* eslint-disable object-curly-newline */
import { gql, useApolloClient } from '@apollo/client';
import initializeApollo from 'apollo/client';
import * as Calculation from 'Components/Calculation';
import Output from 'Components/Output';
import type { BaseOption } from 'Elements/types';
import type { GetServerSideProps } from 'next';
import Head from 'next/head';
import calculateValidationReactions from 'process/calculate/reactions/validation';
@ -14,6 +14,8 @@ 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;
@ -36,11 +38,7 @@ const Grid = styled(Box)`
}
`;
type PageProps = {
graphQLData: Record<string, BaseOption[] | null>;
};
function Home({ graphQLData }: PageProps) {
function Home() {
const store = useStore();
const apolloClient = useApolloClient();
@ -54,19 +52,6 @@ function Home({ graphQLData }: PageProps) {
calculateValidationReactions(store, apolloClient);
});
/**
* set graphql data to store
*/
const { leads, opportunities, transactioncurrencies } = graphQLData;
const { $calculation } = store;
$calculation.$options.setOptions({
selectLead: leads || [],
selectOpportunity: opportunities || [],
selectSupplierCurrency: transactioncurrencies || [],
});
return (
<Grid>
<Head>
@ -81,11 +66,11 @@ function Home({ graphQLData }: PageProps) {
const QUERY_GET_OWNER_DATA = gql`
query GetOwnerData($domainname: String) {
leads(owner_domainname: $domainname) {
selectLead: leads(owner_domainname: $domainname) {
label: fullname
value: leadid
}
opportunities(owner_domainname: $domainname) {
selectOpportunity: opportunities(owner_domainname: $domainname) {
label: name
value: opportunityid
}
@ -94,7 +79,7 @@ const QUERY_GET_OWNER_DATA = gql`
const QUERY_GET_TRANSACTION_CURRENCIES = gql`
query GetTransactionCurrencies {
transactioncurrencies {
selectSupplierCurrency: transactioncurrencies {
label: currencyname
currencysymbol
value: transactioncurrencyid
@ -102,7 +87,7 @@ const QUERY_GET_TRANSACTION_CURRENCIES = gql`
}
`;
export const getServerSideProps: GetServerSideProps<PageProps> = async ({ req }) => {
export const getServerSideProps: GetServerSideProps = async ({ req }) => {
const { cookie = '' } = req.headers;
const user = await fetchUser({
@ -113,7 +98,9 @@ export const getServerSideProps: GetServerSideProps<PageProps> = async ({ req })
const apolloClient = initializeApollo();
const { data: ownerData } = await apolloClient.query({
const {
data: { selectLead, selectOpportunity },
} = await apolloClient.query<GetOwnerData, GetOwnerDataVariables>({
query: QUERY_GET_OWNER_DATA,
variables: {
domainname: getDomainName(user),
@ -121,16 +108,20 @@ export const getServerSideProps: GetServerSideProps<PageProps> = async ({ req })
});
// prettier-ignore
const { data: { transactioncurrencies } } = await apolloClient.query({
const { data: { selectSupplierCurrency } } = await apolloClient.query<GetTransactionCurrencies>({
query: QUERY_GET_TRANSACTION_CURRENCIES,
});
return {
props: {
graphQLData: {
...ownerData,
transactioncurrencies,
calculation: {
options: {
selectLead,
selectOpportunity,
selectSupplierCurrency,
},
},
initialApolloState: apolloClient.cache.extract(),
},
};

View File

@ -1,6 +1,7 @@
/* eslint-disable object-curly-newline */
/* eslint-disable @typescript-eslint/naming-convention */
/* eslint-disable no-underscore-dangle */
import defaultOptions from 'config/default-options';
import { createContext } from 'react';
import RootStore from './root';
@ -17,7 +18,9 @@ export function initializeStore(initialData) {
_store.$user.hydrate(user);
if (calculation?.values) _store.$calculation.$values.hydrate(calculation.values);
if (calculation?.statuses) _store.$calculation.$status.hydrate(calculation.statuses);
if (calculation?.options) _store.$calculation.$options.hydrate(calculation.options);
if (calculation?.options) {
_store.$calculation.$options.hydrate({ ...defaultOptions, ...calculation.options });
}
if (tables?.insurance) {
_store.$tables.insurance.hydrate({