graphql: use aliases for options request

This commit is contained in:
Chika 2022-07-08 20:55:04 +03:00
parent ea9627c6f6
commit 04b87c44e4
4 changed files with 15 additions and 80 deletions

View File

@ -8,15 +8,15 @@
// ====================================================
export interface GetOwnerData_leads {
__typename: 'lead';
fullname: string | null;
leadid: any | null;
__typename: "lead";
label: string | null;
value: any | null;
}
export interface GetOwnerData_opportunities {
__typename: 'opportunity';
name: string | null;
opportunityid: any | null;
__typename: "opportunity";
label: string | null;
value: any | null;
}
export interface GetOwnerData {

View File

@ -2,18 +2,14 @@ import { gql, 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 leadOpportunityUrlsReactions from 'process/lead-opportunity/reactions/urls';
import { fetchUser } from 'services/user';
import { getDomainName } from 'services/user/tools';
import { useStore } from 'stores/hooks';
import styled from 'styled-components';
import { convertEntitiesToOptions } from 'tools/entity';
import type { BasePageProps } from 'types/page';
import { Box } from 'UIKit/grid';
import { min } from 'UIKit/mq';
import type { GetOwnerData, GetOwnerDataVariables } from './__generated__/GetOwnerData';
const Grid = styled(Box)`
display: flex;
@ -36,9 +32,7 @@ const Grid = styled(Box)`
}
`;
type PageProps = BasePageProps & { graphQLData: GraphQLData };
function Home({ graphQLData }: PageProps) {
function Home({ graphQLData }) {
const store = useStore();
const apolloClient = useApolloClient();
@ -51,16 +45,12 @@ function Home({ graphQLData }: PageProps) {
* set owner data to store
*/
const { ownerData } = graphQLData;
const leads = ownerData.leads ? convertEntitiesToOptions(ownerData.leads) : [];
const opportunities = ownerData.opportunities
? convertEntitiesToOptions(ownerData.opportunities)
: [];
const { $calculation } = store;
$calculation.$options.setOptions({
selectLead: leads,
selectOpportunity: opportunities,
selectLead: ownerData?.leads || [],
selectOpportunity: ownerData.opportunities || [],
});
return (
@ -75,24 +65,20 @@ function Home({ graphQLData }: PageProps) {
);
}
type GraphQLData = {
ownerData: GetOwnerData;
};
const QUERY_GET_OWNER_DATA = gql`
query GetOwnerData($domainname: String) {
leads(owner_domainname: $domainname) {
fullname
leadid
label: fullname
value: leadid
}
opportunities(owner_domainname: $domainname) {
name
opportunityid
label: name
value: opportunityid
}
}
`;
export const getServerSideProps: GetServerSideProps<PageProps> = async (ctx) => {
export const getServerSideProps = async (ctx) => {
const user = await fetchUser({
headers: ctx?.req?.headers?.cookie
? {
@ -103,7 +89,7 @@ export const getServerSideProps: GetServerSideProps<PageProps> = async (ctx) =>
const apolloClient = initializeApollo();
const { data: ownerData } = await apolloClient.query<GetOwnerData, GetOwnerDataVariables>({
const { data: ownerData } = await apolloClient.query({
query: QUERY_GET_OWNER_DATA,
variables: {
domainname: getDomainName(user),
@ -112,7 +98,6 @@ export const getServerSideProps: GetServerSideProps<PageProps> = async (ctx) =>
return {
props: {
user,
graphQLData: {
ownerData,
},

View File

@ -1,31 +0,0 @@
/* eslint-disable import/prefer-default-export */
import type { BaseOption } from 'Elements/types';
const propsMap = {
lead: {
name: 'fullname',
value: 'leadid',
},
opportunity: {
name: 'name',
value: 'opportunityid',
},
};
type PropsMap = typeof propsMap;
function getEntityName(entities: any[]): keyof PropsMap {
// eslint-disable-next-line no-underscore-dangle
return entities.at(0)?.__typename;
}
export function convertEntitiesToOptions(entities: any[]): BaseOption[] {
const entityName = getEntityName(entities);
const map = propsMap[entityName];
return entities.map((entity) => ({
label: entity[map.name],
value: entity[map.value],
}));
}

View File

@ -1,19 +0,0 @@
import type { NormalizedCacheObject } from '@apollo/client';
import type { User } from 'services/user/types';
import type { CalculationOptions } from 'stores/calculation/options/types';
import type { CalculationStatuses } from 'stores/calculation/statuses/types';
import type { CalculationValues } from 'stores/calculation/values/types';
import type { InsuranceTableData } from 'stores/tables/insurance';
export interface BasePageProps {
user: User;
initialApolloState: NormalizedCacheObject;
calculation?: {
values: CalculationValues;
statuses: CalculationStatuses;
options: CalculationOptions;
};
tables?: {
insurance: InsuranceTableData;
};
}