From e3cc8548599380f0254098add756b346069e3bf6 Mon Sep 17 00:00:00 2001 From: Jacob Peattie Date: Tue, 5 Sep 2023 15:25:03 +1000 Subject: [PATCH 1/2] Rename sync scripts directory. --- assets/js/{sync => sync-ui}/components/common/date-time.js | 0 assets/js/{sync => sync-ui}/components/common/message-log.js | 0 assets/js/{sync => sync-ui}/components/common/progress-bar.js | 0 assets/js/{sync => sync-ui}/components/icons/pause.js | 0 assets/js/{sync => sync-ui}/components/icons/play.js | 0 assets/js/{sync => sync-ui}/components/icons/stop.js | 0 assets/js/{sync => sync-ui}/components/icons/sync.js | 0 assets/js/{sync => sync-ui}/components/icons/thumbs-down.js | 0 assets/js/{sync => sync-ui}/components/icons/thumbs-up.js | 0 assets/js/{sync => sync-ui}/components/sync-page.js | 0 assets/js/{sync => sync-ui}/components/sync/controls.js | 0 assets/js/{sync => sync-ui}/components/sync/log.js | 0 assets/js/{sync => sync-ui}/components/sync/panel.js | 0 assets/js/{sync => sync-ui}/components/sync/progress.js | 0 assets/js/{sync => sync-ui}/components/sync/status.js | 0 assets/js/{sync => sync-ui}/config.js | 0 assets/js/{sync => sync-ui}/hooks.js | 0 assets/js/{sync => sync-ui}/index.js | 0 assets/js/{sync => sync-ui}/utilities.js | 0 package.json | 2 +- 20 files changed, 1 insertion(+), 1 deletion(-) rename assets/js/{sync => sync-ui}/components/common/date-time.js (100%) rename assets/js/{sync => sync-ui}/components/common/message-log.js (100%) rename assets/js/{sync => sync-ui}/components/common/progress-bar.js (100%) rename assets/js/{sync => sync-ui}/components/icons/pause.js (100%) rename assets/js/{sync => sync-ui}/components/icons/play.js (100%) rename assets/js/{sync => sync-ui}/components/icons/stop.js (100%) rename assets/js/{sync => sync-ui}/components/icons/sync.js (100%) rename assets/js/{sync => sync-ui}/components/icons/thumbs-down.js (100%) rename assets/js/{sync => sync-ui}/components/icons/thumbs-up.js (100%) rename assets/js/{sync => sync-ui}/components/sync-page.js (100%) rename assets/js/{sync => sync-ui}/components/sync/controls.js (100%) rename assets/js/{sync => sync-ui}/components/sync/log.js (100%) rename assets/js/{sync => sync-ui}/components/sync/panel.js (100%) rename assets/js/{sync => sync-ui}/components/sync/progress.js (100%) rename assets/js/{sync => sync-ui}/components/sync/status.js (100%) rename assets/js/{sync => sync-ui}/config.js (100%) rename assets/js/{sync => sync-ui}/hooks.js (100%) rename assets/js/{sync => sync-ui}/index.js (100%) rename assets/js/{sync => sync-ui}/utilities.js (100%) diff --git a/assets/js/sync/components/common/date-time.js b/assets/js/sync-ui/components/common/date-time.js similarity index 100% rename from assets/js/sync/components/common/date-time.js rename to assets/js/sync-ui/components/common/date-time.js diff --git a/assets/js/sync/components/common/message-log.js b/assets/js/sync-ui/components/common/message-log.js similarity index 100% rename from assets/js/sync/components/common/message-log.js rename to assets/js/sync-ui/components/common/message-log.js diff --git a/assets/js/sync/components/common/progress-bar.js b/assets/js/sync-ui/components/common/progress-bar.js similarity index 100% rename from assets/js/sync/components/common/progress-bar.js rename to assets/js/sync-ui/components/common/progress-bar.js diff --git a/assets/js/sync/components/icons/pause.js b/assets/js/sync-ui/components/icons/pause.js similarity index 100% rename from assets/js/sync/components/icons/pause.js rename to assets/js/sync-ui/components/icons/pause.js diff --git a/assets/js/sync/components/icons/play.js b/assets/js/sync-ui/components/icons/play.js similarity index 100% rename from assets/js/sync/components/icons/play.js rename to assets/js/sync-ui/components/icons/play.js diff --git a/assets/js/sync/components/icons/stop.js b/assets/js/sync-ui/components/icons/stop.js similarity index 100% rename from assets/js/sync/components/icons/stop.js rename to assets/js/sync-ui/components/icons/stop.js diff --git a/assets/js/sync/components/icons/sync.js b/assets/js/sync-ui/components/icons/sync.js similarity index 100% rename from assets/js/sync/components/icons/sync.js rename to assets/js/sync-ui/components/icons/sync.js diff --git a/assets/js/sync/components/icons/thumbs-down.js b/assets/js/sync-ui/components/icons/thumbs-down.js similarity index 100% rename from assets/js/sync/components/icons/thumbs-down.js rename to assets/js/sync-ui/components/icons/thumbs-down.js diff --git a/assets/js/sync/components/icons/thumbs-up.js b/assets/js/sync-ui/components/icons/thumbs-up.js similarity index 100% rename from assets/js/sync/components/icons/thumbs-up.js rename to assets/js/sync-ui/components/icons/thumbs-up.js diff --git a/assets/js/sync/components/sync-page.js b/assets/js/sync-ui/components/sync-page.js similarity index 100% rename from assets/js/sync/components/sync-page.js rename to assets/js/sync-ui/components/sync-page.js diff --git a/assets/js/sync/components/sync/controls.js b/assets/js/sync-ui/components/sync/controls.js similarity index 100% rename from assets/js/sync/components/sync/controls.js rename to assets/js/sync-ui/components/sync/controls.js diff --git a/assets/js/sync/components/sync/log.js b/assets/js/sync-ui/components/sync/log.js similarity index 100% rename from assets/js/sync/components/sync/log.js rename to assets/js/sync-ui/components/sync/log.js diff --git a/assets/js/sync/components/sync/panel.js b/assets/js/sync-ui/components/sync/panel.js similarity index 100% rename from assets/js/sync/components/sync/panel.js rename to assets/js/sync-ui/components/sync/panel.js diff --git a/assets/js/sync/components/sync/progress.js b/assets/js/sync-ui/components/sync/progress.js similarity index 100% rename from assets/js/sync/components/sync/progress.js rename to assets/js/sync-ui/components/sync/progress.js diff --git a/assets/js/sync/components/sync/status.js b/assets/js/sync-ui/components/sync/status.js similarity index 100% rename from assets/js/sync/components/sync/status.js rename to assets/js/sync-ui/components/sync/status.js diff --git a/assets/js/sync/config.js b/assets/js/sync-ui/config.js similarity index 100% rename from assets/js/sync/config.js rename to assets/js/sync-ui/config.js diff --git a/assets/js/sync/hooks.js b/assets/js/sync-ui/hooks.js similarity index 100% rename from assets/js/sync/hooks.js rename to assets/js/sync-ui/hooks.js diff --git a/assets/js/sync/index.js b/assets/js/sync-ui/index.js similarity index 100% rename from assets/js/sync/index.js rename to assets/js/sync-ui/index.js diff --git a/assets/js/sync/utilities.js b/assets/js/sync-ui/utilities.js similarity index 100% rename from assets/js/sync/utilities.js rename to assets/js/sync-ui/utilities.js diff --git a/package.json b/package.json index 96ae7faf5c..c3c4ea949e 100644 --- a/package.json +++ b/package.json @@ -69,7 +69,7 @@ "facets-meta-range-block-view-script": "./assets/js/blocks/facets/meta-range/view.js", "related-posts-block-script": "./assets/js/blocks/related-posts/index.js", "settings-script": "./assets/js/settings.js", - "sync-script": "./assets/js/sync/index.js", + "sync-script": "./assets/js/sync-ui/index.js", "sites-admin-script": "./assets/js/sites-admin.js", "stats-script": "./assets/js/stats.js", "status-report-script": "./assets/js/status-report/index.js", From b07072d3d2e4f57eb5dd8d327bbc9028a831e99c Mon Sep 17 00:00:00 2001 From: Jacob Peattie Date: Tue, 5 Sep 2023 16:56:10 +1000 Subject: [PATCH 2/2] Abstract Sync page logic into a provider pattern. --- .../sync-page.js => apps/settings-page.js} | 112 +++- assets/js/sync-ui/components/sync/controls.js | 8 +- assets/js/sync-ui/components/sync/panel.js | 39 +- assets/js/sync-ui/index.js | 545 +---------------- assets/js/sync/index.js | 549 ++++++++++++++++++ assets/js/{sync-ui => sync/src}/hooks.js | 17 +- assets/js/{sync-ui => sync/src}/utilities.js | 0 7 files changed, 683 insertions(+), 587 deletions(-) rename assets/js/sync-ui/{components/sync-page.js => apps/settings-page.js} (56%) create mode 100644 assets/js/sync/index.js rename assets/js/{sync-ui => sync/src}/hooks.js (95%) rename assets/js/{sync-ui => sync/src}/utilities.js (100%) diff --git a/assets/js/sync-ui/components/sync-page.js b/assets/js/sync-ui/apps/settings-page.js similarity index 56% rename from assets/js/sync-ui/components/sync-page.js rename to assets/js/sync-ui/apps/settings-page.js index b13cb51efa..1c809dba94 100644 --- a/assets/js/sync-ui/components/sync-page.js +++ b/assets/js/sync-ui/apps/settings-page.js @@ -7,34 +7,82 @@ import { __, sprintf } from '@wordpress/i18n'; /** * Internal dependencies. */ -import SyncPanel from './sync/panel'; +import { useSync } from '../../sync'; +import SyncPanel from '../components/sync/panel'; /** * Sync page component. * - * @param {object} props Component props. - * @param {boolean} props.isCli If sync is a CLI sync. - * @param {boolean} props.isComplete If sync is complete. - * @param {boolean} props.isDeleting If sync is a delete and sync. - * @param {boolean} props.isEpio If ElasticPress is using ElasticPress.io. - * @param {boolean} props.isFailed If sync has failed. - * @param {boolean} props.isPaused If sync is paused. - * @param {boolean} props.isSyncing If sync is running. - * @param {number} props.itemsProcessed Number of items processed. - * @param {number} props.itemsTotal Number of items to process. - * @param {string} props.lastSyncDateTime Date and time of last sync in ISO-8601. - * @param {boolean} props.lastSyncFailed If the last sync had failures. - * @param {object[]} props.log Sync message log. - * @param {Function} props.onDelete Callback for clicking delete and sync. - * @param {Function} props.onPause Callback for clicking pause. - * @param {Function} props.onResume Callback for clicking resume. - * @param {Function} props.onStop Callback for clicking stop. - * @param {Function} props.onSync Callback for clicking sync. - * @param {string} props.syncStartDateTime Date and time of current sync in ISO 8601. * @returns {WPElement} Sync page component. */ -export default ({ isCli, isComplete, isDeleting, isEpio, isFailed, isSyncing, log, ...props }) => { - const isInitialSync = props.lastSyncDateTime === null; +export default () => { + const { + isCli, + isComplete, + isDeleting, + isEpio, + isFailed, + isSyncing, + lastSyncDateTime, + log, + logMessage, + pauseSync, + resumeSync, + startSync, + stopSync, + } = useSync(); + + const isInitialSync = lastSyncDateTime === null; + + /** + * Handle clicking delete and sync button. + * + * @returns {void} + */ + const onDelete = async () => { + startSync(true); + logMessage(__('Starting delete and sync…', 'elasticpress'), 'info'); + }; + + /** + * Handle clicking pause button. + * + * @returns {void} + */ + const onPause = () => { + pauseSync(); + logMessage(__('Pausing sync…', 'elasticpress'), 'info'); + }; + + /** + * Handle clicking play button. + * + * @returns {void} + */ + const onResume = () => { + resumeSync(); + logMessage(__('Resuming sync…', 'elasticpress'), 'info'); + }; + + /** + * Handle clicking stop button. + * + * @returns {void} + */ + const onStop = () => { + stopSync(); + logMessage(__('Sync stopped', 'elasticpress'), 'info'); + }; + + /** + * Handle clicking sync button. + * + * @returns {void} + */ + const onSync = async () => { + startSync(false); + logMessage(__('Starting sync…', 'elasticpress'), 'info'); + }; return ( <> @@ -58,15 +106,17 @@ export default ({ isCli, isComplete, isDeleting, isEpio, isFailed, isSyncing, lo 'elasticpress', ) } - isComplete={isComplete} isDisabled={(isSyncing && isDeleting) || (isSyncing && isCli)} - isFailed={isFailed} - isSyncing={isSyncing && !isDeleting} + isLoading={isSyncing && !isDeleting} logMessages={log.filter((m) => !m.isDeleting)} showLastSync showProgress={!isDeleting && (isSyncing || isComplete || isFailed)} showSync - {...props} + onDelete={onDelete} + onPause={onPause} + onResume={onResume} + onSync={onSync} + onStop={onStop} /> {!isInitialSync ? ( @@ -76,10 +126,8 @@ export default ({ isCli, isComplete, isDeleting, isEpio, isFailed, isSyncing, lo 'If you are still having issues with your search results, you may need to do a completely fresh sync.', 'elasticpress', )} - isComplete={isComplete} isDisabled={(isSyncing && !isDeleting) || (isSyncing && isCli)} - isFailed={isFailed} - isSyncing={isSyncing && isDeleting} + isLoading={isSyncing && isDeleting} logMessages={log.filter((m) => m.isDeleting)} showDelete showProgress={isDeleting && (isSyncing || isComplete || isFailed)} @@ -87,7 +135,11 @@ export default ({ isCli, isComplete, isDeleting, isEpio, isFailed, isSyncing, lo 'All indexed data on ElasticPress will be deleted without affecting anything on your WordPress website. This may take a few hours depending on the amount of content that needs to be synced and indexed. While this is happening, searches will use the default WordPress results', 'elasticpress', )} - {...props} + onDelete={onDelete} + onPause={onPause} + onResume={onResume} + onSync={onSync} + onStop={onStop} /> ) : null} diff --git a/assets/js/sync-ui/components/sync/controls.js b/assets/js/sync-ui/components/sync/controls.js index d32817447e..b4fc918bcb 100644 --- a/assets/js/sync-ui/components/sync/controls.js +++ b/assets/js/sync-ui/components/sync/controls.js @@ -18,8 +18,8 @@ import stop from '../icons/stop'; * * @param {object} props Component props. * @param {boolean} props.disabled If controls are disabled. + * @param {boolean} props.isLoading If syncing is in progress. * @param {boolean} props.isPaused If syncing is paused. - * @param {boolean} props.isSyncing If syncing is in progress. * @param {Function} props.onPause Pause button click callback. * @param {Function} props.onResume Play button click callback. * @param {Function} props.onStop Stop button click callback. @@ -27,13 +27,13 @@ import stop from '../icons/stop'; * @param {boolean} props.showSync If sync button is shown. * @returns {WPElement} Component. */ -export default ({ disabled, isPaused, isSyncing, onPause, onResume, onStop, onSync, showSync }) => { +export default ({ disabled, isLoading, isPaused, onPause, onResume, onStop, onSync, showSync }) => { /** * Render. */ return (
- {showSync && !isSyncing ? ( + {showSync && !isLoading ? (