-
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.
Merge remote-tracking branch 'origin/master' into add/sync-package.js…
…on-engines
- Loading branch information
Showing
75 changed files
with
3,938 additions
and
1,174 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
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,4 @@ | ||
Significance: major | ||
Type: added | ||
|
||
Add connection components. |
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
189 changes: 189 additions & 0 deletions
189
projects/js-packages/connection/components/main/index.jsx
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,189 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import React, { useEffect, useCallback, useState } from 'react'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { Button } from '@wordpress/components'; | ||
import PropTypes from 'prop-types'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import InPlaceConnection from '../in-place-connection'; | ||
import restApi from '../../tools/jetpack-rest-api-client'; | ||
|
||
/** | ||
* The in-place connection component. | ||
* | ||
* @param {object} props -- The properties. | ||
* @param {string} props.authorizationUrl -- The authorization URL. | ||
* @param {string} props.connectLabel -- The "Connect" button label. | ||
* @param {string} props.inPlaceTitle -- The title for the In-Place Connection component. | ||
* @param {boolean} props.forceCalypsoFlow -- Whether to go straight to Calypso flow, skipping the In-Place flow. | ||
* @param {string} props.apiRoot -- API root URL, required. | ||
* @param {string} props.apiNonce -- API Nonce, required. | ||
* @param {string} props.registrationNonce -- Separate registration nonce, required. | ||
* @param {boolean} props.isRegistered -- Whether the site is registered (has blog token), required. | ||
* @param {boolean} props.isUserConnected -- Whether the current user is connected (has user token), required. | ||
* @param {boolean} props.hasConnectedOwner -- Whether the site has connection owner, required. | ||
* @param {Function} props.onRegistered -- The callback to be called upon registration success. | ||
* @param {Function} props.onUserConnected -- The callback to be called when the connection is fully established. | ||
* @param {Function} props.redirectFunc -- The redirect function (`window.location.assign()` by default). | ||
* | ||
* @returns {React.Component} The in-place connection component. | ||
*/ | ||
const Main = props => { | ||
const [ isRegistering, setIsRegistering ] = useState( false ); | ||
const [ isUserConnecting, setIsUserConnecting ] = useState( false ); | ||
|
||
const { | ||
apiRoot, | ||
apiNonce, | ||
connectLabel, | ||
authorizationUrl, | ||
forceCalypsoFlow, | ||
isRegistered, | ||
isUserConnected, | ||
onRegistered, | ||
onUserConnected, | ||
registrationNonce, | ||
redirectFunc, | ||
from, | ||
redirectUri, | ||
} = props; | ||
|
||
/** | ||
* Initialize the REST API. | ||
*/ | ||
useEffect( () => { | ||
restApi.setApiRoot( apiRoot ); | ||
restApi.setApiNonce( apiNonce ); | ||
}, [ apiRoot, apiNonce ] ); | ||
|
||
/** | ||
* Initialize the user connection process. | ||
*/ | ||
const connectUser = useCallback( | ||
url => { | ||
url = url || authorizationUrl; | ||
|
||
if ( ! url.includes( '?' ) ) { | ||
url += '?'; | ||
} | ||
|
||
if ( from ) { | ||
url += '&from=' + encodeURIComponent( from ); | ||
} | ||
|
||
if ( ! url ) { | ||
throw new Error( 'Authorization URL is required' ); | ||
} | ||
|
||
if ( forceCalypsoFlow ) { | ||
redirectFunc( url ); | ||
return; | ||
} | ||
|
||
setIsUserConnecting( true ); | ||
}, | ||
[ authorizationUrl, forceCalypsoFlow, setIsUserConnecting, redirectFunc, from ] | ||
); | ||
|
||
/** | ||
* Callback for the user connection success. | ||
*/ | ||
const onUserConnectedCallback = useCallback( () => { | ||
setIsUserConnecting( false ); | ||
|
||
if ( onUserConnected ) { | ||
onUserConnected(); | ||
} | ||
}, [ setIsUserConnecting, onUserConnected ] ); | ||
|
||
/** | ||
* Initialize the site registration process. | ||
*/ | ||
const registerSite = useCallback( | ||
e => { | ||
e && e.preventDefault(); | ||
|
||
if ( isRegistered ) { | ||
connectUser(); | ||
return; | ||
} | ||
|
||
setIsRegistering( true ); | ||
|
||
restApi | ||
.registerSite( registrationNonce, redirectUri ) | ||
.then( response => { | ||
setIsRegistering( false ); | ||
|
||
if ( onRegistered ) { | ||
onRegistered( response ); | ||
} | ||
|
||
connectUser( response.authorizeUrl ); | ||
} ) | ||
.catch( error => { | ||
throw error; | ||
} ); | ||
}, | ||
[ setIsRegistering, isRegistered, onRegistered, connectUser, registrationNonce, redirectUri ] | ||
); | ||
|
||
if ( isRegistered && isUserConnected ) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<div className="jp-connection-main"> | ||
{ ! isUserConnecting && ( | ||
<Button | ||
label={ connectLabel } | ||
onClick={ registerSite } | ||
isPrimary | ||
disabled={ isRegistering || isUserConnecting } | ||
> | ||
{ connectLabel } | ||
</Button> | ||
) } | ||
|
||
{ isUserConnecting && ( | ||
<InPlaceConnection | ||
connectUrl={ authorizationUrl } | ||
title={ props.inPlaceTitle } | ||
onComplete={ onUserConnectedCallback } | ||
displayTOS={ props.hasConnectedOwner || isRegistered } | ||
/> | ||
) } | ||
</div> | ||
); | ||
}; | ||
|
||
Main.propTypes = { | ||
authorizationUrl: PropTypes.string.isRequired, | ||
connectLabel: PropTypes.string, | ||
inPlaceTitle: PropTypes.string, | ||
forceCalypsoFlow: PropTypes.bool, | ||
apiRoot: PropTypes.string.isRequired, | ||
apiNonce: PropTypes.string.isRequired, | ||
isRegistered: PropTypes.bool.isRequired, | ||
isUserConnected: PropTypes.bool.isRequired, | ||
hasConnectedOwner: PropTypes.bool.isRequired, | ||
onRegistered: PropTypes.func, | ||
onUserConnected: PropTypes.func, | ||
registrationNonce: PropTypes.string.isRequired, | ||
redirectFunc: PropTypes.func, | ||
from: PropTypes.string, | ||
redirectUri: PropTypes.string, | ||
}; | ||
|
||
Main.defaultProps = { | ||
inPlaceTitle: __( 'Connect your WordPress.com account', 'jetpack' ), | ||
forceCalypsoFlow: false, | ||
connectLabel: __( 'Connect', 'jetpack' ), | ||
redirectFunc: url => window.location.assign( url ), | ||
}; | ||
|
||
export default Main; |
75 changes: 75 additions & 0 deletions
75
projects/js-packages/connection/components/main/test/component.jsx
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,75 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import React from 'react'; | ||
import { expect } from 'chai'; | ||
import { shallow } from 'enzyme'; | ||
import ShallowRenderer from 'react-test-renderer/shallow'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import Main from '../index'; | ||
|
||
describe( 'Main', () => { | ||
const testProps = { | ||
apiNonce: 'test', | ||
apiRoot: 'https://example.org/wp-json/', | ||
authorizationUrl: 'https://jetpack.wordpress.com/jetpack.authorize/1/?response_type=code', | ||
hasConnectedOwner: false, | ||
isRegistered: false, | ||
isUserConnected: false, | ||
registrationNonce: 'test2', | ||
}; | ||
|
||
describe( 'Render the Main component', () => { | ||
const renderer = new ShallowRenderer(); | ||
renderer.render( <Main { ...testProps } /> ); | ||
|
||
const wrapper = shallow( renderer.getRenderOutput() ); | ||
|
||
it( 'component exists', () => { | ||
expect( wrapper.find( 'Main' ) ).to.exist; | ||
} ); | ||
|
||
const button = wrapper.find( 'ForwardRef(Button)' ); | ||
|
||
it( 'renders the register button', () => { | ||
expect( button.text() ).to.be.equal( 'Connect' ); | ||
} ); | ||
} ); | ||
|
||
describe( 'Render the user connection - iframe', () => { | ||
const renderer = new ShallowRenderer(); | ||
renderer.render( <Main { ...testProps } isRegistered={ true } /> ); | ||
|
||
shallow( renderer.getRenderOutput() ).find( 'ForwardRef(Button)' ).simulate( 'click' ); | ||
const wrapper = shallow( renderer.getRenderOutput() ); | ||
|
||
it( 'renders the InPlaceConnection', () => { | ||
expect( wrapper.find( 'InPlaceConnection' ).props().connectUrl ).to.be.equal( | ||
testProps.authorizationUrl | ||
); | ||
} ); | ||
} ); | ||
|
||
describe( 'Render the user connection - calypso', () => { | ||
let redirectUrl = null; | ||
const redirectFunc = url => ( redirectUrl = url ); | ||
|
||
const renderer = new ShallowRenderer(); | ||
renderer.render( | ||
<Main | ||
{ ...testProps } | ||
isRegistered={ true } | ||
forceCalypsoFlow={ true } | ||
redirectFunc={ redirectFunc } // eslint-disable-line react/jsx-no-bind | ||
/> | ||
); | ||
shallow( renderer.getRenderOutput() ).find( 'ForwardRef(Button)' ).simulate( 'click' ); | ||
|
||
it( 'the redirect happened', () => { | ||
expect( redirectUrl ).to.be.equal( testProps.authorizationUrl ); | ||
} ); | ||
} ); | ||
} ); |
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
Oops, something went wrong.