fix api types, add more form fields

This commit is contained in:
uubulb
2024-11-15 21:28:48 +08:00
parent 08560316a0
commit b1a9a231a7
5 changed files with 432 additions and 354 deletions

View File

@@ -29,40 +29,53 @@ import { useForm } from "react-hook-form"
import { z } from "zod"
import { zodResolver } from "@hookform/resolvers/zod"
import { ModelService } from "@/types"
import { createService, updateService } from "@/api/service"
import { Checkbox } from "./ui/checkbox"
import { Label } from "./ui/label"
interface ServiceCardProps {
className?: string;
data?: ModelService;
}
const formSchema = z.object({
const serviceFormSchema = z.object({
cover: z.number(),
duration: z.number().min(30),
enable_show_in_service: z.boolean().default(false),
enable_trigger_task: z.boolean().default(false),
fail_trigger_tasks: z.array(z.number()),
latency_notify: z.boolean(),
max_latency: z.number(),
min_latency: z.number(),
name: z.string(),
notification_group_id: z.number(),
notify: z.boolean(),
recover_trigger_tasks: z.array(z.number()),
skip_servers: z.record(z.boolean()),
target: z.string(),
type: z.number(),
enableShowInService: z.boolean().default(false),
duration: z.number().min(30),
})
});
const monitorTypes = {
const serviceTypes = {
1: "HTTP GET (Certificate expiration and changes)",
2: "ICMP Ping",
3: "TCPing",
}
const serviceCoverageTypes = {
0: "All excludes specific servers",
1: "Only specific servers",
}
export const ServiceCard: React.FC<ServiceCardProps> = ({ className, data }) => {
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
name: data?.name,
target: data?.target,
type: data?.type,
enableShowInService: data?.enable_show_in_service,
duration: data?.duration,
},
const form = useForm<z.infer<typeof serviceFormSchema>>({
resolver: zodResolver(serviceFormSchema),
defaultValues: data,
})
function onSubmit(values: z.infer<typeof formSchema>) {
const onSubmit = (values: z.infer<typeof serviceFormSchema>) => {
data?.id ? updateService(data.id, values)
: createService(values);
}
return (
@@ -78,7 +91,7 @@ export const ServiceCard: React.FC<ServiceCardProps> = ({ className, data }) =>
</DialogHeader>
<div className="items-center">
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-2">
<FormField
control={form.control}
name="name"
@@ -99,7 +112,7 @@ export const ServiceCard: React.FC<ServiceCardProps> = ({ className, data }) =>
<FormItem>
<FormLabel>Target</FormLabel>
<FormControl>
<Input type="link" {...field} />
<Input type="link" placeholder="HTTP (https://t.tt)Ping (t.tt)TCP (t.tt:80)" {...field} />
</FormControl>
<FormMessage />
</FormItem>
@@ -118,10 +131,61 @@ export const ServiceCard: React.FC<ServiceCardProps> = ({ className, data }) =>
</SelectTrigger>
</FormControl>
<SelectContent>
{Object.entries(monitorTypes).map(([k, v]) => (
<SelectItem value={k}>
{v}
</SelectItem>
{Object.entries(serviceTypes).map(([k, v]) => (
<SelectItem value={k}>{v}</SelectItem>
))}
</SelectContent>
</Select>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="enable_show_in_service"
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">Show in Service</Label>
</div>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="duration"
render={({ field }) => (
<FormItem>
<FormLabel>Interval</FormLabel>
<FormControl>
<Input type="number" placeholder="Seconds" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="cover"
render={({ field }) => (
<FormItem>
<FormLabel>Coverage</FormLabel>
<Select onValueChange={field.onChange} defaultValue={`${field.value}`}>
<FormControl>
<SelectTrigger>
<SelectValue placeholder="Select service type" />
</SelectTrigger>
</FormControl>
<SelectContent>
{Object.entries(serviceCoverageTypes).map(([k, v]) => (
<SelectItem value={k}>{v}</SelectItem>
))}
</SelectContent>
</Select>