fix: Fix previw/delivery switch
This commit is contained in:
committed by
Benedikt Rötsch
parent
3f16c4531c
commit
f61c13d02e
3
app.js
3
app.js
@@ -14,6 +14,8 @@ const categories = require('./routes/categories')
|
|||||||
const about = require('./routes/about')
|
const about = require('./routes/about')
|
||||||
const settings = require('./routes/settings')
|
const settings = require('./routes/settings')
|
||||||
const sitemap = require('./routes/sitemap')
|
const sitemap = require('./routes/sitemap')
|
||||||
|
const lessons = require('./routes/lessons')
|
||||||
|
|
||||||
const { initClient, getSpace } = require('./services/contentful')
|
const { initClient, getSpace } = require('./services/contentful')
|
||||||
const breadcrumb = require('./lib/breadcrumb')
|
const breadcrumb = require('./lib/breadcrumb')
|
||||||
const app = express()
|
const app = express()
|
||||||
@@ -90,6 +92,7 @@ app.use(async function (req, res, next) {
|
|||||||
|
|
||||||
app.use('/', index)
|
app.use('/', index)
|
||||||
app.use('/courses', courses)
|
app.use('/courses', courses)
|
||||||
|
app.use('/lessons', lessons)
|
||||||
app.use('/categories', categories)
|
app.use('/categories', categories)
|
||||||
app.use('/about', about)
|
app.use('/about', about)
|
||||||
app.use('/settings', settings)
|
app.use('/settings', settings)
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
exports.catchErrors = (fn) => {
|
exports.catchErrors = (fn) => {
|
||||||
return function (req, res, next) {
|
return function (req, res, next) {
|
||||||
return fn(req, res, next).catch((e) => {
|
return fn(req, res, next).catch((e) => {
|
||||||
|
console.log(e)
|
||||||
next(e)
|
next(e)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
2
public
2
public
Submodule public updated: 632b2e22e9...941a9d81ce
@@ -5,7 +5,7 @@ const router = express.Router()
|
|||||||
|
|
||||||
/* GET the about landing page. */
|
/* GET the about landing page. */
|
||||||
router.get('/', catchErrors(async function (req, res, next) {
|
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 })
|
res.render('landingPage', { title: 'About', landingPage })
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
|||||||
@@ -8,8 +8,8 @@ router.get('/', catchErrors(async function (req, res, next) {
|
|||||||
// we get all the entries with the content type `course`
|
// we get all the entries with the content type `course`
|
||||||
let courses = []
|
let courses = []
|
||||||
let categories = []
|
let categories = []
|
||||||
courses = await getCourses(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.currentLocale.id)
|
categories = await getCategories(res.locals.currentLocale.code, res.locals.currentApi.id)
|
||||||
res.render('courses', { title: `All Courses (${courses.length})`, categories, courses })
|
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 {
|
try {
|
||||||
categories = await getCategories()
|
categories = await getCategories()
|
||||||
activeCategory = categories.find((category) => category.fields.slug === req.params.category)
|
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) {
|
} catch (e) {
|
||||||
console.log('Error ', e)
|
console.log('Error ', e)
|
||||||
}
|
}
|
||||||
@@ -31,7 +31,7 @@ router.get('/categories/:category', catchErrors(async function (req, res, next)
|
|||||||
|
|
||||||
/* GET course detail. */
|
/* GET course detail. */
|
||||||
const courseRoute = catchErrors(async function (req, res, next) {
|
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 lessons = course.fields.lessons
|
||||||
const lessonIndex = lessons.findIndex((lesson) => lesson.fields.slug === req.params.lslug)
|
const lessonIndex = lessons.findIndex((lesson) => lesson.fields.slug === req.params.lslug)
|
||||||
const lesson = lessons[lessonIndex]
|
const lesson = lessons[lessonIndex]
|
||||||
@@ -47,7 +47,7 @@ router.get('/:slug/lessons', courseRoute)
|
|||||||
|
|
||||||
/* GET course lesson detail. */
|
/* GET course lesson detail. */
|
||||||
router.get('/:cslug/lessons/:lslug', catchErrors(async function (req, res, next) {
|
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 lessons = course.fields.lessons
|
||||||
const lessonIndex = lessons.findIndex((lesson) => lesson.fields.slug === req.params.lslug)
|
const lessonIndex = lessons.findIndex((lesson) => lesson.fields.slug === req.params.lslug)
|
||||||
const lesson = lessons[lessonIndex]
|
const lesson = lessons[lessonIndex]
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ const router = express.Router()
|
|||||||
|
|
||||||
/* GET the home landing page. */
|
/* GET the home landing page. */
|
||||||
router.get('/', catchErrors(async function (req, res, next) {
|
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
|
let title = landingPage.fields.title
|
||||||
if (!title || landingPage.fields.slug === 'home') {
|
if (!title || landingPage.fields.slug === 'home') {
|
||||||
title = 'The Example App'
|
title = 'The Example App'
|
||||||
|
|||||||
@@ -3,8 +3,11 @@ const { createClient } = require('contentful')
|
|||||||
let cdaClient = null
|
let cdaClient = null
|
||||||
let cpaClient = null
|
let cpaClient = null
|
||||||
|
|
||||||
|
// Initialize our client
|
||||||
exports.initClient = (options) => {
|
exports.initClient = (options) => {
|
||||||
|
// Getting the version the app version
|
||||||
const { version } = require('../package.json')
|
const { version } = require('../package.json')
|
||||||
|
|
||||||
const config = options || {
|
const config = options || {
|
||||||
space: process.env.CF_SPACE,
|
space: process.env.CF_SPACE,
|
||||||
cda: process.env.CF_ACCESS_TOKEN,
|
cda: process.env.CF_ACCESS_TOKEN,
|
||||||
@@ -28,9 +31,9 @@ exports.getSpace = assert((api = `cda`) => {
|
|||||||
return client.getSpace()
|
return client.getSpace()
|
||||||
}, 'Space')
|
}, 'Space')
|
||||||
|
|
||||||
|
// 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`) => {
|
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
|
|
||||||
const client = api === 'cda' ? cdaClient : cpaClient
|
const client = api === 'cda' ? cdaClient : cpaClient
|
||||||
return client.getEntries({
|
return client.getEntries({
|
||||||
content_type: 'course',
|
content_type: 'course',
|
||||||
@@ -41,8 +44,8 @@ exports.getCourses = assert((locale = 'en-US', api = `cda`) => {
|
|||||||
.then((response) => response.items)
|
.then((response) => response.items)
|
||||||
}, 'Course')
|
}, 'Course')
|
||||||
|
|
||||||
|
// Landing pages like the home or about page are fully controlable via Contentful.
|
||||||
exports.getLandingPage = (slug, locale = 'en-US', api = `cda`) => {
|
exports.getLandingPage = (slug, locale = 'en-US', api = `cda`) => {
|
||||||
// Landing pages like the home or about page are fully controlable via Contentful.
|
|
||||||
const client = api === 'cda' ? cdaClient : cpaClient
|
const client = api === 'cda' ? cdaClient : cpaClient
|
||||||
return client.getEntries({
|
return client.getEntries({
|
||||||
content_type: 'landingPage',
|
content_type: 'landingPage',
|
||||||
@@ -53,10 +56,10 @@ exports.getLandingPage = (slug, locale = 'en-US', api = `cda`) => {
|
|||||||
.then((response) => response.items[0])
|
.then((response) => response.items[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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`) => {
|
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
|
|
||||||
const client = api === 'cda' ? cdaClient : cpaClient
|
const client = api === 'cda' ? cdaClient : cpaClient
|
||||||
return client.getEntries({
|
return client.getEntries({
|
||||||
content_type: 'course',
|
content_type: 'course',
|
||||||
@@ -73,6 +76,9 @@ exports.getCategories = assert((locale = 'en-US', api = `cda`) => {
|
|||||||
.then((response) => response.items)
|
.then((response) => response.items)
|
||||||
}, 'Course')
|
}, '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`) => {
|
exports.getCoursesByCategory = assert((category, locale = 'en-US', api = `cda`) => {
|
||||||
const client = api === 'cda' ? cdaClient : cpaClient
|
const client = api === 'cda' ? cdaClient : cpaClient
|
||||||
return client.getEntries({
|
return client.getEntries({
|
||||||
@@ -85,6 +91,7 @@ exports.getCoursesByCategory = assert((category, locale = 'en-US', api = `cda`)
|
|||||||
.then((response) => response.items)
|
.then((response) => response.items)
|
||||||
}, 'Category')
|
}, 'Category')
|
||||||
|
|
||||||
|
// Utitlities functions
|
||||||
function assert (fn, context) {
|
function assert (fn, context) {
|
||||||
return function (req, res, next) {
|
return function (req, res, next) {
|
||||||
return fn(req, res, next)
|
return fn(req, res, next)
|
||||||
|
|||||||
Reference in New Issue
Block a user