2

okay. I'm confused as to the best way to do this:

the following pieces are in play: a node js server, a client-side react(with redux), a MYSql DB.

in the client app I have lists (many but for this issue, assume one), that I want to be able to reorder by drag and drop.

in the mysql DB the times are stored to represent a linked list (with a nextKey, lastKey, and productionKey(primary), along with the data fields),

//mysql column [productionKey, lastKey,nextKey, ...(other data)]

the current issue I'm having is a render issue. it stutters after every change.

I'm using these two function to get the initial order and to reorder

  function SortLinkedList(linkedList)   
  { 
  var sortedList = [];
  var map = new Map();
  var currentID = null;
  for(var i = 0; i < linkedList.length; i++)
      {
      var item = linkedList[i];
  
      if(item?.lastKey === null)
          {
          currentID = item?.productionKey;

          sortedList.push(item);
      }
      else
          {
          map.set(item?.lastKey, i);
      }
  }
  while(sortedList.length < linkedList.length)
      {
      var nextItem = linkedList[map.get(currentID)];

      sortedList.push(nextItem);
      currentID = nextItem?.productionKey;

  }
  
  const filteredSafe=sortedList.filter(x=>x!==undefined)
    //undefined appear because server has not fully updated yet, so linked list is broken
    //nothing will render without this
  
  return  filteredSafe
  ;
}

const reorder = (list, startIndex, endIndex) => {
  const result = Array.from(list);
  const [removed] = result.splice(startIndex, 1);
  result.splice(endIndex, 0, removed);

    const adjustedResult = result.map((x,i,arr)=>{
        if(i==0){
            x.lastKey=null;
        }else{
            x.lastKey=arr[i-1].productionKey;
        }
        if(i==arr.length-1){
            x.nextKey=null;
        }else{
            x.nextKey=arr[i+1].productionKey;

        }
        return x;
    })

  return adjustedResult;
};


I've got this function to get the items

const getItems = (list,jobList) =>
  {
   return list.map((x,i)=>{
    const jobName=jobList.find(y=>y.jobsessionkey==x.attachedJobKey)?.JobName;
        return {
            id:`ProductionCardM${x.machineID}ID${x.productionKey}`,
            attachedJobKey: x.attachedJobKey,
            lastKey:  x.lastKey,
            machineID:  x.machineID,
            nextKey:  x.nextKey,
            productionKey:  x.productionKey,
            content:jobName
        }
    })

  }

my onDragEnd

const  onDragEnd=(result)=> {
    if (!result.destination) {
        return;
      }
   
    // dropped outside the list
 

    const items = reorder(
      state.items,
      result.source.index,
      result.destination.index,
      
    );

        dispatch(sendAdjustments(items)); 
//sends update to server 
//server updates mysql 
//server sends back update events from mysql in packets
//props sent to DnD component are updated
       

  }

so the actual bug looks like the graphics are glitching - as things get temporarily filtered in the sortLinkedList function - resulting in jumpy divs. is there a smoother way to handle this client->server->DB->server->client dataflow that results in a consistent handling in DnD?

UPDATE:

still trying to solve this. currently implemented a lock pattern.


  useEffect(()=>{
    if(productionLock){
    setState({
      items: SortLinkedList(getItems(data,jobList)),
      droppables: [{ id: "Original: not Dynamic" }]
    })
    setLoading(false);
    }else{
      console.log("locking first");
      setLoading(true);
    }
  },[productionLock])

where production lock is set to true and false from triggers on the server...

basically: the app sends the data to the server, the server processes the request, then sends new data back, when it's finished the server sends the unlock signal.

which should trigger this update happening once, but it does not, it still re-renders on each state update to the app from the server.

altruios
  • 986
  • 1
  • 10
  • 29

1 Answers1

0

What’s the code for sendAdjustments()?

You should update locally first, otherwise DnD pulls it back to its original position while you wait for backend to finish. This makes it appear glitchy. E.g:

  1. Set the newly reordered list locally as your state
  2. Send network request
  3. If it fails, reverse local list state back to the original list