|
16 | 16 | email = (JSON.parse(decode(getCookie("token")))).sub;
|
17 | 17 | id = ((JSON.parse(decode(getCookie("token")))).id).toString();
|
18 | 18 | console.log(id);
|
| 19 | + |
| 20 | + date = new Date(); |
| 21 | + day = date.getDate(); |
| 22 | + month = date.getMonth() + 1; |
| 23 | + year = (date.getFullYear()) -2000; |
| 24 | + currentDate = `0${month}-${day}-${year}`; |
| 25 | + console.log(currentDate) |
19 | 26 |
|
20 | 27 | function update() {
|
21 | 28 | //pull from backend and update values
|
22 | 29 | // placeholders
|
23 |
| - function getRandomInt(max) { |
24 |
| - return Math.floor(Math.random() * max); |
25 |
| - } |
26 |
| - steps = (getRandomInt(100)); |
27 |
| - calories = (getRandomInt(1000)); |
| 30 | + var requestOptions = { |
| 31 | + method: 'GET', |
| 32 | + redirect: 'follow' |
| 33 | + }; |
| 34 | + |
| 35 | + fetch("http://localhost:8085/api/person/" + id, requestOptions) |
| 36 | + .then(response => response.text()) |
| 37 | + .then(result => { |
| 38 | + const stats = JSON.parse(result).stats[currentDate]; |
| 39 | + document.getElementById("welcome").innerHTML = "Welcome " + email + "!"; |
| 40 | + document.getElementById("steps").innerHTML = stats ? stats.steps : "N/A"; |
| 41 | + document.getElementById("calories").innerHTML = stats ? stats.calories : "N/A"; |
| 42 | + }) |
| 43 | + .catch(error => console.log('error', error)); |
28 | 44 | //placeholders
|
29 | 45 | document.getElementById("welcome").innerHTML = "Welcome " + email + "!";
|
30 | 46 | document.getElementById("steps").innerHTML = steps;
|
|
38 | 54 | document.getElementById("calories").innerHTML = calories;
|
39 | 55 | }
|
40 | 56 | function post() {
|
41 |
| - date = new Date(); |
42 |
| - day = date.getDate(); |
43 |
| - month = date.getMonth() + 1; |
44 |
| - year = (date.getFullYear()) -2000; |
45 |
| - currentDate = `0${month}-${day}-${year}`; |
46 | 57 | console.log(currentDate);
|
47 | 58 | steps = document.getElementById("steps").innerHTML;
|
48 | 59 | calories = document.getElementById("calories").innerHTML;
|
|
88 | 99 | if (data.status!=401 || data.status!=400) {
|
89 | 100 | document.cookie = "token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
|
90 | 101 | alert("user deleted!")
|
91 |
| - //window.location.replace("/Lesson_Frontend/sign_in/"); |
| 102 | + window.location.replace("/Lesson_Frontend/sign_in/"); |
92 | 103 | } else {
|
93 | 104 | console.log("bad email and password");
|
94 | 105 | }
|
95 | 106 | })
|
96 | 107 | .catch(error => console.log('error', error));
|
97 | 108 | }
|
| 109 | + function signOut() { |
| 110 | + document.cookie = "token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;"; |
| 111 | + alert("signed out!") |
| 112 | + window.location.replace("/Lesson_Frontend/sign_in/"); |
| 113 | + } |
98 | 114 | </script>
|
99 | 115 |
|
100 | 116 | <body onload="update()">
|
|
111 | 127 | <button onClick="edit()">Edit</button>
|
112 | 128 | <button onClick="post()">Update</button>
|
113 | 129 | <div></div>
|
| 130 | + <button onClick="signOut()">Sign Out</button> |
| 131 | + <div></div> |
114 | 132 | <button onClick="delUser()">Delete user</button>
|
115 | 133 | </div>
|
116 | 134 | </div>
|
|
0 commit comments