You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
107 lines
2.5 KiB
107 lines
2.5 KiB
const moment = require("moment")
|
|
|
|
const {postApi} = require("../../../../../rpdata/api/Post")
|
|
|
|
module.exports = class {
|
|
onCreate(input) {
|
|
this.state = {
|
|
log: input.log,
|
|
modal: null,
|
|
removed: false,
|
|
}
|
|
}
|
|
|
|
open(modal) {
|
|
this.state.modal = modal
|
|
}
|
|
|
|
close() {
|
|
this.state.modal = null
|
|
}
|
|
|
|
/**
|
|
* Patch the posts
|
|
*
|
|
* @param {{id:string, [x:string]: any}[]} patches
|
|
*/
|
|
patch(patches) {
|
|
const posts = this.state.log.posts.slice()
|
|
|
|
for (const patch of patches) {
|
|
const index = posts.findIndex(p => p.id === patch.id)
|
|
if (index == -1) {
|
|
console.warn("Post not found for patch:", patch)
|
|
// TODO: Force full refresh
|
|
continue
|
|
}
|
|
|
|
posts[index] = Object.assign(Object.assign({}, posts[index]), patch)
|
|
}
|
|
|
|
// Sort posts and look for continuity issues (duplicate or skipped positions)
|
|
posts.sort((a, b) => a.position - b.position)
|
|
for (let i = 1; i < posts.length; ++i) {
|
|
if (posts[i].position !== posts[i - 1].position + 1) {
|
|
console.warn("Discontinuity detected!")
|
|
// TODO: Force full refresh
|
|
}
|
|
}
|
|
|
|
this.state.log.posts = posts
|
|
this.state.log = Object.assign({}, this.state.log)
|
|
}
|
|
|
|
movePost(post, toPosition, relative) {
|
|
if (relative) {
|
|
toPosition = post.position + toPosition
|
|
}
|
|
|
|
postApi.move({id: post.id, toPosition}).then(patches => {
|
|
this.patch(patches)
|
|
}).catch(err => {
|
|
console.warn(err)
|
|
})
|
|
}
|
|
|
|
removePost(post) {
|
|
postApi.remove({id: post.id}).then(() => {
|
|
this.state.log.posts = this.state.log.posts.filter(p => p.id !== post.id)
|
|
|
|
for (const p of this.state.log.posts) {
|
|
if (p.position > post.position) {
|
|
p.position--
|
|
}
|
|
}
|
|
|
|
this.state.log = Object.assign({}, this.state.log)
|
|
}).catch(errs => {
|
|
console.warn("Failed to delete:", errs)
|
|
this.state.error = "Failed to delete: " + (errs.message || errs[0].message)
|
|
})
|
|
}
|
|
|
|
postEdited(patch) {
|
|
this.patch([patch])
|
|
}
|
|
|
|
logEdited(patch) {
|
|
this.state.log = Object.assign(Object.assign({}, this.state.log), patch)
|
|
}
|
|
|
|
postAdded(post) {
|
|
this.state.log.posts = this.state.log.posts.concat([post])
|
|
this.state.log = Object.assign({}, this.state.log)
|
|
}
|
|
|
|
logRemoved() {
|
|
this.state.removed = true
|
|
}
|
|
|
|
get title() {
|
|
if (this.state.log.title) {
|
|
return this.state.log.title
|
|
}
|
|
|
|
return `${this.state.log.channel.name} – ${moment(this.state.log.date).format("MMMM D, YYYY")}`
|
|
}
|
|
}
|