mirror of
https://github.com/mudabbir-ahmad/MAD-Treasure-Hunt.git
synced 2026-10-08 03:00:20 +00:00
Refactor session management to use AsyncStorage for persistence; update game settings and leaderboard screens for improved functionality
This commit is contained in:
1 parent
2646c7b9bc
commit
5183039b7b
26 files changed
+636
-849
No files matched your search
@@ -1,4 +1,8 @@
|
||||
// --- Session Store ---
|
||||
// --- Session Store (with AsyncStorage persistence) ---
|
||||
|
||||
import AsyncStorage from '@react-native-async-storage/async-storage';
|
||||
|
||||
const STORAGE_KEY = '@mad_session';
|
||||
|
||||
const session = {
|
||||
currentUid: null,
|
||||
@@ -7,6 +11,7 @@ const session = {
|
||||
currentTid: null,
|
||||
isBusiness: null,
|
||||
isAcceptedAdmin: false,
|
||||
teamsEnabled: false,
|
||||
};
|
||||
|
||||
// --- Handlers ---
|
||||
@@ -18,6 +23,7 @@ const setSessionUser = (user) => {
|
||||
session.currentTid = user?.TGid ?? null;
|
||||
session.isBusiness = user?.isBusiness ?? null;
|
||||
session.isAcceptedAdmin = Boolean(user?.IsAcceptedAdmin);
|
||||
persistSession();
|
||||
};
|
||||
|
||||
const setSessionGroup = (gid, sgid = null) => {
|
||||
@@ -25,10 +31,17 @@ const setSessionGroup = (gid, sgid = null) => {
|
||||
if (sgid !== null && sgid !== undefined) {
|
||||
session.currentSGid = sgid;
|
||||
}
|
||||
persistSession();
|
||||
};
|
||||
|
||||
const setSessionTeam = (tid) => {
|
||||
session.currentTid = tid ?? null;
|
||||
persistSession();
|
||||
};
|
||||
|
||||
const setSessionTeamsEnabled = (val) => {
|
||||
session.teamsEnabled = Boolean(val);
|
||||
persistSession();
|
||||
};
|
||||
|
||||
const getSession = () => ({ ...session });
|
||||
@@ -40,6 +53,36 @@ const clearSession = () => {
|
||||
session.currentTid = null;
|
||||
session.isBusiness = null;
|
||||
session.isAcceptedAdmin = false;
|
||||
AsyncStorage.removeItem(STORAGE_KEY).catch(() => {});
|
||||
};
|
||||
|
||||
export { setSessionUser, setSessionGroup, setSessionTeam, getSession, clearSession };
|
||||
// Clear game data but keep user logged in
|
||||
const clearGameSession = () => {
|
||||
session.currentGid = null;
|
||||
session.currentSGid = null;
|
||||
session.currentTid = null;
|
||||
session.isAcceptedAdmin = false;
|
||||
session.teamsEnabled = false;
|
||||
persistSession();
|
||||
};
|
||||
|
||||
// --- Persistence helpers ---
|
||||
|
||||
const persistSession = () => {
|
||||
AsyncStorage.setItem(STORAGE_KEY, JSON.stringify(session)).catch(() => {});
|
||||
};
|
||||
|
||||
const loadSession = async () => {
|
||||
try {
|
||||
const raw = await AsyncStorage.getItem(STORAGE_KEY);
|
||||
if (raw) {
|
||||
const saved = JSON.parse(raw);
|
||||
Object.assign(session, saved);
|
||||
}
|
||||
} catch (_) {
|
||||
// Ignore read errors — start fresh
|
||||
}
|
||||
return { ...session };
|
||||
};
|
||||
|
||||
export { setSessionUser, setSessionGroup, setSessionTeam, setSessionTeamsEnabled, getSession, clearSession, clearGameSession, loadSession };
|
||||
@@ -4,14 +4,12 @@ const useGameHook = () => {
|
||||
// Initialisation ------------
|
||||
|
||||
const usersEndpoint = `${API_BASE_URL}/users`;
|
||||
const gameTypesEndpoint = `${API_BASE_URL}/game-types`;
|
||||
const groupsEndpoint = `${API_BASE_URL}/groups`;
|
||||
const subgroupsEndpoint = `${API_BASE_URL}/subgroups`;
|
||||
const subgroupMembershipsEndpoint = `${API_BASE_URL}/subgroup-memberships`;
|
||||
const teamsEndpoint = `${API_BASE_URL}/teams`;
|
||||
const teamMembersEndpoint = `${API_BASE_URL}/team-members`;
|
||||
const gameDataEndpoint = `${API_BASE_URL}/game-data`;
|
||||
const adminWaitlistEndpoint = `${API_BASE_URL}/admin-waitlist`;
|
||||
|
||||
// Handlers -------------------
|
||||
|
||||
@@ -26,18 +24,7 @@ const useGameHook = () => {
|
||||
return response.isSuccess ? response.result : null;
|
||||
};
|
||||
|
||||
// Game types
|
||||
const getGameTypes = async () => {
|
||||
const response = await API.get(gameTypesEndpoint);
|
||||
return response.isSuccess ? response.result : [];
|
||||
};
|
||||
|
||||
// Groups
|
||||
const getCreatedPrivateGame = async (userId) => {
|
||||
const response = await API.get(`${groupsEndpoint}?CreatedByUid=${userId}`);
|
||||
return response.isSuccess ? response.result : [];
|
||||
};
|
||||
|
||||
const createPrivateGame = async (payload) => {
|
||||
const response = await API.post(groupsEndpoint, payload);
|
||||
return response.isSuccess ? response.result : null;
|
||||
@@ -74,11 +61,6 @@ const useGameHook = () => {
|
||||
return response.isSuccess;
|
||||
};
|
||||
|
||||
const joinAsAdmin = async (payload) => {
|
||||
const response = await API.post(adminWaitlistEndpoint, payload);
|
||||
return response.isSuccess ? response.result : null;
|
||||
};
|
||||
|
||||
// Teams
|
||||
const getTeams = async (gid) => {
|
||||
const response = await API.get(`${teamsEndpoint}?Gid=${gid}`);
|
||||
@@ -152,14 +134,11 @@ const useGameHook = () => {
|
||||
return {
|
||||
getUser,
|
||||
updateUser,
|
||||
getGameTypes,
|
||||
getCreatedPrivateGame,
|
||||
createPrivateGame,
|
||||
getLobby,
|
||||
updateGroup,
|
||||
getSubgroups,
|
||||
joinPrivateGame,
|
||||
joinAsAdmin,
|
||||
getGroupMembers,
|
||||
removeMember,
|
||||
getTeams,
|
||||
|
||||
Reference in new issue
Block a user