Created
March 15, 2018 09:48
-
-
Save craigbilner/bf7d8d9eb1134c52c4702e31114edc22 to your computer and use it in GitHub Desktop.
functional approach to data decorattion
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
import getLeadAsset from "./get-lead-asset"; | |
const prepend = ({ type, data }, list) => { | |
if (!data) { | |
return list; | |
} | |
return [{ type, data }, ...list]; | |
}; | |
const append = ({ type, data }, list) => { | |
if (!data) { | |
return list; | |
} | |
return [...list, { type, data }]; | |
}; | |
const prepareDataForListView = articleData => { | |
const { isVideo, leadAsset } = getLeadAsset(articleData); | |
const articleHeaderData = { | |
label: articleData.label, | |
headline: articleData.headline, | |
standfirst: articleData.standfirst, | |
flags: articleData.flags, | |
isVideo | |
}; | |
const articleMidContainerData = { | |
publicationName: articleData.publicationName, | |
publishedTime: articleData.publishedTime, | |
byline: articleData.byline | |
}; | |
const relatedArticlesData = articleData.relatedArticles | |
? { | |
relatedArticles: articleData.relatedArticles, | |
relatedArticlesLayout: articleData.relatedArticlesLayout | |
} | |
: null; | |
const data = [ | |
{ type: "header", data: articleHeaderData }, | |
{ type: "middleContainer", data: articleMidContainerData }, | |
...articleData.content.map((rowData, index) => { | |
const item = { | |
type: "articleBodyRow", | |
data: Object.assign({}, rowData), | |
index | |
}; | |
if (rowData.name === "ad") { | |
item.data.attributes = { | |
...item.data.attributes, | |
...{ section: articleData.section } | |
}; | |
} | |
return item; | |
}) | |
]; | |
return prepend( | |
{ type: "leadAsset", data: leadAsset }, | |
append({ type: "relatedArticles", data: relatedArticlesData }, data) | |
); | |
}; | |
export default prepareDataForListView; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment