Skip to content

Commit

Permalink
ScreenLink: Introduce component to simplify linking between screens.
Browse files Browse the repository at this point in the history
The caller no longer needs to deal with the importing `clickScreenLink` and setting it as the `onClick` handler.
  • Loading branch information
iandunn committed Jan 24, 2023
1 parent f3cd680 commit 09a195a
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 40 deletions.
34 changes: 16 additions & 18 deletions settings/src/components/account-status.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Icon, cancelCircleFilled, check, chevronRight, warning } from '@wordpre
* Internal dependencies
*/
import { GlobalContext } from '../script';
import ScreenLink from './screen-link';

/**
* Render the Account Status.
Expand Down Expand Up @@ -65,27 +66,24 @@ export default function AccountStatus() {
* Render a card for the status of the given setting.
*/
function SettingStatusCard( { screen, status, headerText, bodyText } ) {
const { clickScreenLink } = useContext( GlobalContext );

let screenUrl = new URL( document.location.href );
screenUrl.searchParams.set( 'screen', screen );

return (
<Card className={ 'wporg-2fa__status-card wporg-2fa__status-card-' + screen }>
<a
href={ screenUrl.href }
onClick={ ( event ) => clickScreenLink( event, screen ) }
>
<CardHeader>
<StatusIcon status={ status } />
{ headerText }
</CardHeader>
<ScreenLink
screen={ screen }
anchorText={
<>
<CardHeader>
<StatusIcon status={ status } />
{ headerText }
</CardHeader>

<CardBody>
<p>{ bodyText }</p>
<Icon icon={ chevronRight } />
</CardBody>
</a>
<CardBody>
<p>{ bodyText }</p>
<Icon icon={ chevronRight } />
</CardBody>
</>
}
/>
</Card>
);
}
Expand Down
35 changes: 35 additions & 0 deletions settings/src/components/screen-link.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* WordPress dependencies
*/
import { useContext } from '@wordpress/element';

/**
* Internal dependencies
*/
import { GlobalContext } from '../script';

export default function ScreenLink( { screen, anchorText, buttonStyle = false } ) {
const { clickScreenLink } = useContext( GlobalContext );
const classes = [];
let screenUrl = new URL( document.location.href );

screenUrl.searchParams.set( 'screen', screen );

if ( 'primary' === buttonStyle ) {
classes.push( 'components-button' );
classes.push( 'is-primary' );
} else if ( 'secondary' === buttonStyle ) {
classes.push( 'components-button' );
classes.push( 'is-secondary' );
}

return (
<a
href={ screenUrl.href }
onClick={ ( event ) => clickScreenLink( event, screen ) }
className={ classes.join( ' ' ) }
>
{ anchorText }
</a>
)
}
6 changes: 6 additions & 0 deletions settings/src/components/screen-link.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* Overwrite bbPress styles with Gutenberg styles */
#bbpress-forums .wp-block-wporg-two-factor-settings a.components-button,
#bbpress-forums .wp-block-wporg-two-factor-settings a.components-button:hover {
box-shadow: inset 0 0 0 1px var(--wp-components-color-accent,var(--wp-admin-theme-color,#007cba));
text-decoration: none;
}
19 changes: 4 additions & 15 deletions settings/src/components/totp.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { RawHTML, useCallback, useContext, useEffect, useState } from '@wordpres
* Internal dependencies
*/
import SetupProgressBar from './setup-progress-bar';
import ScreenLink from './screen-link'
import { refreshRecord } from '../utilities';
import { GlobalContext } from '../script';

Expand Down Expand Up @@ -75,9 +76,6 @@ function Setup() {
}
} );

const cancelUrl = new URL( document.location.href );
cancelUrl.searchParams.set( 'screen', 'account-status' );

return (
<>
<SetupProgressBar step="totp" />
Expand Down Expand Up @@ -108,7 +106,6 @@ function Setup() {
setVerifyCode={ setVerifyCode }
qrCodeUrl={ qrCodeUrl }
secretKey={ secretKey }
cancelUrl={ cancelUrl }
/>

{ error &&
Expand Down Expand Up @@ -197,8 +194,7 @@ function createQrCode( data ) {
/**
* Render the form for entering the TOTP code.
*/
function SetupForm( { handleEnable, verifyCode, setVerifyCode, qrCodeUrl, secretKey, cancelUrl } ) {
const { clickScreenLink } = useContext( GlobalContext );
function SetupForm( { handleEnable, verifyCode, setVerifyCode, qrCodeUrl, secretKey } ) {
const verifyCodeLength = 6;
const canSubmit = qrCodeUrl && secretKey && verifyCode && verifyCode.length === verifyCodeLength;

Expand Down Expand Up @@ -230,14 +226,7 @@ function SetupForm( { handleEnable, verifyCode, setVerifyCode, qrCodeUrl, secret
Enable
</Button>

<Button
isLink
href={ cancelUrl.href }
onClick={ ( event ) => clickScreenLink( event, 'account-status' ) }
>
Cancel
{/* todo this should look like a button not a link */}
</Button>
<ScreenLink screen="account-status" anchorText="Cancel" buttonStyle="secondary" />
</div>
</form>
);
Expand Down Expand Up @@ -278,7 +267,7 @@ function Manage() {

<p>
Make sure you've created { ' ' }
<a href="?screen=backup-codes" onClick={ ( event ) => clickScreenLink( event, 'backup-codes' ) }>backup codes</a> { ' ' }
<ScreenLink screen="backup-codes" anchorText="backup codes" /> { ' ' }
and saved them in a safe location, in case you ever lose your device. You may also need them when transitioning to a new device.
Without them you may permenantly lose access to your account.
</p>
Expand Down
1 change: 1 addition & 0 deletions settings/src/components/totp.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

.wporg-2fa__verify-code {
width: 10ch;
margin-bottom: 20px;
}

.wporg-2fa__enabled-status {
Expand Down
17 changes: 10 additions & 7 deletions settings/src/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Card, CardHeader, CardBody, Flex, Spinner } from '@wordpress/components
* Internal dependencies
*/
import { getUserRecord } from './utilities';
import ScreenLink from './components/screen-link'
import AccountStatus from './components/account-status';
import Password from './components/password';
import EmailAddress from './components/email-address';
Expand Down Expand Up @@ -108,13 +109,15 @@ function Main( { userId } ) {
<Card>
<CardHeader className="wporg-2fa__navigation" size="xSmall">
<Flex>
<a
href="?screen=account-status"
onClick={ ( event ) => clickScreenLink( event, 'account-status' ) }
>
<Icon icon={ arrowLeft } />
Back
</a>
<ScreenLink
screen="account-status"
anchorText={
<>
<Icon icon={ arrowLeft } />
Back
</>
}
/>

<h3>{ screen.replace( '-', ' ' ).replace( 'totp', 'One Time Passwords' ) }</h3>
</Flex>
Expand Down
1 change: 1 addition & 0 deletions settings/src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@
@import "components/totp";
@import "components/setup-progress-bar";
@import "components/global-notice";
@import "components/screen-link";

0 comments on commit 09a195a

Please sign in to comment.