2024-12-12 15:32:33 +01:00
|
|
|
"use client";
|
|
|
|
|
|
2025-05-16 15:54:17 +02:00
|
|
|
import { Card, CardContent } from "@/components/ui/card";
|
|
|
|
|
import { FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, useZodForm } from "@/components/ui/form";
|
|
|
|
|
import { Input } from "@/components/ui/input";
|
|
|
|
|
import { Form } from "@/components/ui/form";
|
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
|
import { useRouter } from "next/navigation";
|
|
|
|
|
import { useMutation } from "@tanstack/react-query";
|
|
|
|
|
import { TooltipProvider } from "@/components/ui/tooltip";
|
2026-05-24 17:09:12 +02:00
|
|
|
import { DatabaseSchema, DatabaseType } from "@/features/database/database-form.schema";
|
|
|
|
|
import { updateDatabaseAction } from "@/features/database/database-form.action";
|
2025-05-16 15:54:17 +02:00
|
|
|
import { toast } from "sonner";
|
2024-12-12 15:32:33 +01:00
|
|
|
|
|
|
|
|
export type DatabaseFormProps = {
|
|
|
|
|
defaultValues?: DatabaseType;
|
|
|
|
|
databaseId?: string;
|
2025-05-16 15:54:17 +02:00
|
|
|
};
|
2024-12-12 15:32:33 +01:00
|
|
|
|
|
|
|
|
export const DatabaseForm = (props: DatabaseFormProps) => {
|
2025-05-16 15:54:17 +02:00
|
|
|
const { defaultValues, databaseId } = props;
|
2024-12-12 15:32:33 +01:00
|
|
|
|
2025-05-16 15:54:17 +02:00
|
|
|
const isCreate = !Boolean(defaultValues);
|
2024-12-12 15:32:33 +01:00
|
|
|
|
|
|
|
|
const form = useZodForm({
|
|
|
|
|
schema: DatabaseSchema,
|
2025-05-16 15:54:17 +02:00
|
|
|
defaultValues: { ...defaultValues },
|
2024-12-12 15:32:33 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const router = useRouter();
|
|
|
|
|
|
|
|
|
|
const mutation = useMutation({
|
|
|
|
|
mutationFn: async (values: DatabaseType) => {
|
2025-05-16 15:54:17 +02:00
|
|
|
if (!databaseId) {
|
|
|
|
|
throw new Error("Database ID is required");
|
|
|
|
|
}
|
|
|
|
|
const database = await updateDatabaseAction({ id: databaseId, data: values });
|
2024-12-12 15:32:33 +01:00
|
|
|
|
2025-05-16 15:54:17 +02:00
|
|
|
if (!database) {
|
|
|
|
|
toast.error("Failed to update database");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-12-13 14:08:24 +01:00
|
|
|
if (database.serverError) {
|
2025-05-16 15:54:17 +02:00
|
|
|
toast.error(database.serverError);
|
2024-12-13 14:08:24 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
toast.success(`Database settings successfully updated!`);
|
|
|
|
|
|
2025-05-16 15:54:17 +02:00
|
|
|
router.back();
|
|
|
|
|
},
|
|
|
|
|
});
|
2024-12-12 15:32:33 +01:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<TooltipProvider>
|
|
|
|
|
<Card>
|
|
|
|
|
<CardContent>
|
2025-05-16 15:54:17 +02:00
|
|
|
<Form
|
|
|
|
|
form={form}
|
|
|
|
|
className="flex flex-col gap-4 mt-3"
|
|
|
|
|
onSubmit={async (values) => {
|
|
|
|
|
await mutation.mutateAsync(values);
|
|
|
|
|
}}
|
2024-12-12 15:32:33 +01:00
|
|
|
>
|
|
|
|
|
<FormField
|
|
|
|
|
control={form.control}
|
|
|
|
|
name="name"
|
2025-05-16 15:54:17 +02:00
|
|
|
render={({ field }) => (
|
2024-12-12 15:32:33 +01:00
|
|
|
<FormItem>
|
|
|
|
|
<FormLabel>Name</FormLabel>
|
|
|
|
|
<FormControl>
|
2025-05-16 15:54:17 +02:00
|
|
|
<Input disabled placeholder="Database 1" {...field} />
|
2024-12-12 15:32:33 +01:00
|
|
|
</FormControl>
|
|
|
|
|
<FormDescription>Your database project name setup in agent</FormDescription>
|
2025-05-16 15:54:17 +02:00
|
|
|
<FormMessage />
|
2024-12-12 15:32:33 +01:00
|
|
|
</FormItem>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
<FormField
|
|
|
|
|
control={form.control}
|
|
|
|
|
name="dbms"
|
2025-05-16 15:54:17 +02:00
|
|
|
render={({ field }) => (
|
2024-12-12 15:32:33 +01:00
|
|
|
<FormItem>
|
|
|
|
|
<FormLabel>Database type</FormLabel>
|
|
|
|
|
<FormControl>
|
2025-05-16 15:54:17 +02:00
|
|
|
<Input disabled placeholder="PostgreSQL" {...field} />
|
2024-12-12 15:32:33 +01:00
|
|
|
</FormControl>
|
|
|
|
|
<FormDescription>Your database project name setup in agent</FormDescription>
|
2025-05-16 15:54:17 +02:00
|
|
|
<FormMessage />
|
2024-12-12 15:32:33 +01:00
|
|
|
</FormItem>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
<FormField
|
|
|
|
|
control={form.control}
|
|
|
|
|
name="description"
|
2025-05-16 15:54:17 +02:00
|
|
|
render={({ field }) => (
|
2024-12-12 15:32:33 +01:00
|
|
|
<FormItem>
|
|
|
|
|
<FormLabel>Description</FormLabel>
|
|
|
|
|
<FormControl>
|
2025-05-16 15:54:17 +02:00
|
|
|
<Input placeholder="Prod database for project 1" {...field} />
|
2024-12-12 15:32:33 +01:00
|
|
|
</FormControl>
|
|
|
|
|
<FormDescription>Add a short description about this database</FormDescription>
|
2025-05-16 15:54:17 +02:00
|
|
|
<FormMessage />
|
2024-12-12 15:32:33 +01:00
|
|
|
</FormItem>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
2025-05-16 15:54:17 +02:00
|
|
|
<Button>{isCreate ? `Create database` : `Save database`}</Button>
|
2024-12-12 15:32:33 +01:00
|
|
|
</Form>
|
|
|
|
|
</CardContent>
|
|
|
|
|
</Card>
|
|
|
|
|
</TooltipProvider>
|
2025-05-16 15:54:17 +02:00
|
|
|
);
|
|
|
|
|
};
|