- LaneDrag / DragEnd, fixed to support Virtual Lists.

Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
Dave Richer
2024-06-30 21:58:08 -04:00
parent 4db2b73397
commit 7f75eeadb9
4 changed files with 58 additions and 61 deletions

View File

@@ -53,7 +53,6 @@ const LaneHelper = {
return updateLanes(state, newLanes);
},
// TODO: Unverified, needs to be hoisted.
removeCardFromLane: (state, { laneId, cardId }) => {
// Clone the state to avoid mutation
const newLanes = cloneDeep(state.lanes);
@@ -88,50 +87,58 @@ const LaneHelper = {
});
},
// TODO: This has been updated to new DND Lib, verified.
moveCardAcrossLanes: (state, ...args) => {
return state;
// console.dir({
// state,
// args: { fromLaneId, toLaneId, cardId, index }
// });
// // Clone the state to avoid mutation
// const newLanes = cloneDeep(state.lanes);
//
// // Find the source and destination lanes using the lane indices
// const fromLane = newLanes[fromLaneId];
// const toLane = newLanes[toLaneId];
//
// // Find the card in the source lane
// const cardIndex = fromLane.cards.findIndex((card) => card.id === cardId);
// if (cardIndex === -1) {
// throw new Error("Card not found in the source lane");
// }
//
// // Remove the card from the source lane
// const [card] = fromLane.cards.splice(cardIndex, 1);
//
// // Insert the card into the destination lane at the specified index
// toLane.cards.splice(index, 0, card);
//
// let idx = 0;
//
// // Update the lane and card indexes for all lanes
// newLanes.forEach((lane, laneIndex) => {
// lane.cards.forEach((card, cardIndex) => {
// card.idx = idx;
// card.laneIndex = laneIndex;
// card.cardIndex = cardIndex;
// card.laneId = lane.id;
// idx++;
// });
// });
//
// return update(state, {
// lanes: { $set: newLanes }
// });
},
/**
* Move a card from one lane to another
* @param state
* @param fromLaneId
* @param toLaneId
* @param cardId
* @param index
* @returns {unknown}
*/
moveCardAcrossLanes: (state, { fromLaneId, toLaneId, cardId, index }) => {
// Clone the state to avoid mutation
const newLanes = cloneDeep(state.lanes);
console.dir({ fromLaneId, toLaneId, cardId, index });
// Find the source and destination lanes using the lane IDs
const fromLane = newLanes.find((lane) => lane.id === fromLaneId);
const toLane = newLanes.find((lane) => lane.id === toLaneId);
if (!fromLane || !toLane) {
throw new Error("Source or destination lane not found");
}
// Find the card in the source lane
const cardIndex = fromLane.cards.findIndex((card) => card.id === cardId);
if (cardIndex === -1) {
throw new Error("Card not found in the source lane");
}
// Remove the card from the source lane
const [card] = fromLane.cards.splice(cardIndex, 1);
// Insert the card into the destination lane at the specified index
toLane.cards.splice(index, 0, card);
let idx = 0;
// Update the lane and card indexes for all lanes
newLanes.forEach((lane, laneIndex) => {
lane.cards.forEach((card, cardIndex) => {
card.idx = idx;
card.laneIndex = laneIndex;
card.cardIndex = cardIndex;
card.laneId = lane.id;
idx++;
});
});
return update(state, {
lanes: { $set: newLanes }
});
},
updateCardsForLane: (state, { laneId, cards }) => {
const lanes = state.lanes.map((lane) => (lane.id === laneId ? updateLaneCards(lane, cards) : lane));
return updateLanes(state, lanes);