|
| 1 | +import { Button } from "@/components/ui/button" |
| 2 | +import { |
| 3 | + Dialog, |
| 4 | + DialogClose, |
| 5 | + DialogContent, |
| 6 | + DialogDescription, |
| 7 | + DialogFooter, |
| 8 | + DialogHeader, |
| 9 | + DialogTitle, |
| 10 | + DialogTrigger, |
| 11 | +} from "@/components/ui/dialog" |
| 12 | +import { Input } from "@/components/ui/input" |
| 13 | +import { |
| 14 | + Form, |
| 15 | + FormControl, |
| 16 | + FormField, |
| 17 | + FormItem, |
| 18 | + FormLabel, |
| 19 | + FormMessage, |
| 20 | +} from "@/components/ui/form" |
| 21 | +import { ScrollArea } from "@/components/ui/scroll-area" |
| 22 | +import { useForm } from "react-hook-form" |
| 23 | +import { z } from "zod" |
| 24 | +import { zodResolver } from "@hookform/resolvers/zod" |
| 25 | +import { getProfile, updateProfile } from "@/api/user" |
| 26 | +import { useState } from "react" |
| 27 | +import { useMainStore } from "@/hooks/useMainStore" |
| 28 | +import { toast } from "sonner" |
| 29 | + |
| 30 | +const profileFormSchema = z.object({ |
| 31 | + original_password: z.string().min(5).max(72), |
| 32 | + new_password: z.string().min(8).max(72), |
| 33 | +}); |
| 34 | + |
| 35 | +export const ProfileCard = ({ className }: { className: string }) => { |
| 36 | + const form = useForm<z.infer<typeof profileFormSchema>>({ |
| 37 | + resolver: zodResolver(profileFormSchema), |
| 38 | + defaultValues: { |
| 39 | + original_password: '', |
| 40 | + new_password: '', |
| 41 | + }, |
| 42 | + resetOptions: { |
| 43 | + keepDefaultValues: false, |
| 44 | + } |
| 45 | + }) |
| 46 | + |
| 47 | + const { setProfile } = useMainStore(); |
| 48 | + const [open, setOpen] = useState(false); |
| 49 | + |
| 50 | + const onSubmit = async (values: z.infer<typeof profileFormSchema>) => { |
| 51 | + try { |
| 52 | + await updateProfile(values); |
| 53 | + } catch (e) { |
| 54 | + toast("Update failed", { |
| 55 | + description: `${e}`, |
| 56 | + }) |
| 57 | + return; |
| 58 | + } |
| 59 | + const profile = await getProfile(); |
| 60 | + setProfile(profile); |
| 61 | + setOpen(false); |
| 62 | + form.reset(); |
| 63 | + } |
| 64 | + |
| 65 | + return ( |
| 66 | + <Dialog open={open} onOpenChange={setOpen}> |
| 67 | + <DialogTrigger asChild> |
| 68 | + <Button variant="outline" className={className}> |
| 69 | + Update Password |
| 70 | + </Button> |
| 71 | + </DialogTrigger> |
| 72 | + <DialogContent className="sm:max-w-xl"> |
| 73 | + <ScrollArea className="max-h-[calc(100dvh-5rem)] p-3"> |
| 74 | + <div className="items-center mx-1"> |
| 75 | + <DialogHeader> |
| 76 | + <DialogTitle>Update Server</DialogTitle> |
| 77 | + <DialogDescription /> |
| 78 | + </DialogHeader> |
| 79 | + <Form {...form}> |
| 80 | + <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-2 my-2"> |
| 81 | + <FormField |
| 82 | + control={form.control} |
| 83 | + name="original_password" |
| 84 | + render={({ field }) => ( |
| 85 | + <FormItem> |
| 86 | + <FormLabel>Original Password</FormLabel> |
| 87 | + <FormControl> |
| 88 | + <Input |
| 89 | + {...field} |
| 90 | + /> |
| 91 | + </FormControl> |
| 92 | + <FormMessage /> |
| 93 | + </FormItem> |
| 94 | + )} |
| 95 | + /> |
| 96 | + <FormField |
| 97 | + control={form.control} |
| 98 | + name="new_password" |
| 99 | + render={({ field }) => ( |
| 100 | + <FormItem> |
| 101 | + <FormLabel>New Password</FormLabel> |
| 102 | + <FormControl> |
| 103 | + <Input |
| 104 | + {...field} |
| 105 | + /> |
| 106 | + </FormControl> |
| 107 | + <FormMessage /> |
| 108 | + </FormItem> |
| 109 | + )} |
| 110 | + /> |
| 111 | + |
| 112 | + <DialogFooter className="justify-end"> |
| 113 | + <DialogClose asChild> |
| 114 | + <Button type="button" className="my-2" variant="secondary"> |
| 115 | + Close |
| 116 | + </Button> |
| 117 | + </DialogClose> |
| 118 | + <Button type="submit" className="my-2">Submit</Button> |
| 119 | + </DialogFooter> |
| 120 | + </form> |
| 121 | + </Form> |
| 122 | + </div> |
| 123 | + </ScrollArea> |
| 124 | + </DialogContent> |
| 125 | + </Dialog> |
| 126 | + ) |
| 127 | +} |
0 commit comments