logs-content: Added move up/down mutations
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
const moment = require("moment")
|
||||
|
||||
const {postApi} = require("../../../../../rpdata/api/Post")
|
||||
|
||||
module.exports = class {
|
||||
onCreate(input) {
|
||||
this.state = {
|
||||
@@ -7,6 +9,46 @@ module.exports = class {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
postApi.move({id: post.id, toPosition}).then(patches => {
|
||||
this.patch(patches)
|
||||
}).catch(err => {
|
||||
console.warn(err)
|
||||
})
|
||||
}
|
||||
|
||||
get title() {
|
||||
if (this.state.log.title) {
|
||||
return this.state.log.title
|
||||
|
||||
@@ -2,6 +2,6 @@
|
||||
<main>
|
||||
<div class="logs-content">
|
||||
<h1 class="color-primary">${component.title}</h1>
|
||||
<post for(post in state.log.posts) post=post characters=state.log.characters />
|
||||
<post for(post in state.log.posts) on-move("movePost", post) post=post characters=state.log.characters last=(post.position === state.log.posts.length) />
|
||||
</div>
|
||||
</main>
|
||||
Reference in New Issue
Block a user