campaign_builder_controller.js
2 years ago
chart_controller.js
1 year ago
chart_geo_controller.js
2 years ago
chart_interval_controller.js
2 years ago
clipboard_controller.js
2 years ago
copy_report_controller.js
2 years ago
create_report_controller.js
2 years ago
delete_data_controller.js
2 years ago
delete_report_controller.js
2 years ago
easepick_controller.js
2 years ago
export_reports_controller.js
2 years ago
filters_controller.js
1 year ago
group_controller.js
2 years ago
import_reports_controller.js
2 years ago
migration_redirect_controller.js
2 years ago
modal_controller.js
2 years ago
plugin_group_options_controller.js
1 year ago
pruner_controller.js
1 year ago
quick_stats_controller.js
2 years ago
real_time_controller.js
2 years ago
rename_report_controller.js
2 years ago
report_controller.js
1 year ago
reset_analytics_controller.js
2 years ago
save_report_controller.js
2 years ago
select_input_controller.js
2 years ago
set_favorite_report_controller.js
2 years ago
sort_controller.js
2 years ago
sortable_reports_controller.js
2 years ago
table_columns_controller.js
2 years ago
woocommerce_settings_controller.js
1 year ago
pruner_controller.js
98 lines
| 1 | import {Controller} from "@hotwired/stimulus" |
| 2 | import MicroModal from "micromodal" |
| 3 | |
| 4 | export default class extends Controller { |
| 5 | static targets = ['cutoffs', 'saveButton', 'confirmButton', 'statusMessage', 'confirmationText'] |
| 6 | |
| 7 | isModalActuallyOpen = false |
| 8 | |
| 9 | saveClick() { |
| 10 | this.actuallySave(false) |
| 11 | } |
| 12 | |
| 13 | async confirmClick() { |
| 14 | this.confirmButtonTarget.setAttribute('disabled', 'disabled') |
| 15 | this.confirmButtonTarget.innerText = this.confirmButtonTarget.dataset.loadingText |
| 16 | |
| 17 | await this.actuallySave(true) |
| 18 | |
| 19 | this.confirmButtonTarget.removeAttribute('disabled') |
| 20 | this.confirmButtonTarget.innerText = this.confirmButtonTarget.dataset.originalText |
| 21 | |
| 22 | this.hideConfirmationModal() |
| 23 | } |
| 24 | |
| 25 | selectChanged() { |
| 26 | this.saveButtonTarget.removeAttribute('disabled') |
| 27 | } |
| 28 | |
| 29 | cancelConfirmation() { |
| 30 | this.hideConfirmationModal() |
| 31 | this.saveButtonTarget.removeAttribute('disabled') |
| 32 | this.saveButtonTarget.innerText = this.saveButtonTarget.dataset.originalText |
| 33 | } |
| 34 | |
| 35 | async actuallySave(isConfirmed = false) { |
| 36 | this.saveButtonTarget.setAttribute('disabled', 'disabled') |
| 37 | this.saveButtonTarget.innerText = this.saveButtonTarget.dataset.loadingText |
| 38 | |
| 39 | const response = await this.sendRequest({ |
| 40 | pruningCutoff: this.cutoffsTarget.value, |
| 41 | isConfirmed: isConfirmed |
| 42 | }) |
| 43 | |
| 44 | if (!response.wasSuccessful && response.confirmationText) { |
| 45 | this.showConfirmationModal(response.confirmationText) |
| 46 | } else { |
| 47 | this.statusMessageTarget.classList.toggle('is-scheduled', response.isEnabled) |
| 48 | this.statusMessageTarget.querySelector('p').innerHTML = response.statusMessage |
| 49 | |
| 50 | this.saveButtonTarget.removeAttribute('disabled') |
| 51 | this.saveButtonTarget.innerText = this.saveButtonTarget.dataset.originalText |
| 52 | this.saveButtonTarget.setAttribute('disabled', 'disabled'); |
| 53 | |
| 54 | this.hideConfirmationModal() |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | async sendRequest({ |
| 59 | pruningCutoff, |
| 60 | isConfirmed |
| 61 | }) { |
| 62 | const data = { |
| 63 | ...iawpActions.configure_pruner, |
| 64 | pruningCutoff: pruningCutoff, |
| 65 | isConfirmed: isConfirmed, |
| 66 | } |
| 67 | const response = await jQuery.post(ajaxurl, data) |
| 68 | |
| 69 | return { |
| 70 | wasSuccessful: response.success, |
| 71 | confirmationText: response.data.confirmationText, |
| 72 | isEnabled: response.data.isEnabled, |
| 73 | statusMessage: response.data.statusMessage, |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | showConfirmationModal = (confirmationText) => { |
| 78 | this.confirmationTextTarget.innerText = confirmationText |
| 79 | this.isModalActuallyOpen = true |
| 80 | MicroModal.show("prune-modal", { |
| 81 | onClose: () => { |
| 82 | this.cancelConfirmation() |
| 83 | } |
| 84 | }) |
| 85 | } |
| 86 | |
| 87 | hideConfirmationModal = (e) => { |
| 88 | if (e && e.target !== e.currentTarget) { |
| 89 | return |
| 90 | } |
| 91 | |
| 92 | if (this.isModalActuallyOpen) { |
| 93 | this.isModalActuallyOpen = false |
| 94 | MicroModal.close("prune-modal") |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 |