tahamajs/IE / Projects /front /src /components /dashboard /ProfileEditModal.tsx
tahamajs's picture
download
raw
8.36 kB
import { useState, useEffect, useCallback } from "react";
import { UserProfile } from "@/types";
import { X } from "lucide-react";
import { toast } from "react-toastify";
interface ProfileEditModalProps {
isOpen: boolean;
user: UserProfile | null;
onClose: () => void;
onSave: (data: Partial<UserProfile>, avatarFile?: File | null) => Promise<boolean>;
}
export function ProfileEditModal({ isOpen, user, onClose, onSave }: ProfileEditModalProps) {
const [form, setForm] = useState({
fullName: "",
academicTitle: "",
affiliation: "",
bio: "",
orcid: "",
googleScholar: "",
website: "",
});
const [avatarFile, setAvatarFile] = useState<File | null>(null);
const [avatarPreview, setAvatarPreview] = useState<string | null>(null);
const [saving, setSaving] = useState(false);
const [errors, setErrors] = useState<Record<string, string>>({});
// Load user data when modal opens
useEffect(() => {
if (user) {
setForm({
fullName: user.fullName || "",
academicTitle: user.academicTitle || "",
affiliation: user.affiliation || "",
bio: user.bio || "",
orcid: user.orcid || "",
googleScholar: user.googleScholar || "",
website: user.website || "",
});
setAvatarPreview(user.avatarUrl || null);
setAvatarFile(null);
setErrors({});
}
}, [user]);
// Validate fields
const validate = useCallback((): boolean => {
const newErrors: Record<string, string> = {};
// ORCID format: https://orcid.org/XXXX-XXXX-XXXX-XXXX
if (form.orcid && !/^https:\/\/orcid\.org\/\d{4}-\d{4}-\d{4}-\d{4}$/.test(form.orcid)) {
newErrors.orcid = "ORCID must be a valid URL (e.g., https://orcid.org/0000-0000-0000-0000)";
}
// Google Scholar – any URL is fine, but we can warn
if (form.googleScholar && !/^https?:\/\//.test(form.googleScholar)) {
newErrors.googleScholar = "Must be a valid URL starting with http:// or https://";
}
// Website URL
if (form.website && !/^https?:\/\//.test(form.website)) {
newErrors.website = "Must be a valid URL starting with http:// or https://";
}
setErrors(newErrors);
return Object.keys(newErrors).length === 0;
}, [form]);
// Check if form has changed
const hasChanges = (): boolean => {
if (!user) return false;
return (
form.fullName !== (user.fullName || "") ||
form.academicTitle !== (user.academicTitle || "") ||
form.affiliation !== (user.affiliation || "") ||
form.bio !== (user.bio || "") ||
form.orcid !== (user.orcid || "") ||
form.googleScholar !== (user.googleScholar || "") ||
form.website !== (user.website || "") ||
avatarFile !== null
);
};
// Handle escape key
useEffect(() => {
const handleEscape = (e: KeyboardEvent) => {
if (e.key === "Escape" && isOpen) onClose();
};
document.addEventListener("keydown", handleEscape);
return () => document.removeEventListener("keydown", handleEscape);
}, [isOpen, onClose]);
// Handle click outside (optional: close on backdrop click)
const handleBackdropClick = (e: React.MouseEvent<HTMLDivElement>) => {
if (e.target === e.currentTarget) onClose();
};
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
if (!validate()) return;
if (!hasChanges()) {
toast.info("No changes to save");
onClose();
return;
}
setSaving(true);
const success = await onSave(form, avatarFile);
setSaving(false);
if (success) onClose();
};
const handleAvatarChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const file = e.target.files?.[0];
if (file) {
if (file.size > 5 * 1024 * 1024) {
toast.error("Avatar file must be less than 5MB");
return;
}
setAvatarFile(file);
setAvatarPreview(URL.createObjectURL(file));
}
};
if (!isOpen) return null;
const inputClass =
"w-full border rounded-lg p-2 dark:bg-gray-700 focus:ring-2 focus:ring-primary focus:border-transparent disabled:opacity-50";
return (
<div
className="fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4"
onClick={handleBackdropClick}
>
<div className="bg-white dark:bg-gray-800 rounded-2xl max-w-md w-full max-h-[90vh] overflow-y-auto shadow-xl">
<div className="sticky top-0 bg-white dark:bg-gray-800 p-6 border-b flex justify-between items-center">
<h2 className="text-xl font-bold">Edit Profile</h2>
<button onClick={onClose} className="text-gray-500 hover:text-gray-700 text-2xl" aria-label="Close">
&times;
</button>
</div>
<form onSubmit={handleSubmit} className="p-6 space-y-4">
<div>
<label className="block text-sm font-medium mb-1">Avatar</label>
<input type="file" accept="image/*" onChange={handleAvatarChange} className="w-full text-sm" />
{avatarPreview && (
<img src={avatarPreview} alt="Preview" className="mt-2 w-16 h-16 rounded-full object-cover" />
)}
</div>
<div>
<label className="block text-sm font-medium mb-1">Full Name</label>
<input
type="text"
value={form.fullName}
onChange={(e) => setForm({ ...form, fullName: e.target.value })}
className={inputClass}
/>
</div>
<div>
<label className="block text-sm font-medium mb-1">Academic Title</label>
<input
type="text"
value={form.academicTitle}
onChange={(e) => setForm({ ...form, academicTitle: e.target.value })}
className={inputClass}
/>
</div>
<div>
<label className="block text-sm font-medium mb-1">Affiliation</label>
<input
type="text"
value={form.affiliation}
onChange={(e) => setForm({ ...form, affiliation: e.target.value })}
className={inputClass}
/>
</div>
<div>
<label className="block text-sm font-medium mb-1">Bio</label>
<textarea
rows={3}
value={form.bio}
onChange={(e) => setForm({ ...form, bio: e.target.value })}
className={inputClass}
/>
</div>
<div>
<label className="block text-sm font-medium mb-1">ORCID URL</label>
<input
type="url"
value={form.orcid}
onChange={(e) => setForm({ ...form, orcid: e.target.value })}
className={inputClass}
placeholder="https://orcid.org/0000-0000-0000-0000"
/>
{errors.orcid && <p className="text-red-500 text-xs mt-1">{errors.orcid}</p>}
</div>
<div>
<label className="block text-sm font-medium mb-1">Google Scholar URL</label>
<input
type="url"
value={form.googleScholar}
onChange={(e) => setForm({ ...form, googleScholar: e.target.value })}
className={inputClass}
/>
{errors.googleScholar && <p className="text-red-500 text-xs mt-1">{errors.googleScholar}</p>}
</div>
<div>
<label className="block text-sm font-medium mb-1">Personal Website</label>
<input
type="url"
value={form.website}
onChange={(e) => setForm({ ...form, website: e.target.value })}
className={inputClass}
/>
{errors.website && <p className="text-red-500 text-xs mt-1">{errors.website}</p>}
</div>
<div className="flex justify-end gap-3 pt-4">
<button type="button" onClick={onClose} className="px-4 py-2 border rounded-full">
Cancel
</button>
<button
type="submit"
disabled={saving || (!hasChanges() && !validate())}
className="px-4 py-2 bg-primary text-white rounded-full disabled:opacity-50"
>
{saving ? "Saving..." : "Save Changes"}
</button>
</div>
</form>
</div>
</div>
);
}

Xet Storage Details

Size:
8.36 kB
·
Xet hash:
7c96f6b03e189e9b2274ffd95ce22e5f5ce64f5a20c98a82f29755975d14c83d

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.