| @@ -1,0 +1,84 @@ | ||
| 1 | +/** | |
| 2 | + * Callback used to post a survey form after clicking | |
| 3 | + * a button contained within a RSS feed. | |
| 4 | + * | |
| 5 | + * @param mixed button The button element. | |
| 6 | + * | |
| 7 | + * @return boolean | |
| 8 | + */ | |
| 9 | +function vboRssSubmitSurvey(button) { | |
| 10 | + // recover closest modal | |
| 11 | + var modal = jQuery(button).closest('.modal[id^="jmodal"]'); | |
| 12 | + | |
| 13 | + if (!modal.length) { | |
| 14 | + // abort, modal not found | |
| 15 | + return false; | |
| 16 | + } | |
| 17 | + | |
| 18 | + // take form inside the modal | |
| 19 | + var form = modal.find('form'); | |
| 20 | + | |
| 21 | + if (!form.length) { | |
| 22 | + // abort, no specified forms | |
| 23 | + return false; | |
| 24 | + } | |
| 25 | + | |
| 26 | + // retrieve feed ID from modal data | |
| 27 | + var feedId = modal.attr('data-feed-id'); | |
| 28 | + var submitDate; | |
| 29 | + | |
| 30 | + if (typeof localStorage !== 'undefined') { | |
| 31 | + // get submission date of the survey, if any | |
| 32 | + submitDate = localStorage.getItem('vikbooking.rss.survey.' + feedId); | |
| 33 | + } | |
| 34 | + | |
| 35 | + // disable button to avoid double submit | |
| 36 | + jQuery(button).prop('disabled', true); | |
| 37 | + | |
| 38 | + // extract title from modal | |
| 39 | + var subject = jQuery(modal).find('.modal-header h3').text().trim(); | |
| 40 | + | |
| 41 | + // serialize form to array | |
| 42 | + var data = form.serializeArray(); | |
| 43 | + // push subject within form | |
| 44 | + data.push({name: 'subject', value: subject}); | |
| 45 | + | |
| 46 | + // create request promise | |
| 47 | + new Promise((resolve, reject) => { | |
| 48 | + // check whether the feed ID has been already submitted | |
| 49 | + if (submitDate) { | |
| 50 | + reject('Survey already submitted on ' + submitDate); | |
| 51 | + return false; | |
| 52 | + } | |
| 53 | + | |
| 54 | + // make self AJAX request to post survey | |
| 55 | + doAjax( | |
| 56 | + 'admin-ajax.php?action=vikbooking&task=feedback.survey', | |
| 57 | + jQuery.param(data), | |
| 58 | + function(resp) { | |
| 59 | + resolve(resp); | |
| 60 | + }, | |
| 61 | + function(err) { | |
| 62 | + reject(err); | |
| 63 | + } | |
| 64 | + ); | |
| 65 | + }).then((data) => { | |
| 66 | + if (typeof localStorage !== 'undefined') { | |
| 67 | + // register survey within the pool to avoid several submissions | |
| 68 | + localStorage.setItem('vikbooking.rss.survey.' + feedId, new Date().toUTCString()); | |
| 69 | + } | |
| 70 | + }).catch((error) => { | |
| 71 | + console.error(error); | |
| 72 | + }).finally(() => { | |
| 73 | + // look for a button to auto-dismiss the modal | |
| 74 | + var closeBtn = modal.find('#rss-feed-dismiss'); | |
| 75 | + | |
| 76 | + if (closeBtn.length) { | |
| 77 | + // trigger click to dismiss the modal | |
| 78 | + closeBtn.trigger('click'); | |
| 79 | + } else { | |
| 80 | + // otherwise manually close the modal | |
| 81 | + wpCloseJModal(modal.attr('id')); | |
| 82 | + } | |
| 83 | + }); | |
| 84 | +} | |