Skip to content

Instantly share code, notes, and snippets.

@robcolburn
Last active February 5, 2016 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robcolburn/3655d8660de97cd64c2e to your computer and use it in GitHub Desktop.
Save robcolburn/3655d8660de97cd64c2e to your computer and use it in GitHub Desktop.
diff --git a/applications/responsive/ShowPage/ShowPage.js b/applications/responsive/ShowPage/ShowPage.js
index af588ae..0adbee3 100644
--- a/applications/responsive/ShowPage/ShowPage.js
+++ b/applications/responsive/ShowPage/ShowPage.js
@@ -13,7 +13,7 @@ import recordPageView from '../../../lib/recordPageView';
import {isClassic, isTonightShowClassic} from '../../../lib/getShowType';
import derivativeSrc from '../../../lib/derivativeSrc';
import derivativeSrcSet from '../../../lib/derivativeSrcSet';
-import {getRelationship} from '../../../utils/jsonAPIHelpers';
+import {getRelationship, getRelationships, getDeepRelationship} from '../../../utils/jsonAPIHelpers';
import Mixpanel from '../../../lib/analytics/Mixpanel';
import Guests from '../../../components/Guests/Guests';
@@ -433,24 +433,22 @@ ShowPage.queries = {
})
.then(payload => {
const show = payload.data.data;
- const credits = getRelationship(payload, show, 'credit') || [];
- const guests = getRelationship(payload, show, 'guests') || [];
- const genereticProperties = getRelationship(payload, show, 'genereticProperties');
+ const {credits, guests, genereticProperties, season, coverImageMobile, image} = getRelationships(payload, show);
return {
id: show.id,
...show.attributes,
- credits: credits.map(credit => ({
+ credits: (credits || []).map(credit => ({
...credit,
- image: getRelationship(payload, credit, 'person') ? derivativeSrc(getRelationship(payload, getRelationship(payload, credit, 'person'), 'image'), 'landscape.widescreen.size640.x1') : null
+ image: derivativeSrc(getDeepRelationship(payload, credit, 'person.image'), 'landscape.widescreen.size640.x1') || null
})),
- seasons: getRelationship(payload, show, 'season'),
- image: getRelationship(payload, show, 'coverImageMobile') || getRelationship(payload, show, 'image'),
+ seasons: season,
+ image: coverImageMobile || image,
imageSet: (
- derivativeSrcSet(getRelationship(payload, show, 'coverImageMobile'), [
+ derivativeSrcSet(coverImageMobile, [
'landscape.widescreen.size640.x1',
'landscape.widescreen.size640.x2'
])
- || derivativeSrcSet(getRelationship(payload, show, 'image'), [
+ || derivativeSrcSet(image, [
'landscape.widescreen.size640.x1',
'landscape.widescreen.size640.x2'
])
@@ -466,19 +464,14 @@ ShowPage.queries = {
)
// Prefix human-readable
.map(seasonNumber => `Season ${seasonNumber}`),
- guests: guests
+ guests: (guests || [])
.map(guest => guest.attributes && guest.attributes.guest && ({...guest.attributes}))
.filter(guest => guest !== null),
collections: (() => {
// Check for a showCollection under Generetic Properties.
- const genereticCollection = genereticProperties && getRelationship(payload, genereticProperties, 'showCollection');
+ const genereticCollections = genereticProperties && getDeepRelationship(payload, genereticProperties, 'showCollection.collections');
// Use the generetic collection if it's not empty, otherwise get the master collection from the show.
- const showCollection = genereticCollection || getRelationship(payload, show, 'showCollection');
- if (!showCollection) {
- return null;
- }
- // Get the array of collections within the master collection.
- const collections = getRelationship(payload, showCollection, 'collections');
+ const collections = genereticCollections || getDeepRelationship(payload, show, 'showCollection.collections');
if (!collections) {
return null;
}
diff --git a/test/Util/jsonAPIHelpers.js b/test/Util/jsonAPIHelpers.js
index 7196e26..3b4f1a4 100644
--- a/test/Util/jsonAPIHelpers.js
+++ b/test/Util/jsonAPIHelpers.js
@@ -1,5 +1,10 @@
import {expect} from 'chai';
-import {getIncluded, getRelationship} from '../../utils/jsonAPIHelpers';
+import {
+ getIncluded,
+ getRelationship,
+ getRelationships,
+ getDeepRelationship
+} from '../../utils/jsonAPIHelpers';
describe('jsonAPIHelpers', () => {
describe('getIncluded', () => {
@@ -68,4 +73,82 @@ describe('jsonAPIHelpers', () => {
expect(rel[1].type).to.equal('tests');
});
});
+
+ describe('getRelationships', () => {
+ const payload = {
+ data: {
+ included: [
+ {type: 'cats', id: '1', name: 'kitty'},
+ {type: 'dogs', id: '2', name: 'sam'}
+ ]
+ }
+ };
+ it('should get a set of relationships', () => {
+ const resource = {
+ relationships: {
+ cat: {
+ data: {type: 'cats', id: '1'}
+ },
+ dog: {
+ data: {type: 'dogs', id: '2'}
+ }
+ }
+ };
+
+ const rels = getRelationships(payload, resource);
+
+ expect(rels.cat.name).to.equal('kitty');
+ expect(rels.dog.name).to.equal('sam');
+ });
+ });
+
+ describe('getDeepRelationship', () => {
+ const payload = {
+ data: {
+ included: [
+ {type: 'cats', id: '1', name: 'kitty',
+ relationships: {
+ bird: {
+ data: {type: 'birds', id: '2'}
+ }
+ }
+ },
+ {type: 'birds', id: '2', name: 'jay',
+ relationships: {
+ worm: {
+ data: {type: 'worms', id: '3'}
+ }
+ }
+ },
+ {type: 'worms', id: '3', name: 'jim'}
+ ]
+ }
+ };
+ it('should get a deeply nested of relationship', () => {
+ const resource = {
+ relationships: {
+ cat: {
+ data: {type: 'cats', id: '1'}
+ }
+ }
+ };
+
+ const rel = getDeepRelationship(payload, resource, 'cat.bird.worm');
+
+ expect(rel.name).to.equal('jim');
+ });
+ it('should get undefined for non-existent relationship', () => {
+ const resource = {
+ relationships: {
+ cat: {
+ data: {type: 'cats', id: '1'}
+ }
+ }
+ };
+
+ const rel = getDeepRelationship(payload, resource, 'cat.dog');
+
+ expect(rel).to.be.undefined;
+ });
+ });
});
diff --git a/utils/jsonAPIHelpers.js b/utils/jsonAPIHelpers.js
index ded4058..50829eb 100644
--- a/utils/jsonAPIHelpers.js
+++ b/utils/jsonAPIHelpers.js
@@ -33,7 +33,7 @@ export function getIncluded(payload, type, id) {
* @param {Object} resource
* The resource to find included data for
* @param {String} key
- * The type of included data to retrieve
+ * The relationship to included data to retrieve
*
* @return {Array|Object|undefined}
* The found included data object(s) or undefined if the requested included
@@ -58,3 +58,45 @@ export function getRelationship(payload, resource, key) {
return ret;
}
+
+/**
+ * Fetches specified included data associated to a resource
+ *
+ * @param {Object} payload
+ * The payload encapsulating the included data
+ * @param {Object} resource
+ * The resource to find included data for
+ *
+ * @return {Object}
+ * The found included data object(s) keyed by relationships.
+ */
+export function getRelationships(payload, resource) {
+ return Object.keys(resource.relationships).reduce((result, key) => {
+ result[key] = getRelationship(payload, resource, key);
+ return result;
+ }, {});
+}
+
+/**
+ * Fetches specified related data associated to a resource,
+ * and related data assocociated to that included resource...
+ *
+ * @param {Object} payload
+ * The payload encapsulating the included data
+ * @param {Object} parentResource
+ * The first resource to find included data for
+ * @param {String} deepKey
+ * The relationship to included data to retrieve, delimited by periods.
+ *
+ * @return {Array|Object|undefined}
+ * The found included data object(s) or undefined if the requested included
+ * data is not part of the API response
+ *
+ * @throws {Error}
+ * Thrown if the requested relationships is not part of the included payload
+ */
+export function getDeepRelationship(payload, parentResource, deepKey) {
+ return deepKey.split('.').reduce((subResource, key) =>
+ subResource && getRelationship(payload, subResource, key)
+ , parentResource);
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment