@keditor/components
Version:
A library of generic web components that are accessible, framework agnostic, possible to style, and easy to use with data provided by Contentful
43 lines (39 loc) • 2.05 kB
JavaScript
import get from "lodash/get";
// eslint-disable-next-line class-methods-use-this
export async function getEntryData(entryId, accesstoken, spaceId, environmentId){
await new Promise(r => setTimeout(r, 3000));
const entryIdData = await fetch(`https://cdn.contentful.com/spaces/${spaceId}/environments/${environmentId}/entries/${entryId}?access_token=${accesstoken}`);
const entry = await entryIdData.json()
// eslint-disable-next-line dot-notation
entry.fields['ContentType'] = get(entry,"sys.contentType.sys.id") ? get(entry,"sys.contentType.sys.id") : "empty";
return entry.fields;
}
// eslint-disable-next-line class-methods-use-this
export async function getAssetForId(entryId, accesstoken, spaceId, environmentId){
await new Promise(r => setTimeout(r, 3000));
const getAssetForIdData = await fetch(`https://cdn.contentful.com/spaces/${spaceId}/environments/${environmentId}/assets/${entryId}?access_token=${accesstoken}`);
const resp = await getAssetForIdData.json()
return get(resp, "fields.file.url");
}
// eslint-disable-next-line class-methods-use-this
export async function getHeroData(heroData, accesstoken, spaceId, environmentId){
const heroDataArr = await Promise.all(heroData && heroData.map(async (imgData) => {
const id = get(imgData, 'sys.id');
const obj = {};
obj[id] = await getEntryData(id, accesstoken, spaceId, environmentId);
const asset = await getAssetForId(get(obj[id], "image.sys.id"), accesstoken, spaceId, environmentId);
obj[id].imageUrl = (asset !== null) ? asset : 'default.jpg';
await new Promise(r => setTimeout(r, 3000));
return obj
}));
return heroDataArr;
}
export async function getAssetForSlider(imageObj, accesstoken, spaceId, environmentId){
const sliderData = await Promise.all(imageObj && imageObj.map(async (imgData) => {
const idData = get(imgData, 'sys.id');
const dataSet = await getAssetForId(idData, accesstoken, spaceId, environmentId)
await new Promise(r => setTimeout(r, 3000));
return dataSet;
}));
return sliderData;
}