Files
portabase/src/features/database/database-form.tsx
T

113 lines
4.5 KiB
TypeScript
Raw Normal View History

"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";
export type DatabaseFormProps = {
defaultValues?: DatabaseType;
databaseId?: string;
2025-05-16 15:54:17 +02:00
};
export const DatabaseForm = (props: DatabaseFormProps) => {
2025-05-16 15:54:17 +02:00
const { defaultValues, databaseId } = props;
2025-05-16 15:54:17 +02:00
const isCreate = !Boolean(defaultValues);
const form = useZodForm({
schema: DatabaseSchema,
2025-05-16 15:54:17 +02:00
defaultValues: { ...defaultValues },
});
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 });
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();
},
});
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);
}}
>
<FormField
control={form.control}
name="name"
2025-05-16 15:54:17 +02:00
render={({ field }) => (
<FormItem>
<FormLabel>Name</FormLabel>
<FormControl>
2025-05-16 15:54:17 +02:00
<Input disabled placeholder="Database 1" {...field} />
</FormControl>
<FormDescription>Your database project name setup in agent</FormDescription>
2025-05-16 15:54:17 +02:00
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="dbms"
2025-05-16 15:54:17 +02:00
render={({ field }) => (
<FormItem>
<FormLabel>Database type</FormLabel>
<FormControl>
2025-05-16 15:54:17 +02:00
<Input disabled placeholder="PostgreSQL" {...field} />
</FormControl>
<FormDescription>Your database project name setup in agent</FormDescription>
2025-05-16 15:54:17 +02:00
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="description"
2025-05-16 15:54:17 +02:00
render={({ field }) => (
<FormItem>
<FormLabel>Description</FormLabel>
<FormControl>
2025-05-16 15:54:17 +02:00
<Input placeholder="Prod database for project 1" {...field} />
</FormControl>
<FormDescription>Add a short description about this database</FormDescription>
2025-05-16 15:54:17 +02:00
<FormMessage />
</FormItem>
)}
/>
2025-05-16 15:54:17 +02:00
<Button>{isCreate ? `Create database` : `Save database`}</Button>
</Form>
</CardContent>
</Card>
</TooltipProvider>
2025-05-16 15:54:17 +02:00
);
};