|
| 1 | +/* global prplInteractiveTaskFormListener, prplDocumentReady, progressPlanner */ |
| 2 | + |
| 3 | +/* |
| 4 | + * Set the site date format. |
| 5 | + * |
| 6 | + * Dependencies: progress-planner/recommendations/interactive-task |
| 7 | + */ |
| 8 | + |
| 9 | +prplInteractiveTaskFormListener.customSubmit( { |
| 10 | + taskId: 'set-date-format', |
| 11 | + popoverId: 'prpl-popover-set-date-format', |
| 12 | + callback: () => { |
| 13 | + const format = document.querySelector( |
| 14 | + '#prpl-popover-set-date-format input[name="date_format"]:checked' |
| 15 | + ); |
| 16 | + const customFormat = document.querySelector( |
| 17 | + '#prpl-popover-set-date-format input[name="date_format_custom"]' |
| 18 | + ); |
| 19 | + |
| 20 | + fetch( progressPlanner.ajaxUrl, { |
| 21 | + method: 'POST', |
| 22 | + headers: { |
| 23 | + 'Content-Type': 'application/x-www-form-urlencoded', |
| 24 | + }, |
| 25 | + body: new URLSearchParams( { |
| 26 | + action: 'prpl_interactive_task_submit_set-date-format', |
| 27 | + nonce: progressPlanner.nonce, |
| 28 | + date_format: format.value, |
| 29 | + date_format_custom: customFormat.value, |
| 30 | + } ), |
| 31 | + } ); |
| 32 | + }, |
| 33 | +} ); |
| 34 | + |
| 35 | +prplDocumentReady( () => { |
| 36 | + // Handle date format radio button clicks |
| 37 | + document |
| 38 | + .querySelectorAll( |
| 39 | + '#prpl-popover-set-date-format input[name="date_format"]' |
| 40 | + ) |
| 41 | + .forEach( function ( input ) { |
| 42 | + input.addEventListener( 'click', function () { |
| 43 | + if ( 'date_format_custom_radio' !== this.id ) { |
| 44 | + const customInput = document.querySelector( |
| 45 | + '#prpl-popover-set-date-format input[name="date_format_custom"]' |
| 46 | + ); |
| 47 | + const fieldset = customInput.closest( 'fieldset' ); |
| 48 | + const exampleElement = fieldset.querySelector( '.example' ); |
| 49 | + const formatText = |
| 50 | + this.parentElement.querySelector( |
| 51 | + '.format-i18n' |
| 52 | + ).textContent; |
| 53 | + |
| 54 | + customInput.value = this.value; |
| 55 | + exampleElement.textContent = formatText; |
| 56 | + } |
| 57 | + } ); |
| 58 | + } ); |
| 59 | + |
| 60 | + // Handle custom date format input |
| 61 | + const customDateInput = document.querySelector( |
| 62 | + 'input[name="date_format_custom"]' |
| 63 | + ); |
| 64 | + |
| 65 | + customDateInput.addEventListener( 'click', function () { |
| 66 | + document.getElementById( 'date_format_custom_radio' ).checked = true; |
| 67 | + } ); |
| 68 | + |
| 69 | + customDateInput.addEventListener( 'input', function () { |
| 70 | + document.getElementById( 'date_format_custom_radio' ).checked = true; |
| 71 | + |
| 72 | + const format = this; |
| 73 | + const fieldset = format.closest( 'fieldset' ); |
| 74 | + const example = fieldset.querySelector( '.example' ); |
| 75 | + |
| 76 | + // Debounce the event callback while users are typing. |
| 77 | + clearTimeout( format.dataset.timer ); |
| 78 | + format.dataset.timer = setTimeout( function () { |
| 79 | + // If custom date is not empty. |
| 80 | + if ( format.value ) { |
| 81 | + // Find the spinner element within the fieldset |
| 82 | + const spinner = fieldset.querySelector( '.spinner' ); |
| 83 | + if ( spinner ) { |
| 84 | + spinner.classList.add( 'is-active' ); |
| 85 | + } |
| 86 | + |
| 87 | + // Use fetch instead of $.post |
| 88 | + fetch( progressPlanner.ajaxUrl, { |
| 89 | + method: 'POST', |
| 90 | + headers: { |
| 91 | + 'Content-Type': 'application/x-www-form-urlencoded', |
| 92 | + }, |
| 93 | + body: new URLSearchParams( { |
| 94 | + action: 'date_format', |
| 95 | + date: format.value, |
| 96 | + } ), |
| 97 | + } ) |
| 98 | + .then( function ( response ) { |
| 99 | + return response.text(); |
| 100 | + } ) |
| 101 | + .then( function ( data ) { |
| 102 | + example.textContent = data; |
| 103 | + } ) |
| 104 | + .catch( function ( error ) { |
| 105 | + console.error( 'Error:', error ); |
| 106 | + } ) |
| 107 | + .finally( function () { |
| 108 | + if ( spinner ) { |
| 109 | + spinner.classList.remove( 'is-active' ); |
| 110 | + } |
| 111 | + } ); |
| 112 | + } |
| 113 | + }, 500 ); |
| 114 | + } ); |
| 115 | +} ); |
0 commit comments