Skip to content

Instantly share code, notes, and snippets.

@klummy
Created February 12, 2019 08:40
Show Gist options
  • Select an option

  • Save klummy/80c5bbdd8c364d3cefdcacdedeebac62 to your computer and use it in GitHub Desktop.

Select an option

Save klummy/80c5bbdd8c364d3cefdcacdedeebac62 to your computer and use it in GitHub Desktop.
const mockContent = {
about: '...',
generic: '...',
home: '...',
termsAndConditions: '...',
}
/**
* Handles getting content from the content service
* @param {string} contentID Required: the ID for the content
* @param {options} options Additional options for the service
* @returns {Promise} A promise containing the content data
*/
const fetchFromContentServiceApi = (contentID, options) => {
return new Promise((resolve, reject) => {
const content = mockContent[contentID]
if (content) {
resolve(content)
return
}
reject(new Error('Content not found, use \'generic\' as the contentID to get filler, generic text'))
});
}
/**
* Get content from a described content service
* @param {string} contentID Required: the ID for the content
* @param {options} options Additional options for the service
* @returns {Promise} A promise containing the content data
*/
export const getContent = (contentID, options) => {
return new Promise((resolve, reject) => {
if (!contentID) {
reject(new Error('Content ID is required'))
return
}
fetchFromContentServiceApi(contentID)
.then((response) => {
resolve(response)
}).catch((err) => {
reject(err)
});
});
}
// Sample usage:
const MySamplePage = async () => {
const pageContent = await getContent('generic')
return (
<div>
{pageContent}
</div>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment