11'use client'
22
3- import { useAuth } from '@/hooks'
43import * as Sentry from '@sentry/nextjs'
5- import { useCreateOrganization } from '@/hooks/queries'
4+ import { useUpdateOrganization } from '@/hooks/queries'
65import { schemas } from '@polar-sh/client'
76import { Box } from '@polar-sh/orbit/Box'
87import Button from '@polar-sh/ui/components/atoms/Button'
@@ -66,11 +65,10 @@ interface FormSchema {
6665
6766export function ProductDetailsStep ( ) {
6867 const router = useRouter ( )
69- const { setUserOrganizations } = useAuth ( )
7068 const { data, updateData, setApiLoading, showApiResponse } =
7169 useOnboardingData ( )
7270 const { trackStepViewed, trackStepCompleted } = useOnboardingV2Tracking ( )
73- const createOrganization = useCreateOrganization ( )
71+ const updateOrganization = useUpdateOrganization ( )
7472 const [ loading , setLoading ] = useState <
7573 'validating' | 'submitting' | 'submitting-anyway' | null
7674 > ( null )
@@ -132,36 +130,32 @@ export function ProductDetailsStep() {
132130 const submitOrg = async ( formData : FormSchema ) => {
133131 setApiLoading ( true )
134132
133+ if ( ! data . organizationId ) {
134+ form . setError ( 'root' , {
135+ message : 'Organization setup is incomplete. Please start again.' ,
136+ } )
137+ await showApiResponse ( 400 , 'Failed to update organization' )
138+ return false
139+ }
140+
135141 const switching = formData . currentlySellingOn . length > 0
136142 const switchingFrom = (
137143 switching ? formData . currentlySellingOn [ 0 ] : null
138144 ) as schemas [ 'OrganizationDetails' ] [ 'switching_from' ]
139145
140- const { data : org , error } = await createOrganization . mutateAsync ( {
141- name : data . orgName ! ,
142- slug : data . orgSlug ! ,
143- default_presentment_currency :
144- ( data . defaultCurrency as schemas [ 'PresentmentCurrency' ] ) || 'usd' ,
145- country : ( data . businessCountry || data . country || undefined ) as
146- | schemas [ 'OrganizationCreate' ] [ 'country' ]
147- | undefined ,
148- default_tax_behavior : 'location' ,
149- legal_entity :
150- data . organizationType === 'company'
151- ? {
152- type : 'company' as const ,
153- registered_name : data . registeredBusinessName ! ,
154- }
155- : { type : 'individual' as const } ,
156- ...( formData . supportEmail && { email : formData . supportEmail } ) ,
157- ...( formData . productUrl && { website : formData . productUrl } ) ,
158- details : {
159- product_description : formData . productDescription ,
160- selling_categories : formData . sellingCategories ,
161- pricing_models : formData . pricingModel ,
162- switching,
163- switching_from : switchingFrom ,
164- } satisfies schemas [ 'OrganizationDetails' ] ,
146+ const { error } = await updateOrganization . mutateAsync ( {
147+ id : data . organizationId ,
148+ body : {
149+ ...( formData . supportEmail && { email : formData . supportEmail } ) ,
150+ ...( formData . productUrl && { website : formData . productUrl } ) ,
151+ details : {
152+ product_description : formData . productDescription ,
153+ selling_categories : formData . sellingCategories ,
154+ pricing_models : formData . pricingModel ,
155+ switching,
156+ switching_from : switchingFrom ,
157+ } satisfies schemas [ 'OrganizationDetails' ] ,
158+ } ,
165159 } )
166160
167161 if ( error ) {
@@ -173,14 +167,11 @@ export function ProductDetailsStep() {
173167 ? ( error . detail [ 0 ] ?. msg ?? 'Validation failed' )
174168 : 'Something went wrong, please try again.' ,
175169 } )
176- showApiResponse ( 400 , 'Failed to create organization' )
170+ await showApiResponse ( 400 , 'Failed to update organization' )
177171 return false
178172 }
179173
180- setUserOrganizations ( ( prev ) => [ ...prev , org ] )
181- updateData ( { organizationId : org . id , orgSlug : org . slug } )
182-
183- trackStepCompleted ( 'product' , { organization_id : org . id } )
174+ trackStepCompleted ( 'product' , { organization_id : data . organizationId } )
184175 await showApiResponse ( 200 , 'OK' )
185176 router . push ( '/onboarding/complete' )
186177 return true
0 commit comments