@@ -47,20 +47,45 @@ export const OrganizationDomainsStep = (): JSX.Element => {
4747 const { goPrev, goNext, isFirstStep, isLastStep } = useWizard ( ) ;
4848 const card = useCardState ( ) ;
4949 const [ domainToRemove , setDomainToRemove ] = useState < OrganizationDomainResource | null > ( null ) ;
50+ const [ isContinuing , setIsContinuing ] = useState ( false ) ;
5051
5152 const handleCreateDomain = async ( domain : string ) => {
5253 card . setError ( undefined ) ;
5354
5455 try {
55- const createdDomain = await createDomain ( domain ) ;
56+ await createDomain ( domain ) ;
57+ } catch ( err : any ) {
58+ const apiError = getFieldError ( err ) ?? getGlobalError ( err ) ;
59+ card . setError ( apiError ) ;
60+ }
61+ } ;
5662
57- if ( createdDomain && enterpriseConnection ) {
58- const domains = Array . from ( new Set ( [ ...enterpriseConnection . domains , createdDomain . name ] ) ) ;
59- await updateConnection ( enterpriseConnection . id , { domains } ) ;
60- }
63+ const handleContinue = async ( ) => {
64+ card . setError ( undefined ) ;
65+ setIsContinuing ( true ) ;
66+
67+ const verifiedDomainNames =
68+ organizationDomains
69+ ?. filter ( domain => domain . ownershipVerification ?. status === 'verified' )
70+ . map ( domain => domain . name ) ?? [ ] ;
71+ const domains = Array . from ( new Set ( [ ...( enterpriseConnection ?. domains ?? [ ] ) , ...verifiedDomainNames ] ) ) ;
72+ const hasNewDomains = domains . length !== ( enterpriseConnection ?. domains ?. length ?? 0 ) ;
73+
74+ if ( ! enterpriseConnection || ! hasNewDomains ) {
75+ setIsContinuing ( false ) ;
76+ void goNext ( ) ;
77+ return ;
78+ }
79+
80+ // New verified domains have been added, so we need to update the connection
81+ try {
82+ await updateConnection ( enterpriseConnection . id , { domains } ) ;
83+ void goNext ( ) ;
6184 } catch ( err : any ) {
6285 const apiError = getFieldError ( err ) ?? getGlobalError ( err ) ;
6386 card . setError ( apiError ) ;
87+ } finally {
88+ setIsContinuing ( false ) ;
6489 }
6590 } ;
6691
@@ -157,8 +182,9 @@ export const OrganizationDomainsStep = (): JSX.Element => {
157182 isDisabled = { isFirstStep }
158183 />
159184 < Step . Footer . Continue
160- onClick = { ( ) => goNext ( ) }
161- isDisabled = { isLastStep || ! hasAllDomainsVerified }
185+ onClick = { handleContinue }
186+ isDisabled = { isLastStep || ! hasAllDomainsVerified || isContinuing }
187+ isLoading = { isContinuing }
162188 />
163189 </ Step . Footer >
164190 </ Step >
0 commit comments