-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add migration cancellation error (#96293)
* Add notice on error * Extract hook with cancellation logic * Fix tracking to pass string instead of nested object Nested objects are not supported. * Fix typo * Extract useCancellation to another file * Rename hook for more clarity * Add test to validate the notice * Test dismiss notice
- Loading branch information
Showing
3 changed files
with
146 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...erview/components/migration-overview/components/migration-pending/use-cancel-migration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { recordTracksEvent } from '@automattic/calypso-analytics'; | ||
import { useCallback, useEffect, useState } from 'react'; | ||
import { useMigrationCancellation } from 'calypso/data/site-migration/landing/use-migration-cancellation'; | ||
import { useSiteExcerptsQueryInvalidator } from 'calypso/data/sites/use-site-excerpts-query'; | ||
import { useDispatch } from 'calypso/state'; | ||
import { requestSite } from 'calypso/state/sites/actions'; | ||
import type { SiteDetails } from '@automattic/data-stores'; | ||
|
||
const useCancelMigration = ( site: SiteDetails ) => { | ||
const dispatch = useDispatch(); | ||
const invalidateSiteExcerptsQuery = useSiteExcerptsQueryInvalidator(); | ||
|
||
const { | ||
mutate: mutateCancelMigration, | ||
isSuccess: isCancellationSuccess, | ||
isPending: isCancelling, | ||
error: cancellationError, | ||
} = useMigrationCancellation( site.ID ); | ||
|
||
const [ isModalVisible, setIsModalVisible ] = useState( false ); | ||
const [ errorNoticeDismissed, setErrorNoticeDismissed ] = useState( false ); | ||
|
||
const reloadSite = useCallback( () => { | ||
dispatch( requestSite( site.ID ) ); | ||
// invalidate the site excerpts query to refresh the /sites sidebar | ||
invalidateSiteExcerptsQuery(); | ||
}, [ dispatch, invalidateSiteExcerptsQuery, site.ID ] ); | ||
|
||
useEffect( () => { | ||
if ( isCancellationSuccess ) { | ||
recordTracksEvent( 'calypso_pending_migration_canceled' ); | ||
reloadSite(); | ||
} | ||
}, [ isCancellationSuccess, reloadSite ] ); | ||
|
||
useEffect( () => { | ||
if ( cancellationError ) { | ||
recordTracksEvent( 'calypso_pending_migration_cancel_error', { | ||
error: cancellationError.message, | ||
} ); | ||
} | ||
}, [ cancellationError ] ); | ||
|
||
const openModal = useCallback( () => { | ||
setIsModalVisible( true ); | ||
}, [] ); | ||
|
||
const closeModal = useCallback( () => { | ||
setIsModalVisible( false ); | ||
}, [] ); | ||
|
||
const dismissErrorNotice = useCallback( () => { | ||
setErrorNoticeDismissed( true ); | ||
}, [] ); | ||
|
||
const cancelMigration = useCallback( () => { | ||
setErrorNoticeDismissed( false ); | ||
mutateCancelMigration(); | ||
setIsModalVisible( false ); | ||
}, [ mutateCancelMigration ] ); | ||
|
||
return { | ||
isModalVisible, | ||
isLoading: isCancelling || isCancellationSuccess, | ||
showErrorNotice: cancellationError && ! errorNoticeDismissed, | ||
cancelMigration, | ||
openModal, | ||
closeModal, | ||
dismissErrorNotice, | ||
}; | ||
}; | ||
|
||
export default useCancelMigration; |