Skip to content
Open
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
21 changes: 11 additions & 10 deletions assets/src/admin/search/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,14 @@ const OneSearchSettingsPage = () => {
settings?.onesearch_indexable_entities?.entities || {}
);
setEntitiesLoadFailed( false );
} catch {
} catch ( error ) {
// Saving now would overwrite the selections of every site.
setEntitiesLoadFailed( true );
setNotice( {
type: 'error',
message: __(
'Error fetching indexable entities.',
'onesearch'
),
message:
( error as { message?: string } )?.message ||
__( 'Error fetching indexable entities.', 'onesearch' ),
} );
}
};
Expand Down Expand Up @@ -141,13 +140,15 @@ const OneSearchSettingsPage = () => {
setPostTypeErrors(
Array.isArray( data?.errors ) ? data.errors : []
);
} catch {
} catch ( error ) {
setNotice( {
type: 'error',
message: __(
'Error fetching post types from sites.',
'onesearch'
),
message:
( error as { message?: string } )?.message ||
__(
'Error fetching post types from sites.',
'onesearch'
),
} );
}
};
Expand Down
6 changes: 4 additions & 2 deletions assets/src/admin/settings/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
import { useState, useRef, useCallback } from 'react';
import { __ } from '@wordpress/i18n';
import { Notice, Snackbar } from '@wordpress/components';

Check warning on line 9 in assets/src/admin/settings/page.tsx

View workflow job for this annotation

GitHub Actions / CSS/JS Lint / JS Lint & TypeScript

Use `Notice` from `@wordpress/ui` instead
import apiFetch from '@wordpress/api-fetch';

