151 lines
4.6 KiB
JavaScript
151 lines
4.6 KiB
JavaScript
import React, { useState } from 'react';
|
|
import { useAuth } from '../context/AuthContext';
|
|
import {
|
|
Dialog,
|
|
DialogContent,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
DialogDescription,
|
|
DialogFooter
|
|
} from './ui/dialog';
|
|
import { Button } from './ui/button';
|
|
import { Input } from './ui/input';
|
|
import { Label } from './ui/label';
|
|
import { toast } from 'sonner';
|
|
import { Lock } from 'lucide-react';
|
|
|
|
const ChangePasswordDialog = ({ open, onOpenChange }) => {
|
|
const { changePassword } = useAuth();
|
|
const [loading, setLoading] = useState(false);
|
|
const [formData, setFormData] = useState({
|
|
currentPassword: '',
|
|
newPassword: '',
|
|
confirmPassword: ''
|
|
});
|
|
|
|
const handleInputChange = (e) => {
|
|
const { name, value } = e.target;
|
|
setFormData(prev => ({ ...prev, [name]: value }));
|
|
};
|
|
|
|
const handleSubmit = async (e) => {
|
|
e.preventDefault();
|
|
|
|
if (formData.newPassword.length < 6) {
|
|
toast.error('New password must be at least 6 characters');
|
|
return;
|
|
}
|
|
|
|
if (formData.newPassword !== formData.confirmPassword) {
|
|
toast.error('New passwords do not match');
|
|
return;
|
|
}
|
|
|
|
setLoading(true);
|
|
|
|
try {
|
|
await changePassword(formData.currentPassword, formData.newPassword);
|
|
toast.success('Password changed successfully!');
|
|
|
|
// Reset form
|
|
setFormData({
|
|
currentPassword: '',
|
|
newPassword: '',
|
|
confirmPassword: ''
|
|
});
|
|
|
|
// Close dialog
|
|
onOpenChange(false);
|
|
} catch (error) {
|
|
const errorMessage = error.response?.data?.detail || 'Failed to change password';
|
|
toast.error(errorMessage);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
|
<DialogContent className="sm:max-w-md bg-white">
|
|
<DialogHeader>
|
|
<div className="flex items-center gap-2 mb-2">
|
|
<div className="inline-flex items-center justify-center w-10 h-10 rounded-full bg-[#f1eef9]">
|
|
<Lock className="h-5 w-5 text-[#ff9e77]" />
|
|
</div>
|
|
<DialogTitle className="text-2xl font-semibold text-[#422268]" style={{ fontFamily: "'Inter', sans-serif" }}>
|
|
Change Password
|
|
</DialogTitle>
|
|
</div>
|
|
<DialogDescription className="text-[#664fa3]" style={{ fontFamily: "'Nunito Sans', sans-serif" }}>
|
|
Update your password to keep your account secure.
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
|
|
<form onSubmit={handleSubmit} className="space-y-4 mt-4">
|
|
<div>
|
|
<Label htmlFor="currentPassword">Current Password</Label>
|
|
<Input
|
|
id="currentPassword"
|
|
name="currentPassword"
|
|
type="password"
|
|
required
|
|
value={formData.currentPassword}
|
|
onChange={handleInputChange}
|
|
placeholder="Enter current password"
|
|
className="h-12 rounded-xl border-2 border-[#ddd8eb] focus:border-[#664fa3]"
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<Label htmlFor="newPassword">New Password</Label>
|
|
<Input
|
|
id="newPassword"
|
|
name="newPassword"
|
|
type="password"
|
|
required
|
|
value={formData.newPassword}
|
|
onChange={handleInputChange}
|
|
placeholder="Enter new password (min. 6 characters)"
|
|
className="h-12 rounded-xl border-2 border-[#ddd8eb] focus:border-[#664fa3]"
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<Label htmlFor="confirmPassword">Confirm New Password</Label>
|
|
<Input
|
|
id="confirmPassword"
|
|
name="confirmPassword"
|
|
type="password"
|
|
required
|
|
value={formData.confirmPassword}
|
|
onChange={handleInputChange}
|
|
placeholder="Re-enter new password"
|
|
className="h-12 rounded-xl border-2 border-[#ddd8eb] focus:border-[#664fa3]"
|
|
/>
|
|
</div>
|
|
|
|
<DialogFooter className="mt-6">
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
onClick={() => onOpenChange(false)}
|
|
className="rounded-full px-6"
|
|
>
|
|
Cancel
|
|
</Button>
|
|
<Button
|
|
type="submit"
|
|
disabled={loading}
|
|
className="bg-[#DDD8EB] text-[#422268] hover:bg-white rounded-full px-6 disabled:opacity-50"
|
|
>
|
|
{loading ? 'Changing...' : 'Change Password'}
|
|
</Button>
|
|
</DialogFooter>
|
|
</form>
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
};
|
|
|
|
export default ChangePasswordDialog;
|