|
25 | 25 |
|
26 | 26 | const BuddySystemButton = { |
27 | 27 | config: {}, |
| 28 | + unlinkConfirmationModal: { |
| 29 | + signals: { |
| 30 | + show: '', |
| 31 | + close: '', |
| 32 | + } |
| 33 | + }, |
| 34 | + modal: null, |
| 35 | + setupConfirmationModal: false, |
28 | 36 |
|
29 | 37 | setConfig(config) { |
30 | 38 | this.config = config; |
|
40 | 48 |
|
41 | 49 | const triggerButton = e.target.closest(triggerSelector); |
42 | 50 | const container = triggerButton.closest(`.${this.config.bnt_class}`); |
43 | | - if (triggerButton.dataset.submitted === 'true') return Promise.resolve(); |
44 | | - |
45 | | - const values = new FormData(); |
46 | | - values.append('usr_id', container.dataset.buddyId); |
47 | | - values.append('action', triggerButton.dataset.action); |
48 | | - values.append(`cmd[${BuddySystem.config.transition_state_cmd}]`, 1); |
49 | | - |
50 | | - return disableButtons(container) |
51 | | - .then(() => fetch(BuddySystem.config.http_post_url, { |
52 | | - method: 'POST', |
53 | | - headers: { Accept: 'application/json' }, |
54 | | - body: values, |
55 | | - })) |
56 | | - .then((response) => { |
57 | | - if (!response.ok) throw new Error('Request failed'); |
58 | | - return response.json(); |
59 | | - }) |
60 | | - .then((data) => processResponse(container, data)) |
61 | | - .then(() => { |
62 | | - container.querySelector(toggleSelector)?.focus(); |
63 | | - }) |
64 | | - .catch((error) => { |
65 | | - console.error(error); |
66 | | - enableButtons(container); |
67 | | - container.querySelector(toggleSelector)?.focus(); |
| 51 | + |
| 52 | + const widgetClickAction = () => { |
| 53 | + if (triggerButton.dataset.submitted === 'true') return Promise.resolve(); |
| 54 | + |
| 55 | + const values = new FormData(); |
| 56 | + values.append('usr_id', container.dataset.buddyId); |
| 57 | + values.append('action', triggerButton.dataset.action); |
| 58 | + values.append(`cmd[${BuddySystem.config.transition_state_cmd}]`, 1); |
| 59 | + |
| 60 | + return disableButtons(container) |
| 61 | + .then(() => fetch(BuddySystem.config.http_post_url, { |
| 62 | + method: 'POST', |
| 63 | + headers: { Accept: 'application/json' }, |
| 64 | + body: values, |
| 65 | + })) |
| 66 | + .then((response) => { |
| 67 | + if (!response.ok) throw new Error('Request failed'); |
| 68 | + return response.json(); |
| 69 | + }) |
| 70 | + .then((data) => processResponse(container, data)) |
| 71 | + .then(() => { |
| 72 | + container.querySelector(toggleSelector)?.focus(); |
| 73 | + }) |
| 74 | + .catch((error) => { |
| 75 | + console.error(error); |
| 76 | + enableButtons(container); |
| 77 | + container.querySelector(toggleSelector)?.focus(); |
| 78 | + }); |
| 79 | + }; |
| 80 | + |
| 81 | + if (triggerButton.dataset.action === 'unlink') { |
| 82 | + return showUnlinkConfirmationModal().then(() => { |
| 83 | + return widgetClickAction(); |
68 | 84 | }); |
| 85 | + } |
| 86 | + |
| 87 | + return widgetClickAction(); |
69 | 88 | }; |
70 | 89 |
|
| 90 | + const showUnlinkConfirmationModal = () => new Promise((resolve) => { |
| 91 | + if (!this.setupConfirmationModal) { |
| 92 | + fetch(BuddySystem.config.async_get_unlink_modal_confirmation_html) |
| 93 | + .then((response) => { |
| 94 | + if (!response.ok) throw new Error('Request failed'); |
| 95 | + return response.json(); |
| 96 | + }) |
| 97 | + .then((data) => { |
| 98 | + const wrapper = document.createElement('div'); |
| 99 | + const modalFragment = document.createRange().createContextualFragment(data.html); |
| 100 | + |
| 101 | + wrapper.appendChild(modalFragment); |
| 102 | + document.body.append(wrapper) |
| 103 | + this.modal = wrapper.querySelector("dialog"); |
| 104 | + |
| 105 | + this.unlinkConfirmationModal.signals.show = data.signals.show; |
| 106 | + this.unlinkConfirmationModal.signals.close = data.signals.close |
| 107 | + |
| 108 | + this.modal.querySelector('input[type="submit"]').addEventListener( |
| 109 | + 'click', |
| 110 | + (event) => onModalSubmitClicked(event, resolve), |
| 111 | + {once: true} |
| 112 | + ); |
| 113 | + }).then(() => { |
| 114 | + this.setupConfirmationModal = true; |
| 115 | + global.jQuery(document).trigger(this.unlinkConfirmationModal.signals.show, {}); |
| 116 | + }) |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + const submitButton = this.modal.querySelector('input[type="submit"]'); |
| 121 | + submitButton.addEventListener( |
| 122 | + 'click', |
| 123 | + (event) => onModalSubmitClicked(event, resolve), |
| 124 | + {once: true} |
| 125 | + ); |
| 126 | + global.jQuery(document).trigger(this.unlinkConfirmationModal.signals.show, {}); |
| 127 | + }); |
| 128 | + |
| 129 | + const onModalSubmitClicked = (event, resolve) => { |
| 130 | + event.preventDefault(); |
| 131 | + global.jQuery(document).trigger(this.unlinkConfirmationModal.signals.close, {}); |
| 132 | + resolve(); |
| 133 | + } |
| 134 | + |
71 | 135 | const disableButtons = (container) => new Promise((resolve) => { |
72 | 136 | document.querySelectorAll(`.${this.config.bnt_class}`).forEach((btnContainer) => { |
73 | 137 | if (btnContainer.dataset.buddyId === container.dataset.buddyId) { |
|
0 commit comments