From 28a29c3f67c96e41de7cdb9437782c00cd0f1a27 Mon Sep 17 00:00:00 2001
From: Belinda Marion Kobusingye <46527380+Codebmk@users.noreply.github.com>
Date: Fri, 24 Jan 2025 05:05:17 +0300
Subject: [PATCH 1/5] fix current org change
---
.../common/components/AQNumberCard/index.jsx | 2 +-
.../Dropdowns/OrganizationDropdown.jsx | 15 ++++-----
.../dataDownload/modules/DataDownload.jsx | 9 +++--
.../src/core/hooks/useGetActiveGroupId.jsx | 33 +++++++++++--------
4 files changed, 34 insertions(+), 25 deletions(-)
diff --git a/platform/src/common/components/AQNumberCard/index.jsx b/platform/src/common/components/AQNumberCard/index.jsx
index e9a7b64672..db48cd24bf 100644
--- a/platform/src/common/components/AQNumberCard/index.jsx
+++ b/platform/src/common/components/AQNumberCard/index.jsx
@@ -330,7 +330,7 @@ const AQNumberCard = ({ className = '' }) => {
[dispatch],
);
- if (loading) {
+ if (loading || isFetchingActiveGroup) {
return (
{
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 {
@@ -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) {
diff --git a/platform/src/common/components/Modal/dataDownload/modules/DataDownload.jsx b/platform/src/common/components/Modal/dataDownload/modules/DataDownload.jsx
index 47b7c9c9db..9faabf3b3f 100644
--- a/platform/src/common/components/Modal/dataDownload/modules/DataDownload.jsx
+++ b/platform/src/common/components/Modal/dataDownload/modules/DataDownload.jsx
@@ -58,6 +58,7 @@ const DataDownload = ({ onClose }) => {
id: activeGroupId,
title: groupTitle,
groupList,
+ loading: isFetchingActiveGroup,
} = useGetActiveGroup();
const preferencesData = useSelector(
(state) => state.defaults.individual_preferences,
@@ -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.
diff --git a/platform/src/core/hooks/useGetActiveGroupId.jsx b/platform/src/core/hooks/useGetActiveGroupId.jsx
index fd896c33a6..acea9d2f7d 100644
--- a/platform/src/core/hooks/useGetActiveGroupId.jsx
+++ b/platform/src/core/hooks/useGetActiveGroupId.jsx
@@ -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) {
From a4f28e9ee9dc696635d42226f3d32cdd8c89803e Mon Sep 17 00:00:00 2001
From: Belinda Marion Kobusingye <46527380+Codebmk@users.noreply.github.com>
Date: Fri, 24 Jan 2025 05:19:33 +0300
Subject: [PATCH 2/5] lowercase
---
platform/src/core/hooks/useGetActiveGroupId.jsx | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/platform/src/core/hooks/useGetActiveGroupId.jsx b/platform/src/core/hooks/useGetActiveGroupId.jsx
index acea9d2f7d..b58a41afc0 100644
--- a/platform/src/core/hooks/useGetActiveGroupId.jsx
+++ b/platform/src/core/hooks/useGetActiveGroupId.jsx
@@ -15,7 +15,9 @@ export function useGetActiveGroup() {
setLoading(true);
const matchingGroup = userInfo?.groups?.find(
- (group) => group.grp_title.toLowerCase() === chartData?.organizationName,
+ (group) =>
+ group.grp_title.toLowerCase() ===
+ chartData?.organizationName.toLowerCase(),
);
setActiveGroup(matchingGroup);
From 21eabe017eca746d4b1f1eca4cf99bba5fee6080 Mon Sep 17 00:00:00 2001
From: Belinda Marion Kobusingye <46527380+Codebmk@users.noreply.github.com>
Date: Fri, 24 Jan 2025 05:24:36 +0300
Subject: [PATCH 3/5] id consistency
---
platform/src/core/hooks/useGetActiveGroupId.jsx | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/platform/src/core/hooks/useGetActiveGroupId.jsx b/platform/src/core/hooks/useGetActiveGroupId.jsx
index b58a41afc0..ca8dfbc006 100644
--- a/platform/src/core/hooks/useGetActiveGroupId.jsx
+++ b/platform/src/core/hooks/useGetActiveGroupId.jsx
@@ -28,9 +28,9 @@ export function useGetActiveGroup() {
if (!userInfo || !userInfo.groups || !chartData?.organizationName) {
return {
loading,
- id: activeGroup?.id || null,
+ id: activeGroup?._id || null,
title: activeGroup?.grp_title || null,
- userID: userInfo?.id || null,
+ userID: userInfo?._id || null,
groupList: userInfo?.groups || [],
};
}
From 13c4d1116270cdd6f18abbe0f8e8b3527536e781 Mon Sep 17 00:00:00 2001
From: Belinda Marion Kobusingye <46527380+Codebmk@users.noreply.github.com>
Date: Fri, 24 Jan 2025 05:25:37 +0300
Subject: [PATCH 4/5] remove unncessaring loading state update
---
platform/src/core/hooks/useGetActiveGroupId.jsx | 4 ----
1 file changed, 4 deletions(-)
diff --git a/platform/src/core/hooks/useGetActiveGroupId.jsx b/platform/src/core/hooks/useGetActiveGroupId.jsx
index ca8dfbc006..3ab1f61849 100644
--- a/platform/src/core/hooks/useGetActiveGroupId.jsx
+++ b/platform/src/core/hooks/useGetActiveGroupId.jsx
@@ -7,10 +7,6 @@ export function useGetActiveGroup() {
const userInfo = useSelector((state) => state?.login?.userInfo);
const chartData = useSelector((state) => state.chart);
- useEffect(() => {
- setLoading(false);
- }, [userInfo]);
-
useEffect(() => {
setLoading(true);
From 4d78bb3ed13184d0680defc11d7974c5127bc370 Mon Sep 17 00:00:00 2001
From: Belinda Marion Kobusingye <46527380+Codebmk@users.noreply.github.com>
Date: Fri, 24 Jan 2025 05:30:47 +0300
Subject: [PATCH 5/5] remove duplicate org name matching logic
---
.../src/core/hooks/useGetActiveGroupId.jsx | 24 +++++++++++--------
1 file changed, 14 insertions(+), 10 deletions(-)
diff --git a/platform/src/core/hooks/useGetActiveGroupId.jsx b/platform/src/core/hooks/useGetActiveGroupId.jsx
index 3ab1f61849..0ab5dcba4f 100644
--- a/platform/src/core/hooks/useGetActiveGroupId.jsx
+++ b/platform/src/core/hooks/useGetActiveGroupId.jsx
@@ -1,6 +1,11 @@
import { useEffect, useState } from 'react';
import { useSelector } from 'react-redux';
+const findGroupByOrgName = (groups, orgName) =>
+ groups?.find(
+ (group) => group.grp_title.toLowerCase() === orgName?.toLowerCase(),
+ );
+
export function useGetActiveGroup() {
const [activeGroup, setActiveGroup] = useState(null);
const [loading, setLoading] = useState(true);
@@ -10,10 +15,9 @@ export function useGetActiveGroup() {
useEffect(() => {
setLoading(true);
- const matchingGroup = userInfo?.groups?.find(
- (group) =>
- group.grp_title.toLowerCase() ===
- chartData?.organizationName.toLowerCase(),
+ const matchingGroup = findGroupByOrgName(
+ userInfo?.groups,
+ chartData?.organizationName,
);
setActiveGroup(matchingGroup);
@@ -33,10 +37,9 @@ export function useGetActiveGroup() {
// Prioritize stored group if it exists in user's groups
if (chartData.organizationName) {
- const storedGroupInUserGroups = userInfo.groups.find(
- (group) =>
- group.grp_title.toLowerCase() ===
- chartData.organizationName.toLowerCase(),
+ const storedGroupInUserGroups = findGroupByOrgName(
+ userInfo?.groups,
+ chartData?.organizationName,
);
if (storedGroupInUserGroups) {
@@ -51,8 +54,9 @@ export function useGetActiveGroup() {
}
// Find group matching chart organization name
- const matchingGroup = userInfo.groups.find(
- (group) => group.grp_title === chartData.organizationName,
+ const matchingGroup = findGroupByOrgName(
+ userInfo?.groups,
+ chartData?.organizationName,
);
if (matchingGroup) {