-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog-food.js
79 lines (69 loc) · 2.47 KB
/
log-food.js
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
67
68
69
70
71
72
73
74
75
76
77
78
79
(function logFoodWrapper($) {
EatLogGo.checkLogin();
if (window.File && window.FileReader && window.FileList && window.Blob) {
document.getElementById('fphoto').addEventListener('change', handleFileSelect, false);
} else {
$("#fphoto-wrap").hide();
}
function handleClick(e) {
e.preventDefault();
logEntry($("#fdate").val(), $("#type").val(), $("#details").val(), $("#fphoto-b64").val());
}
$("#log-food-submit").click(handleClick);
function logEntry(date, type, details, encodedPicture) {
$.ajax({
method: 'POST',
url: _config.api.invokeUrl + '/food-log',
headers: {
"Authorization": EatLogGo.globalAuthToken,
},
data: JSON.stringify({
Date: date,
Type: type,
Details: details,
EncodedPicture: encodedPicture
}),
contentType: 'application/json',
beforeSend: function Processing() {
$("#log-food-submit").removeClass("btn-primary")
.addClass("btn-secondary")
.off("click",handleClick)
.text("Processing....");
},
success: handleSuccess,
error: function ajaxError(jqXHR, textStatus, errorThrown) {
alert('An error occured when logging your entry\n');
$("#log-food-submit").removeClass("btn-secondary")
.addClass("btn-primary")
.on("click",handleClick)
.text("Submit");
}
});
}
function handleSuccess() {
alert("Food Log entry successfully added.");
window.location.href = "log.html";
}
function handleFileSelect(evt) {
var f = evt.target.files[0]; // FileList object
var reader = new FileReader();
// Closure to capture the file information.
reader.onload = (function(theFile) {
return function(e) {
var binaryData = e.target.result;
//Converting Binary Data to base 64
var base64String = window.btoa(binaryData);
//showing file converted to base64
$("#fphoto-b64").val(base64String);
};
})(f);
// Read in the image file as a data URL.
reader.readAsBinaryString(f);
}
function setCurrentDate() {
var now = new Date();
var today = now.getFullYear() + "-" + (now.getMonth()+1).toString().padStart(2,"0") + "-" + now.getDate().toString().padStart(2,"0");
$("#fdate").val(today);
}
setCurrentDate();
}(jQuery));