/** * WordPress dependencies. */ import { Button, Flex, FlexItem, Notice, Panel, PanelBody, TabPanel } from '@wordpress/components'; import { useMemo, useState, WPElement } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; /** * Internal dependencies. */ import { useSettingsScreen } from '../../settings-screen'; import { syncUrl } from '../config'; import { useFeatureSettings } from '../provider'; import Feature from '../components/feature'; import Tab from '../components/tab'; /** * Styles. */ import '../style.css'; /** * Feature settings dashboard app. * * @returns {WPElement} Reports component. */ export default () => { const { createNotice } = useSettingsScreen(); const { features, isBusy, isModified, isSyncing, isSyncRequired, resetSettings, saveSettings, setIsSyncing, } = useFeatureSettings(); /** * URL to start a sync. */ const syncNowUrl = useMemo(() => { const url = new URL(syncUrl); url.searchParams.append('do_sync', 'features'); return url.toString(); }, []); /** * Generic error notice. */ const errorNotice = __('Could not save feature settings. Please try again.', 'elasticpress'); /** * Action when a sync is in progress */ const isSyncingActions = [ { url: syncUrl, label: __('View sync status', 'elasticpress'), }, ]; /** * Notice when a sync is in progress. */ const isSyncingNotice = __('Cannot save settings while a sync is in progress.', 'elasticpress'); /** * Reset notice. */ const resetNotice = __('Changes to feature settings discarded.', 'elasticpress'); /** * Action when syncing later. */ const syncLaterActions = [ { url: syncNowUrl, label: __('Sync', 'elasticpress'), }, ]; /** * Prompt when syncing later. */ const syncLaterConfirm = __( 'If you choose to sync later some settings changes may not take effect until the sync is performed. Save and sync later?', 'elasticpress', ); /** * Prompt when syncing now. */ const syncNowConfirm = __( 'Saving these settings will begin re-syncing your content. Save and sync now?', 'elasticpress', ); /** * Notice when syncing now. */ const syncNowNotice = __('Feature settings saved. Starting sync…', 'elasticpress'); /** * Success notice. */ const successNotice = __('Feature settings saved.', 'elasticpress'); /** * Whether the user has chosen to sync later when saving. Used to show the * busy state on the correct button. */ const [willSyncLater, setWillSyncLater] = useState(false); /** * Feature settings tabs. */ const tabs = features .filter((f) => f.isVisible) .map((f) => { return { name: f.slug, title: , }; }); /** * Error handler. * * @param {Error} e Error object. */ const onError = (e) => { if (e.data === 'is_syncing') { createNotice('error', isSyncingNotice, { actions: isSyncingActions }); setIsSyncing(true); return; } const errorMessage = `${__( 'ElasticPress: Could not save feature settings.', 'elasticpress', )}\n${e.message}`; console.error(errorMessage); // eslint-disable-line no-console createNotice('error', errorNotice); }; /** * Form submission event handler. * * @param {Event} event Submit event. * @returns {void} */ const onSubmit = async (event) => { event.preventDefault(); if (isSyncRequired) { // eslint-disable-next-line no-alert if (!window.confirm(syncNowConfirm)) { return; } } setWillSyncLater(false); try { await saveSettings(); if (isSyncRequired) { createNotice('success', syncNowNotice); window.location = syncNowUrl; } else { createNotice('success', successNotice); } } catch (e) { onError(e); } }; /** * Save and sync later button click event. * * @returns {void} */ const onClickSyncLater = async () => { // eslint-disable-next-line no-alert if (!window.confirm(syncLaterConfirm)) { return; } setWillSyncLater(true); try { await saveSettings(false); createNotice('success', successNotice, { actions: syncLaterActions }); } catch (e) { onError(e); } }; /** * Form reset event handler. * * @param {Event} event Reset event. * @returns {void} */ const onReset = (event) => { event.preventDefault(); resetSettings(); createNotice('success', resetNotice); }; return (
{isSyncing ? ( {isSyncingNotice} ) : null} {({ name }) => } {isSyncRequired ? ( ) : null} {isModified ? ( ) : null}
); };