1
- import React , { useCallback , useMemo } from 'react'
1
+ import React , { useCallback , useMemo , useRef } from 'react'
2
2
import {
3
+ SortingOrder ,
4
+ SortableTableHeaderCell ,
3
5
ErrorScreenNotAuthorized ,
4
6
ERROR_EMPTY_SCREEN ,
5
7
noop ,
6
8
Pagination ,
7
9
Reload ,
8
10
TOAST_ACCESS_DENIED ,
9
11
useAsync ,
12
+ DEFAULT_BASE_PAGE_SIZE ,
13
+ useUrlFilters ,
10
14
} from '@devtron-labs/devtron-fe-common-lib'
11
- import { API_STATUS_CODES , DEFAULT_BASE_PAGE_SIZE , SortingOrder } from '../../../../../config'
15
+ import { API_STATUS_CODES } from '../../../../../config'
12
16
13
17
import { getPermissionGroupList } from '../../authorization.service'
14
18
import { permissionGroupLoading , SortableKeys } from './constants'
15
19
import PermissionGroupListHeader from './PermissionGroupListHeader'
16
20
import PermissionGroupRow from './PermissionGroupRow'
17
21
import { useAuthorizationContext } from '../../AuthorizationProvider'
18
22
import { importComponentFromFELibrary } from '../../../../../components/common'
19
- import useUrlFilters from '../../shared/hooks/useUrlFilters'
20
- import SortableTableHeaderCell from '../../../../../components/common/SortableTableHeaderCell'
21
23
import FiltersEmptyState from '../../shared/components/FilterEmptyState/FilterEmptyState.component'
22
24
import NoPermissionGroups from './NoPermissionGroups'
25
+ import { abortPreviousRequests , getIsRequestAborted } from '../../utils'
23
26
24
27
const PermissionGroupInfoBar = importComponentFromFELibrary ( 'PermissionGroupInfoBar' , noop , 'function' )
25
28
@@ -46,9 +49,19 @@ const PermissionGroupList = () => {
46
49
} ) ,
47
50
[ pageSize , offset , searchKey , sortBy , sortOrder ] ,
48
51
)
49
- const [ isLoading , result , error , reload ] = useAsync ( ( ) => getPermissionGroupList ( filterConfig ) , [ filterConfig ] )
52
+ const abortControllerRef = useRef ( new AbortController ( ) )
53
+ const [ isLoading , result , error , reload ] = useAsync (
54
+ ( ) =>
55
+ abortPreviousRequests (
56
+ ( ) => getPermissionGroupList ( filterConfig , abortControllerRef . current . signal ) ,
57
+ abortControllerRef ,
58
+ ) ,
59
+ [ filterConfig ] ,
60
+ )
50
61
const { isAutoAssignFlowEnabled } = useAuthorizationContext ( )
51
62
63
+ const showLoadingState = isLoading || getIsRequestAborted ( error )
64
+
52
65
const getPermissionGroupDataForExport = useCallback (
53
66
( ) =>
54
67
getPermissionGroupList ( {
@@ -62,7 +75,7 @@ const PermissionGroupList = () => {
62
75
[ filterConfig ] ,
63
76
)
64
77
65
- if ( ! isLoading ) {
78
+ if ( ! showLoadingState ) {
66
79
if ( error ) {
67
80
if ( [ API_STATUS_CODES . PERMISSION_DENIED , API_STATUS_CODES . UNAUTHORIZED ] . includes ( error . code ) ) {
68
81
return (
@@ -72,7 +85,7 @@ const PermissionGroupList = () => {
72
85
/>
73
86
)
74
87
}
75
- return < Reload reload = { reload } />
88
+ return < Reload reload = { reload } className = "flex-grow-1" />
76
89
}
77
90
78
91
// The null state is shown only when filters are not applied
@@ -81,78 +94,79 @@ const PermissionGroupList = () => {
81
94
}
82
95
}
83
96
97
+ // Disable the filter actions
98
+ const isActionsDisabled = showLoadingState || ! ( result . totalCount && result . permissionGroups . length )
99
+
84
100
const sortByName = ( ) => {
85
101
handleSorting ( SortableKeys . name )
86
102
}
87
103
88
- const handleClearFilters = ( ) => {
89
- clearFilters ( )
90
- }
91
-
92
104
return (
93
105
< div className = "flexbox-col dc__gap-8 flex-grow-1" >
94
106
< PermissionGroupListHeader
95
- disabled = { isLoading || ! ( result . totalCount && result . permissionGroups . length ) }
107
+ disabled = { isActionsDisabled }
96
108
handleSearch = { handleSearch }
97
109
initialSearchText = { searchKey }
98
110
getDataToExport = { getPermissionGroupDataForExport }
99
111
/>
100
- { isAutoAssignFlowEnabled && < PermissionGroupInfoBar /> }
101
- < div className = "flexbox-col flex-grow-1" >
102
- < div className = "user-permission__header cn-7 fs-12 fw-6 lh-20 dc__uppercase pl-20 pr-20 dc__border-bottom" >
103
- < span />
104
- < SortableTableHeaderCell
105
- title = "Name"
106
- sortOrder = { sortOrder }
107
- isSorted = { sortBy === SortableKeys . name }
108
- triggerSorting = { sortByName }
109
- disabled = { isLoading }
110
- />
111
- < span > Description</ span >
112
- < span />
112
+ { isAutoAssignFlowEnabled && (
113
+ < div className = "pl-20 pr-20" >
114
+ < PermissionGroupInfoBar />
113
115
</ div >
114
- { isLoading || ( result . totalCount && result . permissionGroups . length ) ? (
115
- < >
116
- { isLoading ? (
117
- permissionGroupLoading . map ( ( permissionGroup ) => (
118
- < div
119
- className = "user-permission__row pl-20 pr-20 show-shimmer-loading"
120
- key = { `permission-group-list-${ permissionGroup . id } ` }
121
- >
122
- < span className = "child child-shimmer-loading" />
123
- < span className = "child child-shimmer-loading" />
124
- < span className = "child child-shimmer-loading" />
125
- </ div >
126
- ) )
127
- ) : (
128
- < >
129
- < div className = "fs-13 fw-4 lh-20 cn-9 flex-grow-1" >
130
- { result . permissionGroups . map ( ( permissionGroup , index ) => (
131
- < PermissionGroupRow
132
- { ...permissionGroup }
133
- index = { index }
134
- key = { `permission-group-${ permissionGroup . id } ` }
135
- refetchPermissionGroupList = { reload }
136
- />
137
- ) ) }
138
- </ div >
139
- { result . totalCount > DEFAULT_BASE_PAGE_SIZE && (
140
- < Pagination
141
- rootClassName = "flex dc__content-space pl-20 pr-20 dc__border-top"
142
- size = { result . totalCount }
143
- offset = { offset }
144
- pageSize = { pageSize }
145
- changePage = { changePage }
146
- changePageSize = { changePageSize }
116
+ ) }
117
+ { isLoading || ( result . totalCount && result . permissionGroups . length ) ? (
118
+ < div className = "flexbox-col flex-grow-1" >
119
+ < div className = "user-permission__header cn-7 fs-12 fw-6 lh-20 dc__uppercase pl-20 pr-20 dc__border-bottom" >
120
+ < span />
121
+ < SortableTableHeaderCell
122
+ title = "Name"
123
+ sortOrder = { sortOrder }
124
+ isSorted = { sortBy === SortableKeys . name }
125
+ triggerSorting = { sortByName }
126
+ disabled = { isActionsDisabled }
127
+ />
128
+ < span > Description</ span >
129
+ < span />
130
+ </ div >
131
+ { showLoadingState ? (
132
+ permissionGroupLoading . map ( ( permissionGroup ) => (
133
+ < div
134
+ className = "user-permission__row pl-20 pr-20 show-shimmer-loading"
135
+ key = { `permission-group-list-${ permissionGroup . id } ` }
136
+ >
137
+ < span className = "child child-shimmer-loading" />
138
+ < span className = "child child-shimmer-loading" />
139
+ < span className = "child child-shimmer-loading" />
140
+ </ div >
141
+ ) )
142
+ ) : (
143
+ < >
144
+ < div className = "fs-13 fw-4 lh-20 cn-9 flex-grow-1" >
145
+ { result . permissionGroups . map ( ( permissionGroup , index ) => (
146
+ < PermissionGroupRow
147
+ { ...permissionGroup }
148
+ index = { index }
149
+ key = { `permission-group-${ permissionGroup . id } ` }
150
+ refetchPermissionGroupList = { reload }
147
151
/>
148
- ) }
149
- </ >
150
- ) }
151
- </ >
152
- ) : (
153
- < FiltersEmptyState clearFilters = { handleClearFilters } />
154
- ) }
155
- </ div >
152
+ ) ) }
153
+ </ div >
154
+ { result . totalCount > DEFAULT_BASE_PAGE_SIZE && (
155
+ < Pagination
156
+ rootClassName = "flex dc__content-space pl-20 pr-20 dc__border-top"
157
+ size = { result . totalCount }
158
+ offset = { offset }
159
+ pageSize = { pageSize }
160
+ changePage = { changePage }
161
+ changePageSize = { changePageSize }
162
+ />
163
+ ) }
164
+ </ >
165
+ ) }
166
+ </ div >
167
+ ) : (
168
+ < FiltersEmptyState clearFilters = { clearFilters } />
169
+ ) }
156
170
</ div >
157
171
)
158
172
}
0 commit comments