-
Notifications
You must be signed in to change notification settings - Fork 0
/
functionality.js
214 lines (209 loc) · 8.54 KB
/
functionality.js
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
var session = "";
var userid = 0;
var foodplaceid = 0;
function setEvents() {
let rq = new XMLHttpRequest();
let places = document.getElementById("foodplaces");
let searchform = document.getElementById("searchform");
document.body.addEventListener("click", e => {
if(e.target && e.target.matches("button#search-btn")) {
let budget = document.getElementById("budget").value;
let food = document.getElementById("food").value;
let loc = document.getElementById("location").value;
rq.onreadystatechange = () => {
if(rq.readyState == 4) {
searchform.style.display = "none";
places.style.display = "flex";
let foodPlace = rq.responseText;
let parser = new DOMParser();
let xmlDoc = parser.parseFromString(foodPlace, "text/xml");
let foodPlaceTag = xmlDoc.getElementsByTagName("foodplace");
if(foodPlaceTag.length != 0 ) {
places.innerHTML = "<h4 id='search-fp--title'>Good day young foodie! We have found " + foodPlaceTag.length + " great foodplace/s near you! Enjoy your budget meal!</h4>";
}
else {
places.innerHTML = "<h4 id='search-fp--title'>Good day young foodie! The queries you entered does not have any match from our records. Please try with more specific queries next time. Happy to serve!</h4>";
}
for (let i=0; i<foodPlaceTag.length; i++) {
foodplaceid = foodPlaceTag[i].childNodes[0].textContent;
let name = foodPlaceTag[i].childNodes[1].textContent;
let add = foodPlaceTag[i].childNodes[2].textContent;
let menu = foodPlaceTag[i].childNodes[3].textContent;
let price = foodPlaceTag[i].childNodes[4].textContent;
places.insertAdjacentHTML('beforeend', "<div class='fp'></p><p class='search-fp--name'>" + name + "</p><p class='search-fp--add'>@"+add+"</p><p class='search-fp--menu'> Menu: "+menu+"</p><p class='search-fp--price'>Price: "+price+"</p><button id='review-btn' onclick='addReview();'>Write a review</button></div>");
}
}
};
rq.open("GET", "/budgetmealapp/search?amount="+budget+"&food="+food+"&location="+loc+"&id="+session, true);
rq.setRequestHeader("Content-Type", "text/xml");
rq.send();
}
else if(e.target && e.target.matches("button#post-btn")) {
let name = document.getElementById("fpname").value;
let address = document.getElementById("fpaddress").value;
let menu = document.getElementById("fpmenu").value;
let price = document.getElementById("fpprice").value;
let photo = document.getElementById("fpphoto").value;
rq.onreadystatechange = () => {
if(rq.readyState == 4) {
alert("FoodPlace added!");
}
};
rq.open("POST", "/budgetmealapp/addfoodplace?id="+session, true);
rq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
rq.send("fpname="+name+"&fpaddress="+address+"&fpmenu="+menu+"&fpprice="+price+"&fpphoto="+photo);
}
else if(e.target && e.target.matches("button#account-btn")) {
let fname = document.getElementById("fname").value;
let lname = document.getElementById("lname").value;
let age = document.getElementById("age").value;
let un = document.getElementById("user").value;
let pw = document.getElementById("pw").value;
let email = document.getElementById("email").value;
let loc = document.getElementById("loc").value;
let food = document.getElementById("food").value;
let en = document.getElementById("place").value;
let user = document.getElementById("usertype").value;
rq.onreadystatechange = () => {
if(rq.readyState == 4) {
location.href = "/budgetmealapp/login";
}
};
rq.open("POST", "/budgetmealapp/addperson?id="+session, true);
rq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
rq.send("firstname="+fname+"&lastname="+lname+"&age="+age+"&username="+un+"&password="+pw+"&email="+
email+"&location="+loc+"&food="+food+"&place="+en+"&usertype="+user);
}
else if(e.target && e.target.matches("button#login-btn")) {
let un = document.getElementById("username").value;
let pass = document.getElementById("password").value;
rq.onreadystatechange = () => {
if(rq.readyState == 4) {
session = rq.responseText;
let jsession = JSON.parse(session);
if(session.includes("not match")) {
document.getElementById("login-error").innerHTML = "<small>"+session+"</small>";ssss
}
else {
localStorage.setItem("sessionid", jsession[0].sessionid);
localStorage.setItem("userid", jsession[0].userid);
userid = jsession[0].userid;
if(jsession[0].usertype == "foodie") {
location.href="/budgetmealapp/search-place";
}
else if(jsession[0].usertype == "manager") {
location.href="/budgetmealapp/postfoodplace";
}
}
}
};
rq.open("GET", "/budgetmealapp/login?username="+un+"&password="+pass, true);
rq.setRequestHeader("Content-Type", "text/html");
rq.send();
}
else if(e.target && e.target.matches("a#logout-btn")) {
localStorage.clear();
}
});
}
function addReview() {
let popup = document.createElement('div');
popup.className = 'fullscreenpopup';
let container = document.createElement('div');
container.className = 'fullscreenpopup_container';
popup.appendChild(container);
let content = document.createElement('div');
content.className = 'fullscreenpopup_content';
container.appendChild(content);
let p1 = document.createElement('p');
p1.appendChild(document.createTextNode("Write a review"));
content.appendChild(p1);
//review text
let review_txt = document.createElement('p');
let input_text = document.createElement('textarea');
input_text.setAttribute('placeholder', 'Type your review');
input_text.setAttribute('id', 'review-text');
input_text.setAttribute('cols', '60');
input_text.setAttribute('rows', '15');
input_text.setAttribute('wrap', 'on');
review_txt.appendChild(input_text);
content.appendChild(review_txt);
//review rating
let review_rating = document.createElement('p');
let select_type = document.createElement('select');
select_type.setAttribute('id', 'review-rating');
let rating_option = document.createElement('option');
rating_option.value = "";
rating_option.text = "Select Rating";
let one_option = document.createElement('option');
one_option.value = "1";
one_option.text = "1";
let two_option = document.createElement('option');
two_option.value = "2";
two_option.text = "2";
let three_option = document.createElement('option');
three_option.value = "3";
three_option.text = "3";
let four_option = document.createElement('option');
four_option.value = "4";
four_option.text = "4";
let five_option = document.createElement('option');
five_option.value = "5";
five_option.text = "5";
select_type.add(rating_option, null);
select_type.add(one_option, null);
select_type.add(two_option, null);
select_type.add(three_option, null);
select_type.add(four_option, null);
select_type.add(five_option, null);
review_rating.appendChild(select_type);
content.appendChild(review_rating);
//reviewerid
let input_rid = document.createElement('input');
input_rid.setAttribute('type', 'text');
input_rid.setAttribute('hidden', true);
input_rid.setAttribute('id', 'reviewer-id');
content.appendChild(input_rid);
//foodplaceid
let input_fid = document.createElement('input');
input_fid.setAttribute('type', 'text');
input_fid.setAttribute('hidden', true);
input_fid.setAttribute('id', 'foodplace-id');
content.appendChild(input_fid);
let p2 = document.createElement('p');
content.appendChild(p2);
let button = document.createElement('button');
button.setAttribute('id', 'addreview-btn');
button.appendChild(document.createTextNode('Post'));
button.onclick = () => {
let text = document.getElementById("review-text").value;
let rating = document.getElementById("review-rating").value;
let rq = new XMLHttpRequest();
rq.onreadystatechange = () => {
if(rq.readyState == 4) {
console.log("Review Success!");
console.log(userid);
console.log(foodplaceid);
}
};
rq.open("POST", "/budgetmealapp/addreview", true);
rq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
rq.send("text="+text+"&rating="+rating+"&foodplace="+foodplaceid+"&reviewer="+userid);
document.body.removeChild(popup);
}
p2.appendChild(button);
//cancelbutton
let p_cancel = document.createElement('p');
content.appendChild(p_cancel);
let cancelBtn = document.createElement('button');
cancelBtn.setAttribute('id', 'cancel-btn');
cancelBtn.onclick = () => {
document.body.removeChild(popup);
}
cancelBtn.appendChild(document.createTextNode('Cancel'));
p_cancel.append(cancelBtn);
document.body.appendChild(popup);
}
window.onload = () => {
setEvents();
}