-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/update navigation file on creation of new collection and resource room #80
Feat/update navigation file on creation of new collection and resource room #80
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
left a comment
classes/Navigation.js
Outdated
const axios = require('axios'); | ||
const validateStatus = require('../utils/axios-utils') | ||
|
||
// Import logger | ||
const logger = require('../logger/logger'); | ||
|
||
// Import error | ||
const { NotFoundError } = require('../errors/NotFoundError') | ||
|
||
const GITHUB_ORG_NAME = process.env.GITHUB_ORG_NAME | ||
const BRANCH_REF = process.env.BRANCH_REF | ||
|
||
class Navigation { | ||
constructor(accessToken, siteName) { | ||
this.accessToken = accessToken | ||
this.siteName = siteName | ||
} | ||
|
||
async read() { | ||
try { | ||
const endpoint = `https://api.github.com/repos/${GITHUB_ORG_NAME}/${this.siteName}/contents/_data/navigation.yml` | ||
|
||
const params = { | ||
"ref": BRANCH_REF, | ||
} | ||
|
||
const resp = await axios.get(endpoint, { | ||
validateStatus, | ||
params, | ||
headers: { | ||
Authorization: `token ${this.accessToken}`, | ||
"Content-Type": "application/json" | ||
} | ||
}) | ||
|
||
if (resp.status === 404) throw new NotFoundError ('Navigation page does not exist') | ||
|
||
const { content, sha } = resp.data | ||
|
||
return { content, sha } | ||
|
||
} catch (err) { | ||
throw err | ||
} | ||
} | ||
|
||
async update(newContent, sha) { | ||
const endpoint = `https://api.github.com/repos/${GITHUB_ORG_NAME}/${this.siteName}/contents/_data/navigation.yml` | ||
|
||
const params = { | ||
"message": 'Edit navigation', | ||
"content": newContent, | ||
"branch": BRANCH_REF, | ||
"sha": sha | ||
} | ||
|
||
await axios.put(endpoint, params, { | ||
headers: { | ||
Authorization: `token ${this.accessToken}`, | ||
"Content-Type": "application/json" | ||
} | ||
}) | ||
} | ||
} | ||
|
||
module.exports = { Navigation } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually there's no need to create a Navigation
class, to prevent duplication of code, we can create a new File
and apply the existing DataType
type to retrieve the navigation.yml
file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This commit also fixes a bug with the existing delete endpoint where the wrong key was being retrieved for the filename.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm, left a nit
classes/Collection.js
Outdated
for (let i = 0; i < navContentObject.links.length; i++) { | ||
if (navContentObject.links[i].collection === collectionName) { | ||
navContentObject.links.splice(i,1) | ||
const newNavContent = base64.encode(yaml.safeDump(navContentObject)) | ||
await nav.update(NAV_FILE_NAME, newNavContent, navSha) | ||
break | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: generally it's safer to practice immutable programming (for a more detailed reason why: https://stackoverflow.com/questions/34385243/why-is-immutability-so-important-or-needed-in-javascript) so what we could do here, instead of unnaturally breaking a for loop, is:
const newNavLinks = navContentObject.links.filter(< filter out collection name>)
const newNavContentObject = {
...navContentObject,
links: newNavLinks,
}
Since the code here works and is performant I don't have a problem with it so I'll leave it up to you
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yeah this way makes more sense. I've changed this in both resourceroom and collections in fb58d14
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
…e room (#80) * Feat: create new Navigation class to load navigation file * Feat: add to navigation on creation of new collection * feat: add to navigation on creation of resource room * Fix: remove redundant permalink field in config file * Fix: use existing File to retrieve navigation instead * Fix: use existing File to retrieve nav file for resourceroom * Fix: remove unused class * Fix: update nav file when deleting and renaming collectionPages This commit also fixes a bug with the existing delete endpoint where the wrong key was being retrieved for the filename. * Feat: handle nav changes on deleting and renaming resource room * Fix: change for loop to filter/map instead
This PR adds new functionality to automatically add newly created collections and resource rooms to the navigation bar, and meant to partially address issue #234 on the isomercms-frontend repo. To be reviewed with PR #256 on the isomercms-frontend repo.
I merged the frontend PR too early - there's a small extra fix on the frontend involving deleting of folders, which is fixed in PR #257 instead