-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Hotfix] Fix current org change #2402
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,39 +1,46 @@ | ||||||||||||||||||||||||||||||||||||
import { useEffect, useMemo, useState } from 'react'; | ||||||||||||||||||||||||||||||||||||
import { useEffect, useState } from 'react'; | ||||||||||||||||||||||||||||||||||||
import { useSelector } from 'react-redux'; | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
export function useGetActiveGroup() { | ||||||||||||||||||||||||||||||||||||
const [activeGroup, setActiveGroup] = useState(null); | ||||||||||||||||||||||||||||||||||||
const [loading, setLoading] = useState(true); | ||||||||||||||||||||||||||||||||||||
const userInfo = useSelector((state) => state?.login?.userInfo); | ||||||||||||||||||||||||||||||||||||
const chartData = useSelector((state) => state.chart); | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
const activeGroupFromStorage = useMemo(() => { | ||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||
return JSON.parse(localStorage.getItem('activeGroup') || 'null'); | ||||||||||||||||||||||||||||||||||||
} catch (error) { | ||||||||||||||||||||||||||||||||||||
console.error('Error parsing activeGroup from local storage:', error); | ||||||||||||||||||||||||||||||||||||
return null; | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
}, []); | ||||||||||||||||||||||||||||||||||||
useEffect(() => { | ||||||||||||||||||||||||||||||||||||
setLoading(false); | ||||||||||||||||||||||||||||||||||||
}, [userInfo]); | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential race condition in loading state management. The first useEffect blindly sets - useEffect(() => {
- setLoading(false);
- }, [userInfo]); |
||||||||||||||||||||||||||||||||||||
useEffect(() => { | ||||||||||||||||||||||||||||||||||||
setLoading(true); | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
const matchingGroup = userInfo?.groups?.find( | ||||||||||||||||||||||||||||||||||||
(group) => | ||||||||||||||||||||||||||||||||||||
group.grp_title.toLowerCase() === | ||||||||||||||||||||||||||||||||||||
chartData?.organizationName.toLowerCase(), | ||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
setActiveGroup(matchingGroup); | ||||||||||||||||||||||||||||||||||||
setLoading(false); | ||||||||||||||||||||||||||||||||||||
}, [userInfo, activeGroupFromStorage]); | ||||||||||||||||||||||||||||||||||||
}, [chartData?.organizationName]); | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
// If no userInfo or groups, return stored or default values | ||||||||||||||||||||||||||||||||||||
if (!userInfo || !userInfo.groups || !chartData?.organizationName) { | ||||||||||||||||||||||||||||||||||||
return { | ||||||||||||||||||||||||||||||||||||
loading, | ||||||||||||||||||||||||||||||||||||
id: activeGroupFromStorage?.id || null, | ||||||||||||||||||||||||||||||||||||
title: activeGroupFromStorage?.grp_title || null, | ||||||||||||||||||||||||||||||||||||
id: activeGroup?.id || null, | ||||||||||||||||||||||||||||||||||||
title: activeGroup?.grp_title || null, | ||||||||||||||||||||||||||||||||||||
userID: userInfo?.id || null, | ||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inconsistent ID property access across the hook. The hook uses both - id: activeGroup?.id || null,
+ id: activeGroup?._id || null, Also, consider consolidating the return object creation into a helper function to ensure consistency: const createReturnObject = (group, loading, userInfo) => ({
loading,
id: group?._id || null,
title: group?.grp_title || null,
userID: userInfo?._id || null,
groupList: userInfo?.groups || [],
}); Also applies to: 49-52 |
||||||||||||||||||||||||||||||||||||
groupList: userInfo?.groups || [], | ||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
// Prioritize stored group if it exists in user's groups | ||||||||||||||||||||||||||||||||||||
if (activeGroupFromStorage) { | ||||||||||||||||||||||||||||||||||||
if (chartData.organizationName) { | ||||||||||||||||||||||||||||||||||||
const storedGroupInUserGroups = userInfo.groups.find( | ||||||||||||||||||||||||||||||||||||
(group) => group._id === activeGroupFromStorage._id, | ||||||||||||||||||||||||||||||||||||
(group) => | ||||||||||||||||||||||||||||||||||||
group.grp_title.toLowerCase() === | ||||||||||||||||||||||||||||||||||||
chartData.organizationName.toLowerCase(), | ||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Duplicate group matching logic detected. The case-insensitive group matching logic is duplicated. Consider extracting it into a helper function. + const findGroupByOrgName = (groups, orgName) =>
+ groups?.find(group =>
+ group.grp_title.toLowerCase() === orgName?.toLowerCase()
+ );
- const storedGroupInUserGroups = userInfo.groups.find(
- (group) =>
- group.grp_title.toLowerCase() ===
- chartData.organizationName.toLowerCase(),
- );
+ const storedGroupInUserGroups = findGroupByOrgName(
+ userInfo.groups,
+ chartData.organizationName
+ ); 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||
if (storedGroupInUserGroups) { | ||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Synchronizing local and remote states.
Immediately updating local storage and Redux could lead to mismatches if the update request fails. Consider deferring local storage updates until the dispatch call succeeds, or handle failures more robustly.