|
63 | 63 | }, |
64 | 64 | { |
65 | 65 | text: params.modal_button_try_again, |
66 | | - class: 'button mailchimp-sf-button', |
| 66 | + class: 'button mailchimp-sf-button button-primary', |
67 | 67 | click() { |
68 | 68 | $(this).dialog('close'); |
69 | 69 | setConnectButtonLoading(); |
|
420 | 420 | const userSyncSettingsPage = $('.mailchimp-sf-user-sync-page'); |
421 | 421 | if (userSyncSettingsPage.length > 0) { |
422 | 422 | const syncExistingContactsOnly = $( |
423 | | - 'tr.mailchimp-user-sync-existing-contacts-only input[type="checkbox"]', |
| 423 | + 'input[type="checkbox"][name="mailchimp_sf_user_sync_settings[existing_contacts_only]"]', |
424 | 424 | ); |
425 | 425 | if (syncExistingContactsOnly) { |
426 | 426 | syncExistingContactsOnly.change(function () { |
427 | 427 | if (this.checked) { |
428 | | - $('tr.mailchimp-user-sync-subscriber-status').hide(); |
| 428 | + $('div.mailchimp-user-sync-subscriber-status').hide(); |
429 | 429 | } else { |
430 | | - $('tr.mailchimp-user-sync-subscriber-status').show(); |
| 430 | + $('div.mailchimp-user-sync-subscriber-status').show(); |
431 | 431 | } |
432 | 432 | }); |
433 | 433 |
|
|
491 | 491 | $('#mailchimp-sf-clear-user-sync-errors').on('click', function (e) { |
492 | 492 | e.preventDefault(); |
493 | 493 | $(this).prop('disabled', true); |
494 | | - $('.mailchimp-sf-user-sync-errors-header-actions .spinner').addClass('is-active'); |
| 494 | + $('.mailchimp-sf-user-sync-errors-footer-actions .spinner').addClass('is-active'); |
495 | 495 |
|
496 | 496 | $.ajax({ |
497 | 497 | url: params.ajax_url, |
|
504 | 504 | success(response) { |
505 | 505 | if (response && response.success) { |
506 | 506 | $(tableSelector + ' tbody').html(noErrorsFoundRow); |
507 | | - $('.mailchimp-sf-user-sync-errors-header-actions .spinner').removeClass( |
| 507 | + $('.mailchimp-sf-user-sync-errors-footer-actions .spinner').removeClass( |
508 | 508 | 'is-active', |
509 | 509 | ); |
510 | 510 | } else { |
|
554 | 554 | }); |
555 | 555 | }); |
556 | 556 | })(jQuery); // eslint-disable-line no-undef |
| 557 | + |
| 558 | +// Form settings. |
| 559 | +(function ($) { |
| 560 | + /** |
| 561 | + * Initialize form settings functionality |
| 562 | + */ |
| 563 | + function initFormSettings() { |
| 564 | + const $form = $('#mailchimp-sf-settings-form'); |
| 565 | + const $userSyncForm = $('.mailchimp-sf-user-sync-form'); |
| 566 | + const $submitButtons = $('input[type="submit"].mailchimp-sf-button-submit'); |
| 567 | + const params = window.mailchimp_sf_admin_params || {}; |
| 568 | + const ajaxUrl = params.ajax_url || ''; |
| 569 | + const ajaxNonce = params.preview_form_nonce || ''; |
| 570 | + |
| 571 | + // Initially hide all submit buttons |
| 572 | + $submitButtons.hide(); |
| 573 | + |
| 574 | + /** |
| 575 | + * Toggle submit buttons visibility based on form state |
| 576 | + * |
| 577 | + * @param {jQuery} $changedInput - The input that was changed |
| 578 | + */ |
| 579 | + function toggleSubmitButtons($changedInput) { |
| 580 | + $changedInput |
| 581 | + .closest('.mailchimp-sf-section') |
| 582 | + .find('.mailchimp-sf-button-submit') |
| 583 | + .show(); |
| 584 | + $changedInput |
| 585 | + .closest('.mailchimp-sf-section') |
| 586 | + .find('.mailchimp-sf-section-footer') |
| 587 | + .slideDown({ duration: 200 }); |
| 588 | + } |
| 589 | + |
| 590 | + function blockElement(elementSelector) { |
| 591 | + const $el = $(elementSelector); |
| 592 | + $el.append( |
| 593 | + '<div class="block-overlay" style="position:absolute;top:0;left:0;width:100%;height:100%;background:rgba(0,0,0,0.35);z-index:100;display:flex;justify-content:center;align-items:center;">' + |
| 594 | + '<div style="background:#fff;border-radius: 50%;"><span class="spinner is-active" style="margin: 0px;"></span></div>' + |
| 595 | + '</div>', |
| 596 | + ); |
| 597 | + } |
| 598 | + |
| 599 | + function unblockElement(elementSelector) { |
| 600 | + $(elementSelector + ' .block-overlay').remove(); |
| 601 | + } |
| 602 | + |
| 603 | + /** |
| 604 | + * Debounce function |
| 605 | + * |
| 606 | + * @param {Function} func - The function to debounce |
| 607 | + * @param {number} delay - The delay in milliseconds |
| 608 | + * @returns {Function} The debounced function |
| 609 | + */ |
| 610 | + function debounce(func, delay) { |
| 611 | + let timeout; |
| 612 | + return function (...args) { |
| 613 | + clearTimeout(timeout); |
| 614 | + timeout = setTimeout(() => { |
| 615 | + func.apply(this, args); |
| 616 | + }, delay); |
| 617 | + }; |
| 618 | + } |
| 619 | + |
| 620 | + /** |
| 621 | + * Preview the form. |
| 622 | + */ |
| 623 | + function previewForm() { |
| 624 | + const $previewer = $('.mailchimp-sf-form-preview'); |
| 625 | + if ($previewer.length === 0) { |
| 626 | + return; |
| 627 | + } |
| 628 | + |
| 629 | + // Loading overlay. |
| 630 | + blockElement('.mailchimp-sf-form-preview-content'); |
| 631 | + |
| 632 | + const fields = {}; |
| 633 | + const groups = {}; |
| 634 | + $form.find('input[name^="mc_mv_"]').each(function () { |
| 635 | + const $input = $(this); |
| 636 | + fields[$input.data('tag')] = $input.is(':checked'); |
| 637 | + }); |
| 638 | + $form.find('input[name^="mc_show_interest_groups_"]').each(function () { |
| 639 | + const $input = $(this); |
| 640 | + groups[$input.data('group-id')] = $input.is(':checked'); |
| 641 | + }); |
| 642 | + |
| 643 | + const previewData = { |
| 644 | + header: $('#mc_header_content').val(), |
| 645 | + sub_heading: $('#mc_subheader_content').val(), |
| 646 | + submit_text: $('#mc_submit_text').val(), |
| 647 | + fields, |
| 648 | + groups, |
| 649 | + display_unsub_link: $('#mc_use_unsub_link').is(':checked'), |
| 650 | + }; |
| 651 | + |
| 652 | + $.ajax({ |
| 653 | + url: ajaxUrl, |
| 654 | + type: 'POST', |
| 655 | + data: { |
| 656 | + action: 'mailchimp_sf_preview_form', |
| 657 | + nonce: ajaxNonce, |
| 658 | + preview_data: previewData, |
| 659 | + }, |
| 660 | + success(response) { |
| 661 | + if (response.success && response.data) { |
| 662 | + unblockElement('.mailchimp-sf-form-preview-content'); |
| 663 | + $previewer.html(response.data); |
| 664 | + } else { |
| 665 | + unblockElement('.mailchimp-sf-form-preview-content'); |
| 666 | + $previewer.html( |
| 667 | + '<div class="mailchimp-sf-form-preview-error">' + |
| 668 | + params.generic_error + |
| 669 | + '</div>', |
| 670 | + ); |
| 671 | + } |
| 672 | + }, |
| 673 | + error(jqXHR, textStatus, errorThrown) { |
| 674 | + // eslint-disable-next-line no-console |
| 675 | + console.error('Error: ', textStatus, ', Details: ', errorThrown); |
| 676 | + unblockElement('.mailchimp-sf-form-preview-content'); |
| 677 | + $previewer.html( |
| 678 | + '<div class="mailchimp-sf-form-preview-error">' + |
| 679 | + params.generic_error + |
| 680 | + '</div>', |
| 681 | + ); |
| 682 | + }, |
| 683 | + }); |
| 684 | + } |
| 685 | + |
| 686 | + const debouncedPreviewForm = debounce(previewForm, 300); |
| 687 | + |
| 688 | + // Watch for changes on all form elements |
| 689 | + $form.on('input change', 'input, textarea, select', function () { |
| 690 | + const $changedInput = $(this); |
| 691 | + toggleSubmitButtons($changedInput); |
| 692 | + debouncedPreviewForm(); |
| 693 | + }); |
| 694 | + |
| 695 | + // Watch for changes on user sync form elements |
| 696 | + $userSyncForm.on('input change', 'input, textarea, select', function () { |
| 697 | + const $changedInput = $(this); |
| 698 | + toggleSubmitButtons($changedInput); |
| 699 | + }); |
| 700 | + } |
| 701 | + |
| 702 | + // Initialize when document is ready |
| 703 | + $(document).ready(initFormSettings); |
| 704 | +})(jQuery); // eslint-disable-line no-undef |
0 commit comments