diff --git a/assets/src/admin/search/index.tsx b/assets/src/admin/search/index.tsx index 7956d66a..31d03238 100644 --- a/assets/src/admin/search/index.tsx +++ b/assets/src/admin/search/index.tsx @@ -1,13 +1,18 @@ /** * WordPress dependencies */ -import { useState, useEffect, createRoot } from '@wordpress/element'; +import { Button, Modal, Snackbar } from '@wordpress/components'; +import { createRoot, useEffect, useState } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; -import { Snackbar } from '@wordpress/components'; /** * External dependencies */ +import type { + BrandSite, + defaultBrandSite, + NoticeType, +} from '@/admin/settings/page'; import SiteIndexableEntities from '@/components/SiteIndexableEntities'; import SiteModal from '@/components/SiteModal'; import SiteSearchSettings, { @@ -15,15 +20,10 @@ import SiteSearchSettings, { } from '@/components/SiteSearchSettings'; import { API_NAMESPACE, - NONCE, CURRENT_SITE_URL, + NONCE, withTrailingSlash, } from '@/js/utils'; -import type { - BrandSite, - defaultBrandSite, - NoticeType, -} from '@/admin/settings/page'; import type { SiteType } from '@/types/global'; type BrandSiteFormData = typeof defaultBrandSite; @@ -49,6 +49,11 @@ interface FetchAllPostTypesResponse { } const OneSearchSettingsPage = () => { + const sharedSites = window.OneSearchSettings.sharedSites || []; + const hasAlgoliaCreds = + window.OneSearchSettings.hasAlgoliaCredentials ?? false; + const hasPrerequisites = sharedSites.length > 0 && hasAlgoliaCreds; + const [ siteType, setSiteType ] = useState< SiteType >( '' ); const [ showModal, setShowModal ] = useState( false ); const [ editingIndex, setEditingIndex ] = useState< number | null >( null ); @@ -289,44 +294,78 @@ const OneSearchSettingsPage = () => { { siteType === 'governing-site' && ( - - ) } - - { siteType === 'governing-site' && ( - - ) } - - { showModal && ( - { - setShowModal( false ); - setEditingIndex( null ); - setFormData( { name: '', url: '', api_key: '' } ); - } } - editing={ editingIndex !== null } - sites={ sites } - originalData={ - editingIndex !== null - ? sites[ editingIndex ] - : undefined - } - /> + <> +
+ + + +
+ + { showModal && ( + { + setShowModal( false ); + setEditingIndex( null ); + setFormData( { + name: '', + url: '', + api_key: '', + } ); + } } + editing={ editingIndex !== null } + sites={ sites } + originalData={ + editingIndex !== null + ? sites[ editingIndex ] + : undefined + } + /> + ) } + + { ! hasPrerequisites && ( + {} } + shouldCloseOnEsc={ false } + shouldCloseOnClickOutside={ false } + size="medium" + __experimentalHideHeader + > +

{ __( 'Setup Required', 'onesearch' ) }

+

+ { __( + 'You need to add at least one Brand Site and configure your Algolia credentials before you can set up indices and search.', + 'onesearch' + ) } +

+ +
+ ) } + ) } ); diff --git a/assets/src/components/SiteSearchSettings.tsx b/assets/src/components/SiteSearchSettings.tsx index 4b83a37a..d66ef69f 100644 --- a/assets/src/components/SiteSearchSettings.tsx +++ b/assets/src/components/SiteSearchSettings.tsx @@ -7,22 +7,22 @@ import { Card, CardBody, CardHeader, - ToggleControl, - Spinner, Notice, + Spinner, + ToggleControl, } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; /** * External dependencies */ -import { useState, useEffect, useCallback, useRef } from 'react'; +import { useCallback, useEffect, useRef, useState } from 'react'; /** * Internal dependencies */ -import { NONCE, withTrailingSlash } from '../js/utils'; import type { NoticeType } from '@/admin/settings/page'; +import { NONCE, withTrailingSlash } from '../js/utils'; /** * Create NONCE middleware for apiFetch @@ -468,8 +468,9 @@ const SiteSearchSettings = ( { + + { /* Notice for warnings */ } - { /* Notice for warnings */ } { localNotice && ( ; + hasAlgoliaCredentials?: boolean; } export interface OneSearchOnboarding { diff --git a/inc/Modules/Core/Assets.php b/inc/Modules/Core/Assets.php index 84ca8191..14e061e7 100644 --- a/inc/Modules/Core/Assets.php +++ b/inc/Modules/Core/Assets.php @@ -64,17 +64,20 @@ final class Assets implements Registrable { * @return array */ public static function get_localized_data(): array { + $algolia_creds = Search_Settings::get_algolia_credentials(); + if ( empty( self::$localized_data ) ) { self::$localized_data = [ - 'currentSiteUrl' => esc_url( home_url( '/' ) ), - 'indexableEntities' => Search_Settings::get_indexable_entities(), - 'nonce' => wp_create_nonce( 'wp_rest' ), - 'api_key' => Settings::get_api_key(), - 'restNamespace' => Abstract_REST_Controller::NAMESPACE, - 'restUrl' => esc_url( home_url( '/wp-json/' ) ), - 'setupUrl' => admin_url( 'admin.php?page=onesearch-settings' ), - 'sharedSites' => array_values( Settings::get_shared_sites() ), - 'siteType' => Settings::get_site_type(), + 'currentSiteUrl' => esc_url( home_url( '/' ) ), + 'hasAlgoliaCredentials' => ! empty( $algolia_creds['app_id'] ) && ! empty( $algolia_creds['write_key'] ), + 'indexableEntities' => Search_Settings::get_indexable_entities(), + 'nonce' => wp_create_nonce( 'wp_rest' ), + 'api_key' => Settings::get_api_key(), + 'restNamespace' => Abstract_REST_Controller::NAMESPACE, + 'restUrl' => esc_url( home_url( '/wp-json/' ) ), + 'setupUrl' => admin_url( 'admin.php?page=onesearch-settings' ), + 'sharedSites' => array_values( Settings::get_shared_sites() ), + 'siteType' => Settings::get_site_type(), ]; } diff --git a/inc/Modules/Search/Admin.php b/inc/Modules/Search/Admin.php index 06ac2def..74229154 100644 --- a/inc/Modules/Search/Admin.php +++ b/inc/Modules/Search/Admin.php @@ -11,7 +11,6 @@ use OneSearch\Contracts\Interfaces\Registrable; use OneSearch\Modules\Core\Assets; -use OneSearch\Modules\Search\Settings as Search_Settings; use OneSearch\Modules\Settings\Settings; /** @@ -34,7 +33,7 @@ final class Admin implements Registrable { * {@inheritDoc} */ public function register_hooks(): void { - if ( ! Settings::is_governing_site() || empty( Search_Settings::get_algolia_credentials() ) ) { + if ( ! Settings::is_governing_site() ) { return; } add_action( 'admin_menu', [ $this, 'add_submenu' ] ); @@ -45,7 +44,7 @@ public function register_hooks(): void { * Register the settings page. */ public function add_submenu(): void { - // Register the "Indices and Search" submenu only for governing sites with Algolia credentials. + // Register the "Indices and Search" submenu for governing sites. add_submenu_page( self::MENU_SLUG, __( 'Indices and Search', 'onesearch' ), diff --git a/inc/Modules/Settings/Admin.php b/inc/Modules/Settings/Admin.php index 4e852ea4..b0ed4acd 100644 --- a/inc/Modules/Settings/Admin.php +++ b/inc/Modules/Settings/Admin.php @@ -85,7 +85,7 @@ public function add_submenu(): void { * Remove the default submenu added by WordPress. */ public function remove_default_submenu(): void { - if ( Settings::is_governing_site() && ! empty( Settings::get_shared_sites() ) ) { + if ( Settings::is_governing_site() ) { return; } remove_submenu_page( self::MENU_SLUG, self::MENU_SLUG );