mirror of
https://github.com/Buriburizaem0n/admin-frontend-domain.git
synced 2026-02-05 21:20:07 +00:00
221 lines
10 KiB
TypeScript
221 lines
10 KiB
TypeScript
import { updateServer } from "@/api/server"
|
|
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 { Textarea } from "@/components/ui/textarea"
|
|
import { IconButton } from "@/components/xui/icon-button"
|
|
import { conv } from "@/lib/utils"
|
|
import { asOptionalField } from "@/lib/utils"
|
|
import { ModelServer } from "@/types"
|
|
import { zodResolver } from "@hookform/resolvers/zod"
|
|
import { useState } from "react"
|
|
import { useForm } from "react-hook-form"
|
|
import { useTranslation } from "react-i18next"
|
|
import { KeyedMutator } from "swr"
|
|
import { z } from "zod"
|
|
|
|
interface ServerCardProps {
|
|
data: ModelServer
|
|
mutate: KeyedMutator<ModelServer[]>
|
|
}
|
|
|
|
const serverFormSchema = z.object({
|
|
name: z.string().min(1),
|
|
note: asOptionalField(z.string()),
|
|
public_note: asOptionalField(z.string()),
|
|
display_index: z.coerce.number().int(),
|
|
hide_for_guest: asOptionalField(z.boolean()),
|
|
enable_ddns: asOptionalField(z.boolean()),
|
|
ddns_profiles: asOptionalField(z.array(z.number())),
|
|
})
|
|
|
|
export const ServerCard: React.FC<ServerCardProps> = ({ data, mutate }) => {
|
|
const { t } = useTranslation()
|
|
const form = useForm<z.infer<typeof serverFormSchema>>({
|
|
resolver: zodResolver(serverFormSchema),
|
|
defaultValues: data,
|
|
resetOptions: {
|
|
keepDefaultValues: false,
|
|
},
|
|
})
|
|
|
|
const [open, setOpen] = useState(false)
|
|
|
|
const onSubmit = async (values: z.infer<typeof serverFormSchema>) => {
|
|
await updateServer(data.id, values)
|
|
setOpen(false)
|
|
await mutate()
|
|
form.reset()
|
|
}
|
|
|
|
return (
|
|
<Dialog open={open} onOpenChange={setOpen}>
|
|
<DialogTrigger asChild>
|
|
<IconButton variant="outline" icon="edit" />
|
|
</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("EditServer")}</DialogTitle>
|
|
<DialogDescription />
|
|
</DialogHeader>
|
|
<Form {...form}>
|
|
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-2 my-2">
|
|
<FormField
|
|
control={form.control}
|
|
name="name"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>{t("Name")}</FormLabel>
|
|
<FormControl>
|
|
<Input placeholder="My Server" {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="display_index"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>{t("Weight")}</FormLabel>
|
|
<FormControl>
|
|
<Input type="number" placeholder="0" {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="ddns_profiles"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>
|
|
{t("DDNSProfiles") + t("SeparateWithComma")}
|
|
</FormLabel>
|
|
<FormControl>
|
|
<Input
|
|
placeholder="1,2,3"
|
|
{...field}
|
|
value={conv.arrToStr(field.value || [])}
|
|
onChange={(e) => {
|
|
console.log(field.value)
|
|
const arr = conv
|
|
.strToArr(e.target.value)
|
|
.map(Number)
|
|
field.onChange(arr)
|
|
}}
|
|
/>
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="enable_ddns"
|
|
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("EnableDDNS")}
|
|
</Label>
|
|
</div>
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="hide_for_guest"
|
|
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("HideForGuest")}
|
|
</Label>
|
|
</div>
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="note"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>{t("Private") + t("Note")}</FormLabel>
|
|
<FormControl>
|
|
<Textarea className="resize-none" {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="public_note"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>{t("Public") + t("Note")}</FormLabel>
|
|
<FormControl>
|
|
<Textarea className="resize-y" {...field} />
|
|
</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("Submit")}
|
|
</Button>
|
|
</DialogFooter>
|
|
</form>
|
|
</Form>
|
|
</div>
|
|
</ScrollArea>
|
|
</DialogContent>
|
|
</Dialog>
|
|
)
|
|
}
|