Last active
September 6, 2019 12:40
-
-
Save joelvarty/efcb1967900aa3a7009672fb3bf60e73 to your computer and use it in GitHub Desktop.
React Page Router
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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