From 211d69c84601d60e78998626f82e8530e8c8321c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20LAGACHE?= Date: Thu, 18 Jun 2026 12:05:42 +0200 Subject: [PATCH] feat(onboarding): step-project-create uses real action + real DBs from flowData --- .../onboarding/steps/step-project-create.tsx | 150 +++++++++++++----- 1 file changed, 107 insertions(+), 43 deletions(-) diff --git a/src/features/onboarding/steps/step-project-create.tsx b/src/features/onboarding/steps/step-project-create.tsx index 134f174b..9c87b1e7 100644 --- a/src/features/onboarding/steps/step-project-create.tsx +++ b/src/features/onboarding/steps/step-project-create.tsx @@ -2,77 +2,141 @@ import { useState } from "react"; import { useOnboarding } from "@onboardjs/react"; +import { useMutation } from "@tanstack/react-query"; +import { toast } from "sonner"; import { Check, Database } from "lucide-react"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Textarea } from "@/components/ui/textarea"; import { Button } from "@/components/ui/button"; -import { mockDatabases } from "@/features/onboarding/onboarding.mock"; +import { createProjectAction, updateProjectAction } from "@/features/projects/projects.action"; +import type { OnboardingDatabase, OnboardingProjectData } from "@/features/onboarding/onboarding.types"; export const StepProjectCreate = () => { const { next, updateContext, state } = useOnboarding(); - const [name, setName] = useState(""); - const [description, setDescription] = useState(""); - const [databaseIds, setDatabaseIds] = useState([]); + const existingProject = state?.context.flowData.project as OnboardingProjectData | undefined; + const databases = (state?.context.flowData.databases ?? []) as OnboardingDatabase[]; + const orgId = (state?.context.flowData.org as any)?.id as string | undefined; + const isUpdateMode = !!existingProject; + + const [name, setName] = useState(existingProject?.name ?? ""); + const [description, setDescription] = useState(existingProject?.description ?? ""); + const [databaseIds, setDatabaseIds] = useState(existingProject?.databaseIds ?? []); const toggleDb = (id: string) => { setDatabaseIds((prev) => (prev.includes(id) ? prev.filter((v) => v !== id) : [...prev, id])); }; - const onContinue = async () => { - await updateContext({ flowData: { ...state?.context.flowData, project: { name, description, databaseIds } } }); - await next(); - }; + const mutation = useMutation({ + mutationFn: async () => { + if (!orgId) throw new Error("No organisation ID found"); + if (isUpdateMode && existingProject?.id) { + const result = await updateProjectAction({ + data: { name: name.trim(), databases: databaseIds }, + organizationId: orgId, + projectId: existingProject.id, + }); + const updateData = result?.data; + if (!updateData?.success) { + throw new Error(updateData?.actionError?.message ?? "Failed to update project"); + } + await updateContext({ + flowData: { + ...state?.context.flowData, + project: { id: existingProject.id, name: name.trim(), description, databaseIds }, + }, + }); + } else { + const result = await createProjectAction({ + data: { name: name.trim(), databases: databaseIds }, + organizationId: orgId, + }); + const createData = result?.data; + if (!createData?.success) { + throw new Error(createData?.actionError?.message ?? "Failed to create project"); + } + const project = createData.value; + if (!project) throw new Error("Failed to create project"); + await updateContext({ + flowData: { + ...state?.context.flowData, + project: { id: project.id, name: project.name, description, databaseIds }, + }, + }); + } + await next(); + }, + onError: (err: Error) => { + toast.error(err.message); + }, + }); return (
-

Create a project

+

+ {isUpdateMode ? "Update project" : "Create a project"} +

Optional — group databases under a project.

- setName(e.target.value)} placeholder="My project" /> + setName(e.target.value)} + placeholder="My project" + />
-