Skip to content

Commit d45d538

Browse files
author
Piyush Ahuja
committed
Promisify uploadProject
1 parent e708771 commit d45d538

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

src/actions/projects.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,7 @@ export function deleteProject(projectID) {
143143
}
144144

145145
// Upload a project report.
146-
// TODO: convert this function and its callers to use promises (#6).
147-
export function uploadProject(projectID, blob, callback=()=>{}) {
146+
export function uploadProject(projectID, blob) {
148147
return function (dispatch, getState) {
149148
const project = update(getState().projects.projects[projectID], {
150149
data: {$merge: {
@@ -166,9 +165,9 @@ export function uploadProject(projectID, blob, callback=()=>{}) {
166165
).then(response => {
167166
dispatch(receiveProject(project));
168167
dispatch(receiveProjectStatus(projectID, response.data.file_names));
169-
callback(response.data.status_message);
170-
}).catch(error => {
171-
callback(error.response.data.status_message);
168+
return response.data.status_message;
169+
}, error => {
170+
throw new Error(error.response.data.status_message);
172171
});
173172
}
174173
}
@@ -197,12 +196,13 @@ export function downloadProject(project) {
197196
reader.onload = () => {
198197
reject(JSON.parse(reader.result).status_message);
199198
}
200-
199+
201200
reader.onerror = () => {
202201
reject(reader.error);
203202
}
203+
reader.readAsText(error.response.data);
204204
});
205-
reader.readAsText(error.response.data);
205+
206206
}
207207
});
208208
}

src/pages/project_download.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ export function renderDownload(project, label) {
109109
downloadProject(project).then(msg => (
110110
Alert.success(msg)
111111
)).catch(error => (
112-
Alert.error(error.message);
112+
Alert.error(error.message)
113113
));
114114
}}>
115115
{label}

src/pages/project_upload.js

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,27 +88,45 @@ class ProjectUpload extends Component {
8888
// before trying to upload it, and then upload it.
8989
upload() {
9090
const projectID = this.props.match.params.projectID;
91+
const numFiles = this.state.uploads.length;
9192
var zip = new JSZip();
9293
this.state.uploads.forEach(file => {
9394
zip.file(file.name, file);
9495
});
95-
zip.generateAsync({type:"blob"}).then(blob => {
96+
zip.generateAsync({type:"blob"}).catch(error => {
97+
if (error instanceof DOMException){
98+
// XXX: when trying to read from a File that represents a
99+
// directory, Chrome 76 raises NotFoundError, and Firefox 67
100+
// raises NotReadableError. However, if the File represents a
101+
// file which has been removed since it was selected, both
102+
// Firefox and Chrome raise NotFoundError!
103+
if (error.name === "NotReadableError") {
104+
throw new Error("Couldn't read file (directory uploads are not supported)");
105+
} else if (error.name === "NotFoundError") {
106+
throw new Error("File not found (directory uploads are not supported)");
107+
}
108+
}
109+
throw error;
110+
111+
}).then(blob => {
96112
if (blob.size > maxFilesize) {
97-
Alert.error("Selected file(s) too large");
113+
throw new Error(`Selected file${numFiles === 1 ? '' : 's'} too large`);
98114
}
99-
else {
100-
this.props.uploadProject(projectID, blob, (status_message) => {
115+
return this.props.uploadProject(projectID, blob);
116+
}).then(status_message => {
101117
if (status_message === "success" || !status_message) {
102118
Alert.success(`Successfully uploaded your files for ${this.props.projects[projectID].data.title}.`);
103119
this.onSuccessfulUpload();
104120
}
105121
else {
106-
Alert.error(`Error when uploading ${this.props.projects[projectID].data.title}: ${status_message}`);
122+
throw new Error(status_message);
107123
}
108-
});
109-
}
110-
});
124+
}, error => {
125+
Alert.error(`Error when uploading project: ${error.message}`);
126+
});
127+
111128
}
129+
112130

113131
// Add a file to the list of files to upload (the name is because as
114132
// well as clicking to select a file, you can drag and drop files

0 commit comments

Comments
 (0)