mirror of
https://github.com/Buriburizaem0n/admin-frontend-domain.git
synced 2026-09-19 09:40:13 +00:00
* update oauth2 * i18n * fix validation * chore: auto-fix linting and formatting issues --------- Co-authored-by: uubulb <uubulb@users.noreply.github.com>
169 lines
7.1 KiB
TypeScript
169 lines
7.1 KiB
TypeScript
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<z.infer<typeof profileFormSchema>>({
|
|
resolver: zodResolver(profileFormSchema),
|
|
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: z.infer<typeof profileFormSchema>) => {
|
|
try {
|
|
await updateProfile(values)
|
|
} catch (e) {
|
|
toast(t("Error"), {
|
|
description: `${e}`,
|
|
})
|
|
return
|
|
}
|
|
const profile = await getProfile()
|
|
setProfile(profile)
|
|
setOpen(false)
|
|
form.reset()
|
|
}
|
|
|
|
return (
|
|
<Dialog open={open} onOpenChange={setOpen}>
|
|
<DialogTrigger asChild>
|
|
<Button variant="outline" className={className}>
|
|
{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("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 autoComplete="username" {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="original_password"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>{t("OriginalPassword")}</FormLabel>
|
|
<FormControl>
|
|
<Input autoComplete="current-password" {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="new_password"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>{t("NewPassword")}</FormLabel>
|
|
<FormControl>
|
|
<Input {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="reject_password"
|
|
render={({ field }) => (
|
|
<FormItem className="flex items-center space-x-2">
|
|
<FormControl>
|
|
<div className="flex items-center gap-2">
|
|
<Checkbox
|
|
checked={field.value}
|
|
onCheckedChange={field.onChange}
|
|
/>
|
|
<Label className="text-sm">
|
|
{t("RejectPassword")}
|
|
</Label>
|
|
</div>
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
|
|
<DialogFooter className="justify-end">
|
|
<DialogClose asChild>
|
|
<Button type="button" className="my-2" variant="secondary">
|
|
{t("Close")}
|
|
</Button>
|
|
</DialogClose>
|
|
<Button type="submit" className="my-2">
|
|
{t("Confirm")}
|
|
</Button>
|
|
</DialogFooter>
|
|
</form>
|
|
</Form>
|
|
</div>
|
|
</ScrollArea>
|
|
</DialogContent>
|
|
</Dialog>
|
|
)
|
|
}
|