Skip to content

Commit 1d6091c

Browse files
committed
fix(phase/4-5-6): resolve all bugs in badge system combined PR
- ProfileSidebar: define missing clanBadgeCount from unlocked chief badges - Badges.jsx: normalize featuredBadgeId to string (handles populated objects) - Badges.jsx: fix isFeatured comparison with .toString() - Badges.jsx: remove unused userProfile query (dead code) - NotificationListener: replace dynamic CJS require() with top-level ESM import - NotificationListener: fix unused variable lint errors - badge.routes.js: remove stray 'Trigger nodemon restart' comment - auth.controller: populate featuredBadge in getMe so /api/auth/me returns it
1 parent 7541622 commit 1d6091c

5 files changed

Lines changed: 14 additions & 16 deletions

File tree

client/src/components/NotificationListener.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import toast from 'react-hot-toast';
33
import { useNavigate } from 'react-router-dom';
44
import { useSocket } from '../hooks/useSocket';
55
import { FiZap, FiAward } from 'react-icons/fi';
6+
import { api } from '../lib/api';
67
import { useAuth } from '../context/useAuth';
78

89
const NotificationListener = () => {
@@ -33,7 +34,7 @@ const NotificationListener = () => {
3334
});
3435

3536
// Listen for leaderboard updates (optional: show a generic toast or just let the page handle it)
36-
useSocket('leaderboard_update', (data) => {
37+
useSocket('leaderboard_update', () => {
3738
// Other components (like Leaderboard/Clans) will refetch automatically
3839
});
3940

@@ -47,7 +48,6 @@ const NotificationListener = () => {
4748
const checkBadges = React.useCallback(async (notify = false) => {
4849
if (!user) return;
4950
try {
50-
const { api } = require('../lib/api');
5151
const res = await api.get('/api/badges');
5252
const badges = res.data?.data || [];
5353
const newlyUnlocked = [];
@@ -90,6 +90,7 @@ const NotificationListener = () => {
9090
});
9191
});
9292
}
93+
// eslint-disable-next-line no-unused-vars
9394
} catch (e) {
9495
// ignore
9596
}

client/src/components/ProfileSidebar.jsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ const ProfileSidebar = ({ user, summary, profile, badges }) => {
165165
});
166166
};
167167

168+
const clanBadgeCount = React.useMemo(() => {
169+
return (badges || []).filter(b => b.isChiefBadge && b.isUnlocked).length;
170+
}, [badges]);
171+
168172
const sortedBadges = React.useMemo(() => {
169173
const baseBadges = badges?.length
170174
? badges

client/src/pages/Badges.jsx

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ const CategorySection = ({ category, badges, index, onSetFeatured, featuredBadge
197197
badge={badge}
198198
index={i}
199199
onSetFeatured={onSetFeatured}
200-
isFeatured={featuredBadgeId === badge._id}
200+
isFeatured={featuredBadgeId === badge._id?.toString()}
201201
/>
202202
))}
203203
</div>
@@ -220,16 +220,7 @@ const Badges = () => {
220220
staleTime: 60000,
221221
});
222222

223-
const { data: userProfile } = useQuery({
224-
queryKey: ['user-profile'],
225-
queryFn: async () => {
226-
const res = await api.get('/api/users/me'); // Or standard route
227-
return res.data.data;
228-
},
229-
staleTime: 60000,
230-
});
231-
232-
// We actually need the user's featuredBadge ID.
223+
// We actually need the user's featuredBadge ID.
233224
// We can fetch it via /api/profile/stats which returns user info or /api/auth/me
234225
// Wait, the currently logged in user is in auth context. But we can just use the auth/me endpoint or profile
235226
const { data: authUser } = useQuery({
@@ -241,7 +232,10 @@ const Badges = () => {
241232
staleTime: 60000,
242233
});
243234

244-
const featuredBadgeId = authUser?.featuredBadge;
235+
// featuredBadge may be a populated object or a raw ID string
236+
const featuredBadgeId = authUser?.featuredBadge?._id
237+
? authUser.featuredBadge._id.toString()
238+
: authUser?.featuredBadge?.toString() || null;
245239

246240
const setFeaturedMutation = useMutation({
247241
mutationFn: async (badgeId) => {

server/src/features/auth/auth.controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ const logoutAll = async (req, res, next) => {
549549

550550
const getMe = async (req, res, next) => {
551551
try {
552-
const user = await User.findById(req.user.id).populate('clan', 'name').lean();
552+
const user = await User.findById(req.user.id).populate('clan', 'name').populate('featuredBadge').lean();
553553
if (!user) {
554554
res.status(404);
555555
throw new Error('User not found');

server/src/features/badges/badge.routes.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,3 @@ router.post('/award/:userId', protect, awardBadge);
1919
router.delete('/revoke/:userId/:badgeId', protect, revokeBadge);
2020

2121
module.exports = router;
22-
// Trigger nodemon restart

0 commit comments

Comments
 (0)