Skip to content

Commit

Permalink
Merge pull request #2524 from 10up/chore/apply-2491
Browse files Browse the repository at this point in the history
Display a notice that content will be reindexed when enabling a feature that requires it
  • Loading branch information
felipeelia authored Dec 15, 2021
2 parents 7f2790c + bf5d47a commit 78957c9
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 5 deletions.
5 changes: 5 additions & 0 deletions assets/css/dashboard.css
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,11 @@ h2.ep-list-features {
border-left: 4px solid var(--statusOk);
margin-bottom: 10px;
padding: 8px 12px;

&.requirements-status-notice--reindex {
border-color: var(--statusWarning);
display: none; /* Controlled by JavaScript. */
}
}

.ep-feature .settings .action-wrap {
Expand Down
40 changes: 39 additions & 1 deletion assets/js/dashboard.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* eslint-disable camelcase, no-use-before-define */
import { __ } from '@wordpress/i18n';

const { ajaxurl, epDash, history } = window;

const $features = jQuery(document.getElementsByClassName('ep-features'));
Expand Down Expand Up @@ -41,7 +43,7 @@ $features.on('click', '.save-settings', function (event) {
return;
}

const feature = event.target.getAttribute('data-feature');
const { feature, requiresReindex, wasActive } = event.target.dataset;
const $feature = $features.find(`.ep-feature-${feature}`);
const settings = {};
const $settings = $feature.find('.setting-field');
Expand All @@ -61,6 +63,22 @@ $features.on('click', '.save-settings', function (event) {
}
});

const requiresConfirmation = requiresReindex && wasActive === '0' && settings.active === '1';

if (requiresConfirmation) {
// eslint-disable-next-line no-alert
const isConfirmed = window.confirm(
__(
'Enabling this feature will begin re-indexing your content. Do you wish to proceed?',
'elasticpress',
),
);

if (!isConfirmed) {
return;
}
}

$feature.addClass('saving');

jQuery
Expand All @@ -84,6 +102,11 @@ $features.on('click', '.save-settings', function (event) {
$feature.removeClass('feature-active');
}

event.target.dataset.wasActive = settings.active;
event.target
.closest('.settings')
.querySelector('.js-toggle-feature').dataset.wasActive = settings.active;

if (response.data.reindex) {
syncStatus = 'initialsync';

Expand Down Expand Up @@ -573,3 +596,18 @@ $epCredentialsTab.on('click', (e) => {
$epCredentialsAdditionalFields.attr('aria-hidden', 'true');
}
});

$features.on('change', '.js-toggle-feature', function (event) {
const container = event.currentTarget
.closest('.settings')
.querySelector('.requirements-status-notice--reindex');

const { value } = event.target;
const { requiresReindex, wasActive } = event.currentTarget.dataset;

if (requiresReindex && wasActive === '0' && value === '1') {
container.style.display = 'block';
} else {
container.style.display = null;
}
});
2 changes: 1 addition & 1 deletion dist/css/dashboard-styles.min.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/js/dashboard-script.min.js

Large diffs are not rendered by default.

16 changes: 14 additions & 2 deletions includes/classes/Feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,16 @@ public function output_settings_box() {
<?php endforeach; ?>
<?php endif; ?>

<?php if ( $this->requires_install_reindex ) : ?>
<div class="requirements-status-notice requirements-status-notice--reindex" role="status">
<?php esc_html_e( 'Enabling this feature will require re-indexing your content.', 'elasticpress' ); ?>
</div>
<?php endif; ?>

<h3><?php esc_html_e( 'Settings', 'elasticpress' ); ?></h3>

<div class="feature-fields">
<div class="field js-toggle-feature" data-feature="<?php echo esc_attr( $this->slug ); ?>">
<div class="field js-toggle-feature" data-feature="<?php echo esc_attr( $this->slug ); ?>" data-requires-reindex="<?php echo $this->requires_install_reindex ? '1' : '0'; ?>" data-was-active="<?php echo $this->is_active() ? '1' : '0'; ?>">
<div class="field-name status"><?php esc_html_e( 'Status', 'elasticpress' ); ?></div>
<div class="input-wrap <?php if ( 2 === $requirements_status->code ) : ?>disabled<?php endif; ?>">
<label for="feature_active_<?php echo esc_attr( $this->slug ); ?>_enabled"><input name="feature_active_<?php echo esc_attr( $this->slug ); ?>" id="feature_active_<?php echo esc_attr( $this->slug ); ?>_enabled" data-field-name="active" class="setting-field" <?php if ( 2 === $requirements_status->code ) : ?>disabled<?php endif; ?> type="radio" <?php if ( $this->is_active() ) : ?>checked<?php endif; ?> value="1"><?php esc_html_e( 'Enabled', 'elasticpress' ); ?></label><br>
Expand All @@ -288,7 +294,13 @@ public function output_settings_box() {
<?php esc_html_e( 'Setting adjustments to this feature require a re-sync. Use WP-CLI.', 'elasticpress' ); ?>
</span>

<a data-feature="<?php echo esc_attr( $this->slug ); ?>" class="<?php if ( 2 === $requirements_status->code || ( $this->requires_install_reindex && defined( 'EP_DASHBOARD_SYNC' ) && ! EP_DASHBOARD_SYNC ) ) : ?>disabled<?php endif; ?> button button-primary save-settings"><?php esc_html_e( 'Save', 'elasticpress' ); ?></a>
<a
data-feature="<?php echo esc_attr( $this->slug ); ?>"
data-requires-reindex="<?php echo $this->requires_install_reindex ? '1' : '0'; ?>"
data-was-active="<?php echo $this->is_active() ? '1' : '0'; ?>"
class="<?php if ( 2 === $requirements_status->code || ( $this->requires_install_reindex && defined( 'EP_DASHBOARD_SYNC' ) && ! EP_DASHBOARD_SYNC ) ) : ?>disabled<?php endif; ?> button button-primary save-settings">
<?php esc_html_e( 'Save', 'elasticpress' ); ?>
</a>
</div>
<?php
}
Expand Down

0 comments on commit 78957c9

Please sign in to comment.