11"use client" ;
2- import { ConfirmationDialog } from "@/components/ui/ConfirmationDialog" ;
32import { Button } from "@/components/ui/button" ;
43import {
54 Form ,
@@ -14,16 +13,13 @@ import {
1413import { ImageUpload } from "@/components/ui/image-upload" ;
1514import { Input } from "@/components/ui/input" ;
1615import { RadioGroup , RadioGroupItemButton } from "@/components/ui/radio-group" ;
17- import { useDashboardRouter } from "@/lib/DashboardRouter" ;
1816import { zodResolver } from "@hookform/resolvers/zod" ;
1917import { Loader2 } from "lucide-react" ;
2018import Link from "next/link" ;
21- import { useState } from "react" ;
2219import { useForm } from "react-hook-form" ;
2320import { toast } from "sonner" ;
24- import invariant from "tiny-invariant" ;
2521import { z } from "zod" ;
26- import { useCreateEcosystem } from "../../hooks/use- create-ecosystem" ;
22+ import { createEcosystem } from "../../actions/ create-ecosystem" ;
2723
2824const formSchema = z . object ( {
2925 name : z
@@ -40,41 +36,42 @@ const formSchema = z.object({
4036 permission : z . union ( [ z . literal ( "PARTNER_WHITELIST" ) , z . literal ( "ANYONE" ) ] ) ,
4137} ) ;
4238
43- export function CreateEcosystemForm ( props : {
44- ecosystemLayoutPath : string ;
45- } ) {
46- // When set, the confirmation modal is open the this contains the form data to be submitted
47- const [ formDataToBeConfirmed , setFormDataToBeConfirmed ] = useState <
48- z . infer < typeof formSchema > | undefined
49- > ( ) ;
50-
51- const router = useDashboardRouter ( ) ;
39+ export function CreateEcosystemForm ( props : { teamSlug : string } ) {
5240 const form = useForm < z . infer < typeof formSchema > > ( {
5341 resolver : zodResolver ( formSchema ) ,
5442 defaultValues : {
5543 permission : "PARTNER_WHITELIST" ,
5644 } ,
5745 } ) ;
5846
59- const { mutateAsync : createEcosystem , isPending } = useCreateEcosystem ( {
60- onError : ( error ) => {
61- const message =
62- error instanceof Error ? error . message : "Failed to create ecosystem" ;
63- toast . error ( message ) ;
64- } ,
65- onSuccess : ( slug : string ) => {
66- form . reset ( ) ;
67- router . push ( `${ props . ecosystemLayoutPath } /${ slug } ` ) ;
68- } ,
69- } ) ;
70-
7147 return (
7248 < >
7349 < Form { ...form } >
7450 < form
75- onSubmit = { form . handleSubmit ( ( values ) =>
76- setFormDataToBeConfirmed ( values ) ,
77- ) }
51+ onSubmit = { form . handleSubmit ( async ( values ) => {
52+ const res = await createEcosystem ( {
53+ teamSlug : props . teamSlug ,
54+ ...values ,
55+ } ) ;
56+ switch ( res . status ) {
57+ case 401 :
58+ toast . error ( "Please login to create an ecosystem" ) ;
59+ break ;
60+ case 403 :
61+ toast . error (
62+ "You are not authorized to create an ecosystem, please ask your team owner to create it." ,
63+ ) ;
64+ break ;
65+ case 409 :
66+ toast . error ( "An ecosystem with that name already exists." ) ;
67+ break ;
68+ case 500 :
69+ toast . error (
70+ "Failed to create ecosystem, please try again later." ,
71+ ) ;
72+ break ;
73+ }
74+ } ) }
7875 className = "flex flex-col items-stretch gap-8"
7976 >
8077 < div className = "grid gap-6" >
@@ -166,29 +163,15 @@ export function CreateEcosystemForm(props: {
166163 type = "submit"
167164 variant = "primary"
168165 className = "w-full"
169- disabled = { isPending }
166+ disabled = { form . formState . isSubmitting }
170167 >
171- { isPending && < Loader2 className = "mr-1 h-4 w-4 animate-spin" /> }
168+ { form . formState . isSubmitting && (
169+ < Loader2 className = "mr-1 h-4 w-4 animate-spin" />
170+ ) }
172171 Create
173172 </ Button >
174173 </ form >
175174 </ Form >
176- < ConfirmationDialog
177- open = { formDataToBeConfirmed !== undefined }
178- onOpenChange = { ( open ) =>
179- ! open ? setFormDataToBeConfirmed ( undefined ) : null
180- }
181- title = { `Are you sure you want to create ecosystem ${ form . getValues ( ) . name } ?` }
182- description = "Your account will be charged $250 per month."
183- onSubmit = { ( ) => {
184- invariant ( formDataToBeConfirmed , "Form data not found" ) ;
185- createEcosystem ( {
186- name : formDataToBeConfirmed . name ,
187- logo : formDataToBeConfirmed . logo ,
188- permission : formDataToBeConfirmed . permission ,
189- } ) ;
190- } }
191- />
192175 </ >
193176 ) ;
194177}
0 commit comments