1
- import React , { useMemo , useRef } from 'react'
1
+ import React , { useMemo , useRef , useState } from 'react'
2
2
import {
3
3
SortingOrder ,
4
- SortableTableHeaderCell ,
5
- ErrorScreenNotAuthorized ,
6
- ERROR_EMPTY_SCREEN ,
7
- noop ,
8
- Pagination ,
9
- Reload ,
10
- TOAST_ACCESS_DENIED ,
11
4
useAsync ,
12
- DEFAULT_BASE_PAGE_SIZE ,
13
5
useUrlFilters ,
14
6
abortPreviousRequests ,
15
7
getIsRequestAborted ,
8
+ SelectAllDialogStatus ,
9
+ BulkSelectionProvider ,
10
+ BulkSelectionIdentifiersType ,
16
11
} from '@devtron-labs/devtron-fe-common-lib'
17
- import { API_STATUS_CODES } from '../../../../../config'
18
12
19
13
import { getPermissionGroupList } from '../../authorization.service'
20
- import { permissionGroupLoading , SortableKeys } from './constants'
21
- import PermissionGroupListHeader from './PermissionGroupListHeader'
22
- import PermissionGroupRow from './PermissionGroupRow'
23
- import { useAuthorizationContext } from '../../AuthorizationProvider'
24
- import { importComponentFromFELibrary } from '../../../../../components/common'
25
- import FiltersEmptyState from '../../shared/components/FilterEmptyState/FilterEmptyState.component'
26
- import NoPermissionGroups from './NoPermissionGroups'
27
-
28
- const PermissionGroupInfoBar = importComponentFromFELibrary ( 'PermissionGroupInfoBar' , noop , 'function' )
14
+ import { SortableKeys } from './constants'
15
+ import { PermissionGroup } from '../../types'
16
+ import PermissionGroupContainer from './PermissionGroupContainer'
17
+ import { BulkSelectionModalConfig , BulkSelectionModalTypes } from '../../shared/components/BulkSelection'
29
18
30
19
const PermissionGroupList = ( ) => {
31
- const {
32
- pageSize,
33
- offset,
34
- changePage,
35
- changePageSize,
36
- searchKey,
37
- handleSearch,
38
- sortBy,
39
- handleSorting,
40
- sortOrder,
41
- clearFilters,
42
- } = useUrlFilters < SortableKeys > ( { initialSortKey : SortableKeys . name } )
20
+ const [ bulkSelectionModalConfig , setBulkSelectionModalConfig ] = useState < BulkSelectionModalConfig > ( {
21
+ type : null ,
22
+ } )
23
+ const urlFilters = useUrlFilters < SortableKeys > ( { initialSortKey : SortableKeys . name } )
24
+ const { pageSize, offset, searchKey, sortBy, sortOrder } = urlFilters
43
25
const filterConfig = useMemo (
44
26
( ) => ( {
45
27
size : pageSize ,
@@ -58,8 +40,19 @@ const PermissionGroupList = () => {
58
40
abortControllerRef ,
59
41
) ,
60
42
[ filterConfig ] ,
43
+ true ,
44
+ {
45
+ resetOnChange : false ,
46
+ } ,
47
+ )
48
+ const allOnThisPageIdentifiers = useMemo (
49
+ ( ) =>
50
+ result ?. permissionGroups . reduce ( ( acc , group ) => {
51
+ acc [ group . id ] = true
52
+ return acc
53
+ } , { } ) ?? { } ,
54
+ [ result ] ,
61
55
)
62
- const { isAutoAssignFlowEnabled } = useAuthorizationContext ( )
63
56
64
57
const showLoadingState = isLoading || getIsRequestAborted ( error )
65
58
@@ -73,99 +66,33 @@ const PermissionGroupList = () => {
73
66
sortOrder : SortingOrder . ASC ,
74
67
} )
75
68
76
- if ( ! showLoadingState ) {
77
- if ( error ) {
78
- if ( error . code === API_STATUS_CODES . PERMISSION_DENIED ) {
79
- return (
80
- < ErrorScreenNotAuthorized
81
- subtitle = { ERROR_EMPTY_SCREEN . REQUIRED_MANAGER_ACCESS }
82
- title = { TOAST_ACCESS_DENIED . TITLE }
83
- />
84
- )
85
- }
86
- return < Reload reload = { reload } className = "flex-grow-1" />
87
- }
88
-
89
- // The null state is shown only when filters are not applied
90
- if ( result . totalCount === 0 && ! searchKey ) {
91
- return < NoPermissionGroups />
92
- }
93
- }
94
-
95
- // Disable the filter actions
96
- const isActionsDisabled = showLoadingState || ! ( result . totalCount && result . permissionGroups . length )
69
+ const getSelectAllDialogStatus = ( ) => {
70
+ // Set to show the modal, the function is called only if there is an existing selection,
71
+ // so the modal won't open if there is no selection
72
+ setBulkSelectionModalConfig ( {
73
+ type : BulkSelectionModalTypes . selectAllAcrossPages ,
74
+ } )
97
75
98
- const sortByName = ( ) => {
99
- handleSorting ( SortableKeys . name )
76
+ return SelectAllDialogStatus . OPEN
100
77
}
101
78
102
79
return (
103
- < div className = "flexbox-col dc__gap-8 flex-grow-1" >
104
- < PermissionGroupListHeader
105
- disabled = { isActionsDisabled }
106
- handleSearch = { handleSearch }
107
- initialSearchText = { searchKey }
108
- getDataToExport = { getPermissionGroupDataForExport }
80
+ < BulkSelectionProvider < BulkSelectionIdentifiersType < Record < PermissionGroup [ 'id' ] , boolean > > >
81
+ identifiers = { allOnThisPageIdentifiers }
82
+ getSelectAllDialogStatus = { getSelectAllDialogStatus }
83
+ >
84
+ < PermissionGroupContainer
85
+ error = { error }
86
+ getPermissionGroupDataForExport = { getPermissionGroupDataForExport }
87
+ showLoadingState = { showLoadingState }
88
+ totalCount = { result ?. totalCount ?? 0 }
89
+ permissionGroups = { result ?. permissionGroups ?? [ ] }
90
+ refetchPermissionGroupList = { reload }
91
+ urlFilters = { urlFilters }
92
+ bulkSelectionModalConfig = { bulkSelectionModalConfig }
93
+ setBulkSelectionModalConfig = { setBulkSelectionModalConfig }
109
94
/>
110
- { isAutoAssignFlowEnabled && (
111
- < div className = "pl-20 pr-20" >
112
- < PermissionGroupInfoBar />
113
- </ div >
114
- ) }
115
- { isLoading || ( result . totalCount && result . permissionGroups . length ) ? (
116
- < div className = "flexbox-col flex-grow-1" >
117
- < div className = "user-permission__header cn-7 fs-12 fw-6 lh-20 dc__uppercase pl-20 pr-20 dc__border-bottom dc__position-sticky dc__top-0 bcn-0 dc__zi-1" >
118
- < span />
119
- < SortableTableHeaderCell
120
- title = "Name"
121
- sortOrder = { sortOrder }
122
- isSorted = { sortBy === SortableKeys . name }
123
- triggerSorting = { sortByName }
124
- disabled = { isActionsDisabled }
125
- />
126
- < span > Description</ span >
127
- < span />
128
- </ div >
129
- { showLoadingState ? (
130
- permissionGroupLoading . map ( ( permissionGroup ) => (
131
- < div
132
- className = "user-permission__row pl-20 pr-20 show-shimmer-loading"
133
- key = { `permission-group-list-${ permissionGroup . id } ` }
134
- >
135
- < span className = "child child-shimmer-loading" />
136
- < span className = "child child-shimmer-loading" />
137
- < span className = "child child-shimmer-loading" />
138
- </ div >
139
- ) )
140
- ) : (
141
- < >
142
- < div className = "fs-13 fw-4 lh-20 cn-9 flex-grow-1" >
143
- { result . permissionGroups . map ( ( permissionGroup , index ) => (
144
- < PermissionGroupRow
145
- { ...permissionGroup }
146
- index = { index }
147
- key = { `permission-group-${ permissionGroup . id } ` }
148
- refetchPermissionGroupList = { reload }
149
- />
150
- ) ) }
151
- </ div >
152
- { result . totalCount > DEFAULT_BASE_PAGE_SIZE && (
153
- < Pagination
154
- rootClassName = "flex dc__content-space pl-20 pr-20 dc__border-top"
155
- size = { result . totalCount }
156
- offset = { offset }
157
- pageSize = { pageSize }
158
- changePage = { changePage }
159
- changePageSize = { changePageSize }
160
- />
161
- ) }
162
- </ >
163
- ) }
164
- </ div >
165
- ) : (
166
- < FiltersEmptyState clearFilters = { clearFilters } />
167
- ) }
168
- </ div >
95
+ </ BulkSelectionProvider >
169
96
)
170
97
}
171
98
0 commit comments