Skip to content

Commit c7e2e3c

Browse files
committed
peterson-implement-no-badges-found-message
1 parent a715a63 commit c7e2e3c

1 file changed

Lines changed: 76 additions & 46 deletions

File tree

src/components/UserProfile/AssignBadgePopup.jsx

Lines changed: 76 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,32 @@ import { Table, Button, UncontrolledTooltip } from 'reactstrap';
33
import { connect } from 'react-redux';
44
import axios from 'axios';
55
import AssignTableRow from '../Badge/AssignTableRow';
6-
import { assignBadgesByUserID, clearNameAndSelected, addSelectBadge } from '../../actions/badgeManagement';
6+
import {
7+
assignBadgesByUserID,
8+
clearNameAndSelected,
9+
addSelectBadge,
10+
} from '../../actions/badgeManagement';
711
import { ENDPOINTS } from '~/utils/URL';
812
import { boxStyle, boxStyleDark } from '../../styles';
913
import { toast } from 'react-toastify';
1014
import { PROTECTED_ACCOUNT_MODIFICATION_WARNING_MESSAGE } from '~/utils/constants';
15+
import { Spinner } from 'reactstrap';
1116

1217
function 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);
2127
};
2228

2329
// Update: Added toast message effect for success and error. Added restriction: Jae's badges only editable by Jae or Owner
2430
const assignBadges = async () => {
25-
if(props.isRecordBelongsToJaeAndUneditable){
31+
if (props.isRecordBelongsToJaeAndUneditable) {
2632
alert(PROTECTED_ACCOUNT_MODIFICATION_WARNING_MESSAGE);
2733
return;
2834
}
@@ -52,16 +58,17 @@ function AssignBadgePopup(props) {
5258
try {
5359
const response = await axios.get(ENDPOINTS.BADGE());
5460
setBadgeList(response.data);
61+
setisLoadingBadge(false);
5562
} catch (error) {}
5663
};
5764

58-
const filterBadges = (allBadges = []) => {
59-
// guard against non-array inputs
60-
if (!Array.isArray(allBadges)) return [];
61-
return allBadges.filter(({ badgeName }) =>
62-
badgeName.toLowerCase().includes(searchedName.toLowerCase())
63-
);
64-
};
65+
const filterBadges = (allBadges = []) => {
66+
// guard against non-array inputs
67+
if (!Array.isArray(allBadges)) return [];
68+
return allBadges.filter(({ badgeName }) =>
69+
badgeName.toLowerCase().includes(searchedName.toLowerCase()),
70+
);
71+
};
6572

6673
let filteredBadges = filterBadges(badgeList);
6774

@@ -77,8 +84,6 @@ function AssignBadgePopup(props) {
7784

7885
let existBadges = addExistBadges();
7986

80-
81-
8287
return (
8388
<div data-testid="test-assignbadgepopup">
8489
<input
@@ -91,47 +96,72 @@ function AssignBadgePopup(props) {
9196
}}
9297
/>
9398
<div style={{ overflowY: 'scroll', height: '75vh' }}>
94-
<Table data-testid="test-badgeResults" className={darkMode ? 'text-light' : ''}>
95-
<thead>
96-
<tr>
97-
<th>Badge</th>
98-
<th>Name</th>
99-
<th style={{ zIndex: '10' }}>
100-
<i className="fa fa-info-circle" id="SelectInfo" data-testid="test-selectinfo" />
101-
<UncontrolledTooltip
102-
placement="right"
103-
target="SelectInfo"
104-
style={{ backgroundColor: '#666', color: '#fff' }}
105-
data-testid="test-tooltip"
106-
>
107-
<p className="badge_info_icon_text" data-testid="test-tip1">
108-
Hmmm, little blank boxes... what could they mean? Yep, you guessed it, check
109-
those boxes to select the badges you wish to assign a person. Click the
110-
&quot;Confirm&quot; button at the bottom when you&apos;ve selected all you wish
111-
to add.
112-
</p>
113-
<p className="badge_info_icon_text" data-testid="test-tip2">
114-
Want to assign multiple of the same badge to a person? Repeat the process!!
115-
</p>
116-
</UncontrolledTooltip>
117-
</th>
118-
</tr>
119-
</thead>
120-
<tbody>
121-
{filteredBadges.map((value, index) => (
122-
<AssignTableRow badge={value} index={index} key={index} existBadges={existBadges} />
123-
))}
124-
</tbody>
125-
</Table>
99+
{filteredBadges.length > 0 ? (
100+
<Table data-testid="test-badgeResults" className={darkMode ? 'text-light' : ''}>
101+
<thead>
102+
<tr>
103+
<th>Badge</th>
104+
<th>Name</th>
105+
<th style={{ zIndex: '10' }}>
106+
<i className="fa fa-info-circle" id="SelectInfo" data-testid="test-selectinfo" />
107+
<UncontrolledTooltip
108+
placement="right"
109+
target="SelectInfo"
110+
style={{ backgroundColor: '#666', color: '#fff' }}
111+
data-testid="test-tooltip"
112+
>
113+
<p className="badge_info_icon_text" data-testid="test-tip1">
114+
Hmmm, little blank boxes... what could they mean? Yep, you guessed it, check
115+
those boxes to select the badges you wish to assign a person. Click the
116+
&quot;Confirm&quot; button at the bottom when you&apos;ve selected all you
117+
wish to add.
118+
</p>
119+
<p className="badge_info_icon_text" data-testid="test-tip2">
120+
Want to assign multiple of the same badge to a person? Repeat the process!!
121+
</p>
122+
</UncontrolledTooltip>
123+
</th>
124+
</tr>
125+
</thead>
126+
<tbody>
127+
{!isLoadingBadge &&
128+
filteredBadges.map((value, index) => (
129+
<AssignTableRow
130+
badge={value}
131+
index={index}
132+
key={index}
133+
existBadges={existBadges}
134+
/>
135+
))}
136+
</tbody>
137+
</Table>
138+
) : isLoadingBadge && filteredBadges.length === 0 ? (
139+
<div
140+
style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', gap: '10px' }}
141+
>
142+
<h3 className={`text-center ${darkMode ? 'text-light' : 'text-dark'}`}>
143+
Loading Badges...
144+
</h3>
145+
146+
<Spinner />
147+
</div>
148+
) : (
149+
!isLoadingBadge &&
150+
filteredBadges.length === 0 && (
151+
<h3 className={`text-center ${darkMode ? 'text-light' : 'text-dark'}`}>
152+
No badges found
153+
</h3>
154+
)
155+
)}
126156
</div>
127157
<Button
128158
className="btn--dark-sea-green float-right"
129-
style={darkMode ? {...boxStyleDark, margin: 5 } : { ...boxStyle, margin: 5 }}
159+
style={darkMode ? { ...boxStyleDark, margin: 5 } : { ...boxStyle, margin: 5 }}
130160
onClick={assignBadges}
131161
disabled={shouldConfirmButtonDisable}
132162
data-testid="test-button"
133163
>
134-
{!shouldConfirmButtonDisable ? 'Confirm' : 'Updating...'}
164+
{!shouldConfirmButtonDisable ? 'Confirm' : 'Updating...'}
135165
</Button>
136166
</div>
137167
);

0 commit comments

Comments
 (0)