44 useLocation ,
55} from "react-router-dom" ;
66import React , { useState } from 'react' ;
7- import { gql , useQuery } from '@apollo/client' ;
7+ import { gql , useQuery , useLazyQuery } from '@apollo/client' ;
88import get from 'lodash.get' ;
99import Post from './Post' ;
1010import { getUrlSlug } from './utils' ;
@@ -25,6 +25,20 @@ import { getLanguage, getListingPaginationAndFilter } from './utils/queryString'
2525import { languages } from './components/LanguageSelector' ;
2626
2727export default function App ( ) {
28+ const navigationItemQuery = gql `query NavigationItemSlug($codename: String!, $language: String!) {
29+ navigationItem(codename: $codename, language: {language: $language}) {
30+ slug
31+ system {
32+ codename
33+ language {
34+ system {
35+ codename
36+ }
37+ }
38+ }
39+ }
40+ }` ;
41+
2842 const homePageQuery = gql `
2943 query HomePageQuery($codename: String!, $language: String!){
3044 postCollection{
@@ -183,30 +197,37 @@ export default function App() {
183197
184198 const language = getLanguage ( useLocation ( ) ) || languages [ 0 ] . codename ;
185199
186- const { loading, error, fetchMore } = useQuery ( homePageQuery , {
200+ const [ getOtherLanguage , { loading : otherLanguageLoading , error : otherLanguageError , data : otherLanguageData } ] = useLazyQuery ( navigationItemQuery , {
201+ onCompleted : ( data => {
202+ setUrlSlugs ( urlSlugs . concat ( data ) )
203+ } )
204+ } )
205+
206+ const { loading, error } = useQuery ( homePageQuery , {
187207 variables : { codename : homepageCodename , language : language } ,
188- onCompleted : async ( data ) => {
208+ onCompleted : ( data ) => {
189209 const mappings = getMappings ( data ) ;
190210 const siteConfiguration = getSiteConfiguration ( data ) ;
191211
192212 setSiteConfiguration ( siteConfiguration ) ;
193213 setHomepageSeo ( getSeo ( data . homepage ) ) ;
194214
195- await fetchMore ( {
196- variables : { codename : homepageCodename , language : languages . find ( lang => lang . codename !== language ) . codename } ,
197- updateQuery : ( _ , fetchMoreResult ) => {
198- setMappings ( { ... getMappings ( fetchMoreResult . fetchMoreResult ) , ... mappings } ) ;
199- }
200- } ) ;
215+ setMappings ( mappings ) ;
216+
217+ const navigationItem = mappings [ getUrlSlug ( window . location . pathname ) ] ;
218+
219+ languages . filter ( lang => lang . codename != language ) . map ( lang => lang . codename ) . forEach ( langCodename =>
220+ getOtherLanguage ( { variables : { codename : navigationItem . navigationCodename , language : langCodename } } ) ) ;
201221 }
202222 } ) ;
203223
204224 const [ mappings , setMappings ] = useState ( null ) ;
225+ const [ urlSlugs , setUrlSlugs ] = useState ( [ ] ) ;
205226 const [ siteConfiguration , setSiteConfiguration ] = useState ( null ) ;
206227 const [ homepageSeo , setHomepageSeo ] = useState ( null ) ;
207-
208- if ( error || loading || ! mappings || ! siteConfiguration || ! homepageSeo ) {
209- return < GraphQLLoader error = { error } loading = { loading } /> ;
228+ debugger
229+ if ( error || loading || otherLanguageLoading || otherLanguageError || ! otherLanguageData || ! mappings || ! siteConfiguration || ! homepageSeo ) {
230+ return < GraphQLLoader error = { error || otherLanguageError } loading = { loading || otherLanguageLoading } /> ;
210231 }
211232
212233 return (
@@ -217,8 +238,9 @@ export default function App() {
217238
218239 function renderPage ( { location } ) {
219240 const navigationItem = mappings [ getUrlSlug ( location . pathname ) ] ;
220-
241+
221242 if ( ! navigationItem ) {
243+ return < GraphQLLoader error = 'waiting for other requests' /> ;
222244 if ( process . env . NODE_ENV === "development" ) {
223245 console . error ( `Unknown navigation item pathname: ${ location . pathname } ` ) ;
224246 return (
@@ -234,6 +256,7 @@ export default function App() {
234256 const pageProps = {
235257 siteConfiguration,
236258 mappings,
259+ urlSlugs
237260 } ;
238261
239262 if ( navigationItem . navigationType === "homepage" ) {
0 commit comments