ChangePasswordDialog.tsx
4.27 KB
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
"use client";
import { useState } from "react";
import { KeyRound, Loader2 } from "lucide-react";
import { changePassword } from "@/lib/auth";
interface ChangePasswordDialogProps {
open: boolean;
onClose: () => void;
}
export default function ChangePasswordDialog({ open, onClose }: ChangePasswordDialogProps) {
const [oldPassword, setOldPassword] = useState("");
const [newPassword, setNewPassword] = useState("");
const [confirmPassword, setConfirmPassword] = useState("");
const [error, setError] = useState("");
const [done, setDone] = useState(false);
const [loading, setLoading] = useState(false);
if (!open) return null;
const reset = () => {
setOldPassword("");
setNewPassword("");
setConfirmPassword("");
setError("");
setDone(false);
};
const handleClose = () => {
reset();
onClose();
};
const handleSubmit = async (event: React.FormEvent) => {
event.preventDefault();
if (newPassword !== confirmPassword) {
setError("两次输入的新密码不一致");
return;
}
if (newPassword.length < 6) {
setError("新密码长度不能少于 6 位");
return;
}
setLoading(true);
setError("");
try {
await changePassword(oldPassword, newPassword);
setDone(true);
setTimeout(handleClose, 1200);
} catch (err) {
setError(err instanceof Error ? err.message : "修改失败");
} finally {
setLoading(false);
}
};
const inputClass =
"w-full px-3 py-2 text-sm border border-slate-200 rounded-lg outline-none transition focus:border-blue-500 focus:ring-2 focus:ring-blue-500/20";
return (
<div
className="fixed inset-0 z-[60] flex items-center justify-center bg-slate-900/40 backdrop-blur-sm"
onClick={handleClose}
>
<form
onSubmit={handleSubmit}
onClick={(e) => e.stopPropagation()}
className="w-[360px] bg-white rounded-2xl shadow-2xl p-5 space-y-3.5"
>
<div className="flex items-center gap-2.5 pb-1">
<div className="w-9 h-9 bg-blue-50 text-blue-600 rounded-xl flex items-center justify-center">
<KeyRound className="w-4.5 h-4.5" />
</div>
<h3 className="text-sm font-bold text-slate-800">修改密码</h3>
</div>
{done ? (
<p className="text-xs text-emerald-600 bg-emerald-50 border border-emerald-100 rounded-lg px-3 py-2.5">
密码修改成功
</p>
) : (
<>
<input
type="password"
value={oldPassword}
onChange={(e) => setOldPassword(e.target.value)}
placeholder="当前密码"
autoComplete="current-password"
className={inputClass}
/>
<input
type="password"
value={newPassword}
onChange={(e) => setNewPassword(e.target.value)}
placeholder="新密码(至少 6 位)"
autoComplete="new-password"
className={inputClass}
/>
<input
type="password"
value={confirmPassword}
onChange={(e) => setConfirmPassword(e.target.value)}
placeholder="确认新密码"
autoComplete="new-password"
className={inputClass}
/>
{error && (
<div className="text-xs text-red-600 bg-red-50 border border-red-100 rounded-lg px-3 py-2">
{error}
</div>
)}
<div className="flex justify-end gap-2 pt-1">
<button
type="button"
onClick={handleClose}
className="px-3.5 py-1.5 text-xs font-semibold text-slate-600 hover:bg-slate-100 rounded-lg transition"
>
取消
</button>
<button
type="submit"
disabled={loading}
className="px-3.5 py-1.5 text-xs font-semibold text-white bg-blue-600 hover:bg-blue-700 disabled:opacity-60 rounded-lg transition flex items-center gap-1.5"
>
{loading && <Loader2 className="w-3 h-3 animate-spin" />}
确定
</button>
</div>
</>
)}
</form>
</div>
);
}