From b6a6450b29180b400dc73feb4e8a8293c710c1c8 Mon Sep 17 00:00:00 2001 From: Leandro Lourenci Date: Sat, 1 Feb 2020 15:49:27 -0300 Subject: [PATCH] Fix the card moving when the lane id is a non-number (#231) --- assets/index.js | 4 ++-- src/components/Board/index.js | 2 +- src/components/Board/services/index.js | 10 +++++++--- src/components/Board/services/index.spec.js | 17 ++++++++++++----- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/assets/index.js b/assets/index.js index e294c91e..3b05093f 100644 --- a/assets/index.js +++ b/assets/index.js @@ -6,11 +6,11 @@ import getUrlParams from './services/getUrlParams' const board = { lanes: [ { - id: 1, + id: '0206c8d7-4d48-4d97-b867-86fc2d21074d', title: 'Lane Backlog', cards: [ { - id: 1, + id: '0206c8d7-4d48-4d97-b867-86fc2d21075d', title: 'Card title 1', description: 'Card content' }, diff --git a/src/components/Board/index.js b/src/components/Board/index.js index 96381031..60277d6f 100644 --- a/src/components/Board/index.js +++ b/src/components/Board/index.js @@ -212,7 +212,7 @@ function BoardContainer({ onCardDragEnd }) { function handleOnDragEnd(event) { - const coordinates = getCoordinates(event) + const coordinates = getCoordinates(event, board) if (!coordinates.source) return isALaneMove(event.type) diff --git a/src/components/Board/services/index.js b/src/components/Board/services/index.js index 9941d08c..69240fba 100644 --- a/src/components/Board/services/index.js +++ b/src/components/Board/services/index.js @@ -1,4 +1,4 @@ -function getCoordinates(event) { +function getCoordinates(event, board) { if (event.destination === null) return {} const laneSource = { fromPosition: event.source.index } @@ -9,8 +9,8 @@ function getCoordinates(event) { } return { - source: { ...laneSource, fromLaneId: parseInt(event.source.droppableId) }, - destination: { ...laneDestination, toLaneId: parseInt(event.destination.droppableId) } + source: { ...laneSource, fromLaneId: getLane(board, event.source.droppableId).id }, + destination: { ...laneDestination, toLaneId: getLane(board, event.destination.droppableId).id } } } @@ -23,4 +23,8 @@ function getCard(board, sourceCoordinate) { return lane.cards[sourceCoordinate.fromPosition] } +function getLane(board, droppableId) { + return board.lanes.find(({ id }) => String(id) === droppableId) +} + export { getCard, getCoordinates, isALaneMove } diff --git a/src/components/Board/services/index.spec.js b/src/components/Board/services/index.spec.js index 44b14afb..71294e7a 100644 --- a/src/components/Board/services/index.spec.js +++ b/src/components/Board/services/index.spec.js @@ -20,16 +20,23 @@ describe('#getCoordinates', () => { }) describe('when the event is a card move', () => { + const board = { + lanes: [ + { id: 1, cards: [{ id: 1 }] }, + { id: '2', cards: [{ id: 2 }] } + ] + } + const event = { type: 'CARD', - source: { index: 0, droppableId: 0 }, - destination: { index: 1, droppableId: 1 } + source: { index: 0, droppableId: '1' }, + destination: { index: 1, droppableId: '2' } } it('returns the card coordinates', () => { - expect(getCoordinates(event)).toEqual({ - destination: { toLaneId: 1, toPosition: 1 }, - source: { fromLaneId: 0, fromPosition: 0 } + expect(getCoordinates(event, board)).toEqual({ + source: { fromLaneId: 1, fromPosition: 0 }, + destination: { toLaneId: '2', toPosition: 1 } }) }) })