mirror of
https://github.com/Portabase/portabase.git
synced 2026-07-14 11:16:13 +02:00
Adding the Edit/creation for agent.
This commit is contained in:
@@ -0,0 +1,36 @@
|
|||||||
|
import {PageParams} from "@/types/next";
|
||||||
|
import {Page, PageContent, PageDescription, PageHeader, PageTitle} from "@/features/layout/page";
|
||||||
|
import {AgentForm} from "@/components/wrappers/Agent/AgentForm/AgentForm";
|
||||||
|
import {requiredCurrentUser} from "@/auth/current-user";
|
||||||
|
import {prisma} from "@/prisma";
|
||||||
|
import {notFound} from "next/navigation";
|
||||||
|
|
||||||
|
|
||||||
|
export default async function RoutePage(props: PageParams<{
|
||||||
|
agentId: string;
|
||||||
|
}>) {
|
||||||
|
const user = await requiredCurrentUser()
|
||||||
|
const agent = await prisma.agent.findUnique({
|
||||||
|
where: {
|
||||||
|
id: props.params.agentId,
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!agent){
|
||||||
|
notFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Page>
|
||||||
|
<PageHeader>
|
||||||
|
<PageTitle>
|
||||||
|
Edit {agent.name}
|
||||||
|
</PageTitle>
|
||||||
|
</PageHeader>
|
||||||
|
<PageContent>
|
||||||
|
<AgentForm defaultValues={agent} agentId={agent.id}/>
|
||||||
|
</PageContent>
|
||||||
|
</Page>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -1,7 +1,19 @@
|
|||||||
import {PageParams} from "@/types/next";
|
import {PageParams} from "@/types/next";
|
||||||
|
import {Page, PageContent, PageDescription, PageHeader, PageTitle} from "@/features/layout/page";
|
||||||
|
import {AgentForm} from "@/components/wrappers/Agent/AgentForm/AgentForm";
|
||||||
|
|
||||||
|
|
||||||
export default async function RoutePage(props: PageParams<{}>) {
|
export default async function RoutePage(props: PageParams<{}>) {
|
||||||
return (
|
return (
|
||||||
<div>new agent</div>
|
<Page>
|
||||||
|
<PageHeader>
|
||||||
|
<PageTitle>
|
||||||
|
Create new agent
|
||||||
|
</PageTitle>
|
||||||
|
</PageHeader>
|
||||||
|
<PageContent>
|
||||||
|
<AgentForm/>
|
||||||
|
</PageContent>
|
||||||
|
</Page>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
Generated
-8744
File diff suppressed because it is too large
Load Diff
+2
-2
@@ -56,9 +56,9 @@
|
|||||||
"next-intl": "^3.24.0",
|
"next-intl": "^3.24.0",
|
||||||
"next-safe-action": "^7.4.3",
|
"next-safe-action": "^7.4.3",
|
||||||
"next-themes": "^0.3.0",
|
"next-themes": "^0.3.0",
|
||||||
"react": "^18",
|
"react": "19.0.0-rc-66855b96-20241106",
|
||||||
"react-day-picker": "^8.10.1",
|
"react-day-picker": "^8.10.1",
|
||||||
"react-dom": "^18",
|
"react-dom": "19.0.0-rc-66855b96-20241106",
|
||||||
"react-hook-form": "^7.53.1",
|
"react-hook-form": "^7.53.1",
|
||||||
"react-resizable-panels": "^2.1.6",
|
"react-resizable-panels": "^2.1.6",
|
||||||
"react-twc": "^1.4.2",
|
"react-twc": "^1.4.2",
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
/*
|
||||||
|
Warnings:
|
||||||
|
|
||||||
|
- A unique constraint covering the columns `[slug]` on the table `Agent` will be added. If there are existing duplicate values, this will fail.
|
||||||
|
- Added the required column `slug` to the `Agent` table without a default value. This is not possible if the table is not empty.
|
||||||
|
|
||||||
|
*/
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE "Agent" ADD COLUMN "slug" TEXT NOT NULL;
|
||||||
|
|
||||||
|
-- CreateIndex
|
||||||
|
CREATE UNIQUE INDEX "Agent_slug_key" ON "Agent"("slug");
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "Database" ADD CONSTRAINT "Database_agent_id_fkey" FOREIGN KEY ("agent_id") REFERENCES "Agent"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||||
@@ -73,15 +73,19 @@ model User {
|
|||||||
|
|
||||||
model Agent {
|
model Agent {
|
||||||
id String @id @default(cuid())
|
id String @id @default(cuid())
|
||||||
|
slug String @unique
|
||||||
name String
|
name String
|
||||||
description String?
|
description String?
|
||||||
lastContact DateTime? @map("last_contact")
|
lastContact DateTime? @map("last_contact")
|
||||||
createdAt DateTime @default(now()) @map("created_at")
|
createdAt DateTime @default(now()) @map("created_at")
|
||||||
|
databases Database[]
|
||||||
}
|
}
|
||||||
|
|
||||||
model Database {
|
model Database {
|
||||||
id String @id @default(cuid())
|
id String @id @default(cuid())
|
||||||
agentId String @map("agent_id")
|
agentId String @map("agent_id")
|
||||||
|
agent Agent @relation(fields: [agentId], references: [id], onDelete: Cascade)
|
||||||
|
|
||||||
name String
|
name String
|
||||||
description String?
|
description String?
|
||||||
backupPolicy String? @map("backup_policy")
|
backupPolicy String? @map("backup_policy")
|
||||||
|
|||||||
@@ -0,0 +1,130 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import {Card, CardContent, CardHeader, CardTitle} 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";
|
||||||
|
import {AgentSchema, AgentType} from "@/components/wrappers/Agent/AgentForm/action-form.schema";
|
||||||
|
import {toast} from "sonner";
|
||||||
|
import {createAgentAction, updateAgentAction} from "@/components/wrappers/Agent/AgentForm/agent-form.action";
|
||||||
|
|
||||||
|
export type agentFormProps = {
|
||||||
|
defaultValues?: AgentType;
|
||||||
|
agentId?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const AgentForm = (props: agentFormProps) => {
|
||||||
|
|
||||||
|
const isCreate = !Boolean(props.defaultValues)
|
||||||
|
// const defaultValues = isCreate ? {slug: ""} : props.defaultValues
|
||||||
|
|
||||||
|
const form = useZodForm({
|
||||||
|
schema: AgentSchema,
|
||||||
|
defaultValues: props.defaultValues,
|
||||||
|
});
|
||||||
|
|
||||||
|
const router = useRouter();
|
||||||
|
|
||||||
|
const mutation = useMutation({
|
||||||
|
mutationFn: async (values: AgentType) => {
|
||||||
|
console.log("values", values)
|
||||||
|
|
||||||
|
const createAgent = isCreate ? await createAgentAction(values) : await updateAgentAction({
|
||||||
|
id: props.agentId ?? "-",
|
||||||
|
data: values
|
||||||
|
});
|
||||||
|
|
||||||
|
const data = createAgent?.data?.data
|
||||||
|
if (createAgent?.serverError || !data) {
|
||||||
|
console.log(createAgent?.serverError);
|
||||||
|
toast.error(createAgent?.serverError);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
toast.success(`Success`);
|
||||||
|
router.push(`/dashboard/agents/${data.id}`);
|
||||||
|
router.refresh()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return (
|
||||||
|
<TooltipProvider>
|
||||||
|
<Card>
|
||||||
|
<CardContent>
|
||||||
|
<Form form={form}
|
||||||
|
className="flex flex-col gap-4 mt-3"
|
||||||
|
onSubmit={async (values) => {
|
||||||
|
await mutation.mutateAsync(values);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="name"
|
||||||
|
defaultValue=""
|
||||||
|
render={({field}) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>Name</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Input
|
||||||
|
placeholder={"Project 1"} {...field} />
|
||||||
|
</FormControl>
|
||||||
|
<FormDescription>{"Your agent project name"}</FormDescription>
|
||||||
|
<FormMessage/>
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
defaultValue=""
|
||||||
|
|
||||||
|
name="slug"
|
||||||
|
render={({field}) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>Slug</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Input
|
||||||
|
value={field.value ?? ""}
|
||||||
|
placeholder={"agent-5-project-1"} {...field}
|
||||||
|
onChange={(e) => {
|
||||||
|
const value = e.target.value.replaceAll(" ", "-").toLowerCase()
|
||||||
|
field.onChange(value)
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</FormControl>
|
||||||
|
<FormDescription>{'The slug is used in the url of the agent'}</FormDescription>
|
||||||
|
<FormMessage/>
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
defaultValue=""
|
||||||
|
|
||||||
|
name="description"
|
||||||
|
render={({field}) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>Description</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Input
|
||||||
|
placeholder={'This agent is for the client exemple.com'} {...field}
|
||||||
|
value={field.value ?? ""}/>
|
||||||
|
</FormControl>
|
||||||
|
<FormDescription>{"Enter your project agent description"}</FormDescription>
|
||||||
|
<FormMessage/>
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
<Button>
|
||||||
|
{isCreate ? `Create agent` : `Save agent`}
|
||||||
|
</Button>
|
||||||
|
</Form>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</TooltipProvider>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
import {z} from "zod";
|
||||||
|
|
||||||
|
export const AgentSchema = z.object({
|
||||||
|
name: z.string(),
|
||||||
|
slug: z.string().regex(/^[a-zA-Z0-9_-]*$/).min(5).max(25),
|
||||||
|
description: z.string().optional().nullable(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export type AgentType = z.infer<typeof AgentSchema>;
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
"use server"
|
||||||
|
import {ActionError, userAction} from "@/safe-actions";
|
||||||
|
import {prisma} from "@/prisma";
|
||||||
|
import {AgentSchema} from "@/components/wrappers/Agent/AgentForm/action-form.schema";
|
||||||
|
import {z} from "zod";
|
||||||
|
|
||||||
|
|
||||||
|
const verifySlugUniqueness = async (slug: string, agentId?: string) => {
|
||||||
|
const slugExists = await prisma.agent.count({
|
||||||
|
where: {
|
||||||
|
slug: slug,
|
||||||
|
id: agentId ? {
|
||||||
|
not: agentId
|
||||||
|
} : undefined,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log(slugExists)
|
||||||
|
if (slugExists) {
|
||||||
|
throw new ActionError("Slug already exists");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export const createAgentAction = userAction
|
||||||
|
.schema(AgentSchema)
|
||||||
|
.action(async ({parsedInput, ctx}) => {
|
||||||
|
// Verify if slug already exist
|
||||||
|
await verifySlugUniqueness(parsedInput.slug);
|
||||||
|
const agent = await prisma.agent.create({
|
||||||
|
data: {
|
||||||
|
...parsedInput
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// await sendEmailIfUserCreatedFirstForm(ctx.user)
|
||||||
|
|
||||||
|
return {
|
||||||
|
data: agent,
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
export const updateAgentAction = userAction
|
||||||
|
.schema(
|
||||||
|
z.object({
|
||||||
|
id: z.string(),
|
||||||
|
data: AgentSchema,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.action(async ({parsedInput, ctx}) => {
|
||||||
|
await verifySlugUniqueness(parsedInput.data.slug, parsedInput.id);
|
||||||
|
|
||||||
|
console.log("parsedInput", parsedInput.data)
|
||||||
|
|
||||||
|
const updatedAgent = await prisma.agent.update({
|
||||||
|
where: {
|
||||||
|
id: parsedInput.id,
|
||||||
|
},
|
||||||
|
data: parsedInput.data,
|
||||||
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
data: updatedAgent,
|
||||||
|
|
||||||
|
}
|
||||||
|
})
|
||||||
@@ -12,7 +12,7 @@ import {useMutation} from "@tanstack/react-query";
|
|||||||
import {TooltipProvider} from "@/components/ui/tooltip";
|
import {TooltipProvider} from "@/components/ui/tooltip";
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import {PasswordInput} from "@/components/wrappers/Auth/PaswordInput/password-input";
|
import {PasswordInput} from "@/components/wrappers/Auth/PaswordInput/password-input";
|
||||||
import {LoginSchema, LoginType} from "@/components/wrappers/Auth/Login/LoginForm/login-form-schema";
|
import {LoginSchema, LoginType} from "@/components/wrappers/Auth/Login/LoginForm/login-form.schema";
|
||||||
import {signInAction} from "@/features/auth/auth.action";
|
import {signInAction} from "@/features/auth/auth.action";
|
||||||
import {SocialAuthButton} from "@/components/wrappers/Auth/Login/SocialAuth/SocialAuthButtons/SocialAuthButton";
|
import {SocialAuthButton} from "@/components/wrappers/Auth/Login/SocialAuth/SocialAuthButtons/SocialAuthButton";
|
||||||
import Image from 'next/image';
|
import Image from 'next/image';
|
||||||
|
|||||||
Reference in New Issue
Block a user