Created
September 12, 2018 17:51
-
-
Save gate5th/c91b2a6a3f78c3a8c358766e6a2531c0 to your computer and use it in GitHub Desktop.
oldschoolshuffle
This file contains 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
// 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