feat: 修改用户名,简化代码

This commit is contained in:
naiba
2024-11-30 15:02:42 +08:00
parent 7d672fa8c5
commit eb044d42bc
15 changed files with 395 additions and 447 deletions

View File

@@ -32,22 +32,25 @@ import { useTranslation } from "react-i18next";
const profileFormSchema = z.object({
original_password: z.string().min(5).max(72),
new_password: z.string().min(8).max(72),
new_username: z.string().min(1).max(32),
});
export const ProfileCard = ({ className }: { className: string }) => {
const { t } = useTranslation();
const { profile, setProfile } = useMainStore();
const form = useForm<z.infer<typeof profileFormSchema>>({
resolver: zodResolver(profileFormSchema),
defaultValues: {
original_password: '',
new_password: '',
new_username: profile?.username,
},
resetOptions: {
keepDefaultValues: false,
}
})
const { setProfile } = useMainStore();
const [open, setOpen] = useState(false);
const onSubmit = async (values: z.infer<typeof profileFormSchema>) => {
@@ -69,18 +72,33 @@ export const ProfileCard = ({ className }: { className: string }) => {
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>
<Button variant="outline" className={className}>
{t("UpdatePassword")}
{t("UpdateProfile")}
</Button>
</DialogTrigger>
<DialogContent className="sm:max-w-xl">
<ScrollArea className="max-h-[calc(100dvh-5rem)] p-3">
<div className="items-center mx-1">
<DialogHeader>
<DialogTitle>{t("UpdatePassword")}</DialogTitle>
<DialogTitle>{t("UpdateProfile")}</DialogTitle>
<DialogDescription />
</DialogHeader>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-2 my-2">
<FormField
control={form.control}
name="new_username"
render={({ field }) => (
<FormItem>
<FormLabel>{t("NewUsername")}</FormLabel>
<FormControl>
<Input
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="original_password"