api: Added editChapter mutation

This commit is contained in:
2018-09-19 18:54:23 +02:00
parent 6591ad0245
commit 3bfb8fe919
+37 -22
View File
@@ -19,35 +19,50 @@ class Chapter {
this.fictionalDate = fictionalDate != null ? new Date(fictionalDate) : null this.fictionalDate = fictionalDate != null ? new Date(fictionalDate) : null
this.editedDate = new Date(editedDate) this.editedDate = new Date(editedDate)
} }
}
const chapterApi = {
/**
* Call `editChapter(input)` mutation
*
* @param {{id:string, title:string, fictionalDate:Date, source: string}} input
* @returns {Promise<{title:string, fictionalDate:Date, source:string}>}
*/
editChapter(input) {
return query(`
mutation EditChapter($input: ChapterEditInput!) {
editChapter(input: $input) {
title
fictionalDate
source
}
}
`, {input}, {permissions: ["member", "story.edit"]}).then(({editChapter}) => {
if (editChapter.fictionalDate != null) {
editChapter.fictionalDate = new Date(editChapter.fictionalDate)
}
return editChapter
})
},
/** /**
* Query the chapter's editable fields and update this object. This promise returns a boolean * Call `removeChapter(input)` mutation
* whether there has been a change.
* *
* @returns {Promise<boolean>} `true` if the story was edited in the meantime * @param {{id:string}} input
* @returns {Promise<{id: string}>}
*/ */
update() { removeChapter(input) {
return query(` return query(`
query UpdateChapter($id: String!) { mutation RemoveChapter($input: ChapterRemoveInput!) {
update: chapter(id: $id) { removeChapter(input: $input) {
title id
source
fictionalDate
editedDate
} }
} }
`, {id: this.id}).then(({update}) => { `, {input}, {permissions: ["member", "story.edit"]}).then(({removeChapter}) => {
const editedDate = new Date(udpate.editedDate) return removeChapter
const wasEdited = editedDate.getTime() != this.editedDate.getTime()
this.title = update.title
this.source = update.source
this.fictionalDate = new Date(update.fictionalDate)
this.editedDate = editedDate
return wasEdited
}) })
} },
} }
module.exports = {Chapter} module.exports = {Chapter, chapterApi}