diff --git a/src/app/(dashboard)/profile/components/EditProfileComponent.tsx b/src/app/(dashboard)/profile/components/EditProfileComponent.tsx index 95e1181..bac77be 100644 --- a/src/app/(dashboard)/profile/components/EditProfileComponent.tsx +++ b/src/app/(dashboard)/profile/components/EditProfileComponent.tsx @@ -1,14 +1,10 @@ "use client"; -import React, { useState } from 'react'; +import React, { useEffect, useState } from 'react'; import { useRouter } from 'next/navigation'; +import { GetProfileService } from '@/services/profile-services'; +import { MemberProfileDetails } from '@/types/types'; +import toast from 'react-hot-toast'; -type ProfileData = { - name: string; - email: string; - username: string; - bio: string; - profileImage: string; -} type EditProfileProps = { onCancel: () => void; @@ -16,17 +12,28 @@ type EditProfileProps = { export default function EditProfileComponent({ onCancel }: EditProfileProps) { const router = useRouter(); - - const [profileData, setProfileData] = useState({ - name: "John Doe", - email: "johndoe@example.com", - username: "johndoe", - bio: "just a random code to have a profile structure to later code on", - profileImage: "/placeholder.webp" + + const [profileData, setProfileData] = useState({ + memberId:0, + groupId:1, + year:1, + name: "", + rollNo: "", + sex: "", + track: "", + email: "", + hostel: '', + discordId: '', + macAddress: '', }); - + const tracks = ['Web', 'Systems', 'AI', 'Mobile']; + + const [isSubmitting, setIsSubmitting] = useState(false); - const [previewUrl, setPreviewUrl] = useState(profileData.profileImage); + const [isLoading, setIsLoading] = useState(true); + const [GenToggle, setGenToggle] = useState([false, false]); + const [previewUrl, setPreviewUrl] = useState("/placeholder.webp"); + const [isUserEnrolling, setUserEnrolling] = useState(false); const handleChange = (e: React.ChangeEvent) => { const { name, value } = e.target; @@ -36,12 +43,28 @@ export default function EditProfileComponent({ onCancel }: EditProfileProps) { }); }; + useEffect(()=>{ + async function getProfileDetails() { + const member = await GetProfileService.getProfileDetails(); + if(member){ + setProfileData(member); + setGenToggle([member?.sex == "M",member?.sex == "F"]); + setIsLoading(false); + } + else { + setIsLoading(false); + setUserEnrolling(true); + } + } + getProfileDetails(); + },[]) + const handleImageChange = (e: React.ChangeEvent) => { if (e.target.files && e.target.files[0]) { const file = e.target.files[0]; const fileUrl = URL.createObjectURL(file); setPreviewUrl(fileUrl); - + // You would typically upload the image to your server here // and then update the profileData with the returned URL // For this demo, we'll just update the preview @@ -51,17 +74,36 @@ export default function EditProfileComponent({ onCancel }: EditProfileProps) { const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setIsSubmitting(true); - - // Simulate API call - setTimeout(() => { + async function Update() { + let data = await GetProfileService.UpdateProfileDetails(profileData); + if(data){ + setProfileData(data); + handleCancel(); + } + else{ + toast.error("Error in Updating User"); + } setIsSubmitting(false); - // Always redirect back to the main profile page after successful update - router.push('/profile'); - }, 1000); + } + + async function Create() { + let data = await GetProfileService.CreateProfileDetails(profileData); + if(data){ + setProfileData(data); + handleCancel(); + } + else{ + toast.error("Error in Creating User"); + } + setIsSubmitting(false); + } + + !isUserEnrolling ? Update() : Create(); }; const handleCancel = () => { if (onCancel) { + console.log(profileData); onCancel(); } else { // Always navigate to the main profile route @@ -69,103 +111,222 @@ export default function EditProfileComponent({ onCancel }: EditProfileProps) { } }; - return ( + const trackUi: JSX.Element[] = tracks.map((track) => ( + + )); + + if(!isLoading){ + return (
-
-

Edit Profile

- -
-
-
-
- {profileData.name} - -
+ {isUserEnrolling ? ( +

SetUp Profile

+ ) : ( +

Update Profile

+ )} +
+ +
+

Personal Details

+
+
+
+ {profileData.name} + +
+
+ +
+
+
+ + +
+
+ +
+ {['Male', 'Female'].map((label, idx) => ( + + ))} +
+
+
+ + +
+
+ + +
+
+
+ + +
+
+
+ +
- +
+
+
+
+
+ +
+

Additional Details

+
- - Hostel + +
+ +
+ +
- - Discord Id +
- - Mac Address +
- -
- -