blob: 581d5a3b99900ab58b94538a417a7f885b829e2c (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
window.onload = function () {
document.getElementById('nojs').hidden = true;
document.getElementById('block').hidden = false;
};
// File submit AJAX request
// After successful upload, actuate the file by calling actuate() on the successfully uploaded file
document.getElementById('upload').onsubmit = function () {
// Reset error message and success message
document.getElementById('upload-err').innerText = '';
document.getElementById('actuate-err').innerText = '';
document.getElementById('upload-response').innerText = '';
// Make AJAX request
let xhr = new XMLHttpRequest();
xhr.open('POST', '/api/v1/upload');
let formData = new FormData(this);
xhr.send(formData);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
let response = JSON.parse(xhr.responseText);
if (xhr.status === 201) {
// Display upload success message to the user
document.getElementById('upload-response').innerText = response.msg;
actuate(response);
} else {
// Display upload error message to the user
document.getElementById('upload-err').innerText = response.error;
// DEBUG: Print full error if unknown error occurs
if (xhr.status === 500)
console.error(response.error_msg);
}
}
};
document.getElementById('upload').reset();
return false;
};
// Actuate button AJAX request
// Should always be called after upload
// Implies that upload has been successful since it relies on the upload response
//
function actuate(file) {
let xhr = new XMLHttpRequest();
xhr.open('POST', '/api/v1/actuate');
let data = {
name: file.name,
path: file.path,
};
xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
xhr.setRequestHeader('X-CSRF-TOKEN', file.csrf);
xhr.send(JSON.stringify(data));
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
let response = JSON.parse(xhr.responseText);
if (xhr.status === 200) {
console.log(response);
} else {
// Display upload error message to the user
document.getElementById('actuate-err').innerText = response.error;
// DEBUG: Print full error if unknown error occurs
if (xhr.status === 500)
console.error(response.error_msg);
}
}
};
}
|