Skip to content

Commit 2936730

Browse files
authored
Add busy indicator during imports (#563)
1 parent 7e5d489 commit 2936730

File tree

3 files changed

+43
-18
lines changed

3 files changed

+43
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
### Fixed
44
- php 8.5 compatibility fix
5+
- Prevent double submissions by showing busy indicators during report and data imports
56
- Nullpointer in DataloadController::importFile() #560 #555
67
- Extend filter variable replacement tests with multiple filter cases
78
- Update filter option handling for BETWEEN case #559 @[joleaf](https://github.com/joleaf)

js/navigation.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -813,18 +813,25 @@ OCA.Analytics.Navigation = {
813813

814814
handleImportButton: function () {
815815
const fileInput = document.getElementById('importFile');
816+
const importButton = document.getElementById('importDatasetButton');
816817
fileInput.click();
817818
fileInput.addEventListener('change', async () => {
818819
const file = fileInput.files[0];
819820
if (!file) {
820821
return;
821822
}
823+
OCA.Analytics.Sidebar.Backend.setButtonBusy(importButton, true);
822824
const reader = new FileReader();
823825
reader.readAsText(file);
824826
reader.onload = async () => {
825-
OCA.Analytics.Sidebar.Report.import(null, reader.result);
827+
OCA.Analytics.Sidebar.Report.import(null, reader.result, importButton);
828+
fileInput.value = '';
826829
};
827-
})
830+
reader.onerror = () => {
831+
OCA.Analytics.Notification.notification('error', t('analytics', 'Import failed'));
832+
OCA.Analytics.Sidebar.Backend.setButtonBusy(importButton, false);
833+
};
834+
}, {once: true});
828835
},
829836

830837
handleSettingsButton: function () {
@@ -1212,4 +1219,4 @@ document.addEventListener('DOMContentLoaded', function () {
12121219

12131220
document.getElementById('wizardStart').addEventListener('click', OCA.Analytics.Wizard.showFirstStart);
12141221
document.getElementById('importDatasetButton').addEventListener('click', OCA.Analytics.Navigation.handleImportButton);
1215-
});
1222+
});

js/sidebar.js

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -738,15 +738,17 @@ OCA.Analytics.Sidebar.Report = {
738738
window.open(OC.generateUrl('apps/analytics/report/export/') + reportId, '_blank')
739739
},
740740

741-
import: function (path, raw) {
741+
import: function (path, raw, button = null) {
742742
if (typeof raw === 'number') raw = null; // file picker is returning some INT which is not helpful in the service
743743
let requestUrl = OC.generateUrl('apps/analytics/report/import/');
744744
let headers = new Headers();
745745
headers.append('requesttoken', OC.requestToken);
746746
headers.append('OCS-APIREQUEST', 'true');
747747
headers.append('Content-Type', 'application/json');
748748

749-
fetch(requestUrl, {
749+
OCA.Analytics.Sidebar.Backend.setButtonBusy(button, true);
750+
751+
return fetch(requestUrl, {
750752
method: 'POST',
751753
headers: OCA.Analytics.headers(),
752754
body: JSON.stringify({
@@ -757,6 +759,12 @@ OCA.Analytics.Sidebar.Report = {
757759
.then(response => response.json())
758760
.then(data => {
759761
OCA.Analytics.Navigation.init();
762+
})
763+
.catch(error => {
764+
OCA.Analytics.Notification.notification('error', t('analytics', 'Import failed'));
765+
})
766+
.finally(() => {
767+
OCA.Analytics.Sidebar.Backend.setButtonBusy(button, false);
760768
});
761769
},
762770

@@ -919,6 +927,19 @@ OCA.Analytics.Sidebar.Data = {
919927

920928
OCA.Analytics.Sidebar.Backend = {
921929

930+
setButtonBusy: function (button, isBusy) {
931+
if (!button) {
932+
return;
933+
}
934+
if (isBusy) {
935+
button.classList.add('loading');
936+
button.disabled = true;
937+
} else {
938+
button.classList.remove('loading');
939+
button.disabled = false;
940+
}
941+
},
942+
922943
updateData: function () {
923944
const reportId = parseInt(OCA.Analytics.currentDataset);
924945
const button = document.getElementById('updateDataButton');
@@ -1011,9 +1032,8 @@ OCA.Analytics.Sidebar.Backend = {
10111032

10121033
importCsvData: function () {
10131034
const reportId = parseInt(OCA.Analytics.currentDataset);
1014-
const button = document.getElementById('importDataClipboardButton');
1015-
button.classList.add('loading');
1016-
button.disabled = true;
1035+
const button = document.getElementById('importDataClipboardButtonGo');
1036+
OCA.Analytics.Sidebar.Backend.setButtonBusy(button, true);
10171037

10181038
let requestUrl = OC.generateUrl('apps/analytics/data/importCSV');
10191039
fetch(requestUrl, {
@@ -1027,8 +1047,6 @@ OCA.Analytics.Sidebar.Backend = {
10271047
})
10281048
.then(response => response.json())
10291049
.then(data => {
1030-
button.classList.remove('loading');
1031-
button.disabled = false;
10321050
if (data.error === 0) {
10331051
OCA.Analytics.Notification.notification('success', data.insert + ' ' + t('analytics', 'records inserted') + ', ' + data.update + ' ' + t('analytics', 'records updated'));
10341052
if (!OCA.Analytics.isDataset) {
@@ -1041,16 +1059,16 @@ OCA.Analytics.Sidebar.Backend = {
10411059
})
10421060
.catch(error => {
10431061
OCA.Analytics.Notification.notification('error', t('analytics', 'Technical error. Please check the logs.'));
1044-
button.classList.remove('loading');
1045-
button.disabled = false;
1062+
})
1063+
.finally(() => {
1064+
OCA.Analytics.Sidebar.Backend.setButtonBusy(button, false);
10461065
});
10471066
},
10481067

10491068
importFileData: function (path) {
10501069
const reportId = parseInt(OCA.Analytics.currentDataset);
10511070
const button = document.getElementById('importDataFileButton');
1052-
button.classList.add('loading');
1053-
button.disabled = true;
1071+
OCA.Analytics.Sidebar.Backend.setButtonBusy(button, true);
10541072

10551073
let requestUrl = OC.generateUrl('apps/analytics/data/importFile');
10561074
fetch(requestUrl, {
@@ -1064,8 +1082,6 @@ OCA.Analytics.Sidebar.Backend = {
10641082
})
10651083
.then(response => response.json())
10661084
.then(data => {
1067-
button.classList.remove('loading');
1068-
button.disabled = false;
10691085
if (data.error === 0) {
10701086
OCA.Analytics.Notification.notification('success', data.insert + ' ' + t('analytics', 'records inserted') + ', ' + data.update + ' ' + t('analytics', 'records updated'));
10711087
if (!OCA.Analytics.isDataset) {
@@ -1078,8 +1094,9 @@ OCA.Analytics.Sidebar.Backend = {
10781094
})
10791095
.catch(error => {
10801096
OCA.Analytics.Notification.notification('error', t('analytics', 'Technical error. Please check the logs.'));
1081-
button.classList.remove('loading');
1082-
button.disabled = false;
1097+
})
1098+
.finally(() => {
1099+
OCA.Analytics.Sidebar.Backend.setButtonBusy(button, false);
10831100
});
10841101
},
10851102
};

0 commit comments

Comments
 (0)