-
Notifications
You must be signed in to change notification settings - Fork 800
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
unify the way we connect sites on My Jetpack
- Loading branch information
1 parent
d5a79b1
commit e82cdcd
Showing
7 changed files
with
122 additions
and
136 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
63 changes: 9 additions & 54 deletions
63
projects/packages/my-jetpack/_inc/components/welcome-flow/ConnectionStep.tsx
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
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
83 changes: 83 additions & 0 deletions
83
projects/packages/my-jetpack/_inc/hooks/use-connect-site/index.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,83 @@ | ||
import { getRedirectUrl } from '@automattic/jetpack-components'; | ||
import { useCallback, useContext } from 'react'; | ||
import { NoticeContext } from '../../context/notices/noticeContext'; | ||
import { NOTICE_SITE_CONNECTION_ERROR } from '../../context/notices/noticeTemplates'; | ||
import useProductsByOwnership from '../../data/products/use-products-by-ownership'; | ||
import { getMyJetpackWindowInitialState } from '../../data/utils/get-my-jetpack-window-state'; | ||
import useMyJetpackConnection from '../../hooks/use-my-jetpack-connection'; | ||
import useAnalytics from '../use-analytics'; | ||
import type { TracksEvent, TracksProperties } from '../use-analytics'; | ||
import type { MouseEvent } from 'react'; | ||
|
||
interface useConnectSiteProps { | ||
tracksInfo: { | ||
event: TracksEvent; | ||
properties: TracksProperties; | ||
}; | ||
shouldScrollToTop?: boolean; | ||
} | ||
|
||
const useConnectSite = ( { tracksInfo, shouldScrollToTop = false }: useConnectSiteProps ) => { | ||
const { event, properties: tracksEventData } = tracksInfo; | ||
|
||
const { setNotice, resetNotice } = useContext( NoticeContext ); | ||
|
||
const { recordEvent } = useAnalytics(); | ||
const { refetch: refetchOwnershipData } = useProductsByOwnership(); | ||
|
||
const { siteSuffix, adminUrl, myJetpackCheckoutUri } = getMyJetpackWindowInitialState(); | ||
const redirectUri = `&redirect_to=${ myJetpackCheckoutUri }`; | ||
|
||
const connectAfterCheckoutUrl = `?connect_after_checkout=true&admin_url=${ encodeURIComponent( | ||
adminUrl | ||
) }&from_site_slug=${ siteSuffix }&source=my-jetpack`; | ||
const query = `${ connectAfterCheckoutUrl }${ redirectUri }&unlinked=1`; | ||
const jetpackPlansPath = getRedirectUrl( 'jetpack-my-jetpack-site-only-plans', { query } ); | ||
const { handleRegisterSite } = useMyJetpackConnection( { | ||
skipUserConnection: true, | ||
redirectUri, | ||
} ); | ||
|
||
const connectSite = useCallback( | ||
async ( e: MouseEvent< HTMLButtonElement > ) => { | ||
e && e.preventDefault(); | ||
|
||
if ( shouldScrollToTop ) { | ||
window.scrollTo( { | ||
top: 0, | ||
left: 0, | ||
behavior: 'smooth', | ||
} ); | ||
} | ||
|
||
recordEvent( `${ event }_click`, tracksEventData ); | ||
|
||
try { | ||
await handleRegisterSite(); | ||
|
||
recordEvent( `${ event }_success`, tracksEventData ); | ||
|
||
window.location.href = jetpackPlansPath; | ||
} catch { | ||
setNotice( NOTICE_SITE_CONNECTION_ERROR, resetNotice ); | ||
} finally { | ||
refetchOwnershipData(); | ||
} | ||
}, | ||
[ | ||
handleRegisterSite, | ||
jetpackPlansPath, | ||
recordEvent, | ||
refetchOwnershipData, | ||
resetNotice, | ||
setNotice, | ||
tracksEventData, | ||
event, | ||
shouldScrollToTop, | ||
] | ||
); | ||
|
||
return { connectSite }; | ||
}; | ||
|
||
export default useConnectSite; |
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