@@ -7,9 +7,17 @@ var datePurchased = document.getElementById('date');
7
7
var btn = document . getElementById ( 'generateCost' ) ;
8
8
var logoutBtn = document . getElementById ( 'logout' ) ;
9
9
var dataContainer = document . getElementById ( 'data_container' ) ;
10
+ var displayUsername = document . getElementById ( 'display_username' ) ;
10
11
11
12
window . onload = function ( ) {
12
13
displayCurrentItems ( ) ;
14
+ request ( 'username' , 'GET' , function ( err , res ) {
15
+ if ( err ) {
16
+ console . log ( err )
17
+ } else {
18
+ displayUsername . innerText = res
19
+ }
20
+ } )
13
21
} ;
14
22
15
23
//generic xhr XMLHttpRequest
@@ -18,7 +26,6 @@ function request(url, method, cb, body) {
18
26
xhr . onreadystatechange = function ( ) {
19
27
if ( xhr . readyState === 4 ) {
20
28
if ( xhr . status === 200 ) {
21
- console . log ( 'what' , JSON . parse ( xhr . responseText ) )
22
29
cb ( null , JSON . parse ( xhr . responseText ) ) ;
23
30
} else {
24
31
var errorMessage = JSON . parse ( xhr . responseText ) ;
@@ -37,7 +44,6 @@ function displayCurrentItems() {
37
44
clearDataContainer ( ) ;
38
45
var itemContainer = document . createElement ( 'div' ) ;
39
46
itemContainer . className = 'item_container' ;
40
-
41
47
res . forEach ( function ( item , i ) {
42
48
var date = new Date ( res [ i ] . date_purchased ) ;
43
49
var options = { weekday : 'long' , year : 'numeric' , month : 'short' , day : 'numeric' } ;
@@ -86,29 +92,36 @@ btn.addEventListener('click', function(event) {
86
92
request ( 'sumall' , 'GET' , function ( err , res ) {
87
93
if ( err ) console . log ( err ) ;
88
94
clearDataContainer ( ) ;
95
+ var summedCosts = res . summedCosts ;
96
+ var average = res . average ;
97
+ /* Total user Spending */
89
98
var sum_container = document . createElement ( 'div' ) ;
90
99
sum_container . className = 'item_container' ;
91
- var heading = document . createElement ( 'h3' ) ;
92
- var headerText = document . createTextNode ( 'Total user\'s spending' ) ;
93
- heading . appendChild ( headerText ) ;
94
- sum_container . appendChild ( heading ) ;
95
-
96
- res . forEach ( function ( item , i ) {
97
- var displaytext = document . createElement ( 'p' ) ;
98
- displaytext . appendChild ( document . createTextNode ( res [ i ] . user_name + " has spent a total of £" + Number ( res [ i ] . sum ) . toFixed ( 2 ) ) ) ;
100
+ var heading = document . createElement ( 'h3' ) ;
101
+ var headerText = document . createTextNode ( 'Total Spent' ) ;
102
+ heading . appendChild ( headerText ) ;
103
+ sum_container . appendChild ( heading ) ;
104
+ summedCosts . forEach ( function ( item , i ) {
105
+ var displaytext = document . createElement ( 'p' ) ;
106
+ displaytext . appendChild ( document . createTextNode ( summedCosts [ i ] . user_name + " has spent a total of £" + Number ( summedCosts [ i ] . sum ) . toFixed ( 2 ) ) ) ;
99
107
sum_container . appendChild ( displaytext ) ;
100
108
} )
101
109
dataContainer . appendChild ( sum_container ) ;
110
+ /* Average Spending */
111
+ var heading = document . createElement ( 'h3' ) ;
112
+ var headerText = document . createTextNode ( 'The average amount spent was £' + average ) ;
113
+ heading . appendChild ( headerText ) ;
114
+ sum_container . appendChild ( heading ) ;
102
115
} )
103
116
} )
104
117
105
- logoutBtn . addEventListener ( 'click' , function ( event ) {
106
- var xhr = new XMLHttpRequest ( ) ;
107
- xhr . onreadystatechange = function ( ) {
108
- if ( xhr . readyState === 4 && xhr . status === 201 ) {
109
- window . location . href = xhr . getResponseHeader ( 'location' ) ;
110
- }
111
- }
112
- xhr . open ( 'GET' , 'logout' , true ) ;
113
- xhr . send ( ) ;
114
- } )
118
+ logoutBtn . addEventListener ( 'click' , function ( event ) {
119
+ var xhr = new XMLHttpRequest ( ) ;
120
+ xhr . onreadystatechange = function ( ) {
121
+ if ( xhr . readyState === 4 && xhr . status === 201 ) {
122
+ window . location . href = xhr . getResponseHeader ( 'location' ) ;
123
+ }
124
+ }
125
+ xhr . open ( 'GET' , 'logout' , true ) ;
126
+ xhr . send ( ) ;
127
+ } )
0 commit comments