Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add error handling and remove temporary state when error occurs
  • Loading branch information
Kateryna Kodonenko
Kateryna Kodonenko committed Jul 16, 2024
commit bc793c46f6ddb0fb6aca86c8ffe6a5fdf39efa45
10 changes: 1 addition & 9 deletions src/hooks/use-add-site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,8 @@ export function useAddSite() {
await createSite( path, siteName ?? '' );
} catch ( e ) {
Sentry.captureException( e );
getIpcApi().showMessageBox( {
type: 'error',
message: __( 'Failed to create site' ),
detail: __(
'An error occurred while creating the site. Verify your selected local path is an empty directory or an existing WordPress folder and try again. If this problem persists, please contact support.'
),
buttons: [ __( 'OK' ) ],
} );
}
}, [ createSite, proposedSitePath, siteName, sitePath, __ ] );
}, [ createSite, proposedSitePath, siteName, sitePath ] );

const handleSiteNameChange = useCallback(
async ( name: string ) => {
Expand Down
34 changes: 29 additions & 5 deletions src/hooks/use-site-details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,35 @@ export function SiteDetailsProvider( { children }: SiteDetailsProviderProps ) {
] )
);
setSelectedSiteId( tempSiteId ); // Set the temporary ID as the selected site
const data = await getIpcApi().createSite( path, siteName );
setData( data );
const newSite = data.find( ( site ) => site.path === path );
if ( newSite?.id ) {
setSelectedSiteId( newSite.id ); // Update the selected site to the new site's ID

try {
const data = await getIpcApi().createSite( path, siteName );
const newSite = data.find( ( site ) => site.path === path );
if ( newSite?.id ) {
setSelectedSiteId( newSite.id ); // Update the selected site to the new site's ID
}
setData( ( prevData ) =>
sortSites(
prevData.map( ( site ) =>
site.id === tempSiteId ? { ...site, ...newSite, isAddingSite: false } : site
)
)
);
Comment thread
katinthehatsite marked this conversation as resolved.
} catch ( error ) {
console.error( 'Failed to create site:', error );
getIpcApi().showMessageBox( {
type: 'error',
message: __( 'Failed to create site' ),
detail: __(
'An error occurred while creating the site. Verify your selected local path is an empty directory or an existing WordPress folder and try again. If this problem persists, please contact support.'
),
buttons: [ __( 'OK' ) ],
} );
setTimeout( () => {
setData( ( prevData ) =>
sortSites( prevData.filter( ( site ) => site.id !== tempSiteId ) )
);
}, 3000 ); // Delay before removing the temporary site; otherwise, it gets a weird glitch because removed too quickly
}
},
[ setSelectedSiteId ]
Expand Down