@@ -9,15 +9,74 @@ const WorkflowEntryPoints = ({
9
9
field, selected, type, setShowModal, setSelectedValue,
10
10
} ) => {
11
11
const [ data , setData ] = useState ( {
12
- isLoading : true , list : { } , selectedItemId : selected ,
12
+ isLoading : true , list : { } , selectedItemId : selected , key : 'workflow-entry-points' ,
13
13
} ) ;
14
+ const [ prevSelectedHeader , setPrevSelectedHeader ] = useState ( '' ) ;
15
+ const [ sortDirectionRepository , setSortDirectionRepository ] = useState ( 'DESC' ) ;
16
+ const [ sortDirectionName , setSortDirectionName ] = useState ( 'DESC' ) ;
14
17
15
18
const workflowTypes = {
16
19
provision : __ ( 'Provision' ) ,
17
20
reconfigure : __ ( 'Reconfigure' ) ,
18
21
retire : __ ( 'Retirement' ) ,
19
22
} ;
20
23
24
+ const sortFunction = ( selectedHeader , itemA , itemB ) => {
25
+ if ( selectedHeader . key === 'name' ) {
26
+ if ( itemA . name . text < itemB . name . text ) {
27
+ return - 1 ;
28
+ } if ( itemA . name . text > itemB . name . text ) {
29
+ return 1 ;
30
+ } if ( itemA . name . text === itemB . name . text ) {
31
+ return itemA . id - itemB . id ;
32
+ }
33
+ }
34
+ if ( itemA [ 'configuration_script_source.name' ] === undefined ) {
35
+ itemA [ 'configuration_script_source.name' ] = { text : '' } ;
36
+ } else if ( itemB [ 'configuration_script_source.name' ] === undefined ) {
37
+ itemB [ 'configuration_script_source.name' ] = { text : '' } ;
38
+ }
39
+ if ( itemA [ 'configuration_script_source.name' ] . text < itemB [ 'configuration_script_source.name' ] . text ) {
40
+ return - 1 ;
41
+ } if ( itemA [ 'configuration_script_source.name' ] . text > itemB [ 'configuration_script_source.name' ] . text ) {
42
+ return 1 ;
43
+ }
44
+ return 0 ;
45
+ } ;
46
+
47
+ const onSort = ( itemKey ) => {
48
+ const selectedHeader = data . list . headers . find ( ( item ) => item === itemKey ) ;
49
+ if ( selectedHeader ) {
50
+ const sortedList = data . list ;
51
+ sortedList . rows . sort ( ( a , b ) => sortFunction ( selectedHeader , a , b ) ) ;
52
+ if ( prevSelectedHeader === selectedHeader . key ) {
53
+ if ( selectedHeader . key === 'name' ) {
54
+ if ( sortDirectionName === 'ASC' ) {
55
+ sortedList . rows . sort ( ( a , b ) => sortFunction ( selectedHeader , a , b ) ) ;
56
+ } else {
57
+ sortedList . rows . sort ( ( a , b ) => sortFunction ( selectedHeader , b , a ) ) ;
58
+ }
59
+ setSortDirectionName ( sortDirectionName === 'ASC' ? 'DESC' : 'ASC' ) ;
60
+ } else {
61
+ if ( sortDirectionRepository === 'ASC' ) {
62
+ sortedList . rows . sort ( ( a , b ) => sortFunction ( selectedHeader , a , b ) ) ;
63
+ } else {
64
+ sortedList . rows . sort ( ( a , b ) => sortFunction ( selectedHeader , b , a ) ) ;
65
+ }
66
+ setSortDirectionRepository ( sortDirectionRepository === 'ASC' ? 'DESC' : 'ASC' ) ;
67
+ }
68
+ } else {
69
+ setSortDirectionName ( 'DESC' ) ;
70
+ setSortDirectionRepository ( 'DESC' ) ;
71
+ }
72
+ const tempKey = `${ data . key } -${ sortedList . rows [ 0 ] . id } ` ;
73
+ setPrevSelectedHeader ( selectedHeader . key ) ;
74
+ setData ( {
75
+ ...data , isLoading : false , list : sortedList , key : tempKey ,
76
+ } ) ;
77
+ }
78
+ } ;
79
+
21
80
useEffect ( ( ) => {
22
81
http . post ( `/catalog/ae_tree_select_toggle?typ=${ type } ` , { } , { headers : { } , skipJsonParsing : true } )
23
82
. then ( ( _data ) => {
@@ -93,6 +152,9 @@ const WorkflowEntryPoints = ({
93
152
< MiqDataTable
94
153
headers = { data . list . headers }
95
154
rows = { data . list . rows }
155
+ sortable
156
+ onSort = { onSort }
157
+ key = { data . key }
96
158
onCellClick = { ( selectedRow ) => onSelect ( selectedRow . id ) }
97
159
showPagination = { false }
98
160
truncateText = { false }
0 commit comments