From 354df3c4b4f7937b0a0ef4a5258abc1272662bb9 Mon Sep 17 00:00:00 2001 From: Khaled Garbaya Date: Mon, 25 Sep 2017 12:53:19 +0200 Subject: [PATCH] feat(settings): Save settings in cookie --- public/stylesheets/style.css | 16 ++++++++++++++++ routes/settings.js | 13 +++++++++++-- services/contentful.js | 4 +++- views/settings.pug | 9 ++++++++- 4 files changed, 38 insertions(+), 4 deletions(-) diff --git a/public/stylesheets/style.css b/public/stylesheets/style.css index 3664242..96fea31 100644 --- a/public/stylesheets/style.css +++ b/public/stylesheets/style.css @@ -113,6 +113,22 @@ nav ul li:last-child { nav ul li a { transform: skew(15deg); } +/* Form */ +input, textarea { + width:100%; + padding:10px; + border: 1px solid $grey; +} + +label { + padding: 10px 0; + display: block; +} +.form { + background: white; + padding: 2rem; + box-shadow: $grad; +} /* Home Page __ hero*/ .hero { diff --git a/routes/settings.js b/routes/settings.js index 40fd713..c94d002 100644 --- a/routes/settings.js +++ b/routes/settings.js @@ -1,9 +1,18 @@ const express = require('express') const router = express.Router() -/* GET seeting page. */ +/* GET settings page. */ router.get('/', function (req, res, next) { - res.render('settings', { title: 'Settings' }) + const cookie = req.cookies.universitySettings + const settings = cookie || { cpa: '', cda: '', space: '' } + res.render('settings', { title: 'Settings', settings }) +}) + +/* POST settings page. */ +router.post('/', function (req, res, next) { + const settings = {space: req.body.space, cda: req.body.cda, cpa: req.body.cpa} + res.cookie('universitySettings', settings, { maxAge: 900000, httpOnly: true }) + res.render('settings', settings) }) module.exports = router diff --git a/services/contentful.js b/services/contentful.js index ecbf71b..3db4c8e 100644 --- a/services/contentful.js +++ b/services/contentful.js @@ -3,7 +3,9 @@ const { createClient } = require('contentful') const client = createClient({space: process.env.CF_SPACE, accessToken: process.env.CF_ACCESS_TOKEN}) export function getCourses () { - // TODO + // to get all the courses we simply request from Contentful all the entries + // with the content_type `course` + return client.getEntries({content_type: 'course'}) } export function getLessons (courseId) { diff --git a/views/settings.pug b/views/settings.pug index 3d63b9a..737d548 100644 --- a/views/settings.pug +++ b/views/settings.pug @@ -2,4 +2,11 @@ extends layout block content h1= title - p Welcome to #{title} + form(action=`/settings` method="POST" class="form") + label(for="space") Space + input(type="text" name="space" value=settings.space) + label(for="cda") Delivery API Key + input(type="text" name="cda" value=settings.cda) + label(for="cpa") Preview API Key + input(type="text" name="cpa" value=settings.cpa) + input(type="submit" value="Save →" class="button")