@@ -9,15 +9,74 @@ const WorkflowEntryPoints = ({
99 field, selected, type, setShowModal, setSelectedValue,
1010} ) => {
1111 const [ data , setData ] = useState ( {
12- isLoading : true , list : { } , selectedItemId : selected ,
12+ isLoading : true , list : { } , selectedItemId : selected , key : 'workflow-entry-points' ,
1313 } ) ;
14+ const [ prevSelectedHeader , setPrevSelectedHeader ] = useState ( '' ) ;
15+ const [ sortDirectionRepository , setSortDirectionRepository ] = useState ( 'DESC' ) ;
16+ const [ sortDirectionName , setSortDirectionName ] = useState ( 'DESC' ) ;
1417
1518 const workflowTypes = {
1619 provision : __ ( 'Provision' ) ,
1720 reconfigure : __ ( 'Reconfigure' ) ,
1821 retire : __ ( 'Retirement' ) ,
1922 } ;
2023
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+
2180 useEffect ( ( ) => {
2281 http . post ( `/catalog/ae_tree_select_toggle?typ=${ type } ` , { } , { headers : { } , skipJsonParsing : true } )
2382 . then ( ( _data ) => {
@@ -93,6 +152,9 @@ const WorkflowEntryPoints = ({
93152 < MiqDataTable
94153 headers = { data . list . headers }
95154 rows = { data . list . rows }
155+ sortable
156+ onSort = { onSort }
157+ key = { data . key }
96158 onCellClick = { ( selectedRow ) => onSelect ( selectedRow . id ) }
97159 showPagination = { false }
98160 truncateText = { false }
0 commit comments