56 lines
2.1 KiB
TypeScript
56 lines
2.1 KiB
TypeScript
"use client";
|
|||
|
|
|
||
|
|
import { useActionState, useEffect } from "react";
|
||
|
|
import { toast } from "sonner";
|
||
|
|
import { Button } from "@/components/ui/button";
|
||
|
|
import { Input } from "@/components/ui/input";
|
||
|
|
import { Label } from "@/components/ui/label";
|
||
|
|
import { updateProfile, changePassword, type MeState } from "./actions";
|
||
|
|
|
||
|
|
function useToastOnDone(state: MeState, okMsg: string) {
|
||
|
|
useEffect(() => {
|
||
|
|
if (state.ok) toast.success(okMsg);
|
||
|
|
else if (state.error) toast.error(state.error);
|
||
|
|
}, [state, okMsg]);
|
||
|
|
}
|
||
|
|
|
||
|
|
export function ProfileForm({ nickname, username }: { nickname: string; username: string }) {
|
||
|
|
const [state, action, pending] = useActionState(updateProfile, {});
|
||
|
|
useToastOnDone(state, "资料已更新");
|
||
|
|
return (
|
||
|
|
<form action={action} className="max-w-md space-y-4">
|
||
|
|
<div className="space-y-1.5">
|
||
|
|
<Label>用户名</Label>
|
||
|
|
<Input value={username} disabled />
|
||
|
|
</div>
|
||
|
|
<div className="space-y-1.5">
|
||
|
|
<Label htmlFor="nickname">昵称</Label>
|
||
|
|
<Input id="nickname" name="nickname" defaultValue={nickname} />
|
||
|
|
</div>
|
||
|
|
<Button type="submit" disabled={pending}>{pending ? "保存中…" : "保存资料"}</Button>
|
||
|
|
</form>
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
export function PasswordForm() {
|
||
|
|
const [state, action, pending] = useActionState(changePassword, {});
|
||
|
|
useToastOnDone(state, "密码已修改");
|
||
|
|
return (
|
||
|
|
<form action={action} className="max-w-md space-y-4">
|
||
|
|
<div className="space-y-1.5">
|
||
|
|
<Label htmlFor="oldPassword">原密码</Label>
|
||
|
|
<Input id="oldPassword" name="oldPassword" type="password" autoComplete="current-password" />
|
||
|
|
</div>
|
||
|
|
<div className="space-y-1.5">
|
||
|
|
<Label htmlFor="newPassword">新密码</Label>
|
||
|
|
<Input id="newPassword" name="newPassword" type="password" autoComplete="new-password" placeholder="至少 6 位" />
|
||
|
|
</div>
|
||
|
|
<div className="space-y-1.5">
|
||
|
|
<Label htmlFor="confirmPassword">确认新密码</Label>
|
||
|
|
<Input id="confirmPassword" name="confirmPassword" type="password" autoComplete="new-password" />
|
||
|
|
</div>
|
||
|
|
<Button type="submit" disabled={pending}>{pending ? "提交中…" : "修改密码"}</Button>
|
||
|
|
</form>
|
||
|
|
);
|
||
|
|
}
|