Commit d3d903e3 authored by Florie Guibert's avatar Florie Guibert

Epic tree move child with drag and drop

- Review feedback
parent dc37bb2c
...@@ -51,7 +51,7 @@ export default { ...@@ -51,7 +51,7 @@ export default {
}, },
onMove(e) { onMove(e) {
const item = e.relatedContext.element; const item = e.relatedContext.element;
if (item && item.type && item.type === ChildType.Epic) if (item?.type === ChildType.Epic)
this.toggleItem({ this.toggleItem({
parentItem: item, parentItem: item,
isDragging: true, isDragging: true,
......
...@@ -470,9 +470,9 @@ export const moveItem = ( ...@@ -470,9 +470,9 @@ export const moveItem = (
let isFirstChild = false; let isFirstChild = false;
const newParentChildren = state.children[newParentItem.parentReference]; const newParentChildren = state.children[newParentItem.parentReference];
if (newParentChildren !== undefined && newParentChildren.length > 0) { if (newParentChildren?.length > 0) {
adjacentItem = newParentChildren[newIndex]; adjacentItem = newParentChildren[newIndex];
if (adjacentItem === undefined) { if (!adjacentItem) {
adjacentItem = newParentChildren[newParentChildren.length - 1]; adjacentItem = newParentChildren[newParentChildren.length - 1];
relativePosition = relativePositions.Before; relativePosition = relativePositions.Before;
} }
......
...@@ -1379,15 +1379,17 @@ describe('RelatedItemTree', () => { ...@@ -1379,15 +1379,17 @@ describe('RelatedItemTree', () => {
}), }),
); );
testAction( const payload = {
actions.moveItem,
{
oldParentItem: mockParentItem, oldParentItem: mockParentItem,
newParentItem: mockParentItem2, newParentItem: mockParentItem2,
targetItem: mockItems[1], targetItem: mockItems[1],
newIndex: 1, newIndex: 1,
oldIndex: 0, oldIndex: 0,
}, };
testAction(
actions.moveItem,
payload,
state, state,
[ [
{ {
...@@ -1405,13 +1407,7 @@ describe('RelatedItemTree', () => { ...@@ -1405,13 +1407,7 @@ describe('RelatedItemTree', () => {
[ [
{ {
type: 'receiveMoveItemFailure', type: 'receiveMoveItemFailure',
payload: { payload,
oldParentItem: mockParentItem,
newParentItem: mockParentItem2,
targetItem: mockItems[1],
newIndex: 1,
oldIndex: 0,
},
}, },
], ],
); );
...@@ -1420,15 +1416,17 @@ describe('RelatedItemTree', () => { ...@@ -1420,15 +1416,17 @@ describe('RelatedItemTree', () => {
it('should perform MOVE_ITEM mutation before request and dispatch `receiveReorderItemFailure` on request failure', () => { it('should perform MOVE_ITEM mutation before request and dispatch `receiveReorderItemFailure` on request failure', () => {
jest.spyOn(epicUtils.gqClient, 'mutate').mockReturnValue(Promise.reject()); jest.spyOn(epicUtils.gqClient, 'mutate').mockReturnValue(Promise.reject());
testAction( const payload = {
actions.moveItem,
{
oldParentItem: mockParentItem, oldParentItem: mockParentItem,
newParentItem: mockParentItem2, newParentItem: mockParentItem2,
targetItem: mockItems[1], targetItem: mockItems[1],
newIndex: 1, newIndex: 1,
oldIndex: 0, oldIndex: 0,
}, };
testAction(
actions.moveItem,
payload,
state, state,
[ [
{ {
...@@ -1446,13 +1444,7 @@ describe('RelatedItemTree', () => { ...@@ -1446,13 +1444,7 @@ describe('RelatedItemTree', () => {
[ [
{ {
type: 'receiveMoveItemFailure', type: 'receiveMoveItemFailure',
payload: { payload,
oldParentItem: mockParentItem,
newParentItem: mockParentItem2,
targetItem: mockItems[1],
newIndex: 1,
oldIndex: 0,
},
}, },
], ],
); );
......
...@@ -565,6 +565,16 @@ describe('RelatedItemsTree', () => { ...@@ -565,6 +565,16 @@ describe('RelatedItemsTree', () => {
}); });
describe(types.MOVE_ITEM, () => { describe(types.MOVE_ITEM, () => {
const defaultPayload = {
oldParentItem: {
reference: '&1',
},
targetItem: 'bar',
oldIndex: 1,
newIndex: 0,
isFirstChild: false,
};
it('should move an item from one parent to another with children based on provided indexes', () => { it('should move an item from one parent to another with children based on provided indexes', () => {
const newParentItem = { const newParentItem = {
parentReference: '&2', parentReference: '&2',
...@@ -574,14 +584,8 @@ describe('RelatedItemsTree', () => { ...@@ -574,14 +584,8 @@ describe('RelatedItemsTree', () => {
state.children[newParentItem.parentReference] = ['baz']; state.children[newParentItem.parentReference] = ['baz'];
mutations[types.MOVE_ITEM](state, { mutations[types.MOVE_ITEM](state, {
oldParentItem: { ...defaultPayload,
reference: '&1',
},
newParentItem, newParentItem,
targetItem: 'bar',
oldIndex: 1,
newIndex: 0,
isFirstChild: false,
}); });
expect(state.children[state.parentItem.reference]).toEqual( expect(state.children[state.parentItem.reference]).toEqual(
...@@ -600,13 +604,8 @@ describe('RelatedItemsTree', () => { ...@@ -600,13 +604,8 @@ describe('RelatedItemsTree', () => {
state.children[state.parentItem.reference] = ['foo', 'bar']; state.children[state.parentItem.reference] = ['foo', 'bar'];
mutations[types.MOVE_ITEM](state, { mutations[types.MOVE_ITEM](state, {
oldParentItem: { ...defaultPayload,
reference: '&1',
},
newParentItem, newParentItem,
targetItem: 'bar',
oldIndex: 1,
newIndex: 0,
isFirstChild: true, isFirstChild: true,
}); });
...@@ -627,13 +626,9 @@ describe('RelatedItemsTree', () => { ...@@ -627,13 +626,9 @@ describe('RelatedItemsTree', () => {
state.childrenFlags[state.parentItem.reference] = { itemHasChildren: true }; state.childrenFlags[state.parentItem.reference] = { itemHasChildren: true };
mutations[types.MOVE_ITEM](state, { mutations[types.MOVE_ITEM](state, {
oldParentItem: { ...defaultPayload,
reference: '&1',
},
newParentItem, newParentItem,
targetItem: 'bar',
oldIndex: 0, oldIndex: 0,
newIndex: 0,
isFirstChild: true, isFirstChild: true,
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment