form-submission.js
3 months ago
frontend-picker.js
1 week ago
item-select.js
2 months ago
listing.css
3 months ago
listing.js
3 months ago
quick-edit.js
3 months ago
table.css
3 months ago
form-submission.js
202 lines
| 1 | import jQuery from 'jquery'; |
| 2 | import apiFetch from '@wordpress/api-fetch'; |
| 3 | |
| 4 | /** |
| 5 | * Disable inputs on a form |
| 6 | * |
| 7 | * @param {Node} form the form. |
| 8 | * @param {boolean} disabled disable inputs if `true`. |
| 9 | */ |
| 10 | function disable(form, disabled) { |
| 11 | if ('undefined' === typeof disabled) { |
| 12 | disabled = true; |
| 13 | } |
| 14 | |
| 15 | jQuery(form) |
| 16 | .find('select,input,textarea') |
| 17 | .add( |
| 18 | `.submit-placement-form[data-id="${form.id.replace( |
| 19 | 'advanced-ads-placement-form-', |
| 20 | '' |
| 21 | )}"]` |
| 22 | ) |
| 23 | .prop('disabled', disabled); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Submit edit placement form |
| 28 | * |
| 29 | * @param {Node} form the form to be submitted. |
| 30 | */ |
| 31 | function submitEditPlacement(form) { |
| 32 | const $form = jQuery(form), |
| 33 | formData = $form.serialize(); |
| 34 | disable(form); |
| 35 | apiFetch({ |
| 36 | path: '/advanced-ads/v1/placement', |
| 37 | method: 'PUT', |
| 38 | data: { |
| 39 | fields: formData, |
| 40 | }, |
| 41 | }).then(function (response) { |
| 42 | disable(form, false); |
| 43 | |
| 44 | if (response.error) { |
| 45 | // Show an error message if there is a "error" field in the response |
| 46 | disable(form, false); |
| 47 | form.closest('dialog').close(); |
| 48 | window.advancedAds.notifications.addError(response.error); |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | const dialog = form.closest('dialog'); |
| 53 | dialog.advadsTermination.resetInitialValues(); |
| 54 | const rowTitle = jQuery( |
| 55 | `#post-${response.placement_data.id} .column-name .row-title` |
| 56 | ); |
| 57 | rowTitle.text(response.title); |
| 58 | jQuery( |
| 59 | `#post-${response.placement_data.id} .column-ad_group .advads-placement-item-select` |
| 60 | ).val(response.item); |
| 61 | |
| 62 | if ( |
| 63 | response.payload.post_status && |
| 64 | 'draft' === response.payload.post_status |
| 65 | ) { |
| 66 | const rowParent = rowTitle.parent(); |
| 67 | if (!rowParent.text().includes(advancedAds.i18n.placements.draft)) { |
| 68 | rowTitle |
| 69 | .parent() |
| 70 | .append( |
| 71 | jQuery( |
| 72 | `<strong>— <span class="post-state">${advancedAds.i18n.placements.draft}</span></strong>` |
| 73 | ) |
| 74 | ); |
| 75 | } |
| 76 | } else { |
| 77 | rowTitle.siblings().remove(); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Allow add-on to update the table without refreshing the page if needed. |
| 82 | */ |
| 83 | wp.hooks.doAction('advanced-ads-placement-updated', response); |
| 84 | |
| 85 | if (response.reload) { |
| 86 | // Reload the page if needed. |
| 87 | // eslint-disable-next-line no-undef |
| 88 | localStorage.setItem( |
| 89 | 'advadsUpdateMessage', |
| 90 | JSON.stringify({ |
| 91 | type: 'success', |
| 92 | message: advancedAds.i18n.placements.updated, |
| 93 | }) |
| 94 | ); |
| 95 | window.location.reload(); |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | window.advancedAds.notifications.addSuccess( |
| 100 | advancedAds.i18n.placements.updated |
| 101 | ); |
| 102 | |
| 103 | dialog.close(); |
| 104 | }); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Submit create placement form |
| 109 | * |
| 110 | * @param {Node} form the form. |
| 111 | */ |
| 112 | function submitNewPlacement(form) { |
| 113 | const dialog = form.closest('dialog'); |
| 114 | |
| 115 | if ('function' === typeof window[dialog.closeValidation.function]) { |
| 116 | const validForm = window[dialog.closeValidation.function]( |
| 117 | dialog.closeValidation.modal_id |
| 118 | ); |
| 119 | if (!validForm) { |
| 120 | return; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | const formData = jQuery(form).serialize(); |
| 125 | disable(form); |
| 126 | apiFetch({ |
| 127 | path: '/advanced-ads/v1/placement', |
| 128 | method: 'POST', |
| 129 | data: { |
| 130 | fields: formData, |
| 131 | }, |
| 132 | }).then(function (response) { |
| 133 | disable(form, false); |
| 134 | if (response.redirectUrl) { |
| 135 | window.location.href = response.redirectUrl; |
| 136 | } else if (response.reload) { |
| 137 | // Reload the page if needed. |
| 138 | // eslint-disable-next-line no-undef |
| 139 | localStorage.setItem( |
| 140 | 'advadsUpdateMessage', |
| 141 | JSON.stringify({ |
| 142 | type: 'success', |
| 143 | message: advancedAds.i18n.placements.created, |
| 144 | }) |
| 145 | ); |
| 146 | window.location.reload(); |
| 147 | } |
| 148 | }); |
| 149 | } |
| 150 | |
| 151 | // Submit edit placement form |
| 152 | jQuery(document).on('click', '.submit-placement-edit', function () { |
| 153 | submitEditPlacement( |
| 154 | jQuery(`#advanced-ads-placement-form-${this.dataset.id}`)[0] |
| 155 | ); |
| 156 | }); |
| 157 | |
| 158 | // Submit new placement form |
| 159 | jQuery(document).on('click', '#submit-new-placement', function () { |
| 160 | submitNewPlacement(jQuery('#advads-placements-new-form')[0]); |
| 161 | }); |
| 162 | |
| 163 | export default function () { |
| 164 | // Stop normal new placement form submission. |
| 165 | wp.hooks.addFilter( |
| 166 | 'advanced-ads-submit-modal-form', |
| 167 | 'advancedAds', |
| 168 | function (send, form) { |
| 169 | if ('advads-placements-new-form' === form.id) { |
| 170 | submitNewPlacement(form); |
| 171 | return false; |
| 172 | } |
| 173 | return send; |
| 174 | } |
| 175 | ); |
| 176 | |
| 177 | // Stop normal edit placement form submission. |
| 178 | wp.hooks.addFilter( |
| 179 | 'advanced-ads-submit-modal-form', |
| 180 | 'advancedAds', |
| 181 | function (send, form) { |
| 182 | if (0 === form.id.indexOf('advanced-ads-placement-form-')) { |
| 183 | submitEditPlacement(form); |
| 184 | return false; |
| 185 | } |
| 186 | return send; |
| 187 | } |
| 188 | ); |
| 189 | |
| 190 | // Place our custom "Save and close" button to edit forms. |
| 191 | jQuery('[id^="advanced-ads-placement-form-"]').each(function () { |
| 192 | const id = this.id.replace('advanced-ads-placement-form-', ''); |
| 193 | jQuery(`#modal-placement-edit-${id}`).find('.advads-modal-footer').html( |
| 194 | `<button class="button button-primary submit-placement-edit" data-id="${id}">${advancedAds.i18n.placements.closeSave}</button>` // eslint-disable-line no-undef |
| 195 | ); |
| 196 | }); |
| 197 | |
| 198 | jQuery('#modal-placement-new').find('.advads-modal-footer').html( |
| 199 | `<button class="button button-primary" id="submit-new-placement">${advancedAds.i18n.placements.saveNew}</button>` // eslint-disable-line no-undef |
| 200 | ); |
| 201 | } |
| 202 |