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