Total Complexity | 4 |
Complexity/F | 4 |
Lines of Code | 58 |
Function Count | 1 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import { List } from 'immutable'; |
||
2 | |||
3 | import { findTreeNode } from './findTreeNode'; |
||
4 | |||
5 | export const moveTreeNode = ( |
||
6 | treeData, |
||
7 | currentIndex, |
||
8 | currentPath, |
||
9 | nextIndex, |
||
10 | nextPath, |
||
11 | childIdentifier = 'children', |
||
12 | rootIdentifier = 'root' |
||
13 | ) => { |
||
14 | |||
15 | const originalTreeData = treeData; |
||
16 | |||
17 | const { node: currentParent, indexPath: currentIndexPath } = findTreeNode( |
||
18 | treeData, |
||
19 | currentPath, |
||
20 | childIdentifier, |
||
21 | rootIdentifier |
||
22 | ); |
||
23 | |||
24 | if (!currentParent) { |
||
|
|||
25 | return originalTreeData; |
||
26 | } |
||
27 | |||
28 | currentIndexPath.push(...[childIdentifier, currentIndex]); |
||
29 | |||
30 | let node = treeData.getIn(currentIndexPath); |
||
31 | treeData = treeData.deleteIn(currentIndexPath); |
||
32 | |||
33 | const { node: nextParent, indexPath: nextIndexPath } = findTreeNode( |
||
34 | treeData, |
||
35 | nextPath, |
||
36 | childIdentifier, |
||
37 | rootIdentifier |
||
38 | ); |
||
39 | |||
40 | if (!nextParent) { |
||
41 | return originalTreeData; |
||
42 | } |
||
43 | |||
44 | nextIndexPath.push(childIdentifier); |
||
45 | |||
46 | if (!List.isList(treeData.getIn(nextIndexPath))) { |
||
47 | treeData = treeData.setIn(nextIndexPath, List()); |
||
48 | } |
||
49 | |||
50 | node = node.set('parentId', nextPath.last()); |
||
51 | |||
52 | treeData = treeData.setIn( |
||
53 | nextIndexPath, |
||
54 | treeData.getIn(nextIndexPath).insert(nextIndex, node) |
||
55 | ); |
||
56 | |||
57 | return treeData; |
||
58 | }; |
||
59 |