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