@@ -174,11 +174,10 @@ export function uploadProject(projectID, blob, callback=()=>{}) {
174
174
}
175
175
176
176
// Download a previously-uploaded project report.
177
- // TODO: convert this function and its callers to use promises (#6).
178
177
// TODO: it's probably more sensible to just direct the browser to a URL
179
178
// which serves the appropriate Content-Disposition to make it download
180
179
// the page, rather than doing it in JavaScript.
181
- export function downloadProject ( project , callback ) {
180
+ export function downloadProject ( project ) {
182
181
return axios . get ( `${ api_url } /api/projects/${ project . data . id } /file` , {
183
182
responseType : 'blob' ,
184
183
headers : {
@@ -188,16 +187,22 @@ export function downloadProject(project, callback) {
188
187
const rotation_parts = getSeriesPart ( project ) ;
189
188
const filename = `${ rotation_parts [ 0 ] } _${ rotation_parts [ 1 ] } _${ project . data . title } ` ;
190
189
saveAs ( response . data , `${ filename } .zip` ) ;
191
- callback ( "Download complete" , false ) ;
192
- } ) . catch ( response => {
193
- if ( response . response . status === 404 ) {
194
- callback ( "Project not yet uploaded" , true ) ;
190
+ return "Download complete" ;
191
+ } ) . catch ( error => {
192
+ if ( error . response . status === 404 ) {
193
+ throw new Error ( "Project not yet uploaded" ) ;
195
194
} else {
196
- var reader = new FileReader ( ) ;
197
- reader . onload = function ( ) {
198
- callback ( JSON . parse ( reader . result ) . status_message , true ) ;
199
- }
200
- reader . readAsText ( response . response . data ) ;
195
+ return new Promise ( ( resolve , reject ) => {
196
+ var reader = new FileReader ( ) ;
197
+ reader . onload = ( ) => {
198
+ reject ( JSON . parse ( reader . result ) . status_message ) ;
199
+ }
200
+
201
+ reader . onerror = ( ) => {
202
+ reject ( reader . error ) ;
203
+ }
204
+ } ) ;
205
+ reader . readAsText ( error . response . data ) ;
201
206
}
202
207
} ) ;
203
208
}
0 commit comments