File size: 7,784 Bytes
1e92f2d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | import { localizeUrl } from '@automattic/i18n-utils';
import { localize } from 'i18n-calypso';
import PropTypes from 'prop-types';
import { Component } from 'react';
import Notice from 'calypso/components/notice';
import NoticeAction from 'calypso/components/notice/notice-action';
import {
ALREADY_CONNECTED,
ALREADY_CONNECTED_BY_OTHER_USER,
DEFAULT_AUTHORIZE_ERROR,
INSTALL_RESPONSE_ERROR,
INVALID_CREDENTIALS,
IS_DOT_COM,
JETPACK_IS_DISCONNECTED,
JETPACK_IS_VALID,
NOT_ACTIVE_JETPACK,
NOT_CONNECTED_JETPACK,
NOT_EXISTS,
NOT_JETPACK,
NOT_WORDPRESS,
OUTDATED_JETPACK,
RETRY_AUTH,
RETRYING_AUTH,
SECRET_EXPIRED,
SITE_BLOCKED,
USER_IS_ALREADY_CONNECTED_TO_SITE,
WORDPRESS_DOT_COM,
XMLRPC_ERROR,
NOT_CONNECTED_USER,
} from './connection-notice-types';
export class JetpackConnectNotices extends Component {
static propTypes = {
// Supply a function that will be called for flow-ending error cases
// instead of showing a notice.
onActionClick: PropTypes.func,
onTerminalError: PropTypes.func,
noticeType: PropTypes.oneOf( [
ALREADY_CONNECTED,
ALREADY_CONNECTED_BY_OTHER_USER,
DEFAULT_AUTHORIZE_ERROR,
INSTALL_RESPONSE_ERROR,
INVALID_CREDENTIALS,
IS_DOT_COM,
JETPACK_IS_DISCONNECTED,
JETPACK_IS_VALID,
NOT_ACTIVE_JETPACK,
NOT_CONNECTED_JETPACK,
NOT_EXISTS,
NOT_JETPACK,
NOT_WORDPRESS,
OUTDATED_JETPACK,
RETRY_AUTH,
RETRYING_AUTH,
SECRET_EXPIRED,
SITE_BLOCKED,
USER_IS_ALREADY_CONNECTED_TO_SITE,
WORDPRESS_DOT_COM,
XMLRPC_ERROR,
NOT_CONNECTED_USER,
] ).isRequired,
translate: PropTypes.func.isRequired,
url: PropTypes.string,
};
getNoticeValues() {
const { noticeType, onDismissClick, translate } = this.props;
const noticeValues = {
icon: 'notice',
status: 'is-error',
text: translate( 'Invalid site address. Enter a valid WordPress URL.' ),
showDismiss: false,
};
if ( onDismissClick ) {
noticeValues.onDismissClick = onDismissClick;
noticeValues.showDismiss = true;
}
switch ( noticeType ) {
case NOT_EXISTS:
noticeValues.userCanRetry = true;
return noticeValues;
case SITE_BLOCKED:
noticeValues.text = translate(
"This site can't be connected to WordPress.com because it violates our {{a}}Terms of Service{{/a}}.",
{
components: {
a: (
<a
href={ localizeUrl( 'https://wordpress.com/tos/' ) }
rel="noopener noreferrer"
target="_blank"
/>
),
},
}
);
return noticeValues;
case IS_DOT_COM:
noticeValues.status = 'is-success';
noticeValues.icon = 'plugins';
noticeValues.text = translate( 'Good news! WordPress.com sites already have Jetpack.' );
return noticeValues;
case NOT_WORDPRESS:
noticeValues.icon = 'block';
noticeValues.text = translate(
"This site doesn't appear to use WordPress. Please verify the URL."
);
return noticeValues;
case NOT_ACTIVE_JETPACK:
// in use in remote install, which automatically redirects to install
return null;
case OUTDATED_JETPACK:
noticeValues.icon = 'block';
noticeValues.text = translate( 'Update Jetpack to the latest version and try again.' );
return noticeValues;
case JETPACK_IS_DISCONNECTED:
noticeValues.icon = 'link-break';
noticeValues.text = translate(
'Jetpack is currently disconnected. Please reconnect it to continue.'
);
return noticeValues;
case JETPACK_IS_VALID:
noticeValues.status = 'is-success';
noticeValues.icon = 'plugins';
noticeValues.text = translate( 'Jetpack is connected.' );
return noticeValues;
case NOT_JETPACK:
// Not notice required, we will move on to installation
return null;
case WORDPRESS_DOT_COM:
noticeValues.text = translate(
'This is a WordPress.com site, which already includes Jetpack features.'
);
noticeValues.status = 'is-warning';
noticeValues.icon = 'status';
return noticeValues;
case RETRYING_AUTH:
noticeValues.text = translate(
'Error authorizing. Page is refreshing for another attempt.'
);
noticeValues.status = 'is-warning';
noticeValues.icon = 'notice';
noticeValues.userCanRetry = true;
return noticeValues;
case RETRY_AUTH:
noticeValues.text = translate(
'In some cases, authorization can take a few attempts. Please try again.'
);
noticeValues.status = 'is-warning';
noticeValues.icon = 'notice';
noticeValues.userCanRetry = true;
return noticeValues;
case SECRET_EXPIRED:
noticeValues.text = translate( 'Connection expired. Refresh the page and try again.' );
noticeValues.status = 'is-error';
noticeValues.icon = 'notice';
return noticeValues;
case DEFAULT_AUTHORIZE_ERROR:
noticeValues.text = translate(
'Error authorizing your site. Please {{link}}contact support{{/link}}.',
{
components: {
link: (
<a
href="https://jetpack.com/contact-support"
target="_blank"
rel="noopener noreferrer"
/>
),
},
}
);
noticeValues.status = 'is-error';
noticeValues.icon = 'notice';
return noticeValues;
case ALREADY_CONNECTED:
noticeValues.text = translate( 'This site is already connected to WordPress.com.' );
noticeValues.status = 'is-info';
noticeValues.icon = 'notice';
return noticeValues;
case ALREADY_CONNECTED_BY_OTHER_USER:
noticeValues.text = translate(
'This site is already connected to a different WordPress.com user, ' +
'you need to disconnect that user before you can connect another.'
);
noticeValues.status = 'is-warning';
noticeValues.icon = 'notice';
return noticeValues;
case USER_IS_ALREADY_CONNECTED_TO_SITE:
noticeValues.text = translate(
'This WordPress.com account is already connected to another user on this site. ' +
'Please login to another WordPress.com account to complete the connection.'
);
noticeValues.status = 'is-warning';
noticeValues.icon = 'notice';
return noticeValues;
case XMLRPC_ERROR:
noticeValues.text = translate(
"Can't connect to your site. Check if it's accessible and try again."
);
return noticeValues;
case INVALID_CREDENTIALS:
noticeValues.text = translate(
'Invalid credentials. Check your account information and try again.'
);
noticeValues.status = 'is-error';
noticeValues.icon = 'notice';
return noticeValues;
}
}
getNoticeActionText() {
const { noticeType, translate } = this.props;
switch ( noticeType ) {
case XMLRPC_ERROR:
return translate( 'Try again' );
}
return null;
}
componentDidUpdate() {
if ( this.errorIsTerminal() && this.props.onTerminalError ) {
this.props.onTerminalError( this.props.noticeType );
}
}
componentDidMount() {
if ( this.errorIsTerminal() && this.props.onTerminalError ) {
this.props.onTerminalError( this.props.noticeType );
}
}
errorIsTerminal() {
const notice = this.getNoticeValues();
return notice && ! notice.userCanRetry;
}
renderNoticeAction() {
const { onActionClick } = this.props;
const noticeActionText = this.getNoticeActionText();
if ( ! onActionClick || ! noticeActionText ) {
return null;
}
return <NoticeAction onClick={ onActionClick }>{ noticeActionText }</NoticeAction>;
}
render() {
const noticeValues = this.getNoticeValues();
if ( this.errorIsTerminal() && this.props.onTerminalError ) {
return null;
}
if ( noticeValues ) {
return (
<div className="jetpack-connect__notices-container">
<Notice { ...noticeValues }>{ this.renderNoticeAction() }</Notice>
</div>
);
}
return null;
}
}
export default localize( JetpackConnectNotices );
|