fix(api): fix api endpoint logic #3

This commit is contained in:
shafin-r
2025-08-17 19:59:14 +06:00
parent 4f23f357e6
commit e3673951c6
7 changed files with 231 additions and 170 deletions

View File

@ -6,8 +6,7 @@ import styles from "@/css/Header.module.css";
import { useExam } from "@/context/ExamContext";
import { Avatar, AvatarFallback } from "@/components/ui/avatar";
import { useModal } from "@/context/ModalContext";
import { API_URL, getToken } from "@/lib/auth";
import { UserData } from "@/types/auth";
import { useAuth } from "@/context/AuthContext";
interface HeaderProps {
displayUser?: boolean;
@ -29,7 +28,7 @@ const Header = ({
examDuration ? parseInt(examDuration) * 60 : 0
);
const { stopTimer } = useTimer();
const [userData, setUserData] = useState<UserData>();
const { user, isLoading } = useAuth();
useEffect(() => {
if (!examDuration) return;
@ -47,33 +46,6 @@ const Header = ({
return () => clearInterval(timer);
}, [examDuration]);
useEffect(() => {
async function fetchUser() {
try {
const token = await getToken();
if (!token) return;
const response = await fetch(`${API_URL}/me/`, {
method: "GET",
headers: {
Authorization: `Bearer ${token}`,
},
});
if (response.ok) {
const fetchedUserData = await response.json();
setUserData(fetchedUserData);
}
} catch (error) {
console.error("Error fetching user data:", error);
}
}
if (displayUser) {
fetchUser();
}
}, [displayUser]);
const hours = Math.floor(totalSeconds / 3600);
const minutes = Math.floor((totalSeconds % 3600) / 60);
const seconds = totalSeconds % 60;
@ -100,13 +72,11 @@ const Header = ({
<div className={styles.profile}>
<Avatar className="bg-gray-200 w-10 h-10">
<AvatarFallback className=" text-lg">
{userData?.username
? userData.username.charAt(0).toUpperCase()
: ""}
{user?.username ? user.username.charAt(0).toUpperCase() : ""}
</AvatarFallback>
</Avatar>
<span className={styles.text}>
Hello, {userData?.username ? userData.username.split(" ")[0] : ""}
Hello, {user?.username ? user.username.split(" ")[0] : ""}
</span>
</div>
)}

View File

@ -19,10 +19,15 @@ export default function ProfileManager({
setUserData((prev) => (prev ? { ...prev, [field]: value } : prev));
};
console.log(userData);
return (
<div className="mx-auto">
<div className="space-y-4">
{/* Full Name */}
<h1 className="text-xl font-semibold tracking-tight">
Personal Information
</h1>
<div className="space-y-2">
<Label
htmlFor="full_name"
@ -39,61 +44,24 @@ export default function ProfileManager({
readOnly={!edit}
/>
</div>
{/* College */}
<div className="space-y-2">
<Label
htmlFor="college"
htmlFor="username"
className="text-sm font-semibold text-gray-700"
>
College
Username
</Label>
<Input
id="college"
id="username"
type="text"
value={userData.college}
onChange={(e) => handleChange("college", e.target.value)}
value={userData.username}
onChange={(e) => handleChange("username", e.target.value)}
className="bg-gray-50 py-6"
readOnly={!edit}
/>
</div>
{/* SSC & HSC Rolls */}
<div className="flex gap-4">
<div className="space-y-2 w-full">
<Label
htmlFor="ssc_roll"
className="text-sm font-semibold text-gray-700"
>
SSC Roll
</Label>
<Input
id="ssc_roll"
type="number"
value={userData.ssc_roll}
onChange={(e) => handleChange("ssc_roll", Number(e.target.value))}
className="bg-gray-50 py-6"
readOnly={!edit}
/>
</div>
<div className="space-y-2 w-full">
<Label
htmlFor="hsc_roll"
className="text-sm font-semibold text-gray-700"
>
HSC Roll
</Label>
<Input
id="hsc_roll"
type="number"
value={userData.hsc_roll}
onChange={(e) => handleChange("hsc_roll", Number(e.target.value))}
className="bg-gray-50 py-6"
readOnly={!edit}
/>
</div>
</div>
{/* Email */}
<div className="space-y-2">
@ -130,6 +98,111 @@ export default function ProfileManager({
readOnly={!edit}
/>
</div>
<h1 className="text-xl tracking-tight font-semibold">
Educational Background
</h1>
<div className="space-y-2">
<Label
htmlFor="preparation_unit"
className="text-sm font-semibold text-gray-700"
>
Unit
</Label>
<Input
id="preparation_unit"
type="text"
value={userData.preparation_unit}
onChange={(e) => handleChange("preparation_unit", e.target.value)}
className="bg-gray-50 py-6"
readOnly={!edit}
/>
</div>
<div className="space-y-2">
<Label
htmlFor="college"
className="text-sm font-semibold text-gray-700"
>
College
</Label>
<Input
id="college"
type="text"
value={userData.college}
onChange={(e) => handleChange("college", e.target.value)}
className="bg-gray-50 py-6"
readOnly={!edit}
/>
</div>
<div className="flex gap-4">
<div className="space-y-2 w-full">
<Label
htmlFor="ssc_roll"
className="text-sm font-semibold text-gray-700"
>
SSC Roll
</Label>
<Input
id="ssc_roll"
type="number"
value={userData.ssc_roll}
onChange={(e) => handleChange("ssc_roll", Number(e.target.value))}
className="bg-gray-50 py-6"
readOnly={!edit}
/>
</div>
<div className="space-y-2 w-full">
<Label
htmlFor="ssc_board"
className="text-sm font-semibold text-gray-700"
>
SSC Board
</Label>
<Input
id="ssc_board"
type="text"
value={userData.ssc_board}
onChange={(e) => handleChange("ssc_board", e.target.value)}
className="bg-gray-50 py-6"
readOnly={!edit}
/>
</div>
</div>
<div className="flex gap-4">
<div className="space-y-2 w-full">
<Label
htmlFor="hsc_roll"
className="text-sm font-semibold text-gray-700"
>
HSC Roll
</Label>
<Input
id="hsc_roll"
type="number"
value={userData.hsc_roll}
onChange={(e) => handleChange("hsc_roll", Number(e.target.value))}
className="bg-gray-50 py-6"
readOnly={!edit}
/>
</div>
<div className="space-y-2 w-full">
<Label
htmlFor="hsc_board"
className="text-sm font-semibold text-gray-700"
>
HSC Board
</Label>
<Input
id="hsc_board"
type="text"
value={userData.hsc_board}
onChange={(e) => handleChange("hsc_board", e.target.value)}
className="bg-gray-50 py-6"
readOnly={!edit}
/>
</div>
</div>
</div>
</div>
);