Spaces:
Sleeping
Sleeping
File size: 11,281 Bytes
31386d4 0ef5c60 24e6c4c 31386d4 24e6c4c 31386d4 bba3a81 31386d4 24e6c4c 0ef5c60 bba3a81 31386d4 24e6c4c bba3a81 24e6c4c bba3a81 31386d4 bba3a81 24e6c4c bba3a81 24e6c4c 31386d4 0ef5c60 31386d4 0ef5c60 31386d4 bba3a81 24e6c4c bba3a81 24e6c4c bba3a81 24e6c4c 31386d4 0ef5c60 31386d4 0ef5c60 31386d4 0ef5c60 24e6c4c 0ef5c60 31386d4 0ef5c60 31386d4 0ef5c60 24e6c4c 0ef5c60 31386d4 24e6c4c 31386d4 0ef5c60 31386d4 0ef5c60 31386d4 0ef5c60 31386d4 0ef5c60 31386d4 24e6c4c 31386d4 24e6c4c 31386d4 0ef5c60 bba3a81 31386d4 bba3a81 31386d4 0ef5c60 31386d4 24e6c4c 31386d4 24e6c4c 31386d4 24e6c4c 31386d4 0ef5c60 31386d4 0ef5c60 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
import React, { useEffect, useRef, useState } from "react";
import { Button } from "./ui/button";
import { Input } from "./ui/input";
import { Label } from "./ui/label";
import { Textarea } from "./ui/textarea";
import { Dialog, DialogContent, DialogTitle } from "./ui/dialog";
import type { User as UserType } from "../App";
import { toast } from "sonner";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "./ui/select";
interface ProfileEditorProps {
user: UserType;
onSave: (user: UserType) => void;
onClose: () => void;
}
export function ProfileEditor({ user, onSave, onClose }: ProfileEditorProps) {
const [name, setName] = useState(user.name ?? "");
const [email, setEmail] = useState(user.email ?? "");
const [studentId, setStudentId] = useState(user.studentId ?? "");
const [department, setDepartment] = useState(user.department ?? "");
const [yearLevel, setYearLevel] = useState(user.yearLevel ?? "");
const [major, setMajor] = useState(user.major ?? "");
// ✅ bio is editable here (user can override Clare-generated bio)
const [bio, setBio] = useState(user.bio ?? "");
const [learningStyle, setLearningStyle] = useState(user.learningStyle ?? "visual");
const [learningPace, setLearningPace] = useState(user.learningPace ?? "moderate");
const [photoPreview, setPhotoPreview] = useState<string | null>(user.avatarUrl ?? null);
const fileInputRef = useRef<HTMLInputElement>(null);
// ✅ Keep fields in sync if user changes while dialog is open
useEffect(() => {
setName(user.name ?? "");
setEmail(user.email ?? "");
setStudentId(user.studentId ?? "");
setDepartment(user.department ?? "");
setYearLevel(user.yearLevel ?? "");
setMajor(user.major ?? "");
setBio(user.bio ?? "");
setLearningStyle(user.learningStyle ?? "visual");
setLearningPace(user.learningPace ?? "moderate");
setPhotoPreview(user.avatarUrl ?? null);
}, [
user.name,
user.email,
user.studentId,
user.department,
user.yearLevel,
user.major,
user.bio,
user.learningStyle,
user.learningPace,
user.avatarUrl,
]);
const handleSave = () => {
if (!name.trim() || !email.trim()) {
toast.error("Please fill in all required fields");
return;
}
const next: UserType = {
...user,
name: name.trim(),
email: email.trim(),
studentId: studentId.trim() || undefined,
department: department.trim() || undefined,
yearLevel: yearLevel || undefined,
major: major.trim() || undefined,
// ✅ allow user edit
bio: (bio ?? "").slice(0, 200),
learningStyle: learningStyle || undefined,
learningPace: learningPace || undefined,
avatarUrl: photoPreview || undefined,
};
onSave(next);
toast.success("Profile updated successfully!");
onClose();
};
const handlePhotoSelect = (e: React.ChangeEvent<HTMLInputElement>) => {
const file = e.target.files?.[0];
if (!file) return;
if (!file.type.startsWith("image/")) {
toast.error("Please select an image file");
return;
}
if (file.size > 2 * 1024 * 1024) {
toast.error("File size must be less than 2MB");
return;
}
const reader = new FileReader();
reader.onload = (ev) => {
setPhotoPreview(ev.target?.result as string);
toast.success("Photo updated successfully!");
};
reader.readAsDataURL(file);
};
const handleChangePhotoClick = () => fileInputRef.current?.click();
return (
<Dialog
open
onOpenChange={(open) => {
if (!open) onClose();
}}
>
<DialogContent
className="sm:max-w-[800px] p-0 gap-0 max-h-[90vh] overflow-hidden"
style={{ zIndex: 1001, maxWidth: "800px", width: "800px" }}
overlayClassName="!top-16 !left-0 !right-0 !bottom-0 !z-[99]"
overlayStyle={{ top: "64px", left: 0, right: 0, bottom: 0, zIndex: 99, position: "fixed" }}
>
<div className="flex flex-col max-h-[90vh]">
{/* Header */}
<div className="p-4 flex items-center justify-between flex-shrink-0">
<DialogTitle className="text-xl font-medium">Edit Profile</DialogTitle>
</div>
{/* Content */}
<div className="p-6 space-y-6 overflow-y-auto flex-1">
{/* Profile Picture */}
<div className="flex items-center gap-4">
<div className="w-20 h-20 rounded-full bg-gradient-to-br from-red-500 to-orange-500 flex items-center justify-center text-white text-2xl overflow-hidden">
{photoPreview ? (
<img src={photoPreview} alt="Profile" className="w-full h-full object-cover" />
) : (
(name?.charAt(0) || "U").toUpperCase()
)}
</div>
<div>
<input
ref={fileInputRef}
type="file"
accept="image/jpeg,image/png,image/gif,image/webp"
onChange={handlePhotoSelect}
className="hidden"
/>
<Button variant="outline" size="sm" onClick={handleChangePhotoClick}>
Change Photo
</Button>
<p className="text-xs text-muted-foreground mt-1">JPG, PNG or GIF. Max size 2MB</p>
</div>
</div>
{/* Basic Information */}
<div className="space-y-4">
<h3 className="text-sm font-medium">Basic Information</h3>
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<div className="space-y-2">
<Label htmlFor="edit-name">Full Name *</Label>
<Input
id="edit-name"
value={name}
onChange={(e) => setName(e.target.value)}
placeholder="Enter your full name"
/>
</div>
<div className="space-y-2">
<Label htmlFor="edit-email">Email *</Label>
<Input
id="edit-email"
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
placeholder="Enter your email"
/>
</div>
<div className="space-y-2">
<Label htmlFor="edit-student-id">Student ID</Label>
<Input
id="edit-student-id"
value={studentId}
onChange={(e) => setStudentId(e.target.value)}
placeholder="Enter your student ID"
/>
</div>
<div className="space-y-2">
<Label htmlFor="edit-department">Department</Label>
<Input
id="edit-department"
value={department}
onChange={(e) => setDepartment(e.target.value)}
placeholder="Enter your department"
/>
</div>
</div>
</div>
{/* Academic Background */}
<div className="space-y-4">
<h3 className="text-sm font-medium">Academic Background</h3>
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<div className="space-y-2">
<Label htmlFor="edit-year">Year Level</Label>
<Select value={yearLevel} onValueChange={setYearLevel}>
<SelectTrigger id="edit-year">
<SelectValue placeholder="Select year level" />
</SelectTrigger>
<SelectContent>
<SelectItem value="1st Year">1st Year</SelectItem>
<SelectItem value="2nd Year">2nd Year</SelectItem>
<SelectItem value="3rd Year">3rd Year</SelectItem>
<SelectItem value="4th Year">4th Year</SelectItem>
<SelectItem value="Graduate">Graduate</SelectItem>
</SelectContent>
</Select>
</div>
<div className="space-y-2">
<Label htmlFor="edit-major">Major</Label>
<Input
id="edit-major"
value={major}
onChange={(e) => setMajor(e.target.value)}
placeholder="Enter your major"
/>
</div>
</div>
</div>
{/* Bio (Editable) */}
<div className="space-y-2">
<Label htmlFor="edit-bio">Bio</Label>
<Textarea
id="edit-bio"
value={bio}
onChange={(e) => setBio(e.target.value)}
placeholder="Tell us about yourself..."
className="min-h-[100px] resize-none"
maxLength={200}
/>
<p className="text-xs text-muted-foreground">Max 200 characters. You can edit this anytime.</p>
</div>
{/* Learning Preferences */}
<div className="space-y-4">
<h3 className="text-sm font-medium">Learning Preferences</h3>
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<div className="space-y-2">
<Label htmlFor="edit-learning-style">Preferred Learning Style</Label>
<Select value={learningStyle} onValueChange={setLearningStyle}>
<SelectTrigger id="edit-learning-style">
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="visual">Visual</SelectItem>
<SelectItem value="auditory">Auditory</SelectItem>
<SelectItem value="reading">Reading/Writing</SelectItem>
<SelectItem value="kinesthetic">Kinesthetic</SelectItem>
</SelectContent>
</Select>
</div>
<div className="space-y-2">
<Label htmlFor="edit-pace">Learning Pace</Label>
<Select value={learningPace} onValueChange={setLearningPace}>
<SelectTrigger id="edit-pace">
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="slow">Slow & Steady</SelectItem>
<SelectItem value="moderate">Moderate</SelectItem>
<SelectItem value="fast">Fast-paced</SelectItem>
</SelectContent>
</Select>
</div>
</div>
</div>
</div>
{/* Footer */}
<div className="border-t border-border p-4 flex justify-end gap-2 flex-shrink-0">
<Button variant="outline" onClick={onClose}>
Cancel
</Button>
<Button onClick={handleSave}>Save Changes</Button>
</div>
</div>
</DialogContent>
</Dialog>
);
}
|