fix: Fix previw/delivery switch

This commit is contained in:
Khaled Garbaya
2017-10-16 15:52:01 +02:00
committed by Benedikt Rötsch
parent 3f16c4531c
commit f61c13d02e
7 changed files with 25 additions and 14 deletions

3
app.js
View File

@@ -14,6 +14,8 @@ const categories = require('./routes/categories')
const about = require('./routes/about')
const settings = require('./routes/settings')
const sitemap = require('./routes/sitemap')
const lessons = require('./routes/lessons')
const { initClient, getSpace } = require('./services/contentful')
const breadcrumb = require('./lib/breadcrumb')
const app = express()
@@ -90,6 +92,7 @@ app.use(async function (req, res, next) {
app.use('/', index)
app.use('/courses', courses)
app.use('/lessons', lessons)
app.use('/categories', categories)
app.use('/about', about)
app.use('/settings', settings)

View File

@@ -8,6 +8,7 @@
exports.catchErrors = (fn) => {
return function (req, res, next) {
return fn(req, res, next).catch((e) => {
console.log(e)
next(e)
})
}

2
public

Submodule public updated: 632b2e22e9...941a9d81ce

View File

@@ -5,7 +5,7 @@ const router = express.Router()
/* GET the about landing page. */
router.get('/', catchErrors(async function (req, res, next) {
const landingPage = await getLandingPage('about', res.locals.currentLocale.code, res.locals.currentLocale.id)
const landingPage = await getLandingPage('about', res.locals.currentLocale.code, res.locals.currentApi.id)
res.render('landingPage', { title: 'About', landingPage })
}))

View File

@@ -8,8 +8,8 @@ router.get('/', catchErrors(async function (req, res, next) {
// we get all the entries with the content type `course`
let courses = []
let categories = []
courses = await getCourses(res.locals.currentLocale.code, res.locals.currentLocale.id)
categories = await getCategories(res.locals.currentLocale.code, res.locals.currentLocale.id)
courses = await getCourses(res.locals.currentLocale.code, res.locals.currentApi.id)
categories = await getCategories(res.locals.currentLocale.code, res.locals.currentApi.id)
res.render('courses', { title: `All Courses (${courses.length})`, categories, courses })
}))
@@ -22,7 +22,7 @@ router.get('/categories/:category', catchErrors(async function (req, res, next)
try {
categories = await getCategories()
activeCategory = categories.find((category) => category.fields.slug === req.params.category)
courses = await getCoursesByCategory(activeCategory.sys.id, res.locals.currentLocale.code, res.locals.currentLocale.id)
courses = await getCoursesByCategory(activeCategory.sys.id, res.locals.currentLocale.code, res.locals.currentApi.id)
} catch (e) {
console.log('Error ', e)
}
@@ -31,7 +31,7 @@ router.get('/categories/:category', catchErrors(async function (req, res, next)
/* GET course detail. */
const courseRoute = catchErrors(async function (req, res, next) {
let course = await getCourse(req.params.slug, res.locals.currentLocale.code, res.locals.currentLocale.id)
let course = await getCourse(req.params.slug, res.locals.currentLocale.code, res.locals.currentApi.id)
const lessons = course.fields.lessons
const lessonIndex = lessons.findIndex((lesson) => lesson.fields.slug === req.params.lslug)
const lesson = lessons[lessonIndex]
@@ -47,7 +47,7 @@ router.get('/:slug/lessons', courseRoute)
/* GET course lesson detail. */
router.get('/:cslug/lessons/:lslug', catchErrors(async function (req, res, next) {
let course = await getCourse(req.params.cslug, res.locals.currentLocale.code, res.locals.currentLocale.id)
let course = await getCourse(req.params.cslug, res.locals.currentLocale.code, res.locals.currentApi.id)
const lessons = course.fields.lessons
const lessonIndex = lessons.findIndex((lesson) => lesson.fields.slug === req.params.lslug)
const lesson = lessons[lessonIndex]

View File

@@ -5,7 +5,7 @@ const router = express.Router()
/* GET the home landing page. */
router.get('/', catchErrors(async function (req, res, next) {
const landingPage = await getLandingPage('home', res.locals.currentLocale.code, res.locals.currentLocale.id)
const landingPage = await getLandingPage('home', res.locals.currentLocale.code, res.locals.currentApi.id)
let title = landingPage.fields.title
if (!title || landingPage.fields.slug === 'home') {
title = 'The Example App'

View File

@@ -3,8 +3,11 @@ const { createClient } = require('contentful')
let cdaClient = null
let cpaClient = null
// Initialize our client
exports.initClient = (options) => {
// Getting the version the app version
const { version } = require('../package.json')
const config = options || {
space: process.env.CF_SPACE,
cda: process.env.CF_ACCESS_TOKEN,
@@ -28,9 +31,9 @@ exports.getSpace = assert((api = `cda`) => {
return client.getSpace()
}, 'Space')
exports.getCourses = assert((locale = 'en-US', api = `cda`) => {
// to get all the courses we request all the entries
// with the content_type `course` from Contentful
exports.getCourses = assert((locale = 'en-US', api = `cda`) => {
const client = api === 'cda' ? cdaClient : cpaClient
return client.getEntries({
content_type: 'course',
@@ -41,8 +44,8 @@ exports.getCourses = assert((locale = 'en-US', api = `cda`) => {
.then((response) => response.items)
}, 'Course')
exports.getLandingPage = (slug, locale = 'en-US', api = `cda`) => {
// Landing pages like the home or about page are fully controlable via Contentful.
exports.getLandingPage = (slug, locale = 'en-US', api = `cda`) => {
const client = api === 'cda' ? cdaClient : cpaClient
return client.getEntries({
content_type: 'landingPage',
@@ -53,10 +56,10 @@ exports.getLandingPage = (slug, locale = 'en-US', api = `cda`) => {
.then((response) => response.items[0])
}
exports.getCourse = assert((slug, locale = 'en-US', api = `cda`) => {
// the SDK supports link resolution only when you call the collection endpoints
// That's why we are using getEntries with a query instead of getEntry(entryId)
// make sure to specify the content_type whenever you want to perform a query
exports.getCourse = assert((slug, locale = 'en-US', api = `cda`) => {
const client = api === 'cda' ? cdaClient : cpaClient
return client.getEntries({
content_type: 'course',
@@ -73,6 +76,9 @@ exports.getCategories = assert((locale = 'en-US', api = `cda`) => {
.then((response) => response.items)
}, 'Course')
// Getting a course by Category is simply querying all entries
// with a query params `fields.categories.sys.id` equal to the desired category id
// Note that you need to send the `content_type` param to be able to query the entry
exports.getCoursesByCategory = assert((category, locale = 'en-US', api = `cda`) => {
const client = api === 'cda' ? cdaClient : cpaClient
return client.getEntries({
@@ -85,6 +91,7 @@ exports.getCoursesByCategory = assert((category, locale = 'en-US', api = `cda`)
.then((response) => response.items)
}, 'Category')
// Utitlities functions
function assert (fn, context) {
return function (req, res, next) {
return fn(req, res, next)