I am working on a kanban style drag&drop system with ng2-dragula. And I have an issue, and I think it's because every time you drop an item to a new place it sends the data to the server and redo the whole list of items that you can drag around. And if you do it fast enough you can break the drag&drop cycle. Is there a way to limit the intervall you can make an API call? Similar to RxJS debounceTime, but since the list is almost always changing I cannot pipe a filter to it.
Basic constructor and drag event subscribtion:
constructor(private dragulaService: DragulaService, ) {
this.makeUndragabbles();
this.subs.add(this.dragulaService.dropModel('cardList')
.subscribe(({ item, target, source }) => {
const dragObj: DragObject = {
item: item,
stageId: target['id'],
name: this.pipelineConfig.name
};
this.modifyStage(dragObj);
const drake = this.dragulaService.find('cardList').drake; //debug variable
const sourceModel = drake.models[drake.containers.indexOf(source)]; //debug variable
}));
}
First it was for making non draggable items, not it's a bit more:
private makeUndragabbles() {
if (!this.dragulaService.find('cardList')) {
this.dragulaService.createGroup('cardList',
{
copy: false,
revertOnSpill: true,
moves: (el, container, handle, sibling) => {
return !el.classList.contains('nodrag');
},
isContainer: (el) => {
return el.classList.contains('stage');
}
});
}
}
Dragged item emitting function:
private modifyStage(draggedItem) {
this.drag.emit(draggedItem);
}
Rest call function:
private saveDraggedItem(pipelineType: string, statusChangeDTO: StatusChangeDTO) {
if (pipelineType === 'dealStages') {
this.dealService.savePipelineDealStageUsingPOST(statusChangeDTO).pipe(
debounceTime(1000),
)
.subscribe(res => { }
, (err) => this.error.emit(err)
, () => { this.getAllDealsForPipeline(); });
}
}
Emitted data cacher:
drag(draggedItem: DragObject) {
if (draggedItem.item) {
const statusChange: StatusChangeDTO = {
id: draggedItem.item.id,
newStatusId: +draggedItem.stageId
};
this.saveDraggedItem(draggedItem.name, statusChange);
}
}