1
- /*
2
- * Copyright (c) 2025 , RTE (http://www.rte-france.com)
1
+ /**
2
+ * Copyright (c) 2024 , RTE (http://www.rte-france.com)
3
3
* This Source Code Form is subject to the terms of the Mozilla Public
4
4
* License, v. 2.0. If a copy of the MPL was not distributed with this
5
5
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
6
*/
7
7
8
- import { FunctionComponent , useCallback , useEffect , useMemo , useRef , useState } from 'react' ;
8
+ import { FunctionComponent , RefObject , useCallback , useEffect , useMemo , useState } from 'react' ;
9
9
import { useIntl } from 'react-intl' ;
10
- import { Grid } from '@mui/material' ;
11
10
import { GroupAdd } from '@mui/icons-material' ;
12
11
import { GridButton , GridButtonDelete , GridTable , GridTableRef } from '../../components/Grid' ;
13
- import { UserAdminSrv , GroupInfos , UserInfos , UpdateGroupInfos } from '../../services' ;
12
+ import { GroupInfos , UpdateGroupInfos , UserAdminSrv , UserInfos } from '../../services' ;
13
+ import {
14
+ ColDef ,
15
+ GetRowIdParams ,
16
+ ICellEditorParams ,
17
+ RowClickedEvent ,
18
+ SelectionChangedEvent ,
19
+ TextFilterParams ,
20
+ } from 'ag-grid-community' ;
14
21
import { useSnackMessage } from '@gridsuite/commons-ui' ;
15
- import { ColDef , GetRowIdParams , SelectionChangedEvent , TextFilterParams , ICellEditorParams } from 'ag-grid-community' ;
16
22
import DeleteConfirmationDialog from '../common/delete-confirmation-dialog' ;
17
- import MultiSelectEditorComponent from '../common/multi-select-editor-component ' ;
23
+ import { defaultColDef , defaultRowSelection } from '../common/table-config ' ;
18
24
import MultiChipsRendererComponent from '../common/multi-chips-renderer-component' ;
25
+ import MultiSelectEditorComponent from '../common/multi-select-editor-component' ;
19
26
import { UUID } from 'crypto' ;
20
- import { defaultColDef } from '../common/table-config' ;
21
- import AddGroupDialog from './add-group-dialog' ;
22
27
23
- function getRowId ( params : GetRowIdParams < GroupInfos > ) : string {
24
- return params . data . name ;
28
+ export interface GroupsTableProps {
29
+ gridRef : RefObject < GridTableRef < GroupInfos > > ;
30
+ onRowClicked : ( event : RowClickedEvent < GroupInfos > ) => void ;
31
+ setOpenAddGroupDialog : ( open : boolean ) => void ;
25
32
}
26
33
27
- const GroupsPage : FunctionComponent = ( ) => {
34
+ const GroupsTable : FunctionComponent < GroupsTableProps > = ( props ) => {
28
35
const intl = useIntl ( ) ;
29
36
const { snackError } = useSnackMessage ( ) ;
30
- const gridRef = useRef < GridTableRef < GroupInfos > > ( null ) ;
31
- const gridContext = gridRef . current ?. context ;
37
+
38
+ const [ rowsSelection , setRowsSelection ] = useState < GroupInfos [ ] > ( [ ] ) ;
39
+ const [ showDeletionDialog , setShowDeletionDialog ] = useState ( false ) ;
32
40
const [ usersOptions , setUsersOptions ] = useState < string [ ] > ( [ ] ) ;
33
- const [ openAddGroupDialog , setOpenAddGroupDialog ] = useState ( false ) ;
34
41
35
42
useEffect ( ( ) => {
36
43
UserAdminSrv . fetchUsers ( )
@@ -46,12 +53,43 @@ const GroupsPage: FunctionComponent = () => {
46
53
) ;
47
54
} , [ snackError ] ) ;
48
55
56
+ function getRowId ( params : GetRowIdParams < GroupInfos > ) : string {
57
+ return params . data . name ;
58
+ }
59
+
60
+ const onSelectionChanged = useCallback (
61
+ ( event : SelectionChangedEvent < GroupInfos , { } > ) => setRowsSelection ( event . api . getSelectedRows ( ) ?? [ ] ) ,
62
+ [ setRowsSelection ]
63
+ ) ;
64
+
65
+ const onAddButton = useCallback ( ( ) => props . setOpenAddGroupDialog ( true ) , [ props ] ) ;
66
+
67
+ const deleteGroups = useCallback ( ( ) : Promise < void > | undefined => {
68
+ let groupNames = rowsSelection . map ( ( group ) => group . name ) ;
69
+ return UserAdminSrv . deleteGroups ( groupNames )
70
+ . catch ( ( error ) => {
71
+ if ( error . status === 422 ) {
72
+ snackError ( {
73
+ headerId : 'groups.table.integrity.error.delete' ,
74
+ } ) ;
75
+ } else {
76
+ snackError ( {
77
+ messageTxt : error . message ,
78
+ headerId : 'groups.table.error.delete' ,
79
+ } ) ;
80
+ }
81
+ } )
82
+ . then ( ( ) => props . gridRef ?. current ?. context ?. refresh ?.( ) ) ;
83
+ } , [ props . gridRef , rowsSelection , snackError ] ) ;
84
+
85
+ const deleteGroupsDisabled = useMemo ( ( ) => rowsSelection . length <= 0 , [ rowsSelection . length ] ) ;
86
+
49
87
const updateGroupCallback = useCallback (
50
88
( id : UUID , name : string , users : string [ ] ) => {
51
89
const newData : UpdateGroupInfos = {
52
90
id : id ,
53
91
name : name ,
54
- users : users ,
92
+ users : [ ] ,
55
93
} ;
56
94
UserAdminSrv . udpateGroup ( newData )
57
95
. catch ( ( error ) =>
@@ -60,9 +98,9 @@ const GroupsPage: FunctionComponent = () => {
60
98
headerId : 'groups.table.error.update' ,
61
99
} )
62
100
)
63
- . then ( ( ) => gridContext ?. refresh ?.( ) ) ;
101
+ . then ( ( ) => props . gridRef ?. current ?. context ?. refresh ?.( ) ) ;
64
102
} ,
65
- [ gridContext , snackError ]
103
+ [ props . gridRef , snackError ]
66
104
) ;
67
105
68
106
const columns = useMemo (
@@ -114,70 +152,37 @@ const GroupsPage: FunctionComponent = () => {
114
152
[ intl , usersOptions , updateGroupCallback ]
115
153
) ;
116
154
117
- const [ rowsSelection , setRowsSelection ] = useState < GroupInfos [ ] > ( [ ] ) ;
118
- const deleteGroups = useCallback ( ( ) : Promise < void > | undefined => {
119
- let groupNames = rowsSelection . map ( ( group ) => group . name ) ;
120
- return UserAdminSrv . deleteGroups ( groupNames )
121
- . catch ( ( error ) => {
122
- if ( error . status === 422 ) {
123
- snackError ( {
124
- headerId : 'groups.table.integrity.error.delete' ,
125
- } ) ;
126
- } else {
127
- snackError ( {
128
- messageTxt : error . message ,
129
- headerId : 'groups.table.error.delete' ,
130
- } ) ;
131
- }
132
- } )
133
- . then ( ( ) => gridContext ?. refresh ?.( ) ) ;
134
- } , [ gridContext , rowsSelection , snackError ] ) ;
135
- const deleteGroupsDisabled = useMemo ( ( ) => rowsSelection . length <= 0 , [ rowsSelection . length ] ) ;
136
- const [ showDeletionDialog , setShowDeletionDialog ] = useState ( false ) ;
137
-
138
155
return (
139
- < Grid item container direction = "column" spacing = { 2 } component = "section" >
140
- < Grid item container xs sx = { { width : 1 } } >
141
- < GridTable < GroupInfos , { } >
142
- ref = { gridRef }
143
- dataLoader = { UserAdminSrv . fetchGroups }
144
- columnDefs = { columns }
145
- defaultColDef = { defaultColDef }
146
- stopEditingWhenCellsLoseFocus = { true }
147
- gridId = "table-groups"
148
- getRowId = { getRowId }
149
- rowSelection = { {
150
- mode : 'multiRow' ,
151
- enableClickSelection : false ,
152
- checkboxes : true ,
153
- headerCheckbox : true ,
154
- hideDisabledCheckboxes : false ,
155
- } }
156
- onSelectionChanged = { useCallback (
157
- ( event : SelectionChangedEvent < GroupInfos , { } > ) =>
158
- setRowsSelection ( event . api . getSelectedRows ( ) ?? [ ] ) ,
159
- [ ]
160
- ) }
161
- >
162
- < GridButton
163
- labelId = "groups.table.toolbar.add.label"
164
- textId = "groups.table.toolbar.add"
165
- startIcon = { < GroupAdd fontSize = "small" /> }
166
- color = "primary"
167
- onClick = { useCallback ( ( ) => setOpenAddGroupDialog ( true ) , [ ] ) }
168
- />
169
- < GridButtonDelete onClick = { ( ) => setShowDeletionDialog ( true ) } disabled = { deleteGroupsDisabled } />
170
- </ GridTable >
171
- < AddGroupDialog gridRef = { gridRef } open = { openAddGroupDialog } setOpen = { setOpenAddGroupDialog } />
172
- < DeleteConfirmationDialog
173
- open = { showDeletionDialog }
174
- setOpen = { setShowDeletionDialog }
175
- itemType = { intl . formatMessage ( { id : 'form.delete.dialog.group' } ) }
176
- itemNames = { rowsSelection . map ( ( group ) => group . name ) }
177
- deleteFunc = { deleteGroups }
156
+ < >
157
+ < GridTable < GroupInfos , { } >
158
+ ref = { props . gridRef }
159
+ dataLoader = { UserAdminSrv . fetchGroups }
160
+ columnDefs = { columns }
161
+ defaultColDef = { defaultColDef }
162
+ gridId = "table-groups"
163
+ getRowId = { getRowId }
164
+ rowSelection = { defaultRowSelection }
165
+ onRowClicked = { props . onRowClicked }
166
+ onSelectionChanged = { onSelectionChanged }
167
+ >
168
+ < GridButton
169
+ labelId = "groups.table.toolbar.add.label"
170
+ textId = "groups.table.toolbar.add"
171
+ startIcon = { < GroupAdd fontSize = "small" /> }
172
+ color = "primary"
173
+ onClick = { onAddButton }
178
174
/>
179
- </ Grid >
180
- </ Grid >
175
+ < GridButtonDelete onClick = { ( ) => setShowDeletionDialog ( true ) } disabled = { deleteGroupsDisabled } />
176
+ </ GridTable >
177
+
178
+ < DeleteConfirmationDialog
179
+ open = { showDeletionDialog }
180
+ setOpen = { setShowDeletionDialog }
181
+ itemType = { intl . formatMessage ( { id : 'form.delete.dialog.group' } ) }
182
+ itemNames = { rowsSelection . map ( ( user ) => user . name ) }
183
+ deleteFunc = { deleteGroups }
184
+ />
185
+ </ >
181
186
) ;
182
187
} ;
183
- export default GroupsPage ;
188
+ export default GroupsTable ;
0 commit comments