Skip to content

Commit

Permalink
code from livestream faraday-academy#23
Browse files Browse the repository at this point in the history
  • Loading branch information
gwenf committed Mar 27, 2020
1 parent 7e2af55 commit 0608c81
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 18 deletions.
14 changes: 14 additions & 0 deletions curriculum-back/server/api/curricula.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ router.route('/:id/sections/:sectionId/:type/:typeId')
})

router.route('/:id/sections/:sectionId/:type')
.get(async function (req, res) {
try {
const { id, sectionId, type } = req.params

const doc = await Curriculum.findById(id)

const section = doc.sections.id(sectionId)
let item = section[type]

res.send(item)
} catch(err) {
res.status(500).send(err)
}
})
.post(async function (req, res) {
try {
const { id, sectionId, type } = req.params
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<v-col cols="12">
<v-text-field
:placeholder="`Enter ${dialog.type} Link`.toUpperCase()"
v-model="dialog.link"
v-model="dialog.url"
/>
</v-col>
</v-row>
Expand Down
8 changes: 4 additions & 4 deletions curriculum-front/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ export default {
},
async postItem({ commit }, payload) {
const { curriculumId, sectionId, type, body } = payload
const res = await axios.patch(
const res = await axios.post(
`${API_URL}/${curriculumId}/sections/${sectionId}/${type}`,
body
)
commit('updateCurriculum', payload)
commit('upsertItem', payload)
},
async putItem({ commit }, payload) {
const { curriculumId, sectionId, itemId, type, body } = payload
const res = await axios.patch(
`${API_URL}/${curriculumId}/sections/${sectionId}/${type}/${itemId}`,
body
)
commit('updateCurriculum', payload)
commit('upsertItem', payload)
},
async patchItem({ commit }, payload) {
const {
Expand All @@ -46,6 +46,6 @@ export default {
`${API_URL}/${curriculum._id}/sections/${sectionId}/${type}/${item._id}`,
item
)
commit('updateCurriculum', curriculum)
commit('upsertItem', curriculum)
}
}
32 changes: 32 additions & 0 deletions curriculum-front/src/store/mutations.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,38 @@ export default {
// payload: { id, body }
// find and update one curriculum
},
upsertItem(state, payload) {
const { curriculumId, sectionId, type, body } = payload
const cIndex = state.curricula.findIndex((obj) => {
return obj._id === curriculumId
})
const sIndex = state.curricula[cIndex].sections.findIndex((obj) => {
return obj._id === sectionId
})
if (payload.itemId !== undefined) {
const iIndex = state.curricula[cIndex].sections[sIndex][type].findIndex((obj) => {
return obj._id === payload.itemId
})

const { name, url, isCompleted } = body

let updatedItem = {}
if (name) updatedItem.name = name
if (url) updatedItem.url = url
if (isCompleted) updatedItem.isCompleted = isCompleted

// TODO: try adding vm.Set to fix the problem here
// item is being updated in store but not in template

let item = state.curricula[cIndex].sections[sIndex][type][iIndex]
state.curricula[cIndex].sections[sIndex][type][iIndex] = {
...item,
...updatedItem
}
} else {
state.curricula[cIndex].sections[sIndex][type].push(body)
}
},
updateSnackbar(state, settings) {
state.snackbar = {
...state.snackbar,
Expand Down
27 changes: 14 additions & 13 deletions curriculum-front/src/views/DisplayCurriculum.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default {
type: '',
show: false,
name: '',
link: '',
url: '',
isEditing: false,
itemIndex: null,
sectionIndex: null
Expand Down Expand Up @@ -94,15 +94,17 @@ export default {
toggleDialog(type, sectionIndex, itemIndex) {
if (this.dialog.show) {
this.dialog.name = ''
this.dialog.link = ''
this.dialog.url = ''
this.dialog.sectionIndex = null
this.dialog.itemIndex = null
} else if (itemIndex) {
this.dialog.type = type
this.dialog.isEditing = true
} else if (itemIndex !== undefined) {
this.dialog.itemIndex = itemIndex
this.dialog.sectionIndex = sectionIndex
this.dialog.show = true
this.dialog.isEditing = true
const item = this.selectedCurriculum.sections[sectionIndex][`${type}s`][itemIndex]
this.dialog.name = item.name
this.dialog.url = item.url
} else {
this.dialog.sectionIndex = sectionIndex
this.dialog.isEditing = false
Expand All @@ -113,13 +115,13 @@ export default {
saveItem(type) {
// type == 'resources' or 'projects'
const { sections, _id } = this.selectedCurriculum
const { name, link, sectionIndex, itemIndex, isEditing } = this.dialog
const { name, url, sectionIndex, itemIndex, isEditing } = this.dialog
const section = sections[sectionIndex]
const body = {
name,
link
let body = {
name
}
if (url) body.url = url
let payload = {
curriculumId: _id,
sectionId: section._id,
Expand All @@ -128,15 +130,14 @@ export default {
}
if (isEditing) {
const { _id } = section[type][itemIndex]
payload.itemId = _id
payload.itemId = section[type][itemIndex]._id
this.putItem(payload)
} else {
this.postItem(payload)
}
this.dialog.name = ''
this.dialog.link = ''
this.dialog.url = ''
this.dialog.show = false
}
},
Expand Down

0 comments on commit 0608c81

Please sign in to comment.