Skip to content
This repository was archived by the owner on Oct 6, 2025. It is now read-only.

Commit 9f458aa

Browse files
use useLazyQuery for loading slug
1 parent c7056d2 commit 9f458aa

File tree

4 files changed

+49
-18
lines changed

4 files changed

+49
-18
lines changed

src/App.js

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
useLocation,
55
} from "react-router-dom";
66
import React, { useState } from 'react';
7-
import { gql, useQuery } from '@apollo/client';
7+
import { gql, useQuery, useLazyQuery } from '@apollo/client';
88
import get from 'lodash.get';
99
import Post from './Post';
1010
import { getUrlSlug } from './utils';
@@ -25,6 +25,20 @@ import { getLanguage, getListingPaginationAndFilter } from './utils/queryString'
2525
import { languages } from './components/LanguageSelector';
2626

2727
export 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"){

src/components/Header.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const useStyles = makeStyles((theme) => ({
2424
}
2525
}));
2626

27-
function Header({ asset, title, mainMenuActions }) {
27+
function Header({ asset, title, mainMenuActions, urlSlugs }) {
2828
const classes = useStyles();
2929

3030
return (
@@ -48,7 +48,7 @@ function Header({ asset, title, mainMenuActions }) {
4848
<div className={classes.mainMenu}>
4949
{mainMenuActions.map((navigationItem, index) =>
5050
<Action key={index} action={navigationItem} />)}
51-
<LanguageSelector/>
51+
<LanguageSelector urlSlugs={urlSlugs}/>
5252
</div>
5353
</Hidden>
5454
<Hidden mdUp>

src/components/LanguageSelector.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,19 @@ export const languages = [{
2121
}
2222
];
2323

24-
function LanguageSelector() {
24+
function LanguageSelector({urlSlugs}) {
2525
const history = useHistory();
2626
const classes = useStyles();
2727
const handleClick = ({target}) => {
28-
history.push(setLanguage(history.location, target.value));
28+
const index = history.location.pathname.lastIndexOf('/');
29+
const slug = urlSlugs.find(slug => slug.navigationItem.system.language.system.codename === target.value);
30+
if (!slug){
31+
history.push(setLanguage(history.location, target.value));
32+
}
33+
else {
34+
history.location.pathname = history.location.pathname.substring(0, index + 1) + slug.navigationItem.slug;
35+
history.push(setLanguage(history.location, target.value));
36+
}
2937
}
3038
return (
3139
<MuiSelect

src/components/Layout.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ function Layout(props) {
126126
<ThemeProvider theme={theme}>
127127
<CssBaseline />
128128
<Box display="flex" flexDirection="column" alignItems="stretch" alignContent="space-between" className={classes.root}>
129-
<Header {...props.siteConfiguration} />
129+
<Header {...props.siteConfiguration} urlSlugs={props.urlSlugs} />
130130
<main className={classes.flex}>
131131
{props.children}
132132
</main>

0 commit comments

Comments
 (0)