import { getProfile, updateProfile } from "@/api/user" import { Button } from "@/components/ui/button" 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 { ScrollArea } from "@/components/ui/scroll-area" import { useMainStore } from "@/hooks/useMainStore" 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), }) export const ProfileCard = ({ className }: { className: string }) => { const { t } = useTranslation() const { profile, setProfile } = useMainStore() const form = useForm>({ resolver: zodResolver(profileFormSchema), defaultValues: { original_password: "", new_password: "", new_username: profile?.username, }, resetOptions: { keepDefaultValues: false, }, }) const [open, setOpen] = useState(false) const onSubmit = async (values: z.infer) => { try { await updateProfile(values) } catch (e) { toast(t("Error"), { description: `${e}`, }) return } const profile = await getProfile() setProfile(profile) setOpen(false) form.reset() } return (
{t("UpdateProfile")}
( {t("NewUsername")} )} /> ( {t("OriginalPassword")} )} /> ( {t("NewPassword")} )} />
) }