-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
139 lines (130 loc) · 4.45 KB
/
app.js
File metadata and controls
139 lines (130 loc) · 4.45 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//bring in DOM elements
const title = document.getElementById('title');
const bigTotal = document.getElementById('bigTotal');
const userMiles = document.getElementById('userMiles');
const submit = document.getElementById('submit');
const lastRun = document.getElementById('lastRun');
const milesRemaining = document.getElementById('milesRemaining');
const reqDailyAvgDiv = document.getElementById('reqDailyAvgDiv');
const reqDailyAvg = document.getElementById('reqDailyAvg');
const icon = document.getElementById('icon');
const message = document.getElementById('message');
const lastRunDiv = document.getElementById('lastRunDiv');
const milesRemainingDiv = document.getElementById('milesRemainingDiv');
const progressBar = document.getElementById('progressBar');
const percentageDiv = document.getElementById('percentageDiv');
//font awesome
const runIcon = `fas fa-running mx-2`;
const heartIcon = `far fa-heart mx-2`;
message.innerHTML = 'submit';
//datedif
let today = new Date();
let from_date = new Date('2021/12/31');
let difference = from_date > today ? from_date - today : today - from_date;
let datedif = Math.floor(difference / (1000 * 3600 * 24));
// api FETCH all stats
fetch('https://runcounter-v3.herokuapp.com/stats')
.then((response) => response.json())
.then(function (json) {
// get the value out of the JSON
let lastID = json[json.length - 1];
let run_length = userMiles.value;
let runTotal = parseFloat(json[0]['SUM(run_length)']).toFixed(2);
console.log(runTotal);
let run_date = Date();
let stats = json;
let getStats = function (stats) {
console.log(stats);
};
getStats(stats);
});
// api FETCH run_total
fetch('https://runcounter-v3.herokuapp.com/stats/run_total')
.then((response) => response.json())
.then(function (json) {
// get the value out of the JSON
let runTotal = json[0]['SUM(run_length)'];
console.log(runTotal);
//push stats into DOM
//set total
let setTotal = function (runTotal) {
bigTotal.innerHTML = runTotal;
};
setTotal(runTotal);
//set milesRemaining
let setMilesRemaining = function (runTotal) {
milesRemaining.innerHTML = 2000 - runTotal;
milesRemainingDiv.classList.remove('hidden');
};
setMilesRemaining(runTotal);
//set reqDailyAvg
let setReqDailyAvg = function (runTotal, datedif) {
let reqDaily = (2000 - runTotal) / datedif;
let roundDown = reqDaily.toFixed(2);
reqDailyAvg.innerHTML = roundDown;
reqDailyAvgDiv.classList.remove('hidden');
};
setReqDailyAvg(runTotal, datedif);
let setPercentage = function (bigTotal) {
let percentage =
(bigTotal.innerHTML / 2000 / Math.pow(10, -2)).toFixed(2) + '%';
progressBar.setAttribute('style', 'width: ' + percentage);
progressBar.setAttribute('aria-valuenow', percentage);
percentageDiv.innerHTML = percentage;
};
setPercentage(bigTotal);
});
//date for mySQL
let date = new Date();
let year = date.getFullYear().toString();
let month = (date.getMonth() + 1).toString().padStart(2, '0');
let day = date.getDate().toString();
let dateString = year + '-' + month + '-' + day;
//NEW RUN function
const newRun = function () {
let run_date = dateString;
let run_length = parseFloat(lastRun.innerHTML);
fetch('https://runcounter-v3.herokuapp.com/stats', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
run_date: dateString,
run_length: run_length,
}),
});
updateDisplay();
};
const updateDisplay = function () {
icon.className = heartIcon;
lastRun.innerHTML = userMiles.value;
lastRunDiv.classList.remove('hidden');
};
//button switcher
submit.addEventListener('click', function () {
lastRunDiv.classList.remove('hidden');
milesRemainingDiv.classList.remove('hidden');
reqDailyAvgDiv.classList.remove('hidden');
if (userMiles.value == 0) {
message.innerHTML = 'maybe tomorrow?';
} else if (userMiles.value >= 1 && userMiles.value <= 5) {
message.innerHTML = 'okok!';
} else if (userMiles.value >= 6 && userMiles.value <= 9) {
message.innerHTML = 'respect!';
} else if (userMiles.value >= 10) {
message.innerHTML = 'oh nice!!';
}
newRun();
updateDisplay();
});
//countup animation
// import { CountUp } from './js/countUp.min.js';
// const options = {
// duration: 2,
// suffix: 'miles',
// };
// window.onload = function () {
// var countUp = new CountUp('bigTotal', runTotal, options);
// countUp.start();
// };