0

I am working on a drag&drop system. I had an issue with the d&d lib we've been using, solving that introduced a new one.

Old issue with my current solution

My new issue is that while debounceTime works as intended, if i drag more than one item while debounceTime is "active" only the last dragged item gets saved, how can i make sure that every action is saved but not by triggering an API call to our backend via every drop. -> because that's what coused my original old problem.

subs = new Subscription();
debouncer = new Subject();

constructor(private dragulaService: DragulaService, ) {
    this.makeUndragabbles();
    this.subs.add(this.dragulaService.dropModel('cardList')
      .subscribe(({ item, target }) => {
        const dragObj: DragObject = {
          item: item,
          stageId: target['id'],
          name: this.pipelineConfig.name
        };
        this.modifyStage(dragObj);
      }));
// freshly added solution to not trigger API call with every drop
    this.subs.add(this.debouncer.pipe(
      debounceTime(500))
      .subscribe((val) => this.drag.emit(val)));
  }

 private modifyStage(draggedItem) {
    this.debouncer.next(draggedItem);
  }

API call methods (in another component):

private saveDraggedItem(pipelineType: string, statusChangeDTO: StatusChangeDTO) {
    if (pipelineType === 'dealStages') {
      this.dealService.savePipelineDealStageUsingPOST(statusChangeDTO)
        .subscribe(res => { }
          , (err) => this.error.emit(err)
          , () => { this.getAllDealsForPipeline(); });
    }
  }

  drag(draggedItem: DragObject) {
    if (draggedItem.item) {
      const statusChange: StatusChangeDTO = {
        id: draggedItem.item.id,
        newStatusId: +draggedItem.stageId
      };
      this.saveDraggedItem(draggedItem.name, statusChange);
    }
  }
SGalea
  • 712
  • 9
  • 18
Exitl0l
  • 459
  • 2
  • 11
  • 27
  • 2
    share some code here – Poul Kruijt Aug 08 '19 at 11:00
  • @PierreDuc you can see some realted snippets in the link i've shared. – Exitl0l Aug 08 '19 at 11:07
  • You should see this as a separate question. Perhaps somebody comes here later with maybe the same issue, and he first has to click through and browse through the snippets you posted in another question. – Poul Kruijt Aug 08 '19 at 11:30
  • @PierreDuc i've added related snippets here as well. – Exitl0l Aug 08 '19 at 11:31
  • Maybe using a stream that [Guarantees `n` seconds between emit without waiting initially](https://stackoverflow.com/q/54617220/9423231) or using a [Pausable RxJS stream](https://stackoverflow.com/q/56747633/9423231) could help you. – frido Aug 08 '19 at 11:41

0 Answers0