diff --git a/src/components/screens/LeaderboardScreen.js b/src/components/screens/LeaderboardScreen.js index 254faf9..e84061d 100644 --- a/src/components/screens/LeaderboardScreen.js +++ b/src/components/screens/LeaderboardScreen.js @@ -13,6 +13,9 @@ const LeaderboardScreen = ({navigation, route}) => { const isAdmin = session.isAcceptedAdmin; const isBusiness = Boolean(session.isBusiness); const selectedDepartment = route?.params?.department || null; + const effectiveSGid = isBusiness + ? ((isAdmin && selectedDepartment?.SGid) ? selectedDepartment.SGid : (isAdmin ? null : session.currentSGid)) + : null; const {getLobby, getSubgroup, getTeams, getTeamMembers, getGroupMembers, getCaches, getUser, deleteTeam, resetPlayerProgress, resetTeamProgress, getSubgroups} = useGameHook(); // State ---------------------- @@ -34,10 +37,6 @@ const LeaderboardScreen = ({navigation, route}) => { const group = await getLobby(session.currentGid); - const effectiveSGid = isBusiness - ? ((isAdmin && selectedDepartment?.SGid) ? selectedDepartment.SGid : (isAdmin ? null : session.currentSGid)) - : null; - let isTeams = Boolean(group?.TeamsEnabled); if (effectiveSGid !== null && effectiveSGid !== undefined) { const subgroup = await getSubgroup(effectiveSGid); @@ -117,7 +116,7 @@ const LeaderboardScreen = ({navigation, route}) => { } setLoading(false); - }, [session.currentGid, session.currentTid, session.currentSGid, isBusiness, isAdmin, selectedDepartment, selectedDepartment?.SGid]); + }, [session.currentGid, session.currentTid, session.currentSGid, isBusiness, isAdmin, selectedDepartment, selectedDepartment?.SGid, effectiveSGid]); useEffect(() => { loadData(); }, [loadData]); @@ -144,7 +143,7 @@ const LeaderboardScreen = ({navigation, route}) => { text: 'Reset', style: 'destructive', onPress: async () => { - await resetPlayerProgress(session.currentGid, player.uid); + await resetPlayerProgress(session.currentGid, player.uid, effectiveSGid); Alert.alert('Done', `Progress for ${player.name} has been reset.`); await loadData(); }, @@ -159,7 +158,7 @@ const LeaderboardScreen = ({navigation, route}) => { text: 'Reset', style: 'destructive', onPress: async () => { - await resetTeamProgress(session.currentGid, team.tid); + await resetTeamProgress(session.currentGid, team.tid, effectiveSGid); Alert.alert('Done', `Progress for team "${team.name}" has been reset.`); await loadData(); }, diff --git a/src/components/screens/MapScreen.js b/src/components/screens/MapScreen.js index fcc8fad..ff8b843 100644 --- a/src/components/screens/MapScreen.js +++ b/src/components/screens/MapScreen.js @@ -18,6 +18,7 @@ const DEFAULT_REGION = {latitude: 51.5074, longitude: -0.1278, latitudeDelta: 0. const MapScreen = ({navigation, route}) => { const session = getSession(); + const isBusiness = Boolean(session.isBusiness); const {getCaches, claimCache, upsertCache, deleteCache, joinPrivateGame, createPrivateGame, getLobby, getUser, getSubgroups, getGroupByOrgCode} = useGameHook(); // Stable ref to prevent infinite re-render loops @@ -102,6 +103,10 @@ const MapScreen = ({navigation, route}) => { } return map; }, [departments]); + const routeDepartmentSGid = route?.params?.selectedDepartmentSGid; + const effectiveSGid = isBusiness + ? (isAdmin ? (selectedCacheSubgroupId ?? routeDepartmentSGid ?? defaultMemberSGid ?? null) : (session.currentSGid ?? null)) + : null; useEffect(() => { if (!isAdmin || departments.length === 0) return; @@ -127,9 +132,9 @@ const MapScreen = ({navigation, route}) => { setCacheRecords([]); return; } - const rows = await getCachesRef.current(currentGid, null); + const rows = await getCachesRef.current(currentGid, effectiveSGid ?? null); setCacheRecords(rows || []); - }, []); + }, [effectiveSGid]); // Run once on mount useEffect(() => { loadCaches(); }, [loadCaches]); @@ -288,6 +293,7 @@ const MapScreen = ({navigation, route}) => { cacheId, uid: session.currentUid, tid: session.currentTid, + sgid: effectiveSGid, }); setIsClaiming(false); if (result) { @@ -295,7 +301,7 @@ const MapScreen = ({navigation, route}) => { setTimeout(() => setClaimedPopupVisible(false), 3000); } await loadCaches(); - }, [session.currentGid, session.currentUid, session.currentTid, loadCaches]); + }, [session.currentGid, session.currentUid, session.currentTid, effectiveSGid, loadCaches]); const handleCreateCachePress = () => { const fallback = userLocation || {latitude: mapRegion.latitude, longitude: mapRegion.longitude}; diff --git a/src/components/screens/PlayersScreen.js b/src/components/screens/PlayersScreen.js index 62b0959..9493193 100644 --- a/src/components/screens/PlayersScreen.js +++ b/src/components/screens/PlayersScreen.js @@ -14,6 +14,9 @@ const PlayersScreen = ({navigation, route}) => { const isBusiness = Boolean(session.isBusiness); const isAdmin = Boolean(session.isAcceptedAdmin); const selectedDepartment = route?.params?.department || null; + const effectiveSGid = isBusiness + ? ((isAdmin && selectedDepartment?.SGid) ? selectedDepartment.SGid : (isAdmin ? null : session.currentSGid)) + : null; // State ---------------------- @@ -26,10 +29,6 @@ const PlayersScreen = ({navigation, route}) => { const loadData = useCallback(async () => { if (!session.currentGid) { setLoading(false); return; } - const effectiveSGid = isBusiness - ? ((isAdmin && selectedDepartment?.SGid) ? selectedDepartment.SGid : (isAdmin ? null : session.currentSGid)) - : null; - if (isBusiness && isAdmin && !selectedDepartment) { const sgs = await getSubgroups(session.currentGid); setDepartmentList((sgs || []).filter((sg) => !sg.IsAdminGroup)); @@ -122,7 +121,7 @@ const PlayersScreen = ({navigation, route}) => { setPlayers(playerList); setLoading(false); - }, [session.currentGid, session.currentUid, session.currentSGid, isBusiness, isAdmin, selectedDepartment, selectedDepartment?.SGid]); + }, [session.currentGid, session.currentUid, session.currentSGid, isBusiness, isAdmin, selectedDepartment, selectedDepartment?.SGid, effectiveSGid]); useEffect(() => { loadData(); }, [loadData]); @@ -141,7 +140,7 @@ const PlayersScreen = ({navigation, route}) => { text: 'Reset', style: 'destructive', onPress: async () => { - await resetPlayerProgress(session.currentGid, player.Uid); + await resetPlayerProgress(session.currentGid, player.Uid, effectiveSGid); Alert.alert('Done', `Progress for ${player.username} has been reset.`); }, }, diff --git a/src/hooks/useGameHook.js b/src/hooks/useGameHook.js index aa6f429..795d1e5 100644 --- a/src/hooks/useGameHook.js +++ b/src/hooks/useGameHook.js @@ -184,9 +184,13 @@ const useGameHook = () => { }; const claimCache = async (payload) => { + let url = `${cachesEndpoint}/${payload.cacheId}/claim`; + if (payload.sgid !== null && payload.sgid !== undefined) { + url += `?SGid=${payload.sgid}`; + } const response = await API.post( - `${cachesEndpoint}/${payload.cacheId}/claim`, - {Uid: payload.uid, Tid: payload.tid}, + url, + {Uid: payload.uid, Tid: payload.tid, SGid: payload.sgid}, ); return response.isSuccess ? response.result : null; }; @@ -205,13 +209,17 @@ const useGameHook = () => { }; // Player progress reset — clear all claims by a specific user - const resetPlayerProgress = async (gid, uid) => { - const response = await API.post(`${cachesEndpoint}/reset-player/${uid}?Gid=${gid}`, {}); + const resetPlayerProgress = async (gid, uid, sgid = null) => { + let url = `${cachesEndpoint}/reset-player/${uid}?Gid=${gid}`; + if (sgid !== null && sgid !== undefined) url += `&SGid=${sgid}`; + const response = await API.post(url, {}); return response.isSuccess; }; - const resetTeamProgress = async (gid, tid) => { - const response = await API.post(`${cachesEndpoint}/reset-team/${tid}?Gid=${gid}`, {}); + const resetTeamProgress = async (gid, tid, sgid = null) => { + let url = `${cachesEndpoint}/reset-team/${tid}?Gid=${gid}`; + if (sgid !== null && sgid !== undefined) url += `&SGid=${sgid}`; + const response = await API.post(url, {}); return response.isSuccess; };