diff --git a/apps/trading/components/page-query-container/index.tsx b/apps/trading/components/page-query-container/index.tsx new file mode 100644 index 000000000..afb262034 --- /dev/null +++ b/apps/trading/components/page-query-container/index.tsx @@ -0,0 +1,27 @@ +import { QueryHookOptions, useQuery } from '@apollo/client'; +import { DocumentNode } from 'graphql'; +import { ReactNode } from 'react'; + +interface PageQueryContainerProps { + query: DocumentNode; + options?: QueryHookOptions; + children: (data: TData) => ReactNode; +} + +export const PageQueryContainer = ({ + query, + options, + children, +}: PageQueryContainerProps) => { + const { data, loading, error } = useQuery(query, options); + + if (loading || !data) { + return
Loading...
; + } + + if (error) { + return
Something went wrong: {error.message}
; + } + + return <>{children(data)}; +}; diff --git a/apps/trading/pages/markets/[marketId].page.tsx b/apps/trading/pages/markets/[marketId].page.tsx index 4651cd20d..38da85814 100644 --- a/apps/trading/pages/markets/[marketId].page.tsx +++ b/apps/trading/pages/markets/[marketId].page.tsx @@ -1,4 +1,4 @@ -import { gql, useQuery } from '@apollo/client'; +import { gql } from '@apollo/client'; import AutoSizer from 'react-virtualized-auto-sizer'; import classNames from 'classnames'; import { useRouter } from 'next/router'; @@ -11,6 +11,7 @@ import React, { } from 'react'; import debounce from 'lodash/debounce'; import { Market, MarketVariables, Market_market } from './__generated__/Market'; +import { PageQueryContainer } from '../../components/page-query-container'; // Top level page query const MARKET_QUERY = gql` @@ -31,30 +32,24 @@ const MARKET_QUERY = gql` const MarketPage = () => { const { query } = useRouter(); const { w } = useWindowSize(); - const { data, loading, error } = useQuery( - MARKET_QUERY, - { - variables: { marketId: query.marketId as string }, - skip: !query.marketId, - } - ); - - if (loading || !data) { - return
Loading...
; - } - - if (error) { - return
Something went wrong: {error.message}
; - } return ( - <> - {w > 1050 ? ( - - ) : ( - - )} - + + query={MARKET_QUERY} + options={{ + variables: { marketId: query.marketId as string }, + skip: !query.marketId, + }} + > + {(data) => + w > 1050 ? ( + + ) : ( + + ) + } + {} + ); };