Skip to content

Commit 0e0b04a

Browse files
benjaminfruehbackportbot[bot]
authored andcommitted
fix: replace jQuery with plain JavaScript
Signed-off-by: Benjamin Frueh <benjamin.frueh@gmail.com>
1 parent aa8584d commit 0e0b04a

3 files changed

Lines changed: 46 additions & 48 deletions

File tree

.github/workflows/cypress.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ jobs:
9595
matrix:
9696
# Run multiple copies of the current job in parallel
9797
# Please increase the number or runners as your tests suite grows
98-
containers: ['component', '1', '2', '3']
98+
containers: ['1']
9999

100100
name: runner ${{ matrix.containers }}
101101

js/app.js

Lines changed: 43 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,65 +3,63 @@
33
* SPDX-License-Identifier: AGPL-3.0-or-later
44
*/
55

6-
'use strict';
7-
86
/** global: OCA */
97
/** global: OC */
108

119
(function(OC, OCA) {
12-
OCA.DataRequest = OCA.DataRequest || {};
10+
'use strict'
11+
12+
OCA.DataRequest = OCA.DataRequest || {}
1313

1414
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+
})
1919
},
2020

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
2825
}
29-
this._doRequest($context);
26+
this._doRequest(btn)
3027
},
3128

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')
3632

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+
})
4441

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+
}
5346

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')
5551

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
5953

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'))
6359
}
64-
});
65-
}
66-
};
67-
})(OC, OCA);
60+
} finally {
61+
btn.classList.remove('loading')
62+
}
63+
},
64+
}
65+
})(OC, OCA)

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)