Skip to content

Instantly share code, notes, and snippets.

@joelvarty
Last active September 6, 2019 12:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelvarty/efcb1967900aa3a7009672fb3bf60e73 to your computer and use it in GitHub Desktop.
Save joelvarty/efcb1967900aa3a7009672fb3bf60e73 to your computer and use it in GitHub Desktop.
React Page Router
async routePage() {
const api = this.props.agility.client;
try {
//get the sitemap route table
let sitemap = await this.getSitemap(api);
//get the path from the browser
const path = document.location.pathname.toLowerCase();
let pageInSitemap = sitemap[path];
//if we are on the homepage, get the first route
if (path === '/') {
let firstPagePathInSitemap = Object.keys(sitemap)[0];
pageInSitemap = sitemap[firstPagePathInSitemap];
}
//only proceed if this path is in the table
if (pageInSitemap) {
//get the actual page object
const page = await api.getPage({
pageID: pageInSitemap.pageID,
languageCode: this.props.agility.config.languageCode
});
//set this page in our state object
this.setPage(page, pageInSitemap);
} else {
//Could not find page
this.pageNotFound();
}
} catch (error) {
//Throw error
this.handleError('error getting sitemap :(', error);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment