mirror of
https://github.com/mudabbir-ahmad/MAD-Treasure-Hunt.git
synced 2026-10-07 19:00:20 +00:00
Add create game and manage game pages, update navigation and session handling for individual and business users
This commit is contained in:
1 parent
1bd640f8d5
commit
6e5fe2b8b8
21 files changed
+831
-137
No files matched your search
@@ -1,11 +1,21 @@
|
||||
import { useState } from 'react';
|
||||
import { createPrivateGame, joinAsAdmin } from '../API/API';
|
||||
import { getSession, setSessionGroup } from '../Model/SessionStore';
|
||||
import {useState} from 'react';
|
||||
import {createPrivateGame, getCreatedPrivateGame, joinAsAdmin} from '../API/API';
|
||||
import {getSession, setSessionMembership} from '../Model/SessionStore';
|
||||
|
||||
const useManagePrivateSubtaskViewModel = () => {
|
||||
const [businessOrSchoolName, setBusinessOrSchoolName] = useState('');
|
||||
const [error, setError] = useState('');
|
||||
|
||||
const isBusinessUser = Boolean(getSession().currentIsBusiness);
|
||||
|
||||
const findExistingPrivateGame = async () => {
|
||||
const { currentUid } = getSession();
|
||||
if (!currentUid) {
|
||||
return null;
|
||||
}
|
||||
return getCreatedPrivateGame(currentUid);
|
||||
};
|
||||
|
||||
const createNewPrivateGame = async () => {
|
||||
setError('');
|
||||
const { currentUid } = getSession();
|
||||
@@ -14,9 +24,15 @@ const useManagePrivateSubtaskViewModel = () => {
|
||||
userId: currentUid,
|
||||
groupName: businessOrSchoolName.trim() || 'New Private Game',
|
||||
businessOrSchoolName: businessOrSchoolName.trim() || 'Unnamed Business',
|
||||
isBusiness: true,
|
||||
isBusiness: isBusinessUser,
|
||||
});
|
||||
setSessionGroup(group.Gid, 0);
|
||||
|
||||
if (isBusinessUser) {
|
||||
setSessionMembership({ gid: group.Gid, sgid: 0, isAcceptedAdmin: true });
|
||||
} else {
|
||||
setSessionMembership({ gid: group.Gid, sgid: 1, isAcceptedAdmin: false });
|
||||
}
|
||||
|
||||
return group;
|
||||
} catch (e) {
|
||||
setError(e.message);
|
||||
@@ -24,6 +40,26 @@ const useManagePrivateSubtaskViewModel = () => {
|
||||
}
|
||||
};
|
||||
|
||||
const resolveIndividualPrivateRoute = async () => {
|
||||
setError('');
|
||||
try {
|
||||
const existingGroup = await findExistingPrivateGame();
|
||||
if (existingGroup) {
|
||||
setSessionMembership({ gid: existingGroup.Gid, sgid: 1, isAcceptedAdmin: false });
|
||||
return '(routes)/manage-game';
|
||||
}
|
||||
|
||||
const created = await createNewPrivateGame();
|
||||
if (!created) {
|
||||
return null;
|
||||
}
|
||||
return '(routes)/create-game';
|
||||
} catch (e) {
|
||||
setError(e.message);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
const joinAdminForPrivate = async () => {
|
||||
setError('');
|
||||
const { currentUid } = getSession();
|
||||
@@ -34,7 +70,10 @@ const useManagePrivateSubtaskViewModel = () => {
|
||||
});
|
||||
const gid = response?.Gid ?? null;
|
||||
if (gid !== null) {
|
||||
setSessionGroup(gid, 0);
|
||||
setSessionMembership({ gid, sgid: 0, isAcceptedAdmin: !response.queued });
|
||||
}
|
||||
if (response?.queued) {
|
||||
setError('Admin access request submitted. You will see admin tools after approval.');
|
||||
}
|
||||
return response;
|
||||
} catch (e) {
|
||||
@@ -46,8 +85,10 @@ const useManagePrivateSubtaskViewModel = () => {
|
||||
return {
|
||||
businessOrSchoolName,
|
||||
error,
|
||||
isBusinessUser,
|
||||
setBusinessOrSchoolName,
|
||||
createNewPrivateGame,
|
||||
resolveIndividualPrivateRoute,
|
||||
joinAdminForPrivate,
|
||||
};
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user