Skip to content

Commit bedf3a4

Browse files
committed
2 parents 6ef839c + c998de8 commit bedf3a4

File tree

4 files changed

+819
-18
lines changed

4 files changed

+819
-18
lines changed

BA_signin.html

+5-6
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ <h2 style="text-align: center;">Login</h2>
8787
<input type="password" id="password" name="password" placeholder="password" required>
8888
<br>
8989
</form>
90-
<button id="logInButton" type="submit" class="login-button" onclick="logIn()">Login</button>
90+
<button id="logInButton" class="login-button" onclick="logIn()">Login</button>
9191
</div>
9292

9393
<div id="signup-tab-content" class="tab-content">
@@ -100,10 +100,10 @@ <h2 style="text-align: center;">Sign Up</h2>
100100
<input type="password" id="new-password" name="password" placeholder="password" required>
101101
<br>
102102
<input type="date" id="new-dob" name="dob" required>
103-
<br>
104-
103+
105104
</form>
106-
<button id="signUpButton" type="submit" class="login-button" onclick="signUp()">Sign Up</button>
105+
<br>
106+
<button id="signUpButton" class="login-button" onclick="signUp()">Sign Up</button>
107107
</div>
108108

109109
<script>
@@ -166,12 +166,12 @@ <h2 style="text-align: center;">Sign Up</h2>
166166
'Content-Type': 'application/json',
167167
},
168168
body: JSON.stringify(params),
169-
withCredentials: true
170169
})
171170
.then(response => response.json())
172171
.then(data => {
173172
console.log(data);
174173
document.cookie = "token=" + data.token + "; path=/";
174+
console.log(data)
175175
if (data.status!=401) {
176176
window.location.replace("/Lesson_Frontend/profile/");
177177
} else {
@@ -182,4 +182,3 @@ <h2 style="text-align: center;">Sign Up</h2>
182182
}
183183
</script>
184184

185-

BD_profile.html

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
---
2+
layout: base
3+
permalink: /profile/
4+
title: Profile
5+
search_exclude: false
6+
---
7+
<script>
8+
9+
10+
const decode = token => decodeURIComponent(atob(token.split('.')[1].replace('-', '+').replace('_', '/')).split('').map(c => `%${('00' + c.charCodeAt(0).toString(16)).slice(-2)}`).join(''));
11+
function getCookie(name) {
12+
const value = `; ${document.cookie}`;
13+
const parts = value.split(`; ${name}=`);
14+
if (parts.length === 2) return parts.pop().split(';').shift();
15+
}
16+
email = (JSON.parse(decode(getCookie("token")))).sub;
17+
id = ((JSON.parse(decode(getCookie("token")))).id).toString();
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)
26+
27+
function update() {
28+
//pull from backend and update values
29+
// placeholders
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));
44+
//placeholders
45+
document.getElementById("welcome").innerHTML = "Welcome " + email + "!";
46+
document.getElementById("steps").innerHTML = steps;
47+
document.getElementById("calories").innerHTML = calories;
48+
}
49+
function edit() {
50+
steps = prompt("enter steps count: ")
51+
calories = prompt("enter calories burnt: ")
52+
53+
document.getElementById("steps").innerHTML = steps;
54+
document.getElementById("calories").innerHTML = calories;
55+
}
56+
function post() {
57+
console.log(currentDate);
58+
steps = document.getElementById("steps").innerHTML;
59+
calories = document.getElementById("calories").innerHTML;
60+
61+
var myHeaders = new Headers();
62+
myHeaders.append("Content-Type", "application/json");
63+
64+
var raw = JSON.stringify({
65+
"id": id,
66+
"date": currentDate,
67+
"steps": steps,
68+
"calories": calories
69+
});
70+
71+
var requestOptions = {
72+
method: 'POST',
73+
headers: myHeaders,
74+
body: raw,
75+
redirect: 'follow'
76+
};
77+
78+
fetch("http://localhost:8085/api/person/setStats", requestOptions)
79+
.then(response => response.text())
80+
.then(result => alert("stats updated!"))
81+
.catch(error => console.log('error', error));
82+
}
83+
function delUser() {
84+
85+
var myHeaders = new Headers();
86+
myHeaders.append("Content-Type", "application/json");
87+
88+
var requestOptions = {
89+
method: 'DELETE',
90+
headers: myHeaders,
91+
body: raw,
92+
redirect: 'follow'
93+
};
94+
95+
fetch("http://localhost:8085/api/person/delete?email=" + email, requestOptions)
96+
.then(response => response.text())
97+
.then(data => {
98+
console.log(data);
99+
if (data.status!=401 || data.status!=400) {
100+
document.cookie = "token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
101+
alert("user deleted!")
102+
window.location.replace("/Lesson_Frontend/sign_in/");
103+
} else {
104+
console.log("bad email and password");
105+
}
106+
})
107+
.catch(error => console.log('error', error));
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+
}
114+
</script>
115+
116+
<body onload="update()">
117+
<div style="height: 500px; width: 500px; background-color: blueviolet;">
118+
<div style="Font-size: 32px; text-align: center; height: auto; width: 500px; padding: 10px; position: relative;">
119+
Profile
120+
</div>
121+
<div style="text-align: center; height: auto; width: 500px; padding: 10px; position: relative;">
122+
<div style="font-size: 18px;" id="welcome">Welcome!</div>
123+
<div style="font-size: 18px;">Number of steps: </div><div id="steps"></div>
124+
<div style="font-size: 18px;">Calories burnt: </div><div id="calories"></div>
125+
<button onClick="update()">Refresh</button>
126+
<div></div>
127+
<button onClick="edit()">Edit</button>
128+
<button onClick="post()">Update</button>
129+
<div></div>
130+
<button onClick="signOut()">Sign Out</button>
131+
<div></div>
132+
<button onClick="delUser()">Delete user</button>
133+
</div>
134+
</div>
135+
136+
<script>
137+
</script>
138+
</body>

0 commit comments

Comments
 (0)