From ae599ccdfea1502c9c987394ab7703935391e771 Mon Sep 17 00:00:00 2001 From: David Litvak Bruno Date: Thu, 9 Nov 2017 11:24:07 +0100 Subject: [PATCH] Refactor entry state toggle (#48) * chore(editorialFeatures): extract editorial features toggle * chore(refactor): rename function to make more contextual sense --- lib/should-attach-entry-state.js | 3 +++ routes/courses.js | 7 ++++--- routes/landingPage.js | 5 +++-- 3 files changed, 10 insertions(+), 5 deletions(-) create mode 100644 lib/should-attach-entry-state.js diff --git a/lib/should-attach-entry-state.js b/lib/should-attach-entry-state.js new file mode 100644 index 0000000..c0cd8bd --- /dev/null +++ b/lib/should-attach-entry-state.js @@ -0,0 +1,3 @@ +module.exports = (response) => { + return response.locals.settings.editorialFeatures && response.locals.currentApi.id === 'cpa' +} diff --git a/routes/courses.js b/routes/courses.js index b670ce2..37dd244 100644 --- a/routes/courses.js +++ b/routes/courses.js @@ -11,6 +11,7 @@ const { const attachEntryState = require('../lib/entry-state') const enhanceBreadcrumb = require('../lib/enhance-breadcrumb') +const shouldAttachEntryState = require('../lib/should-attach-entry-state') const { updateCookie } = require('../lib/cookies') const { translate } = require('../i18n/i18n') @@ -30,7 +31,7 @@ module.exports.getCourses = async (request, response, next) => { courses = await getCourses(response.locals.currentLocale.code, response.locals.currentApi.id) // Attach entry state flags when using preview API - if (response.locals.settings.editorialFeatures && response.locals.currentApi.id === 'cpa') { + if (shouldAttachEntryState(response)) { courses = await Promise.all(courses.map(attachEntryState)) } @@ -66,7 +67,7 @@ module.exports.getCourse = async (request, response, next) => { updateCookie(response, 'visitedLessons', visitedLessons) // Attach entry state flags when using preview API - if (response.locals.settings.editorialFeatures && response.locals.currentApi.id === 'cpa') { + if (shouldAttachEntryState(response)) { course = await attachEntryState(course) } @@ -127,7 +128,7 @@ module.exports.getLesson = async (request, response, next) => { updateCookie(response, 'visitedLessons', visitedLessons) // Attach entry state flags when using preview API - if (response.locals.settings.editorialFeatures && response.locals.currentApi.id === 'cpa') { + if (shouldAttachEntryState(response)) { lesson = await attachEntryState(lesson) } diff --git a/routes/landingPage.js b/routes/landingPage.js index 9a1a408..bce1e2e 100644 --- a/routes/landingPage.js +++ b/routes/landingPage.js @@ -5,7 +5,8 @@ const url = require('url') const { getLandingPage } = require('../services/contentful') -const attachEntryState = require('./../lib/entry-state') +const attachEntryState = require('../lib/entry-state') +const shouldAttachEntryState = require('../lib/should-attach-entry-state') /** * Renders a landing page when `/` route is requested @@ -27,7 +28,7 @@ module.exports.getLandingPage = async (request, response, next) => { ) // Attach entry state flags when using preview API - if (response.locals.settings.editorialFeatures && response.locals.currentApi.id === 'cpa') { + if (shouldAttachEntryState(response)) { landingPage = await attachEntryState(landingPage) }