|
| 1 | +/* global prplSuggestedTask, progressPlannerAjaxRequest, progressPlanner */ |
| 2 | + |
| 3 | +/* |
| 4 | + * Core Blog Description recommendation. |
| 5 | + * |
| 6 | + * Dependencies: wp-api, progress-planner/suggested-task, progress-planner/web-components/prpl-interactive-task, progress-planner/ajax-request |
| 7 | + */ |
| 8 | + |
| 9 | +// eslint-disable-next-line no-unused-vars |
| 10 | +const prplInteractiveTaskFormListener = { |
| 11 | + /** |
| 12 | + * Add a form listener to an interactive task form. |
| 13 | + * |
| 14 | + * @param {Object} options - The options for the interactive task form listener. |
| 15 | + * @param {string} options.settingAPIKey - The API key for the setting. |
| 16 | + * @param {string} options.setting - The setting to update. |
| 17 | + * @param {string} options.taskId - The ID of the task. |
| 18 | + * @param {string} options.popoverId - The ID of the popover. |
| 19 | + * @param {Function} options.settingCallbackValue - The callback function to get the value of the setting. |
| 20 | + */ |
| 21 | + siteSettings: ( { |
| 22 | + settingAPIKey, |
| 23 | + setting, |
| 24 | + taskId, |
| 25 | + popoverId, |
| 26 | + settingCallbackValue = ( value ) => value, |
| 27 | + } = {} ) => { |
| 28 | + const formElement = document.querySelector( `#${ popoverId } form` ); |
| 29 | + |
| 30 | + if ( ! formElement ) { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + // Add a form listener to the form. |
| 35 | + formElement.addEventListener( 'submit', ( event ) => { |
| 36 | + event.preventDefault(); |
| 37 | + |
| 38 | + // Get the form data. |
| 39 | + const formData = new FormData( formElement ); |
| 40 | + const settingsToPass = {}; |
| 41 | + settingsToPass[ settingAPIKey ] = settingCallbackValue( |
| 42 | + formData.get( setting ) |
| 43 | + ); |
| 44 | + |
| 45 | + const taskEl = document.querySelector( |
| 46 | + `.prpl-suggested-task[data-task-id="${ taskId }"]` |
| 47 | + ); |
| 48 | + |
| 49 | + // Update the blog description. |
| 50 | + wp.api.loadPromise.done( () => { |
| 51 | + const settings = new wp.api.models.Settings( settingsToPass ); |
| 52 | + |
| 53 | + settings.save().then( () => { |
| 54 | + // Close popover. |
| 55 | + document.getElementById( popoverId ).hidePopover(); |
| 56 | + const postId = parseInt( taskEl.dataset.postId ); |
| 57 | + if ( ! postId ) { |
| 58 | + return; |
| 59 | + } |
| 60 | + prplSuggestedTask.maybeComplete( postId ); |
| 61 | + taskEl.setAttribute( 'data-task-action', 'celebrate' ); |
| 62 | + document.dispatchEvent( |
| 63 | + new CustomEvent( 'prpl/celebrateTasks', { |
| 64 | + detail: { |
| 65 | + element: taskEl, |
| 66 | + }, |
| 67 | + } ) |
| 68 | + ); |
| 69 | + } ); |
| 70 | + } ); |
| 71 | + } ); |
| 72 | + }, |
| 73 | + |
| 74 | + customSubmit: ( { taskId, popoverId, callback = () => {} } = {} ) => { |
| 75 | + const formElement = document.querySelector( `#${ popoverId } form` ); |
| 76 | + |
| 77 | + if ( ! formElement ) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + // Add a form listener to the form. |
| 82 | + formElement.addEventListener( 'submit', ( event ) => { |
| 83 | + event.preventDefault(); |
| 84 | + |
| 85 | + callback(); |
| 86 | + |
| 87 | + const taskEl = document.querySelector( |
| 88 | + `.prpl-suggested-task[data-task-id="${ taskId }"]` |
| 89 | + ); |
| 90 | + |
| 91 | + // Close popover. |
| 92 | + document.getElementById( popoverId ).hidePopover(); |
| 93 | + const postId = parseInt( taskEl.dataset.postId ); |
| 94 | + if ( ! postId ) { |
| 95 | + return; |
| 96 | + } |
| 97 | + prplSuggestedTask.maybeComplete( postId ); |
| 98 | + taskEl.setAttribute( 'data-task-action', 'celebrate' ); |
| 99 | + document.dispatchEvent( |
| 100 | + new CustomEvent( 'prpl/celebrateTasks', { |
| 101 | + detail: { |
| 102 | + element: taskEl, |
| 103 | + }, |
| 104 | + } ) |
| 105 | + ); |
| 106 | + } ); |
| 107 | + }, |
| 108 | + |
| 109 | + settings: ( { |
| 110 | + taskId, |
| 111 | + setting, |
| 112 | + value, |
| 113 | + settingPath, |
| 114 | + popoverId, |
| 115 | + settingCallbackValue = ( settingValue ) => settingValue, |
| 116 | + } = {} ) => { |
| 117 | + const formElement = document.querySelector( `#${ popoverId } form` ); |
| 118 | + |
| 119 | + if ( ! formElement ) { |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + formElement.addEventListener( 'submit', ( event ) => { |
| 124 | + event.preventDefault(); |
| 125 | + |
| 126 | + const formData = new FormData( formElement ); |
| 127 | + const settingsToPass = {}; |
| 128 | + settingsToPass[ setting ] = settingCallbackValue( |
| 129 | + formData.get( setting ) |
| 130 | + ); |
| 131 | + |
| 132 | + progressPlannerAjaxRequest( { |
| 133 | + url: progressPlanner.ajaxUrl, |
| 134 | + data: { |
| 135 | + action: 'prpl_interactive_task_submit', |
| 136 | + _ajax_nonce: progressPlanner.nonce, |
| 137 | + post_id: taskId, |
| 138 | + setting, |
| 139 | + value, |
| 140 | + setting_path: settingPath, |
| 141 | + }, |
| 142 | + } ).then( () => { |
| 143 | + const taskEl = document.querySelector( |
| 144 | + `.prpl-suggested-task[data-task-id="${ taskId }"]` |
| 145 | + ); |
| 146 | + |
| 147 | + if ( ! taskEl ) { |
| 148 | + return; |
| 149 | + } |
| 150 | + |
| 151 | + // Close popover. |
| 152 | + document.getElementById( popoverId ).hidePopover(); |
| 153 | + const postId = parseInt( taskEl.dataset.postId ); |
| 154 | + if ( ! postId ) { |
| 155 | + return; |
| 156 | + } |
| 157 | + prplSuggestedTask.maybeComplete( postId ); |
| 158 | + taskEl.setAttribute( 'data-task-action', 'celebrate' ); |
| 159 | + document.dispatchEvent( |
| 160 | + new CustomEvent( 'prpl/celebrateTasks', { |
| 161 | + detail: { |
| 162 | + element: taskEl, |
| 163 | + }, |
| 164 | + } ) |
| 165 | + ); |
| 166 | + } ); |
| 167 | + } ); |
| 168 | + }, |
| 169 | +}; |
0 commit comments