|
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(); |
68 | | - }); |
| 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(() => widgetClickAction()); |
| 83 | + } |
| 84 | + |
| 85 | + return widgetClickAction(); |
| 86 | + }; |
| 87 | + |
| 88 | + const showUnlinkConfirmationModal = () => new Promise((resolve) => { |
| 89 | + if (!this.setupConfirmationModal) { |
| 90 | + fetch(BuddySystem.config.async_get_unlink_modal_confirmation_html) |
| 91 | + .then((response) => { |
| 92 | + if (!response.ok) throw new Error('Request failed'); |
| 93 | + return response.json(); |
| 94 | + }) |
| 95 | + .then((data) => { |
| 96 | + const wrapper = document.createElement('div'); |
| 97 | + const modalFragment = document.createRange().createContextualFragment(data.html); |
| 98 | + |
| 99 | + wrapper.appendChild(modalFragment); |
| 100 | + document.body.append(wrapper); |
| 101 | + this.modal = wrapper.querySelector('dialog'); |
| 102 | + |
| 103 | + this.unlinkConfirmationModal.signals.show = data.signals.show; |
| 104 | + this.unlinkConfirmationModal.signals.close = data.signals.close; |
| 105 | + |
| 106 | + this.modal.querySelector('input[type="submit"]').addEventListener( |
| 107 | + 'click', |
| 108 | + (event) => onModalSubmitClicked(event, resolve), |
| 109 | + { once: true }, |
| 110 | + ); |
| 111 | + }).then(() => { |
| 112 | + this.setupConfirmationModal = true; |
| 113 | + global.jQuery(document).trigger(this.unlinkConfirmationModal.signals.show, {}); |
| 114 | + }); |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + const submitButton = this.modal.querySelector('input[type="submit"]'); |
| 119 | + submitButton.addEventListener( |
| 120 | + 'click', |
| 121 | + (event) => onModalSubmitClicked(event, resolve), |
| 122 | + { once: true }, |
| 123 | + ); |
| 124 | + global.jQuery(document).trigger(this.unlinkConfirmationModal.signals.show, {}); |
| 125 | + }); |
| 126 | + |
| 127 | + const onModalSubmitClicked = (event, resolve) => { |
| 128 | + event.preventDefault(); |
| 129 | + global.jQuery(document).trigger(this.unlinkConfirmationModal.signals.close, {}); |
| 130 | + resolve(); |
69 | 131 | }; |
70 | 132 |
|
71 | 133 | const disableButtons = (container) => new Promise((resolve) => { |
|
298 | 360 |
|
299 | 361 | const shouldReloadAwarenessTool = ( |
300 | 362 | ['ilBuddySystemLinkedRelationState', 'ilBuddySystemRequestedRelationState'].includes(oldState) |
301 | | - && newState !== oldState |
| 363 | + && newState !== oldState |
302 | 364 | ); |
303 | 365 | if (shouldReloadAwarenessTool) { |
304 | 366 | if (typeof global.il.Awareness !== 'undefined') { |
|
0 commit comments