/**
Expand Down Expand Up @@ -130,10 +130,12 @@
) }`,
method: 'DELETE',
} );
} catch {
} catch ( error ) {
setNotice( {
type: 'error',
message: __( 'Failed to update shared sites', 'onesearch' ),
message:
( error as { message?: string } )?.message ||
__( 'Failed to update shared sites', 'onesearch' ),
} );
return;
}
Expand Down
30 changes: 15 additions & 15 deletions assets/src/components/SiteSearchSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,15 @@ const SiteSearchSettings = ( {
// To trigger re-rendering of search configuration component.
setReloadKey( ( k ) => k + 1 );
} )
.catch( () => {
.catch( ( error: unknown ) => {
setNotice( {
type: 'error',
message: __(
'Failed to auto-save disabled sites.',
'onesearch'
),
message:
( error as { message?: string } )?.message ||
__(
'Failed to auto-save disabled sites.',
'onesearch'
),
} );
} );
}
Expand Down Expand Up @@ -246,13 +248,12 @@ const SiteSearchSettings = ( {
);
}
} )
.catch( () => {
.catch( ( error: unknown ) => {
setNotice( {
type: 'error',
message: __(
'Failed to load search settings.',
'onesearch'
),
message:
( error as { message?: string } )?.message ||
__( 'Failed to load search settings.', 'onesearch' ),
} );
} )
.finally( () => {
Expand Down Expand Up @@ -411,13 +412,12 @@ const SiteSearchSettings = ( {
} );
setInitialSettings( settings.onesearch_sites_search_settings );
} )
.catch( () => {
.catch( ( error: unknown ) => {
setNotice( {
type: 'error',
message: __(
'Failed to save search settings.',
'onesearch'
),
message:
( error as { message?: string } )?.message ||
__( 'Failed to save search settings.', 'onesearch' ),
} );
} )
.finally( () => {
Expand Down
36 changes: 21 additions & 15 deletions assets/src/components/SiteSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@ const SiteSettings = () => {
),
} );
}
} catch {
} catch ( error ) {
setNotice( {
type: 'error',
message: __(
'Error regenerating API key. Please try again later.',
'onesearch'
),
message:
( error as { message?: string } )?.message ||
__(
'Error regenerating API key. Please try again later.',
'onesearch'
),
} );
}
}, [] );
Expand All @@ -74,13 +76,15 @@ const SiteSettings = () => {
onesearch_parent_site_url?: string;
} >( { path: '/wp/v2/settings' } );
setGoverningSite( data?.onesearch_parent_site_url || '' );
} catch {
} catch ( error ) {
setNotice( {
type: 'error',
message: __(
'Failed to fetch governing site. Please try again later.',
'onesearch'
),
message:
( error as { message?: string } )?.message ||
__(
'Failed to fetch governing site. Please try again later.',
'onesearch'
),
} );
}
}, [] );
Expand Down Expand Up @@ -109,13 +113,15 @@ const SiteSettings = () => {
'onesearch'
),
} );
} catch {
} catch ( error ) {
setNotice( {
type: 'error',
message: __(
'Failed to disconnect governing site. Please try again later.',
'onesearch'
),
message:
( error as { message?: string } )?.message ||
__(
'Failed to disconnect governing site. Please try again later.',
'onesearch'
),
} );
} finally {
setShowDisconnectionModal( false );
Expand Down
15 changes: 6 additions & 9 deletions assets/src/components/SiteTypeSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,19 @@ const SiteTypeSettings = () => {
setIsSaving( true );

try {
const settings = await apiFetch< {
onesearch_site_type?: SiteType;
} >( {
await apiFetch( {
path: '/wp/v2/settings',
method: 'POST',
data: { onesearch_site_type: value },
} );

if ( ! settings?.onesearch_site_type ) {
throw new Error( 'No site type in response' );
}

// Reload so the settings screen and menu match the new site type.
window.location.reload();
} catch {
setError( __( 'Error setting site type.', 'onesearch' ) );
} catch ( err ) {
setError(
( err as { message?: string } )?.message ||
__( 'Error setting site type.', 'onesearch' )
);
} finally {
setIsSaving( false );
}
Expand Down
12 changes: 7 additions & 5 deletions assets/src/components/deactivation-settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,15 @@ const DeactivationSettings = ( {
type: 'success',
message: __( 'Settings saved.', 'onesearch' ),
} );
} catch {
} catch ( error ) {
setNotice( {
type: 'error',
message: __(
'Failed to save the setting. Please try again.',
'onesearch'
),
message:
( error as { message?: string } )?.message ||
__(
'Failed to save the setting. Please try again.',
'onesearch'
),
} );
} finally {
setIsSaving( false );
Expand Down
11 changes: 9 additions & 2 deletions tests/js/SearchPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ const mockRequests = ( {
return allPostTypes;
}
if ( ! entitiesOk ) {
throw new Error( 'load failed' );
throw {
code: 'rest_forbidden',
message: 'Sorry, you are not allowed to do that.',
data: { status: 403 },
};
}
return { onesearch_indexable_entities: { entities: {} } };
};
Expand Down Expand Up @@ -121,7 +125,7 @@ describe( 'search settings page', () => {
);
} );

it( 'flags the entities as failed to load after an error response', async () => {
it( "flags the entities as failed to load and shows the server's message", async () => {
mockRequests( { entitiesOk: false } );
await mountPage();

Expand All @@ -130,6 +134,9 @@ describe( 'search settings page', () => {
'true'
);
} );
expect(
document.querySelector( '.components-snackbar' )
).toHaveTextContent( 'Sorry, you are not allowed to do that.' );
} );

it( 'passes on the sites that failed to return post types', async () => {
Expand Down
17 changes: 12 additions & 5 deletions tests/js/SettingsPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,10 @@ describe( 'SettingsPage', () => {
},
] );
mockedApiFetch
.mockRejectedValueOnce( new Error( 'delete failed' ) )
.mockResolvedValueOnce( {
governing_site_notified: true,
message: 'Brand site removed successfully.',
} )
.mockResolvedValueOnce( {
shared_sites: [
{
Expand All @@ -274,7 +277,7 @@ describe( 'SettingsPage', () => {
fireEvent.click(
screen.getByRole( 'button', { name: 'Delete Site' } )
);
await screen.findAllByText( 'Failed to update shared sites' );
await screen.findAllByText( 'Brand site removed successfully.' );

// The second notice arrives just before the first one's timer ends.
act( () => {
Expand Down Expand Up @@ -354,7 +357,7 @@ describe( 'SettingsPage', () => {
expect( screen.queryByTestId( 'site-modal' ) ).not.toBeInTheDocument();
} );

it( 'shows an error and leaves the brand site alone when deleting fails', async () => {
it( "shows the server's error and leaves the brand site alone when deleting fails", async () => {
setSharedSites( [
{
id: 'one',
Expand All @@ -363,7 +366,11 @@ describe( 'SettingsPage', () => {
api_key: 'brand-key',
},
] );
mockedApiFetch.mockRejectedValueOnce( new Error( 'delete failed' ) );
mockedApiFetch.mockRejectedValueOnce( {
code: 'onesearch_site_not_found',
message: 'The brand site was not found.',
data: { status: 404 },
} );

render( <SettingsPage /> );

Expand All @@ -372,7 +379,7 @@ describe( 'SettingsPage', () => {
);

const notices = await screen.findAllByText(
'Failed to update shared sites'
'The brand site was not found.'
);
expect( notices.length ).toBeGreaterThanOrEqual( 1 );
expect( screen.getByTestId( 'site-count' ) ).toHaveTextContent( '1' );
Expand Down
12 changes: 9 additions & 3 deletions tests/js/SiteSearchSettings.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ describe( 'SiteSearchSettings', () => {

it( 'shows error when settings fail to load', async () => {
const setNotice = vi.fn();
mockedApiFetch.mockRejectedValueOnce( new Error( 'load failed' ) );
mockedApiFetch.mockRejectedValueOnce( {} );

render(
<SiteSearchSettings
Expand All @@ -334,7 +334,12 @@ describe( 'SiteSearchSettings', () => {

mockedApiFetch
.mockResolvedValueOnce( { onesearch_sites_search_settings: {} } )
.mockRejectedValueOnce( new Error( 'save failed' ) )
.mockRejectedValueOnce( {
code: 'rest_invalid_param',
message:
'Invalid parameter(s): onesearch_sites_search_settings',
data: { status: 400 },
} )
.mockResolvedValueOnce( { onesearch_sites_search_settings: {} } );

render(
Expand All @@ -355,7 +360,8 @@ describe( 'SiteSearchSettings', () => {
await waitFor( () => {
expect( setNotice ).toHaveBeenCalledWith( {
type: 'error',
message: 'Failed to save search settings.',
message:
'Invalid parameter(s): onesearch_sites_search_settings',
} );
} );
} );
Expand Down
Loading
Loading