mirror of
https://github.com/mudabbir-ahmad/peworkshop.git
synced 2026-10-07 19:50:20 +00:00
fixed some things with the member list page. added a start hunt button. removed add member button.
This commit is contained in:
1 parent
ab79a28528
commit
593c51a883
3 files changed
+114
-27
No files matched your search
@@ -10,9 +10,27 @@ export default function TeamPage({ params }) {
|
||||
const [teamMembers, setTeamMembers] = useState([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState("");
|
||||
const [huntActive, setHuntActive] = useState(false);
|
||||
const router = useRouter();
|
||||
const { teamId } = params;
|
||||
|
||||
const checkHuntStatus = useCallback(async () => {
|
||||
try {
|
||||
const response = await fetch("/api/hunt-status", {
|
||||
method: "GET",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
credentials: "include",
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
setHuntActive(data.active || false);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("Failed to check hunt status:", err);
|
||||
}
|
||||
}, []);
|
||||
|
||||
const fetchTeamData = useCallback(async () => {
|
||||
try {
|
||||
if (!teamId || isNaN(parseInt(teamId, 10))) {
|
||||
@@ -63,7 +81,9 @@ export default function TeamPage({ params }) {
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, [teamId, router]);
|
||||
|
||||
await checkHuntStatus();
|
||||
}, [teamId, router, checkHuntStatus]);
|
||||
|
||||
useEffect(() => {
|
||||
// Only attempt to fetch data when session is ready and we have a teamId
|
||||
@@ -141,13 +161,6 @@ export default function TeamPage({ params }) {
|
||||
) : (
|
||||
<div className="empty-state">No team members found</div>
|
||||
)}
|
||||
|
||||
<button
|
||||
className="add-member-button"
|
||||
onClick={() => router.push(`/team/${teamId}/add-user`)}
|
||||
>
|
||||
+ Add Member
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="team-actions">
|
||||
@@ -179,6 +192,44 @@ export default function TeamPage({ params }) {
|
||||
Leave Team
|
||||
</button>
|
||||
|
||||
{session?.user?.role !== "admin" && (
|
||||
<button
|
||||
className="action-button start-button"
|
||||
onClick={async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
|
||||
if (!huntActive) {
|
||||
// If hunt is not active, redirect to the "not started" page
|
||||
router.push(`/hunt-not-started?teamId=${teamId}`);
|
||||
} else {
|
||||
// If hunt is active, try to start the hunt
|
||||
const response = await fetch(
|
||||
`/api/teams/${teamId}/start-hunt`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
credentials: "include",
|
||||
}
|
||||
);
|
||||
|
||||
if (response.ok) {
|
||||
fetchTeamData();
|
||||
} else {
|
||||
const error = await response.json();
|
||||
throw new Error(error.message || "Failed to start hunt");
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
setError(err.message);
|
||||
setLoading(false);
|
||||
}
|
||||
}}
|
||||
>
|
||||
Start Hunt
|
||||
</button>
|
||||
)}
|
||||
|
||||
{session?.user?.role === "admin" && (
|
||||
<button
|
||||
className="action-button start-button"
|
||||
@@ -314,19 +365,6 @@ export default function TeamPage({ params }) {
|
||||
color: #888;
|
||||
}
|
||||
|
||||
.add-member-button {
|
||||
background-color: #68543c;
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 8px;
|
||||
padding: 15px;
|
||||
margin-top: 20px;
|
||||
width: 100%;
|
||||
font-size: 16px;
|
||||
font-weight: bold;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.team-actions {
|
||||
display: flex;
|
||||
gap: 10px;
|
||||
|
||||
@@ -1,15 +1,64 @@
|
||||
import { queryOne } from "../../../lib/db";
|
||||
import { queryOne, run } from "@/lib/db";
|
||||
import { cookies } from "next/headers";
|
||||
import { verifyToken } from "@/app/api/auth/[...nextauth]/route";
|
||||
|
||||
export async function GET() {
|
||||
export const dynamic = "force-dynamic"; // Disable caching
|
||||
|
||||
export async function GET(request) {
|
||||
try {
|
||||
const settings = await queryOne("SELECT active FROM hunt_settings LIMIT 1");
|
||||
// Verify the user is authenticated
|
||||
const cookieStore = await cookies();
|
||||
const sessionToken = cookieStore.get("next-auth.session-token")?.value;
|
||||
|
||||
if (!sessionToken) {
|
||||
return Response.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const userData = verifyToken(sessionToken);
|
||||
if (!userData) {
|
||||
return Response.json({ error: "Invalid session" }, { status: 401 });
|
||||
}
|
||||
|
||||
let huntActive = false;
|
||||
|
||||
try {
|
||||
// First try to create the settings table if it doesn't exist
|
||||
await run(`
|
||||
CREATE TABLE IF NOT EXISTS settings (
|
||||
key TEXT PRIMARY KEY,
|
||||
value TEXT NOT NULL
|
||||
)
|
||||
`);
|
||||
|
||||
// Check if hunt_active setting exists
|
||||
const huntSetting = await queryOne(
|
||||
"SELECT value FROM settings WHERE key = 'hunt_active'"
|
||||
);
|
||||
|
||||
// If the setting exists, use its value
|
||||
if (huntSetting) {
|
||||
huntActive = huntSetting.value === "true";
|
||||
} else {
|
||||
// If setting doesn't exist, create it with default value "false"
|
||||
await run(
|
||||
"INSERT OR IGNORE INTO settings (key, value) VALUES (?, ?)",
|
||||
["hunt_active", "false"]
|
||||
);
|
||||
}
|
||||
} catch (dbError) {
|
||||
console.error("Database error in hunt-status:", dbError);
|
||||
// Continue with default value if there's a DB error
|
||||
}
|
||||
|
||||
// Return the active status
|
||||
return Response.json({
|
||||
active: settings ? Boolean(settings.active) : false,
|
||||
active: huntActive,
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Get hunt status error:", error);
|
||||
console.error("Hunt status check error:", error);
|
||||
return Response.json(
|
||||
{ error: "Failed to fetch hunt status" },
|
||||
{ error: "Failed to check hunt status", active: false },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user