Files
admin-frontend-domain/src/components/user.tsx
T
UUBulbanduubulb f93d266810 improve: add missing try-catch cases (#86)
* improve: add missing try-catch cases

* i18n: support german

* chore: auto-fix linting and formatting issues

---------

Co-authored-by: uubulb <uubulb@users.noreply.github.com>
2025-01-03 22:43:36 +08:00

167 lines
6.6 KiB
TypeScript

import { createUser } 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 {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select"
import { IconButton } from "@/components/xui/icon-button"
import { ModelUser } from "@/types"
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 { KeyedMutator } from "swr"
import { z } from "zod"
interface UserCardProps {
mutate: KeyedMutator<ModelUser[]>
}
const userFormSchema = z.object({
username: z.string().min(1),
role: z.number().int().min(0).max(1),
password: z.string().min(8).max(72),
})
export const UserCard: React.FC<UserCardProps> = ({ mutate }) => {
const { t } = useTranslation()
const form = useForm<z.infer<typeof userFormSchema>>({
resolver: zodResolver(userFormSchema),
defaultValues: {
username: "",
role: 1,
password: "",
},
resetOptions: {
keepDefaultValues: false,
},
})
const [open, setOpen] = useState(false)
const onSubmit = async (values: z.infer<typeof userFormSchema>) => {
try {
await createUser(values)
} catch (e) {
console.error(e)
toast(t("Error"), {
description: t("Results.UnExpectedError"),
})
return
}
setOpen(false)
await mutate()
form.reset()
}
return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>
<IconButton icon="plus" />
</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("NewUser")}</DialogTitle>
<DialogDescription />
</DialogHeader>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-2 my-2">
<FormField
control={form.control}
name="username"
render={({ field }) => (
<FormItem>
<FormLabel>{t("Username")}</FormLabel>
<FormControl>
<Input {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="password"
render={({ field }) => (
<FormItem>
<FormLabel>{t("Password")}</FormLabel>
<FormControl>
<Input {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="role"
render={({ field }) => (
<FormItem>
<FormLabel>{t("Role")}</FormLabel>
<Select
onValueChange={(value) =>
field.onChange(parseInt(value))
}
defaultValue={field.value.toString()}
>
<FormControl>
<SelectTrigger>
<SelectValue
placeholder={t("SelectRole")}
/>
</SelectTrigger>
</FormControl>
<SelectContent>
<SelectItem value="0">{t("Admin")}</SelectItem>
<SelectItem value="1">{t("User")}</SelectItem>
</SelectContent>
</Select>
<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>
)
}