Skip to content

Commit

Permalink
[Feat] Create settings route and class (#48)
Browse files Browse the repository at this point in the history
* feat: add settings router to server file

* feat: add OtherType file type to File class

* feat: add GET settings route

* feat: Create a separate Settings class

* feat: Create a new POST method for settings route

* fix: defer content decoding to frontend

* refactor: remove unnecessary file type in File class

* refactor: general cleanup

* fix: retintroduce yaml package import

* refactor: rename variables for clarity

* feat: send and receive only fields that the frontend requires

This commit limits the response being sent to the frontend to the
relevant fields (title, favicon, resources_name, colors) from the
config.yml file.

It also modifies the code to receive these same fields and make
updates to the config file as opposed to rewriting it completely.

This commit also switches out the base64 library for the js-base64
library for the Settings file as we had some problems with
encoding/decoding with this library in the past

* feat: remove transmission of config sha to frontend
  • Loading branch information
kwajiehao authored Dec 18, 2019
1 parent e00503a commit a4fdfaa
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 3 deletions.
4 changes: 2 additions & 2 deletions classes/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ class Config {
"sha": sha
}

await axios.put(endpoint, params, {
await axios.put(endpoint, params, {
headers: {
Authorization: `token ${this.accessToken}`,
"Content-Type": "application/json"
}
})
} catch (err) {
throw err
console.log(err)
}
}
}
Expand Down
1 change: 0 additions & 1 deletion classes/File.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,5 +223,4 @@ class HomepageType {
}
}


module.exports = { File, PageType, CollectionPageType, ResourcePageType, ResourceType, ImageType, DocumentType, DataType, HomepageType }
89 changes: 89 additions & 0 deletions classes/Settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
const { Base64 } = require('js-base64')
const yaml = require('js-yaml')

// import classes
const { Config } = require('../classes/Config.js')
const { File, DataType } = require('../classes/File.js')

class Settings {
constructor(accessToken, siteName) {
this.accessToken = accessToken
this.siteName = siteName
}

async get() {
try {
// retrieve _config.yml and social-media.yml
const configResp = new Config(this.accessToken, this.siteName)
const IsomerDataFile = new File(this.accessToken, this.siteName)
const dataType = new DataType()
IsomerDataFile.setFileType(dataType)

const { content: config } = await configResp.read()
const socialMediaResp = IsomerDataFile.read('social-media.yml').catch((err) => {
// social-media.yml doesn't exist so we create a social-media.yml
const content = {
facebook: '',
linkedin: '',
twitter: '',
youtube: '',
instagram: '',
}
const socialMediaYml = Base64.encode(yaml.safeDump(content))
const { sha } = IsomerDataFile.create('social-media.yml', socialMediaYml)
return { content, sha }
})
const { content: socialMedia, sha: socialMediaSha } = await socialMediaResp

// convert data to object form
const configContent = yaml.safeLoad(Base64.decode(config));
const socialMediaContent = yaml.safeLoad(Base64.decode(socialMedia));

// retrieve only the relevant config fields
const configFieldsRequired = {
title: configContent.title,
favicon: configContent.favicon,
resources_name: configContent.resources_name,
colors: configContent.colors,

}

return ({ configFieldsRequired, socialMediaContent, socialMediaSha })
} catch (err) {
console.log(err)
}
}

async post(payload) {
try {
// setup
const configResp = new Config(this.accessToken, this.siteName)
const config = await configResp.read()
const IsomerDataFile = new File(this.accessToken, this.siteName)
const dataType = new DataType()
IsomerDataFile.setFileType(dataType)

// extract data
const {
socialMediaSettings,
configSettings,
socialMediaSha,
} = payload

// update config object
const configContent = yaml.safeLoad(Base64.decode(config.content));
Object.keys(configSettings).forEach((setting) => (configContent[setting] = configSettings[setting]));

// update files
const newConfigContent = Base64.encode(yaml.safeDump(configContent))
const newSocialMediaContent = Base64.encode(yaml.safeDump(socialMediaSettings))
await configResp.update(newConfigContent, config.sha)
await IsomerDataFile.update('social-media.yml', newSocialMediaContent, socialMediaSha)
return
} catch (err) {
console.log(err)
}
}
}

module.exports = { Settings }
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"dotenv": "^8.1.0",
"express": "~4.16.1",
"http-errors": "~1.6.3",
"js-base64": "^2.5.1",
"js-yaml": "^3.13.1",
"jsonwebtoken": "^8.5.1",
"lodash": "^4.17.15",
Expand Down
38 changes: 38 additions & 0 deletions routes/settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const express = require('express');
const router = express.Router();
const _ = require('lodash')
const jwtUtils = require('../utils/jwt-utils')

// Import Classes
const { Settings } = require('../classes/Settings.js')

router.get('/:siteName/settings', async function(req, res, next) {
try {
const { oauthtoken } = req.cookies
let { access_token } = jwtUtils.verifyToken(oauthtoken)
const { siteName } = req.params

const settingsFile = new Settings(access_token, siteName)
const settings = await settingsFile.get()
res.status(200).json({ settings })
} catch (err) {
console.log(err)
}
})

router.post('/:siteName/settings', async function(req, res, next) {
try {
const { oauthtoken } = req.cookies
let { access_token } = jwtUtils.verifyToken(oauthtoken)
const { siteName } = req.params

const settings = new Settings(access_token, siteName)
await settings.post(req.body)
res.status(200).send('OK')
} catch (err) {
console.log(err)
}
})


module.exports = router;
2 changes: 2 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const documentsRouter = require('./routes/documents')
const menuRouter = require('./routes/menus')
const homepageRouter = require('./routes/homepage')
const menuDirectoryRouter = require('./routes/menuDirectory')
const settingsRouter = require('./routes/settings')

const app = express();

Expand Down Expand Up @@ -52,6 +53,7 @@ app.use('/sites', documentsRouter)
app.use('/sites', menuRouter)
app.use('/sites', homepageRouter)
app.use('/sites', menuDirectoryRouter)
app.use('/sites', settingsRouter)

// catch 404 and forward to error handler
app.use(function(req, res, next) {
Expand Down

0 comments on commit a4fdfaa

Please sign in to comment.