Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Dashboard: Redesign the Login protection card with toggles for account protection and Secure Sign On.
15 changes: 11 additions & 4 deletions projects/packages/protect/routes/dashboard/components/card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,21 @@ export function UnavailableCard( { icon, title }: { icon: ReactElement; title: s
}

/**
* A row showing the last settings save error, if any.
* A row showing why the last save of one of the card's settings failed, until dismissed.
*
* @param props - Component props.
* @param props.settings - Jetpack settings, with their error.
* @return The row, or nothing.
* @param props.keys - The settings this card saves.
* @return The row, or nothing without an error from one of `keys`.
*/
export function SettingsErrorRow( { settings }: { settings: ProtectSettingsData } ) {
if ( ! settings.error ) {
export function SettingsErrorRow( {
settings,
keys,
}: {
settings: ProtectSettingsData;
keys: string[];
} ) {
if ( ! settings.error || ! settings.errorKeys.some( key => keys.includes( key ) ) ) {
return null;
}
return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { ToggleControl } from '@wordpress/components';
import { useCallback } from '@wordpress/element';
import { SwitchControl } from '@wordpress/ui';
import type { ProtectSettingsData } from '../../data/use-protect-settings';
import type { ReactNode } from 'react';

type Props = {
data: ProtectSettingsData;
name: string;
label: string;
help?: ReactNode;
help?: string;
disabled?: boolean;
};

Expand All @@ -27,13 +26,12 @@ export default function SettingToggle( { data, name, label, help, disabled = fal
const onChange = useCallback( ( value: boolean ) => save( { [ name ]: value } ), [ save, name ] );

return (
<ToggleControl
__nextHasNoMarginBottom
<SwitchControl
label={ label }
help={ help }
description={ help }
checked={ Boolean( data.settings?.[ name ] ) }
disabled={ disabled || data.isSaving( name ) }
onChange={ onChange }
onCheckedChange={ onChange }
/>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ describe( 'useProtectSettings', () => {

expect( result.current.settings ).toEqual( expected );
expect( result.current.error ).toBe( 'Nope' );
expect( result.current.errorKeys ).toEqual( [ 'list' ] );
expect( result.current.isSaving( 'list' ) ).toBe( false );
} );

Expand Down Expand Up @@ -149,6 +150,7 @@ describe( 'useProtectSettings', () => {
const { result } = renderHook( () => useProtectSettings() );
await act( async () => result.current.load() );
expect( result.current.error ).toEqual( expect.any( String ) );
expect( result.current.errorKeys ).toEqual( [] );
expect( result.current.isLoaded ).toBe( false );

const retry = defer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { store as noticesStore } from '@wordpress/notices';
// One id, so a burst of saves shows one snackbar.
const SAVED_NOTICE_ID = 'jetpack-protect-settings-saved';

type Failure = { message: string; keys: string[] };
const NO_KEYS: string[] = [];

/** Flat Jetpack settings, keyed by option or module slug, as `jetpack/v4/settings` returns them. */
export type ProtectSettings = Record< string, unknown >;

Expand All @@ -22,6 +25,8 @@ export type ProtectSettingsData = {
isLoaded: boolean;
waf: WafConfig | null;
error: string | null;
/** The keys whose save failed, so a card shows only its own errors; empty when loading failed. */
errorKeys: string[];
dismissError: () => void;
isSaving: ( key: string ) => boolean;
/** Fetches the settings once; later calls do nothing unless the fetch failed. */
Expand All @@ -43,7 +48,7 @@ export default function useProtectSettings(): ProtectSettingsData {
const [ settings, setSettingsState ] = useState< ProtectSettings | null >( null );
const [ waf, setWaf ] = useState< WafConfig | null >( null );
const [ isLoaded, setIsLoaded ] = useState( false );
const [ error, setError ] = useState< string | null >( null );
const [ failure, setFailure ] = useState< Failure | null >( null );
const [ saving, setSaving ] = useState< string[] >( [] );
const settingsRef = useRef< ProtectSettings | null >( null );
const loadStarted = useRef( false );
Expand All @@ -60,7 +65,7 @@ export default function useProtectSettings(): ProtectSettingsData {
return;
}
loadStarted.current = true;
setError( null );
setFailure( null );

Promise.all( [
apiFetch< ProtectSettings >( { path: '/jetpack/v4/settings' } ),
Expand All @@ -77,7 +82,10 @@ export default function useProtectSettings(): ProtectSettingsData {
} )
.catch( () => {
loadStarted.current = false;
setError( __( 'Your settings couldn’t be loaded.', 'jetpack-protect-pkg' ) );
setFailure( {
message: __( 'Your settings couldn’t be loaded.', 'jetpack-protect-pkg' ),
keys: NO_KEYS,
} );
} );
}, [ setSettings ] );

Expand All @@ -92,7 +100,7 @@ export default function useProtectSettings(): ProtectSettingsData {
keys.forEach( key => savedKeys.current.add( key ) );
setSettings( { ...before, ...patch } );
setSaving( current => [ ...current, ...keys ] );
setError( null );
setFailure( null );
let saved = true;

try {
Expand All @@ -116,10 +124,12 @@ export default function useProtectSettings(): ProtectSettingsData {
}
} );
setSettings( rolledBack );
setError(
( e as { message?: string } )?.message ||
__( 'Your change couldn’t be saved. Try again.', 'jetpack-protect-pkg' )
);
setFailure( {
message:
( e as { message?: string } )?.message ||
__( 'Your change couldn’t be saved. Try again.', 'jetpack-protect-pkg' ),
keys,
} );
saved = false;
} finally {
// One entry per save in flight, so an overlapping save of the same key keeps it busy.
Expand Down Expand Up @@ -148,10 +158,21 @@ export default function useProtectSettings(): ProtectSettingsData {
);

const isSaving = useCallback( ( key: string ) => saving.includes( key ), [ saving ] );
const dismissError = useCallback( () => setError( null ), [] );
const dismissError = useCallback( () => setFailure( null ), [] );

return useMemo(
() => ( { settings, isLoaded, waf, error, dismissError, isSaving, load, save, refresh } ),
[ settings, isLoaded, waf, error, dismissError, isSaving, load, save, refresh ]
() => ( {
settings,
isLoaded,
waf,
error: failure?.message ?? null,
errorKeys: failure?.keys ?? NO_KEYS,
dismissError,
isSaving,
load,
save,
refresh,
} ),
[ settings, isLoaded, waf, failure, dismissError, isSaving, load, save, refresh ]
);
}
34 changes: 33 additions & 1 deletion projects/packages/protect/routes/dashboard/route.scss
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,42 @@ body.toplevel_page_jetpack-protect {
// The row is full-bleed for its divider; the padding re-aligns its contents.
&__row {
box-sizing: border-box;
padding: 16px var(--wp-ui-card-padding);
padding: var(--wpds-dimension-padding-lg) var(--wpds-dimension-padding-2xl);
border-block-start: var(--wpds-border-width-xs) solid var(--wpds-color-stroke-surface-neutral);
}

// Cells split by rules; each cell takes the row's padding,
// so the rules run edge to edge.
&__row.jp-protect-card__cells {
--jp-protect-cells: 2;

display: grid;
grid-template-columns: repeat(var(--jp-protect-cells), minmax(0, 1fr));
padding: 0;

> * {
box-sizing: border-box;
padding: var(--wpds-dimension-padding-lg) var(--wpds-dimension-padding-2xl);
}

> * + * {
border-inline-start:
var(--wpds-border-width-xs) solid
var(--wpds-color-stroke-surface-neutral);
}

@media (max-width: 600px) {
grid-template-columns: minmax(0, 1fr);

> * + * {
border-inline-start: 0;
border-block-start:
var(--wpds-border-width-xs) solid
var(--wpds-color-stroke-surface-neutral);
}
}
}

&__muted {
color: var(--wpds-color-foreground-content-neutral-weak);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ const UNAVAILABLE: FirewallState = {
ipRules: {},
};

// The only setting this card saves.
const FIREWALL_KEYS = [ 'waf' ];

// One id for the test's snackbars, so the result replaces "Testing the firewall…".
const NOTICE_ID = 'jetpack-protect-firewall-test';

Expand Down Expand Up @@ -277,7 +280,7 @@ export default function FirewallOverviewCard( { state, settings, openTab }: Fire
onOpenChange={ setDetailsOpen }
/>
) }
<SettingsErrorRow settings={ settings } />
<SettingsErrorRow settings={ settings } keys={ FIREWALL_KEYS } />
{ ! active && (
<CardRow>
<FirewallToggle data={ settings } />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ describe( 'FirewallOverviewCard', () => {
it( 'turns the firewall on from the card while it is off', async () => {
render( <FirewallOverviewCard { ...props } state={ { ...props.state, active: false } } /> );

await userEvent.click( screen.getByRole( 'checkbox', { name: 'Enable firewall' } ) );
await userEvent.click( screen.getByRole( 'switch', { name: 'Enable firewall' } ) );

expect( props.settings.save ).toHaveBeenCalledWith( { waf: true } );
} );
Expand Down
Loading
Loading