|
3 | 3 | * SPDX-License-Identifier: AGPL-3.0-or-later |
4 | 4 | */ |
5 | 5 |
|
6 | | -'use strict'; |
7 | | - |
8 | 6 | /** global: OCA */ |
9 | 7 | /** global: OC */ |
10 | 8 |
|
11 | 9 | (function(OC, OCA) { |
12 | | - OCA.DataRequest = OCA.DataRequest || {}; |
| 10 | + 'use strict' |
| 11 | + |
| 12 | + OCA.DataRequest = OCA.DataRequest || {} |
13 | 13 |
|
14 | 14 | OCA.DataRequest.App = { |
15 | | - init: function() { |
16 | | - $('#data-request button').on('click', function() { |
17 | | - OCA.DataRequest.App.request($(this)); |
18 | | - }); |
| 15 | + init() { |
| 16 | + document.querySelectorAll('#data-request button').forEach((btn) => { |
| 17 | + btn.addEventListener('click', () => this.request(btn)) |
| 18 | + }) |
19 | 19 | }, |
20 | 20 |
|
21 | | - request: function ($context) { |
22 | | - if(OC.PasswordConfirmation.requiresPasswordConfirmation()) { |
23 | | - var self = this; |
24 | | - OC.PasswordConfirmation.requirePasswordConfirmation(function () { |
25 | | - self._doRequest($context); |
26 | | - }); |
27 | | - return; |
| 21 | + request(btn) { |
| 22 | + if (OC.PasswordConfirmation.requiresPasswordConfirmation()) { |
| 23 | + OC.PasswordConfirmation.requirePasswordConfirmation(() => this._doRequest(btn)) |
| 24 | + return |
28 | 25 | } |
29 | | - this._doRequest($context); |
| 26 | + this._doRequest(btn) |
30 | 27 | }, |
31 | 28 |
|
32 | | - _doRequest($context) { |
33 | | - $context.prop('disabled', 'disabled'); |
34 | | - $context.addClass('loading'); |
35 | | - $context.siblings('span.warning').addClass('hidden').html(''); |
| 29 | + async _doRequest(btn) { |
| 30 | + btn.disabled = true |
| 31 | + btn.classList.add('loading') |
36 | 32 |
|
37 | | - $.ajax({ |
38 | | - type: 'POST', |
39 | | - url: OC.linkToOCS('apps/data_request/api/v1', 2) + $context.data('request'), |
40 | | - dataType: 'json', |
41 | | - beforeSend: function (request) { |
42 | | - request.setRequestHeader('Accept', 'application/json'); |
43 | | - }, |
| 33 | + try { |
| 34 | + const response = await fetch(OC.linkToOCS('apps/data_request/api/v1', 2) + btn.dataset.request, { |
| 35 | + method: 'POST', |
| 36 | + headers: { |
| 37 | + 'Accept': 'application/json', |
| 38 | + 'requesttoken': OC.requestToken, |
| 39 | + }, |
| 40 | + }) |
44 | 41 |
|
45 | | - success: function () { |
46 | | - $context.html($context.html() + ' ' + t('data_request', 'sent!')); |
47 | | - $context.removeClass('loading'); |
48 | | - }, |
49 | | - error: function (response) { |
50 | | - if (response.status !== 429) { |
51 | | - $context.prop('disabled', ''); |
52 | | - } |
| 42 | + if (!response.ok) { |
| 43 | + const data = await response.json().catch(() => null) |
| 44 | + throw { status: response.status, data } |
| 45 | + } |
53 | 46 |
|
54 | | - $context.removeClass('loading'); |
| 47 | + btn.append(' ' + t('data_request', 'sent!')) |
| 48 | + } catch (err) { |
| 49 | + const { status, data } = err |
| 50 | + const warning = btn.parentElement.querySelector('span.warning') |
55 | 51 |
|
56 | | - const errorMessage = response.status === 429 |
57 | | - ? t('data_request', 'Already requested, please try again later.') |
58 | | - : (response.responseJSON?.ocs?.data?.error || t('data_request', 'Request failed')); |
| 52 | + btn.disabled = status === 429 |
59 | 53 |
|
60 | | - $context.siblings('span.warning') |
61 | | - .removeClass('hidden') |
62 | | - .html(errorMessage); |
| 54 | + if (warning) { |
| 55 | + warning.classList.remove('hidden') |
| 56 | + warning.textContent = status === 429 |
| 57 | + ? t('data_request', 'Already requested, please try again later.') |
| 58 | + : (data?.ocs?.data?.error || t('data_request', 'Request failed')) |
63 | 59 | } |
64 | | - }); |
65 | | - } |
66 | | - }; |
67 | | -})(OC, OCA); |
| 60 | + } finally { |
| 61 | + btn.classList.remove('loading') |
| 62 | + } |
| 63 | + }, |
| 64 | + } |
| 65 | +})(OC, OCA) |
0 commit comments