Skip to content

Commit 68854d0

Browse files
authored
Merge pull request #2402 from airqo-platform/hotfix-demo-feedback
[Hotfix] Fix current org change
2 parents b50166a + 4d78bb3 commit 68854d0

File tree

4 files changed

+41
-30
lines changed

4 files changed

+41
-30
lines changed

platform/src/common/components/AQNumberCard/index.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ const AQNumberCard = ({ className = '' }) => {
330330
[dispatch],
331331
);
332332

333-
if (loading) {
333+
if (loading || isFetchingActiveGroup) {
334334
return (
335335
<div
336336
className={`grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4 ${className}`}

platform/src/common/components/Dropdowns/OrganizationDropdown.jsx

+7-8
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,12 @@ const OrganizationDropdown = () => {
5959
setLoading(true);
6060
setSelectedGroupId(group._id);
6161
try {
62-
const response = await dispatch(
62+
await dispatch(
6363
updateUserPreferences({
6464
user_id: userID,
6565
group_id: group._id,
6666
}),
6767
);
68-
if (response?.payload?.success) {
69-
localStorage.setItem('activeGroup', JSON.stringify(group));
70-
dispatch(setOrganizationName(group.grp_title));
71-
} else {
72-
console.warn('Failed to update user preferences');
73-
}
7468
} catch (error) {
7569
console.error('Error updating user preferences:', error);
7670
} finally {
@@ -84,10 +78,15 @@ const OrganizationDropdown = () => {
8478
const handleDropdownSelect = useCallback(
8579
(group) => {
8680
if (group?._id !== activeGroupId) {
81+
// Immediately update organization name
82+
dispatch(setOrganizationName(group.grp_title));
83+
localStorage.setItem('activeGroup', JSON.stringify(group));
84+
85+
// Dispatch preferences update asynchronously
8786
handleUpdatePreferences(group);
8887
}
8988
},
90-
[activeGroupId, handleUpdatePreferences],
89+
[activeGroupId, handleUpdatePreferences, dispatch],
9190
);
9291

9392
if (!activeGroupId || groupList.length === 0) {

platform/src/common/components/Modal/dataDownload/modules/DataDownload.jsx

+7-2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ const DataDownload = ({ onClose }) => {
5858
id: activeGroupId,
5959
title: groupTitle,
6060
groupList,
61+
loading: isFetchingActiveGroup,
6162
} = useGetActiveGroup();
6263
const preferencesData = useSelector(
6364
(state) => state.defaults.individual_preferences,
@@ -128,12 +129,16 @@ const DataDownload = ({ onClose }) => {
128129
* Fetch sites summary whenever the selected organization changes.
129130
*/
130131
useEffect(() => {
132+
if (isFetchingActiveGroup) return;
133+
131134
if (formData.organization) {
132135
dispatch(
133-
fetchSitesSummary({ group: formData.organization.name.toLowerCase() }),
136+
fetchSitesSummary({
137+
group: formData.organization.name.toLowerCase(),
138+
}),
134139
);
135140
}
136-
}, [dispatch, formData.organization]);
141+
}, [dispatch, formData.organization, isFetchingActiveGroup]);
137142

138143
/**
139144
* Clears all selected sites and resets form data.

platform/src/core/hooks/useGetActiveGroupId.jsx

+26-19
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,45 @@
1-
import { useEffect, useMemo, useState } from 'react';
1+
import { useEffect, useState } from 'react';
22
import { useSelector } from 'react-redux';
33

4+
const findGroupByOrgName = (groups, orgName) =>
5+
groups?.find(
6+
(group) => group.grp_title.toLowerCase() === orgName?.toLowerCase(),
7+
);
8+
49
export function useGetActiveGroup() {
10+
const [activeGroup, setActiveGroup] = useState(null);
511
const [loading, setLoading] = useState(true);
612
const userInfo = useSelector((state) => state?.login?.userInfo);
713
const chartData = useSelector((state) => state.chart);
814

9-
const activeGroupFromStorage = useMemo(() => {
10-
try {
11-
return JSON.parse(localStorage.getItem('activeGroup') || 'null');
12-
} catch (error) {
13-
console.error('Error parsing activeGroup from local storage:', error);
14-
return null;
15-
}
16-
}, []);
17-
1815
useEffect(() => {
16+
setLoading(true);
17+
18+
const matchingGroup = findGroupByOrgName(
19+
userInfo?.groups,
20+
chartData?.organizationName,
21+
);
22+
23+
setActiveGroup(matchingGroup);
1924
setLoading(false);
20-
}, [userInfo, activeGroupFromStorage]);
25+
}, [chartData?.organizationName]);
2126

2227
// If no userInfo or groups, return stored or default values
2328
if (!userInfo || !userInfo.groups || !chartData?.organizationName) {
2429
return {
2530
loading,
26-
id: activeGroupFromStorage?.id || null,
27-
title: activeGroupFromStorage?.grp_title || null,
28-
userID: userInfo?.id || null,
31+
id: activeGroup?._id || null,
32+
title: activeGroup?.grp_title || null,
33+
userID: userInfo?._id || null,
2934
groupList: userInfo?.groups || [],
3035
};
3136
}
3237

3338
// Prioritize stored group if it exists in user's groups
34-
if (activeGroupFromStorage) {
35-
const storedGroupInUserGroups = userInfo.groups.find(
36-
(group) => group._id === activeGroupFromStorage._id,
39+
if (chartData.organizationName) {
40+
const storedGroupInUserGroups = findGroupByOrgName(
41+
userInfo?.groups,
42+
chartData?.organizationName,
3743
);
3844

3945
if (storedGroupInUserGroups) {
@@ -48,8 +54,9 @@ export function useGetActiveGroup() {
4854
}
4955

5056
// Find group matching chart organization name
51-
const matchingGroup = userInfo.groups.find(
52-
(group) => group.grp_title === chartData.organizationName,
57+
const matchingGroup = findGroupByOrgName(
58+
userInfo?.groups,
59+
chartData?.organizationName,
5360
);
5461

5562
if (matchingGroup) {

0 commit comments

Comments
 (0)