Skip to content

Commit 97d8c5c

Browse files
Merge pull request #3887 from OneCommunityGlobal/venkataramanan_redirect_to_current_week_tab_on_timelog_click
Venkataramanan. Change timelog to redirect to current week tab on button clicks
2 parents 7552ade + 7b08e6c commit 97d8c5c

6 files changed

Lines changed: 61 additions & 43 deletions

File tree

src/components/Badge/Badge.jsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,15 @@ function Badge(props) {
5050
const generateBadgeText = (totalBadges, badgeCollection, personalBestMaxHrs) => {
5151
if (!totalBadges) return 'You have no badges. ';
5252

53-
const newBadges = badgeCollection.filter(
54-
value => Date.now() - new Date(value.lastModified).getTime() <= WEEK_DIFF,
55-
);
53+
const newBadges = (Array.isArray(badgeCollection) ? badgeCollection : [])
54+
.filter(b => b && b.lastModified)
55+
.filter(b => {
56+
const t = new Date(b.lastModified).getTime();
57+
return Number.isFinite(t) && Date.now() - t <= WEEK_DIFF;
58+
});
5659

5760
const roundedHours = Math.floor(personalBestMaxHrs);
58-
const personalMaxText = newBadges.find(badgeObj => badgeObj.badge.type === 'Personal Max')
61+
const personalMaxText = newBadges.find(badgeObj => badgeObj?.badge?.type === 'Personal Max')
5962
? ` and a personal best of ${roundedHours} ${roundedHours === 1 ? 'hour' : 'hours'} in a week`
6063
: '';
6164

@@ -71,7 +74,7 @@ function Badge(props) {
7174
if (badge?.badge?.badgeName === 'Personal Max' || badge?.badge?.type === 'Personal Max') {
7275
count += 1;
7376
} else {
74-
count += Number(badge.count);
77+
count += Number(badge?.count);
7578
}
7679
});
7780
setTotalBadge(Math.round(count));

src/components/Badge/NewBadges.jsx

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,24 @@ import './Badge.css';
66
function NewBadges(props) {
77
const filterBadges = allBadges => {
88
try {
9-
const filteredList = allBadges.filter(
10-
value => Date.now() - new Date(value.lastModified).getTime() <= WEEK_DIFF,
11-
);
12-
13-
filteredList &&
14-
filteredList.sort((a, b) => {
15-
if (a?.badge?.ranking === 0) return 1;
16-
if (b?.badge?.ranking === 0) return -1;
17-
if (a?.badge?.ranking > b?.badge?.ranking) return 1;
18-
if (a?.badge?.ranking < b?.badge?.ranking) return -1;
19-
if (a?.badge?.badgeName > b?.badge?.badgeName) return 1;
20-
if (a?.badge?.badgeName < b?.badge?.badgeName) return -1;
21-
return 0;
9+
const list = Array.isArray(allBadges) ? allBadges : [];
10+
const filteredList = list
11+
.filter(b => b && b.lastModified) // drop null/undefined/missing lastModified
12+
.filter(b => {
13+
const t = new Date(b.lastModified).getTime();
14+
return Number.isFinite(t) && Date.now() - t <= WEEK_DIFF;
2215
});
16+
17+
filteredList.sort((a, b) => {
18+
if (a?.badge?.ranking === 0) return 1;
19+
if (b?.badge?.ranking === 0) return -1;
20+
if (a?.badge?.ranking > b?.badge?.ranking) return 1;
21+
if (a?.badge?.ranking < b?.badge?.ranking) return -1;
22+
if (a?.badge?.badgeName > b?.badge?.badgeName) return 1;
23+
if (a?.badge?.badgeName < b?.badge?.badgeName) return -1;
24+
return 0;
25+
});
26+
2327
return filteredList;
2428
} catch (error) {
2529
console.log(error);

src/components/Header/Header.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ export function Header(props) {
368368
</NavLink>
369369
</NavItem>
370370
<NavItem className="responsive-spacing">
371-
<NavLink tag={Link} to="/timelog">
371+
<NavLink tag={Link} to="/timelog#currentWeek">
372372
<span className="dashboard-text-link">{TIMELOG}</span>
373373
</NavLink>
374374
</NavItem>

src/components/LeaderBoard/Leaderboard.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1027,7 +1027,7 @@ function LeaderBoard({
10271027
</td>
10281028
<td className="align-middle" aria-label="Description or purpose of the cell">
10291029
<Link
1030-
to={`/timelog/${item.personId}`}
1030+
to={`/timelog/${item.personId}#currentWeek`}
10311031
title={`TangibleEffort: ${item.tangibletime} hours`}
10321032
>
10331033
<Progress value={item.barprogress} color={item.barcolor} />

src/components/TeamMemberTasks/TeamMemberTask.jsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,10 @@ const TeamMemberTask = React.memo(
204204
data-testid="icon"
205205
/>
206206

207-
<Link to={`/timelog/${user.personId}`} className="timelog-info">
207+
<Link
208+
to={`/timelog/${user.personId}#currentWeek`}
209+
className="timelog-info"
210+
>
208211
<i
209212
className="fa fa-clock-o"
210213
aria-hidden="true"

src/components/Timelog/Timelog.jsx

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,11 @@ if (role === 'Volunteer' && userHaveTask.length > 0) {
212212
return tab;
213213
};
214214

215-
useEffect(() => {
216-
const tab = tabMapping[location.hash];
217-
if (tab !== undefined) {
218-
changeTab(tab);
219-
}
220-
}, [location.hash]); // This effect will run whenever the hash changes
215+
useEffect(() => {
216+
if (initialTab != null && !location.hash) {
217+
changeTab(initialTab);
218+
}
219+
}, [initialTab, location.hash]); // This effect will run whenever the hash changes
221220

222221
/* ---------------- methods -------------- */
223222
const updateTimeEntryItems = () => {
@@ -286,9 +285,21 @@ const generateAllTimeEntryItems = () => {
286285
const res = await axios.get(url);
287286

288287
const data = res.data.length > 0 ? res.data : [];
289-
const defaultTabValue = defaultTab(data);
290-
setTimeLogState({ ...timeLogState, isTimeEntriesLoading: false });
291-
setInitialTab(defaultTabValue);
288+
const mappedHash = tabMapping[location.hash];
289+
290+
if (mappedHash !== undefined) {
291+
// If the URL has a known hash, open that tab immediately
292+
setTimeLogState(s => ({
293+
...s,
294+
isTimeEntriesLoading: false,
295+
activeTab: mappedHash,
296+
}));
297+
setInitialTab(null); // so the initialTab effect won’t override
298+
} else {
299+
// No hash → fall back to your existing default logic
300+
setTimeLogState(s => ({ ...s, isTimeEntriesLoading: false }));
301+
setInitialTab(defaultTab(data));
302+
}
292303
} catch (e) {
293304
console.log(e);
294305
}
@@ -345,23 +356,16 @@ const generateAllTimeEntryItems = () => {
345356
}
346357

347358
// Clear the hash to trigger the useEffect on hash change
348-
if (location.hash) {
349-
window.location.hash = '';
350-
}
359+
// if (location.hash) {
360+
// window.location.hash = '';
361+
// }
351362

352363
setTimeLogState({
353364
...timeLogState,
354365
activeTab: tab,
355366
});
356367
};
357368

358-
useEffect(() => {
359-
const tab = tabMapping[location.hash];
360-
if (tab !== undefined) {
361-
changeTab(tab);
362-
}
363-
}, [location.hash]); // This effect will run whenever the hash changes
364-
365369
const handleInputChange = e => {
366370
setTimeLogState({ ...timeLogState, [e.target.name]: e.target.value });
367371
};
@@ -518,6 +522,13 @@ const generateAllTimeEntryItems = () => {
518522

519523
/* ---------------- useEffects -------------- */
520524

525+
useEffect(() => {
526+
const mapped = tabMapping[location.hash];
527+
if (mapped !== undefined) {
528+
setTimeLogState(s => ({ ...s, activeTab: mapped }));
529+
}
530+
}, [location.hash]);
531+
521532
// Update user ID if it changes in the URL
522533
useEffect(() => {
523534
if (urlId) {
@@ -543,9 +554,6 @@ const generateAllTimeEntryItems = () => {
543554
props.getBadgeCount(displayUserId);
544555
}, [displayUserId, props]);
545556

546-
useEffect(() => {
547-
changeTab(initialTab);
548-
}, [initialTab]);
549557

550558
useEffect(() => {
551559
// Build the time log after new data is loaded

0 commit comments

Comments
 (0)