11import axios from 'axios' ;
22import { toast } from 'react-toastify' ;
33import { formatDate } from '~/utils/formatDate' ;
4+ import { ENDPOINTS } from '~/utils/URL' ;
45import {
5- GET_ALL_BADGE_DATA ,
66 ADD_SELECT_BADGE ,
7- REMOVE_SELECT_BADGE ,
87 CLEAR_NAME_AND_SELECTED ,
98 CLEAR_SELECTED ,
9+ CLOSE_ALERT ,
10+ GET_ALL_BADGE_DATA ,
11+ GET_BADGE_COUNT ,
1012 GET_FIRST_NAME ,
1113 GET_LAST_NAME ,
1214 GET_MESSAGE ,
13- CLOSE_ALERT ,
1415 GET_USER_ID ,
15- GET_BADGE_COUNT ,
16+ REMOVE_SELECT_BADGE ,
1617 RESET_BADGE_COUNT ,
1718 SET_ACTIVE_TAB ,
1819} from '../constants/badge' ;
19- import { ENDPOINTS } from '~/utils/URL' ;
2020
2121export const ALERT_DELAY = process . env . NODE_ENV === 'test' ? 0 : 6000 ;
2222
2323const getAllBadges = allBadges => {
2424 const action = {
2525 type : GET_ALL_BADGE_DATA ,
2626 allBadges,
27- } ;
27+ } ;
2828 return action ;
2929} ;
3030
@@ -34,13 +34,13 @@ export const fetchAllBadges = (forceRefresh = false) => {
3434 // Check the endpoint
3535 const baseUrl = ENDPOINTS . BADGE ( ) ;
3636 const url = forceRefresh ? `${ baseUrl } ?t=${ Date . now ( ) } ` : baseUrl ;
37-
37+
3838 const response = await axios . get ( url ) ;
39-
39+
4040 const actionResult = getAllBadges ( response . data ) ;
41-
41+
4242 dispatch ( actionResult ) ;
43-
43+
4444 return response . status ;
4545 } catch ( err ) {
4646 return err . response ?. status || 500 ;
@@ -168,7 +168,7 @@ export const returnUpdatedBadgesCollection = (badgeCollection, selectedBadgesId)
168168 const currentTs = Date . now ( ) ;
169169 const currentDate = formatDate ( ) ;
170170
171- for ( let i = 0 ; i < newBadgeCollection . length ; i += 1 ) {
171+ for ( let i = 0 ; i < newBadgeCollection . length ; i += 1 ) {
172172 const badgeObj = newBadgeCollection [ i ] ;
173173 if ( badgeId === badgeObj . badge ) {
174174 // If the badge is found, increment the count and mark it as included
@@ -200,45 +200,30 @@ export const returnUpdatedBadgesCollection = (badgeCollection, selectedBadgesId)
200200} ;
201201
202202export const returnUpdatedBadgesCollectionSingleUser = ( badgeCollection , selectedBadgesId ) => {
203- let newBadgeCollection = Array . from ( badgeCollection ) ;
204-
205- const updatedOrAddedBadges = { } ;
203+ const newBadgeCollection = badgeCollection . map ( b => ( {
204+ ...b ,
205+ earnedDate : [ ...( b . earnedDate || [ ] ) ] ,
206+ badge : b . badge && typeof b . badge === 'object' ? b . badge . _id : b . badge ,
207+ } ) ) ;
208+ const currentTs = Date . now ( ) ;
209+ const currentDate = formatDate ( ) ;
206210
207211 selectedBadgesId . forEach ( originalBadgeId => {
208212 let badgeId = originalBadgeId ;
209213 if ( badgeId . includes ( 'assign-badge-' ) ) badgeId = badgeId . replace ( 'assign-badge-' , '' ) ;
210214
211- if ( ! updatedOrAddedBadges [ badgeId ] ) {
212- let included = false ;
213- const currentTs = Date . now ( ) ;
214- const currentDate = formatDate ( ) ;
215-
216- newBadgeCollection = newBadgeCollection . map ( badgeObj => {
217- if ( badgeId === badgeObj . badge ) {
218- if ( ! included ) {
219- included = true ;
220- updatedOrAddedBadges [ badgeId ] = true ;
221- return {
222- ...badgeObj ,
223- count : badgeObj . count ? badgeObj . count + 1 : 1 ,
224- lastModified : currentTs ,
225- earnedDate : [ ...badgeObj . earnedDate , currentDate ]
226- } ;
227- }
228- updatedOrAddedBadges [ badgeId ] = true ;
229- }
230- return badgeObj ;
231- } ) ;
232-
233- if ( ! included ) {
234- newBadgeCollection . push ( {
235- badge : badgeId ,
236- count : 1 ,
237- lastModified : currentTs ,
238- earnedDate : [ currentDate ] ,
239- } ) ;
240- updatedOrAddedBadges [ badgeId ] = true ;
241- }
215+ const existing = newBadgeCollection . find ( b => b . badge === badgeId ) ;
216+ if ( existing ) {
217+ existing . count = ( existing . count || 0 ) + 1 ;
218+ existing . lastModified = currentTs ;
219+ existing . earnedDate . push ( currentDate ) ;
220+ } else {
221+ newBadgeCollection . push ( {
222+ badge : badgeId ,
223+ count : 1 ,
224+ lastModified : currentTs ,
225+ earnedDate : [ currentDate ] ,
226+ } ) ;
242227 }
243228 } ) ;
244229
@@ -276,8 +261,10 @@ export const assignBadgesByUserID = (userId, selectedBadges) => {
276261 return ;
277262 }
278263 const { badgeCollection } = res . data ;
279- for ( let i = 0 ; i < badgeCollection . length ; i += 1 ) {
280- badgeCollection [ i ] . badge = badgeCollection [ i ] . badge . _id ;
264+ for ( let i = 0 ; i < badgeCollection . length ; i += 1 ) {
265+ if ( badgeCollection [ i ] . badge && typeof badgeCollection [ i ] . badge === 'object' ) {
266+ badgeCollection [ i ] . badge = badgeCollection [ i ] . badge . _id ;
267+ }
281268 }
282269
283270 const userToBeAssignedBadge = res . data . _id ;
0 commit comments