mirror of
https://github.com/Buriburizaem0n/admin-frontend-domain.git
synced 2026-02-05 05:00:06 +00:00
fix validation & implement profile page (#5)
* fix validation * implement profile page * catch error
This commit is contained in:
127
src/components/profile.tsx
Normal file
127
src/components/profile.tsx
Normal file
@@ -0,0 +1,127 @@
|
||||
import { Button } from "@/components/ui/button"
|
||||
import {
|
||||
Dialog,
|
||||
DialogClose,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogTrigger,
|
||||
} from "@/components/ui/dialog"
|
||||
import { Input } from "@/components/ui/input"
|
||||
import {
|
||||
Form,
|
||||
FormControl,
|
||||
FormField,
|
||||
FormItem,
|
||||
FormLabel,
|
||||
FormMessage,
|
||||
} from "@/components/ui/form"
|
||||
import { ScrollArea } from "@/components/ui/scroll-area"
|
||||
import { useForm } from "react-hook-form"
|
||||
import { z } from "zod"
|
||||
import { zodResolver } from "@hookform/resolvers/zod"
|
||||
import { getProfile, updateProfile } from "@/api/user"
|
||||
import { useState } from "react"
|
||||
import { useMainStore } from "@/hooks/useMainStore"
|
||||
import { toast } from "sonner"
|
||||
|
||||
const profileFormSchema = z.object({
|
||||
original_password: z.string().min(5).max(72),
|
||||
new_password: z.string().min(8).max(72),
|
||||
});
|
||||
|
||||
export const ProfileCard = ({ className }: { className: string }) => {
|
||||
const form = useForm<z.infer<typeof profileFormSchema>>({
|
||||
resolver: zodResolver(profileFormSchema),
|
||||
defaultValues: {
|
||||
original_password: '',
|
||||
new_password: '',
|
||||
},
|
||||
resetOptions: {
|
||||
keepDefaultValues: false,
|
||||
}
|
||||
})
|
||||
|
||||
const { setProfile } = useMainStore();
|
||||
const [open, setOpen] = useState(false);
|
||||
|
||||
const onSubmit = async (values: z.infer<typeof profileFormSchema>) => {
|
||||
try {
|
||||
await updateProfile(values);
|
||||
} catch (e) {
|
||||
toast("Update failed", {
|
||||
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}>
|
||||
Update Password
|
||||
</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>Update Server</DialogTitle>
|
||||
<DialogDescription />
|
||||
</DialogHeader>
|
||||
<Form {...form}>
|
||||
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-2 my-2">
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="original_password"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Original Password</FormLabel>
|
||||
<FormControl>
|
||||
<Input
|
||||
{...field}
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="new_password"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>New Password</FormLabel>
|
||||
<FormControl>
|
||||
<Input
|
||||
{...field}
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<DialogFooter className="justify-end">
|
||||
<DialogClose asChild>
|
||||
<Button type="button" className="my-2" variant="secondary">
|
||||
Close
|
||||
</Button>
|
||||
</DialogClose>
|
||||
<Button type="submit" className="my-2">Submit</Button>
|
||||
</DialogFooter>
|
||||
</form>
|
||||
</Form>
|
||||
</div>
|
||||
</ScrollArea>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user