Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit 8999f5d

Browse files
author
vikasrohit
committed
SUP-1159, Progress bar to show %complete on upload
-- Added event handler for progress notification of upload -- Added progress aggregator for multiple file upload progress -- Showed, for POC, the progress bar with text message on the same page with custom directive
1 parent e0ea727 commit 8999f5d

File tree

5 files changed

+73
-6
lines changed

5 files changed

+73
-6
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.progress-bar
2+
.bar(style="background-color: #F0F0F0;height: 10px;")
3+
.completed(style="background-color: #0096FF;")
4+
.summary
5+
span(ng-bind="completed")
6+
span % {{message}}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
(function() {
2+
'use strict';
3+
4+
angular.module('tcUIComponents').directive('progressBar', progressBar);
5+
6+
progressBar.$inject = ['$timeout', '$parse'];
7+
8+
function progressBar($timeout, $parse) {
9+
return {
10+
restrict: 'E',
11+
templateUrl: 'directives/progress-bar/progress-bar.directive.html',
12+
link: function(scope, element, attr) {
13+
var model = $parse(attr.completed);
14+
var msg = attr.message;
15+
var progress = angular.element(element[0].querySelector('.completed'));
16+
17+
scope.$watch(model, function(newValue, oldValue) {
18+
console.log("Updating progress bar with " + newValue);
19+
scope.completed = Math.round(newValue);
20+
scope.message = msg;
21+
progress.css('width', scope.completed + '%')
22+
});
23+
},
24+
controller: ['$scope', function($scope) {
25+
26+
}]
27+
};
28+
}
29+
})();

app/services/submissions.service.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717

1818
return service;
1919

20-
function getPresignedURL(body, files) {
20+
function getPresignedURL(body, files, progressCallback) {
2121
console.log('Body of request for presigned url: ', body);
2222

2323
return api.all('submissions').customPOST(body)
2424
.then(function(response) {
2525
console.log('POST/Presigned URL Response: ', response.plain());
2626

27-
uploadSubmissionFileToS3(response, response.data.files, files);
27+
uploadSubmissionFileToS3(response, response.data.files, files, progressCallback);
2828
})
2929
.catch(function(err) {
3030
console.log(err);
@@ -33,7 +33,7 @@
3333
});
3434
}
3535

36-
function uploadSubmissionFileToS3(presignedURLResponse, filesWithPresignedURL, files) {
36+
function uploadSubmissionFileToS3(presignedURLResponse, filesWithPresignedURL, files, progressCallback) {
3737

3838
var promises = filesWithPresignedURL.map(function(fileWithPresignedURL) {
3939
var deferred = $q.defer();
@@ -42,6 +42,19 @@
4242
xhr.open('PUT', fileWithPresignedURL.preSignedUploadUrl, true);
4343
xhr.setRequestHeader('Content-Type', fileWithPresignedURL.mediaType);
4444

45+
xhr.upload.addEventListener("progress", function(oEvent) {
46+
if (oEvent.lengthComputable) {
47+
var percentComplete = oEvent.loaded / oEvent.total;
48+
console.log("Completed " + percentComplete);
49+
if (progressCallback && typeof progressCallback === 'function') {
50+
progressCallback.call(progressCallback, fileWithPresignedURL.preSignedUploadUrl, percentComplete*100);
51+
}
52+
// ...
53+
} else {
54+
// Unable to compute progress information since the total size is unknown
55+
}
56+
});
57+
4558
// xhr version of the success callback
4659
xhr.onreadystatechange = function() {
4760
var status = xhr.status;

app/submissions/submit-file/submit-file.controller.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
angular.module('tc.submissions').controller('SubmitFileController', SubmitFileController);
55

6-
SubmitFileController.$inject = ['$stateParams', 'UserService', 'SubmissionsService', 'challengeToSubmitTo'];
6+
SubmitFileController.$inject = ['$scope', '$stateParams', 'UserService', 'SubmissionsService', 'challengeToSubmitTo'];
77

8-
function SubmitFileController($stateParams, UserService, SubmissionsService, challengeToSubmitTo) {
8+
function SubmitFileController($scope, $stateParams, UserService, SubmissionsService, challengeToSubmitTo) {
99
var vm = this;
1010

1111
// Must provide React Select component a list with ID, since currently
@@ -23,9 +23,11 @@
2323
];
2424

2525
var files = {};
26+
var fileUploadProgress = {};
2627
vm.urlRegEx = new RegExp(/^(http(s?):\/\/)?(www\.)?[a-zA-Z0-9\.\-\_]+(\.[a-zA-Z]{2,3})+(\/[a-zA-Z0-9\_\-\s\.\/\?\%\#\&\=]*)?$/);
2728
vm.rankRegEx = new RegExp(/^[1-9]\d*$/);
2829
vm.comments = '';
30+
vm.uploadProgress = 0;
2931
vm.submissionForm = {
3032
files: [],
3133

@@ -166,6 +168,7 @@
166168
}
167169

168170
function uploadSubmission() {
171+
vm.updateProgress = 0;
169172
vm.submissionsBody.data.submitterComments = vm.comments;
170173
vm.submissionsBody.data.submitterRank = vm.submissionForm.submitterRank;
171174

@@ -191,7 +194,19 @@
191194
});
192195
}
193196

194-
SubmissionsService.getPresignedURL(vm.submissionsBody, files);
197+
SubmissionsService.getPresignedURL(vm.submissionsBody, files, updateProgress);
198+
}
199+
200+
function updateProgress(requestId, progress) {
201+
fileUploadProgress[requestId] = progress;
202+
var total = 0, count = 0;
203+
for(var requestId in fileUploadProgress) {
204+
var prog = fileUploadProgress[requestId];
205+
total += prog;
206+
count++;
207+
}
208+
vm.uploadProgress = total / count;
209+
$scope.$apply();
195210
}
196211
}
197212
})();

app/submissions/submit-file/submit-file.jade

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,7 @@
184184
label(for="agree-to-terms") I understand and agree
185185

186186
button.tc-btn.tc-btn-secondary(type="submit", ng-disabled="submissionForm.$invalid") Submit
187+
188+
.panel-body
189+
p Uploading submission for TBD: challenge name
190+
progress-bar(completed="vm.uploadProgress", message="of 3 files uploaded")

0 commit comments

Comments
 (0)