Skip to content

Commit

Permalink
GH-191: Allow loading CMS data via CMS script
Browse files Browse the repository at this point in the history
So that Portal and Docs need not reinvent the wheel.
  • Loading branch information
wesleyboar committed Apr 15, 2021
1 parent 793e10a commit 67149c2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
32 changes: 32 additions & 0 deletions taccsite_cms/static/site_cms/js/modules/importData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Return data from an endpoint
* @param {string} dataURL - The URL from which to get the data
* @param {('html'|'json'|'text')} dataType - The type of data to retrieve
* @returns {Promise<string>} Promise that returns markup if resolved
*/
export function getFromURL(dataURL, dataType) {
const isValidURL = (dataURL && typeof dataURL === 'string');

if (isValidURL) {
return fetch(dataURL).then(response => {
let data;

switch (dataType) {
case 'json':
data = response.json();
break;
case 'html':
case 'text':
default:
data = response.text();
break;
}

return data;
}).catch(err => {
console.error(err);
});
} else {
return Promise.reject(new Error('Invalid URL provided'));
}
}
13 changes: 3 additions & 10 deletions taccsite_cms/static/site_cms/js/modules/importHTML.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getFromURL as getDataFromURL } from './importData.js';

/**
* Create element from HTML string
* @param {String} - HTML representing a single element
Expand Down Expand Up @@ -29,16 +31,7 @@ export function htmlToElements(html) {
* @returns {Promise<string>} Promise that returns markup if resolved
*/
export function getFromURL(markupURL) {
if (markupURL) {
return fetch(markupURL).then(response => {
const markup = response.text();
return markup;
}).catch(err => {
console.error(err);
});
} else {
return Promise.reject(new Error('Invalid `markupURL` provided'));
}
return getDataFromURL(markupURL, 'html');
}

/**
Expand Down
1 change: 1 addition & 0 deletions taccsite_cms/static/static

0 comments on commit 67149c2

Please sign in to comment.