Skip to content

Instantly share code, notes, and snippets.

@gate5th
Created September 12, 2018 17:51
Show Gist options
  • Save gate5th/c91b2a6a3f78c3a8c358766e6a2531c0 to your computer and use it in GitHub Desktop.
Save gate5th/c91b2a6a3f78c3a8c358766e6a2531c0 to your computer and use it in GitHub Desktop.
oldschoolshuffle
// src/cosmicFunctions.js
import Cosmic from 'cosmicjs';
export async function getCosmicJsData() {
const api = Cosmic()
const bucket = api.bucket({
slug: process.env.REACT_APP_COSMIC_BUCKET
})
const arrayOfAllObjectsInBucket = (await bucket.getObjects()).objects;
return organizeCosmicJsDataByObjectType(arrayOfAllObjectsInBucket);
}
function organizeCosmicJsDataByObjectType(arrayOfCosmicJsData) {
//maps through array of objects, returns object with property for
//each slug-type, with the value being an array of the objects
let organizedData = {};
arrayOfCosmicJsData.map((object) => {
const typeSlug = object.type_slug;
//eslint-disable-next-line
organizedData.hasOwnProperty(typeSlug) ? '': organizedData[typeSlug] = [];
organizedData[typeSlug].push(object);
return organizedData;
})
return organizedData;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment