Skip to content

Commit 7e0d8f4

Browse files
author
DimaGalchenko
committed
* FE: replace reorderService with pure functions and get rid of duplicated code
1 parent 548ee35 commit 7e0d8f4

File tree

1 file changed

+71
-48
lines changed

1 file changed

+71
-48
lines changed

client/src/services/reorder.service.ts

Lines changed: 71 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,63 +2,86 @@ import type { DraggableLocation } from "@hello-pangea/dnd";
22

33
import { type Card, type List } from "../common/types/types";
44

5-
export const reorderService = {
6-
reorderLists(items: List[], startIndex: number, endIndex: number): List[] {
7-
const [removed] = items.splice(startIndex, 1);
8-
items.splice(endIndex, 0, removed);
5+
// Pure functions
6+
const removeItemAtIndex = (index: number, items: any[]): any[] => [
7+
...items.slice(0, index),
8+
...items.slice(index + 1),
9+
];
10+
11+
const addItemAtIndex = (index: number, item: any, items: any[]): any[] => [
12+
...items.slice(0, index),
13+
item,
14+
...items.slice(index),
15+
];
916

10-
return items;
11-
},
17+
const moveItem = (fromIndex: number, toIndex: number, items: any[]): any[] => {
18+
const item = items[fromIndex];
19+
const itemsWithoutItem = removeItemAtIndex(fromIndex, items);
20+
return addItemAtIndex(toIndex, item, itemsWithoutItem);
21+
};
1222

13-
reorderCards(
14-
lists: List[],
15-
source: DraggableLocation,
16-
destination: DraggableLocation
17-
): List[] {
18-
const current: Card[] =
19-
lists.find((list) => list.id === source.droppableId)?.cards || [];
20-
const next: Card[] =
21-
lists.find((list) => list.id === destination.droppableId)?.cards || [];
22-
const target: Card = current[source.index];
23+
// Higher-Order Functions
24+
const reorderLists = (startIndex: number, endIndex: number, lists: List[]): List[] =>
25+
moveItem(startIndex, endIndex, lists);
2326

24-
const isMovingInSameList = source.droppableId === destination.droppableId;
27+
const reorderCardsInSameList = (sourceIndex: number, destinationIndex: number, cards: Card[]): Card[] =>
28+
moveItem(sourceIndex, destinationIndex, cards);
2529

26-
if (isMovingInSameList) {
27-
const [removed] = current.splice(source.index, 1);
28-
current.splice(destination.index, 0, removed);
29-
const reordered: Card[] = current;
30+
const reorderCardsInDifferentLists = (
31+
sourceList: List,
32+
sourceIndex: number,
33+
destinationList: List,
34+
destinationIndex: number,
35+
card: Card
36+
): [List, List] => {
37+
const updatedSourceList = {
38+
...sourceList,
39+
cards: removeItemAtIndex(sourceIndex, sourceList.cards),
40+
};
41+
const updatedDestinationList = {
42+
...destinationList,
43+
cards: addItemAtIndex(destinationIndex, card, destinationList.cards),
44+
};
45+
return [updatedSourceList, updatedDestinationList];
46+
};
3047

31-
return lists.map((list) =>
32-
list.id === source.droppableId ? { ...list, cards: reordered } : list
33-
);
34-
}
48+
const reorderCards = (
49+
lists: List[],
50+
source: DraggableLocation,
51+
destination: DraggableLocation
52+
): List[] => {
53+
const sourceList = lists.find(list => list.id === source.droppableId);
54+
const destinationList = lists.find(list => list.id === destination.droppableId);
3555

36-
const newLists = lists.map((list) => {
37-
if (list.id === source.droppableId) {
38-
return {
39-
...list,
40-
cards: this.removeCardFromList(current, source.index),
41-
};
42-
}
56+
if (!sourceList || !destinationList) {
57+
throw new Error('Source/Destination list must be not null')
58+
}
4359

44-
if (list.id === destination.droppableId) {
45-
return {
46-
...list,
47-
cards: this.addCardToList(next, destination.index, target),
48-
};
49-
}
60+
const card = sourceList.cards[source.index];
5061

51-
return list;
52-
});
62+
if (source.droppableId === destination.droppableId) {
63+
const reorderedCards = reorderCardsInSameList(source.index, destination.index, sourceList.cards);
64+
return lists.map(list =>
65+
list.id === source.droppableId ? { ...list, cards: reorderedCards } : list
66+
);
67+
}
5368

54-
return newLists;
55-
},
69+
const [updatedSourceList, updatedDestinationList] = reorderCardsInDifferentLists(
70+
sourceList,
71+
source.index,
72+
destinationList,
73+
destination.index,
74+
card
75+
);
5676

57-
removeCardFromList(cards: Card[], index: number): Card[] {
58-
return cards.slice(0, index).concat(cards.slice(index + 1));
59-
},
77+
return lists.map(list => {
78+
if (list.id === source.droppableId) return updatedSourceList;
79+
if (list.id === destination.droppableId) return updatedDestinationList;
80+
return list;
81+
});
82+
};
6083

61-
addCardToList(cards: Card[], index: number, card: Card): Card[] {
62-
return cards.slice(0, index).concat(card).concat(cards.slice(index));
63-
},
84+
export const reorderService = {
85+
reorderLists,
86+
reorderCards,
6487
};

0 commit comments

Comments
 (0)