| | import { Button } from '@automattic/components'; |
| | import requestExternalAccess from '@automattic/request-external-access'; |
| | import { localize } from 'i18n-calypso'; |
| | import { find, last, some } from 'lodash'; |
| | import PropTypes from 'prop-types'; |
| | import { Component, Fragment } from 'react'; |
| | import { connect } from 'react-redux'; |
| | import QueryKeyringServices from 'calypso/components/data/query-keyring-services'; |
| | import { |
| | deleteStoredKeyringConnection, |
| | requestKeyringConnections, |
| | } from 'calypso/state/sharing/keyring/actions'; |
| | import { |
| | getKeyringConnectionsByName, |
| | isKeyringConnectionsFetching, |
| | } from 'calypso/state/sharing/keyring/selectors'; |
| | import { getKeyringServiceByName } from 'calypso/state/sharing/services/selectors'; |
| |
|
| | const noop = () => {}; |
| |
|
| | class KeyringConnectButton extends Component { |
| | static propTypes = { |
| | service: PropTypes.oneOfType( [ |
| | PropTypes.shape( { |
| | ID: PropTypes.string.isRequired, |
| | connect_URL: PropTypes.string.isRequired, |
| | } ), |
| | PropTypes.bool, |
| | ] ), |
| | isFetching: PropTypes.bool, |
| | keyringConnections: PropTypes.array, |
| | onClick: PropTypes.func, |
| | onConnect: PropTypes.func, |
| | forceReconnect: PropTypes.bool, |
| | primary: PropTypes.bool, |
| | }; |
| |
|
| | static defaultProps = { |
| | onClick: noop, |
| | onConnect: noop, |
| | forceReconnect: false, |
| | primary: false, |
| | }; |
| |
|
| | state = { |
| | isConnecting: false, |
| | isRefreshing: false, |
| | isAwaitingConnections: false, |
| | }; |
| |
|
| | onClick = () => { |
| | this.props.onClick(); |
| | this.performAction(); |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | getConnectionStatus() { |
| | if ( this.props.isFetching || this.props.isAwaitingConnections ) { |
| | |
| | return 'unknown'; |
| | } |
| |
|
| | |
| | if ( this.props.keyringConnections.length === 0 ) { |
| | |
| | return 'not-connected'; |
| | } |
| |
|
| | if ( some( this.props.keyringConnections, { status: 'broken' } ) ) { |
| | |
| | return 'reconnect'; |
| | } |
| |
|
| | |
| | return 'connected'; |
| | } |
| |
|
| | performAction = () => { |
| | const { forceReconnect, keyringConnections } = this.props; |
| | const connectionStatus = this.getConnectionStatus(); |
| |
|
| | |
| | |
| | if ( 'connected' === connectionStatus && ! forceReconnect ) { |
| | this.props.onConnect( last( keyringConnections ) ); |
| | } else { |
| | this.addConnection(); |
| | } |
| | }; |
| |
|
| | |
| | |
| | |
| | addConnection = () => { |
| | this.setState( { isConnecting: true } ); |
| | if ( this.props.service ) { |
| | |
| | |
| | requestExternalAccess( this.props.service.connect_URL, ( { keyring_id: keyringId } ) => { |
| | if ( ! keyringId ) { |
| | this.setState( { isConnecting: false } ); |
| | return; |
| | } |
| |
|
| | |
| | |
| | this.props.requestKeyringConnections( true ); |
| |
|
| | |
| | |
| | this.setState( { isAwaitingConnections: true, keyringId } ); |
| | } ); |
| | } else { |
| | this.setState( { isConnecting: false } ); |
| | } |
| | }; |
| |
|
| | |
| | UNSAFE_componentWillReceiveProps( nextProps ) { |
| | if ( this.state.isAwaitingConnections ) { |
| | this.setState( { |
| | isAwaitingConnections: false, |
| | isRefreshing: false, |
| | } ); |
| |
|
| | if ( this.didKeyringConnectionSucceed( nextProps.keyringConnections ) ) { |
| | const newKeyringConnection = find( nextProps.keyringConnections, { |
| | ID: this.state.keyringId, |
| | } ); |
| | if ( newKeyringConnection ) { |
| | this.props.onConnect( newKeyringConnection ); |
| | } |
| | this.setState( { keyringId: null, isConnecting: false } ); |
| | } |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | didKeyringConnectionSucceed( keyringConnections ) { |
| | const hasAnyConnectionOptions = some( |
| | keyringConnections, |
| | ( keyringConnection ) => |
| | keyringConnection.isConnected === false || keyringConnection.isConnected === undefined |
| | ); |
| |
|
| | if ( ! this.state.keyringId || keyringConnections.length === 0 || ! hasAnyConnectionOptions ) { |
| | this.setState( { isConnecting: false } ); |
| | return false; |
| | } |
| |
|
| | return true; |
| | } |
| |
|
| | render() { |
| | const { primary, service, translate } = this.props; |
| | const { isConnecting, isRefreshing } = this.state; |
| | const status = service ? this.getConnectionStatus() : 'unknown'; |
| | let localPrimary = false; |
| | let warning = false; |
| | let label; |
| |
|
| | const isPending = 'unknown' === status || isRefreshing || isConnecting; |
| |
|
| | if ( 'unknown' === status ) { |
| | label = translate( 'Loading…' ); |
| | } else if ( isRefreshing ) { |
| | label = translate( 'Reconnecting…' ); |
| | warning = true; |
| | } else if ( isConnecting ) { |
| | label = translate( 'Connecting…' ); |
| | } else { |
| | label = this.props.children; |
| | localPrimary = primary; |
| | } |
| |
|
| | return ( |
| | <Fragment> |
| | <QueryKeyringServices /> |
| | <Button |
| | primary={ localPrimary } |
| | scary={ warning } |
| | onClick={ this.onClick } |
| | disabled={ isPending } |
| | > |
| | { label } |
| | </Button> |
| | </Fragment> |
| | ); |
| | } |
| | } |
| |
|
| | export default connect( |
| | ( state, ownProps ) => { |
| | const service = getKeyringServiceByName( state, ownProps.serviceId ); |
| | const keyringConnections = service ? getKeyringConnectionsByName( state, service.ID ) : []; |
| | const isFetching = isKeyringConnectionsFetching( state ); |
| |
|
| | return { |
| | service, |
| | isFetching, |
| | keyringConnections, |
| | }; |
| | }, |
| | { |
| | deleteStoredKeyringConnection, |
| | requestKeyringConnections, |
| | } |
| | )( localize( KeyringConnectButton ) ); |
| |
|