Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My Jetpack: handle redirect when no connection #22549

Merged
merged 17 commits into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
9071f14
[not verified] my-jetpack: add useMyJetpackConnection() simple hook
retrofox Jan 27, 2022
5eed61b
[not verified] my-jetpack: use custom hook to deal with connection
retrofox Jan 27, 2022
a6bdc36
[not verified] changelog
retrofox Jan 27, 2022
9a05bd1
[not verified] my-jetpack: when the site is not connected, redirect t…
retrofox Jan 27, 2022
b250c33
[not verified] my-jetpack: do not render app when no site connected
retrofox Jan 27, 2022
7c9015f
[not verified] my-jetpack: fix rebasing issues
retrofox Jan 28, 2022
9916961
[not verified] my-jetpack: add options to useMyJetpackConnection() hook
retrofox Jan 28, 2022
98b44ac
[not verified] my-jetpack: redirect to Jetpack dashboard when no conn…
retrofox Jan 28, 2022
9becc53
[not verified] my-jetpack: expose redirectUrl from useMyJetpackConnec…
retrofox Jan 28, 2022
a44a7f0
[not verified] my-jetpack: set url in no conenction global error
retrofox Jan 28, 2022
15f8915
[not verified] my-jetpack: check whether user is conected before to r…
retrofox Jan 28, 2022
472cca7
[not verified] my-jetpack: check user when no render because lack of …
retrofox Jan 28, 2022
1e26242
[not verified] my-jetpack: simplify connection status cmp
retrofox Jan 28, 2022
7cb6d90
[not verified] my-jetpack: check only site when redirect because of c…
retrofox Jan 28, 2022
97c5799
[not verified] my-jetpack: restore redirectUri prop to connection com…
retrofox Jan 28, 2022
1159862
Merge branch 'master' into update/my-jetpack-remove-connections-section
retrofox Jan 28, 2022
c9a9133
changelog
retrofox Jan 28, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
/* global myJetpackRest */
/* global myJetpackInitialState */
/**
* External dependencies
*/
import React, { useCallback } from 'react';
import React from 'react';
import { ConnectionStatusCard } from '@automattic/jetpack-connection';
import useMyJetpackConnection from '../../hooks/use-my-jetpack-connection';

/**
* Plan section component.
*
* @returns {object} ConnectionsSection React component.
*/
export default function ConnectionsSection() {
const { apiRoot, apiNonce } = myJetpackRest;
const redirectAfterDisconnect = useCallback( () => {
window.location = myJetpackInitialState.topJetpackMenuItemUrl;
}, [] );
const { apiRoot, apiNonce, redirectUrl } = useMyJetpackConnection();
return (
<ConnectionStatusCard
apiRoot={ apiRoot }
apiNonce={ apiNonce }
redirectUri={ myJetpackInitialState.redirectUri }
onDisconnected={ redirectAfterDisconnect }
/>
<ConnectionStatusCard apiRoot={ apiRoot } apiNonce={ apiNonce } redirectUri={ redirectUrl } />
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import PlansSection from '../plans-section';
import ProductCardsSection from '../product-cards-section';
import useAnalytics from '../../hooks/use-analytics';
import useNoticeWatcher, { useGlobalNotice } from '../../hooks/use-notice';
import useMyJetpackConnection from '../../hooks/use-my-jetpack-connection';
import './style.scss';

/**
Expand Down Expand Up @@ -67,6 +68,12 @@ export default function MyJetpackScreen() {
recordEvent( 'jetpack_myjetpack_page_view' );
}, [ recordEvent ] );

// No render when site is not connected.
const { isSiteConnected } = useMyJetpackConnection( { redirect: true } );
if ( ! isSiteConnected ) {
return null;
}

return (
<div className="jp-my-jetpack-screen">
<AdminPage>
Expand Down
12 changes: 3 additions & 9 deletions projects/packages/my-jetpack/_inc/hooks/use-analytics/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
/* global myJetpackRest */
/**
* External dependencies
*/
import { useEffect } from 'react';
import jetpackAnalytics from '@automattic/jetpack-analytics';
import { useConnection } from '@automattic/jetpack-connection';
import useMyJetpackConnection from '../use-my-jetpack-connection';

const useAnalytics = () => {
const { apiRoot, apiNonce } = myJetpackRest;

const { isUserConnected, userConnectionData } = useConnection( {
apiRoot,
apiNonce,
} );

const { isUserConnected, userConnectionData } = useMyJetpackConnection();
const { login, ID } = userConnectionData.currentUser.wpcomUser;

/**
* Initialize tracks with user data.
* Should run when we have a connected user.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* global myJetpackInitialState */

/* global myJetpackRest */
/**
* WordPress dependencies
*/
import { useEffect } from 'react';
import { useConnection } from '@automattic/jetpack-connection';

/**
* React custom hook to get the site purchases data.
*
* @param {object} options - Options to pass to the hook.
* @param {boolean} options.reditect - Perform a redirect when no connection is found.
* @returns {object} site purchases data
*/
export default function useMyJetpackConnection( options = { redirect: false } ) {
const { apiRoot, apiNonce } = myJetpackRest;
const { topJetpackMenuItemUrl } = myJetpackInitialState;
const { redirect } = options;
const connectionData = useConnection( { apiRoot, apiNonce } );

// Alias: https://github.com/Automattic/jetpack/blob/master/projects/packages/connection/src/class-rest-connector.php/#L315
const isSiteConnected = connectionData.isRegistered;

/*
* When the site is not connect,
* and the `redirect` option is set to `true`,
* redirect to the Jetpack dashboard.
*/
useEffect( () => {
// Bail early when topJetpackMenuItemUrl is not defined.
if ( ! topJetpackMenuItemUrl ) {
return;
}

// Bail early when redirect mode is disabled.
if ( ! redirect ) {
return;
}

// When site is connected, bail early.
if ( isSiteConnected ) {
return;
}

window.location = topJetpackMenuItemUrl;
}, [ isSiteConnected, redirect, topJetpackMenuItemUrl ] );

return {
apiNonce,
apiRoot,
...connectionData,
isSiteConnected,
redirectUrl: topJetpackMenuItemUrl,
};
}
13 changes: 4 additions & 9 deletions projects/packages/my-jetpack/_inc/hooks/use-notice/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
/* global myJetpackRest */
/**
* WordPress dependencies
*/
import { useEffect } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { useConnection } from '@automattic/jetpack-connection';
import { useSelect, useDispatch } from '@wordpress/data';

/**
* Internal dependencies
*/
import { STORE_ID } from '../../state/store';
import useMyJetpackConnection from '../use-my-jetpack-connection';

/**
* React custom hook to get global notices.
Expand All @@ -34,13 +33,9 @@ export function useGlobalNotice() {
* the hook dispatches an action to populate the global notice.
*/
export default function useNoticeWatcher() {
const { apiRoot, apiNonce } = myJetpackRest;
const dispatch = useDispatch();

const { isUserConnected } = useConnection( {
apiRoot,
apiNonce,
} );
const { isUserConnected, redirectUrl } = useMyJetpackConnection();

useEffect( () => {
if ( ! isUserConnected ) {
Expand All @@ -54,11 +49,11 @@ export default function useNoticeWatcher() {
actions: [
{
label: __( 'Connect Jetpack now.', 'jetpack-my-jetpack' ),
url: '#',
url: redirectUrl,
},
],
}
);
}
}, [ isUserConnected, dispatch ] );
}, [ isUserConnected, dispatch, redirectUrl ] );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: added

Handle when site is not connected