Created
February 12, 2019 08:40
-
-
Save klummy/80c5bbdd8c364d3cefdcacdedeebac62 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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