Skip to content

Commit

Permalink
fix current org change
Browse files Browse the repository at this point in the history
  • Loading branch information
Codebmk committed Jan 24, 2025
1 parent 9d1de41 commit 28a29c3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 25 deletions.
2 changes: 1 addition & 1 deletion platform/src/common/components/AQNumberCard/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ const AQNumberCard = ({ className = '' }) => {
[dispatch],
);

if (loading) {
if (loading || isFetchingActiveGroup) {
return (
<div
className={`grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4 ${className}`}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,12 @@ const OrganizationDropdown = () => {
setLoading(true);
setSelectedGroupId(group._id);
try {
const response = await dispatch(
await dispatch(
updateUserPreferences({
user_id: userID,
group_id: group._id,
}),
);
if (response?.payload?.success) {
localStorage.setItem('activeGroup', JSON.stringify(group));
dispatch(setOrganizationName(group.grp_title));
} else {
console.warn('Failed to update user preferences');
}
} catch (error) {
console.error('Error updating user preferences:', error);
} finally {
Expand All @@ -84,10 +78,15 @@ const OrganizationDropdown = () => {
const handleDropdownSelect = useCallback(
(group) => {
if (group?._id !== activeGroupId) {
// Immediately update organization name
dispatch(setOrganizationName(group.grp_title));
localStorage.setItem('activeGroup', JSON.stringify(group));

// Dispatch preferences update asynchronously
handleUpdatePreferences(group);
}
},
[activeGroupId, handleUpdatePreferences],
[activeGroupId, handleUpdatePreferences, dispatch],
);

if (!activeGroupId || groupList.length === 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const DataDownload = ({ onClose }) => {
id: activeGroupId,
title: groupTitle,
groupList,
loading: isFetchingActiveGroup,
} = useGetActiveGroup();
const preferencesData = useSelector(
(state) => state.defaults.individual_preferences,
Expand Down Expand Up @@ -128,12 +129,16 @@ const DataDownload = ({ onClose }) => {
* Fetch sites summary whenever the selected organization changes.
*/
useEffect(() => {
if (isFetchingActiveGroup) return;

if (formData.organization) {
dispatch(
fetchSitesSummary({ group: formData.organization.name.toLowerCase() }),
fetchSitesSummary({
group: formData.organization.name.toLowerCase(),
}),
);
}
}, [dispatch, formData.organization]);
}, [dispatch, formData.organization, isFetchingActiveGroup]);

/**
* Clears all selected sites and resets form data.
Expand Down
33 changes: 19 additions & 14 deletions platform/src/core/hooks/useGetActiveGroupId.jsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,44 @@
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]);

useEffect(() => {
setLoading(true);

const matchingGroup = userInfo?.groups?.find(
(group) => group.grp_title.toLowerCase() === chartData?.organizationName,
);

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,
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(),
);

if (storedGroupInUserGroups) {
Expand Down

0 comments on commit 28a29c3

Please sign in to comment.