Optimize post edit autosave

pull/3445/head
johnniang 2019-05-08 16:11:50 +08:00
parent 16cc1e22d3
commit 0922f72fee
1 changed files with 22 additions and 18 deletions

View File

@ -271,7 +271,7 @@ export default {
this.categories = response.data.data this.categories = response.data.data
}) })
}, },
createOrUpdatePost() { createOrUpdatePost(createSuccess, updateSuccess) {
// Set category ids // Set category ids
this.postToStage.categoryIds = this.selectedCategoryIds this.postToStage.categoryIds = this.selectedCategoryIds
// Set tag ids // Set tag ids
@ -281,17 +281,27 @@ export default {
// Update the post // Update the post
postApi.update(this.postToStage.id, this.postToStage).then(response => { postApi.update(this.postToStage.id, this.postToStage).then(response => {
this.$log.debug('Updated post', response.data.data) this.$log.debug('Updated post', response.data.data)
this.$message.success('文章更新成功') if (updateSuccess) {
updateSuccess()
}
}) })
} else { } else {
// Create the post // Create the post
postApi.create(this.postToStage).then(response => { postApi.create(this.postToStage).then(response => {
this.$log.debug('Created post', response.data.data) this.$log.debug('Created post', response.data.data)
this.$message.success('文章创建成功') if (createSuccess) {
createSuccess()
}
this.postToStage = response.data.data this.postToStage = response.data.data
}) })
} }
}, },
savePost() {
this.createOrUpdatePost(() => this.$message.success('文章创建成功'), () => this.$message.success('文章更新成功'))
},
autoSavePost() {
this.createOrUpdatePost()
},
handleShowDrawer() { handleShowDrawer() {
this.visible = true this.visible = true
}, },
@ -331,24 +341,18 @@ export default {
autoSaveTimer() { autoSaveTimer() {
if (this.timer == null) { if (this.timer == null) {
this.timer = setInterval(() => { this.timer = setInterval(() => {
if (this.postToStage.title != null && this.postToStage.originalContent != null) { this.autoSavePost()
this.postToStage.categoryIds = this.selectedCategoryIds
this.postToStage.tagIds = this.selectedTagIds
if (this.postToStage.id) {
postApi.update(this.postToStage.id, this.postToStage).then(response => {
this.$log.debug('Auto updated post', response.data.data)
})
} else {
postApi.create(this.postToStage).then(response => {
this.$log.debug('Auto saved post', response.data.data)
this.postToStage = response.data.data
})
}
}
}, 15000) }, 15000)
} }
} }
},
beforeRouteLeave(to, from, next) {
if (this.timer !== null) {
clearInterval(this.timer)
}
// Auto save the post
this.autoSavePost()
next()
} }
} }
</script> </script>