import { getProfile, updateProfile } from "@/api/user" import { Button } from "@/components/ui/button" import { Checkbox } from "@/components/ui/checkbox" import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog" import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { ScrollArea } from "@/components/ui/scroll-area" import { useMainStore } from "@/hooks/useMainStore" import { asOptionalField } from "@/lib/utils" import { zodResolver } from "@hookform/resolvers/zod" import { useState } from "react" import { useForm } from "react-hook-form" import { useTranslation } from "react-i18next" import { toast } from "sonner" import { z } from "zod" 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), reject_password: asOptionalField(z.boolean()), }) export const ProfileCard = ({ className }: { className: string }) => { const { t } = useTranslation() const { profile, setProfile } = useMainStore() const form = useForm({ resolver: zodResolver(profileFormSchema) as any, defaultValues: { original_password: "", new_password: "", new_username: profile?.username, reject_password: profile?.reject_password, }, resetOptions: { keepDefaultValues: false, }, }) const [open, setOpen] = useState(false) const onSubmit = async (values: any) => { try { await updateProfile(values) } catch (e) { console.error(e) toast(t("Error"), { description: t("Results.UnExpectedError"), }) return } const profile = await getProfile() setProfile(profile) setOpen(false) form.reset() } return (
{t("UpdateProfile")}
( {t("NewUsername")} )} /> ( {t("OriginalPassword")} )} /> ( {t("NewPassword")} )} /> (
)} />
) }