fixed a bit

This commit is contained in:
bobbert committed 2025-03-11 23:02:22 +00:00
1 parent 45b0f98971
commit a0fb2b9970
9 files changed
+314 -177

No files matched your search

+27 -38
View File
@@ -1,5 +1,5 @@
"use client";
import { useState } from "react";
import { useState, useEffect } from "react";
import { useSession } from "next-auth/react";
import { useRouter } from "next/navigation";
import Link from "next/link";
@@ -20,61 +20,50 @@ export default function CreateTeam() {
const { data: session, status } = useSession();
const [teamName, setTeamName] = useState("");
const [error, setError] = useState("");
const [isLoading, setIsLoading] = useState(false);
const [isLoading, setIsLoading] = useState(true);
const router = useRouter();
if (status === "loading") {
return <div className="loading">Loading...</div>;
}
if (!session || status === "unauthenticated") {
router.replace("/login");
return null;
}
if (session?.user?.role === "admin") {
router.replace("/admin");
return null;
}
useEffect(() => {
if (status === "authenticated") {
if (session.user.teamId) {
router.push(`/team/${session.user.teamId}`);
} else {
setIsLoading(false);
}
} else if (status === "unauthenticated") {
router.push("/login");
}
}, [status, session, router]);
const handleCreateTeam = async (e) => {
e.preventDefault();
if (isLoading || !teamName.trim()) return;
setIsLoading(true);
setError("");
try {
const response = await fetch("/api/teams/create", {
const response = await fetch("/api/teams", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
name: teamName,
userId: session.user.id,
}),
body: JSON.stringify({ name: teamName }),
});
const result = await response.json();
if (result.success) {
// Update the user's session with the new teamId
await fetch("/api/auth/session", {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
teamId: result.teamId,
}),
});
const data = await response.json();
router.push(`/team/${result.teamId}`);
} else {
setError(result.error || "Failed to create team");
if (!response.ok) {
throw new Error(data.error || "Failed to create team");
}
router.push(`/team/${data.teamId}`);
} catch (err) {
console.error("Team creation error:", err);
setError("An unexpected error occurred");
} finally {
setError(err.message);
setIsLoading(false);
}
};
if (isLoading) {
return <div className="loading">Loading...</div>;
}
return (
<div className="auth-page">
<div className="page-header">