|
@ -1,5 +1,7 @@ |
|
|
const moment = require("moment") |
|
|
const moment = require("moment") |
|
|
|
|
|
|
|
|
|
|
|
const {postApi} = require("../../../../../rpdata/api/Post") |
|
|
|
|
|
|
|
|
module.exports = class { |
|
|
module.exports = class { |
|
|
onCreate(input) { |
|
|
onCreate(input) { |
|
|
this.state = { |
|
|
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() { |
|
|
get title() { |
|
|
if (this.state.log.title) { |
|
|
if (this.state.log.title) { |
|
|
return this.state.log.title |
|
|
return this.state.log.title |
|
|