Skip to content
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

Conversation

alexanderleegs
Copy link
Contributor

@alexanderleegs alexanderleegs commented Nov 23, 2020

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

Copy link
Contributor

@kwajiehao kwajiehao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left a comment

Comment on lines 1 to 66
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 }
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, resolved in e9c5760, 4a0b24c, a1c294d

kwajiehao
kwajiehao previously approved these changes Nov 25, 2020
Copy link
Contributor

@kwajiehao kwajiehao left a 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

Comment on lines 83 to 90
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
}
}
Copy link
Contributor

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

Copy link
Contributor Author

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

Copy link
Contributor

@kwajiehao kwajiehao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@alexanderleegs alexanderleegs merged commit 1810c35 into staging Nov 25, 2020
@alexanderleegs alexanderleegs deleted the feat/update-navigation-file-on-creation-of-new-collection-and-resource-room branch November 25, 2020 07:29
harishv7 pushed a commit that referenced this pull request Feb 17, 2023
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants