Skip to content
Snippets Groups Projects
Commit c00a9ed2 authored by john's avatar john
Browse files

implement chapters dnd with patches

parent 231cd631
No related branches found
No related tags found
No related merge requests found
...@@ -80,32 +80,52 @@ export class Division extends React.Component { ...@@ -80,32 +80,52 @@ export class Division extends React.Component {
let toUpdate = [] let toUpdate = []
// dragging upwards
if (dragIndex > hoverIndex) { if (dragIndex > hoverIndex) {
const toModify = _.filter(chapters, function (c) { // find the chapters that changed place
const toModify = _.filter(chapters, c => {
return c.index >= hoverIndex && c.index < dragIndex return c.index >= hoverIndex && c.index < dragIndex
}) })
_.forEach(toModify, function (c) {
c.index += 1 // build the patches for the chapters' updates
// update(book, c) const patches = _.map(toModify, chapter => {
return {
id: chapter.id,
index: (chapter.index + 1)
}
}) })
toUpdate = _.union(toUpdate, toModify)
} else if (dragIndex < hoverIndex) { toUpdate = _.union(toUpdate, patches)
}
// dragging downwards
if (dragIndex < hoverIndex) {
// TODO -- refactor?
// do the same as above
const toModify = _.filter(chapters, function (c) { const toModify = _.filter(chapters, function (c) {
return c.index <= hoverIndex && c.index > dragIndex return c.index <= hoverIndex && c.index > dragIndex
}) })
_.forEach(toModify, function (c) {
c.index -= 1 const patches = _.map(toModify, chapter => {
// update(book, c) return {
id: chapter.id,
index: (chapter.index - 1)
}
}) })
toUpdate = _.union(toUpdate, toModify)
toUpdate = _.union(toUpdate, patches)
} }
dragChapter.index = hoverIndex // add the dragged chapter to the list of patches that are needed
// update(book, dragChapter) const draggedPatch = {
toUpdate.push(dragChapter) id: dragChapter.id,
index: hoverIndex
}
toUpdate.push(draggedPatch)
_.forEach(toUpdate, function (chapter) { // perform all the updates
update(book, chapter) _.forEach(toUpdate, patch => {
update(book, patch)
}) })
} }
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment