1- import { useState , useEffect } from 'react' ;
1+ import { useState , useEffect , useMemo } from 'react' ;
22import { Table , Button , UncontrolledTooltip } from 'reactstrap' ;
33import { connect } from 'react-redux' ;
44import axios from 'axios' ;
55import AssignTableRow from '../Badge/AssignTableRow' ;
6- import { assignBadgesByUserID , clearNameAndSelected , addSelectBadge } from '../../actions/badgeManagement' ;
6+ import {
7+ assignBadgesByUserID ,
8+ clearNameAndSelected ,
9+ addSelectBadge ,
10+ } from '../../actions/badgeManagement' ;
711import { ENDPOINTS } from '~/utils/URL' ;
812import { boxStyle , boxStyleDark } from '../../styles' ;
913import { toast } from 'react-toastify' ;
1014import { PROTECTED_ACCOUNT_MODIFICATION_WARNING_MESSAGE } from '~/utils/constants' ;
15+ import { Spinner } from 'reactstrap' ;
1116
1217function AssignBadgePopup ( props ) {
13- const { darkMode} = props ;
18+ const { darkMode } = props ;
1419 const [ searchedName , setSearchedName ] = useState ( '' ) ;
1520 const [ badgeList , setBadgeList ] = useState ( [ ] ) ;
1621 // Added state to disable confirm button while updating.
1722 const [ shouldConfirmButtonDisable , setConfirmButtonDisable ] = useState ( false ) ;
23+ const [ isLoadingBadge , setisLoadingBadge ] = useState ( true ) ;
1824
1925 const onSearch = text => {
2026 setSearchedName ( text ) ;
@@ -53,18 +59,21 @@ function AssignBadgePopup(props) {
5359 try {
5460 const response = await axios . get ( ENDPOINTS . BADGE ( ) ) ;
5561 setBadgeList ( response . data ) ;
62+ setisLoadingBadge ( false ) ;
5663 } catch ( error ) { }
5764 } ;
5865
59- const filterBadges = ( allBadges = [ ] ) => {
60- // guard against non-array inputs
61- if ( ! Array . isArray ( allBadges ) ) return [ ] ;
62- return allBadges . filter ( ( { badgeName } ) =>
63- badgeName . toLowerCase ( ) . includes ( searchedName . toLowerCase ( ) )
64- ) ;
65- } ;
66+ const filterBadges = ( allBadges = [ ] ) => {
67+ // guard against non-array inputs
68+ if ( ! Array . isArray ( allBadges ) ) return [ ] ;
69+ return allBadges . filter ( ( { badgeName } ) =>
70+ badgeName . toLowerCase ( ) . includes ( searchedName . toLowerCase ( ) ) ,
71+ ) ;
72+ } ;
6673
67- let filteredBadges = filterBadges ( badgeList ) ;
74+ const filteredBadges = useMemo ( ( ) => {
75+ return filterBadges ( badgeList ) ;
76+ } , [ badgeList , searchedName ] ) ;
6877
6978 const addExistBadges = ( ) => {
7079 if ( props . userProfile && props . userProfile . badgeCollection ) {
@@ -78,8 +87,6 @@ function AssignBadgePopup(props) {
7887 } ;
7988 let existBadges = addExistBadges ( ) ;
8089
81-
82-
8390 return (
8491 < div data-testid = "test-assignbadgepopup" >
8592 < input
@@ -92,47 +99,72 @@ function AssignBadgePopup(props) {
9299 } }
93100 />
94101 < div style = { { overflowY : 'scroll' , height : '75vh' } } >
95- < Table data-testid = "test-badgeResults" className = { darkMode ? 'text-light' : '' } >
96- < thead >
97- < tr >
98- < th > Badge</ th >
99- < th > Name</ th >
100- < th style = { { zIndex : '10' } } >
101- < i className = "fa fa-info-circle" id = "SelectInfo" data-testid = "test-selectinfo" />
102- < UncontrolledTooltip
103- placement = "right"
104- target = "SelectInfo"
105- style = { { backgroundColor : '#666' , color : '#fff' } }
106- data-testid = "test-tooltip"
107- >
108- < p className = "badge_info_icon_text" data-testid = "test-tip1" >
109- Hmmm, little blank boxes... what could they mean? Yep, you guessed it, check
110- those boxes to select the badges you wish to assign a person. Click the
111- "Confirm" button at the bottom when you've selected all you wish
112- to add.
113- </ p >
114- < p className = "badge_info_icon_text" data-testid = "test-tip2" >
115- Want to assign multiple of the same badge to a person? Repeat the process!!
116- </ p >
117- </ UncontrolledTooltip >
118- </ th >
119- </ tr >
120- </ thead >
121- < tbody >
122- { filteredBadges . map ( ( value , index ) => (
123- < AssignTableRow badge = { value } index = { index } key = { index } existBadges = { existBadges } />
124- ) ) }
125- </ tbody >
126- </ Table >
102+ { ! isLoadingBadge && ( props . isTableOpen ?? filteredBadges . length > 0 ) ? (
103+ < Table data-testid = "test-badgeResults" className = { darkMode ? 'text-light' : '' } >
104+ < thead
105+ style = {
106+ darkMode
107+ ? { backgroundColor : '#1c2541' , color : '#fff' }
108+ : { backgroundColor : '#f0f8ff' , color : 'black' }
109+ }
110+ >
111+ < tr >
112+ < th > Badge</ th >
113+ < th > Name</ th >
114+ < th style = { { zIndex : '10' } } >
115+ < i className = "fa fa-info-circle" id = "SelectInfo" data-testid = "test-selectinfo" />
116+ < UncontrolledTooltip
117+ placement = "right"
118+ target = "SelectInfo"
119+ style = { { backgroundColor : '#666' , color : '#fff' } }
120+ data-testid = "test-tooltip"
121+ >
122+ < p className = "badge_info_icon_text" data-testid = "test-tip1" >
123+ Hmmm, little blank boxes... what could they mean? Yep, you guessed it, check
124+ those boxes to select the badges you wish to assign a person. Click the
125+ "Confirm" button at the bottom when you've selected all you
126+ wish to add.
127+ </ p >
128+ < p className = "badge_info_icon_text" data-testid = "test-tip2" >
129+ Want to assign multiple of the same badge to a person? Repeat the process!!
130+ </ p >
131+ </ UncontrolledTooltip >
132+ </ th >
133+ </ tr >
134+ </ thead >
135+ < tbody >
136+ { filteredBadges . map ( ( value , index ) => (
137+ < AssignTableRow badge = { value } index = { index } key = { index } existBadges = { existBadges } />
138+ ) ) }
139+ </ tbody >
140+ </ Table >
141+ ) : isLoadingBadge && filteredBadges . length === 0 ? (
142+ < div
143+ style = { { display : 'flex' , justifyContent : 'center' , alignItems : 'center' , gap : '10px' } }
144+ >
145+ < h3 className = { `text-center ${ darkMode ? 'text-light' : 'text-dark' } ` } >
146+ Loading Badges...
147+ </ h3 >
148+
149+ < Spinner color = "primary" />
150+ </ div >
151+ ) : (
152+ ! isLoadingBadge &&
153+ filteredBadges . length === 0 && (
154+ < h3 className = { `text-center ${ darkMode ? 'text-light' : 'text-dark' } ` } >
155+ No badges found
156+ </ h3 >
157+ )
158+ ) }
127159 </ div >
128160 < Button
129161 className = "btn--dark-sea-green float-right"
130- style = { darkMode ? { ...boxStyleDark , margin : 5 } : { ...boxStyle , margin : 5 } }
162+ style = { darkMode ? { ...boxStyleDark , margin : 5 } : { ...boxStyle , margin : 5 } }
131163 onClick = { assignBadges }
132164 disabled = { shouldConfirmButtonDisable }
133165 data-testid = "test-button"
134166 >
135- { ! shouldConfirmButtonDisable ? 'Confirm' : 'Updating...' }
167+ { ! shouldConfirmButtonDisable ? 'Confirm' : 'Updating...' }
136168 </ Button >
137169 </ div >
138170 ) ;
0 commit comments