-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
248 lines (202 loc) · 7.71 KB
/
script.js
File metadata and controls
248 lines (202 loc) · 7.71 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
const getElement = (selector) => document.querySelector(selector);
const getElements = (selector) => document.querySelectorAll(selector);
// Toggle icon and topnav
const menuIcon = getElement("#menu-icon");
const topnav = getElement(".topnav");
menuIcon.addEventListener("click", () => {
menuIcon.classList.toggle('bx-x');
topnav.classList.toggle('active');
});
// Open and close buttons for login
const openClosePairs = [
{ open: ".btnOpen", close: ".close", box: ".box" },
{ open: ".btnOpen1", close: ".close1", box: ".box1" },
{ open: ".btnOpen2", close: ".close2", box: ".box2" },
{ open: ".btnOpen3", close: ".close3", box: ".box3" },
{ open: ".btnOpen4", close: ".close4", box: ".box4" },
{ open: ".btnOpen5", close: ".close5", box: ".box5" },
];
openClosePairs.forEach((pair) => {
const openBtn = getElement(pair.open);
const closeBtn = getElement(pair.close);
const box = getElement(pair.box);
openBtn.addEventListener("click", (e) => {
e.preventDefault();
openBtn.style.display = "none";
box.style.display = "block";
});
closeBtn.addEventListener("click", () => {
openBtn.style.display = "inline";
box.style.display = "none";
});
});
// Profile image upload
// Get references to the elements
// Get references to the elements
const imgDiv = document.querySelector(".user-image");
const img = document.querySelector("#photo");
const fileInput = document.getElementById("file");
const selectCam = document.querySelector(".user-image");
const replaceCam = document.getElementById("replaceCam");
// Attach event listener to file input
fileInput.addEventListener("change", function () {
const chosenFile = this.files[0];
if (chosenFile) {
const reader = new FileReader();
reader.addEventListener("load", function () {
img.setAttribute("src", reader.result);
});
reader.readAsDataURL(chosenFile);
}
const selectedCam = selectCam.options[selectCam.selectedIndex].text;
replaceCam.textContent = selectedCam;
});
// API consumption
// For handling Registration
function handleRegister() {
const username = document.getElementById("username");
const email = document.getElementById("email");
const password = document.getElementById("password");
const locate = document.getElementById("location");
const date = document.getElementById("date");
const cpassword = document.getElementById("cpassword");
console.log(username.value, email.value, password.value, locate.value, date.value, cpassword.value);
fetch("https://reach-0hh4.onrender.com/authenticationRoute/register", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
username: username.value,
email: email.value,
password: password.value,
location: locate.value,
date: date.value,
}),
})
.then((res) => res.json())
.then((data) => {
console.log(data);
if (data.success) {
window.location.href = 'index2.html';
} else {
console.error(data.error);
}
})
.catch((err) => {
console.error(err);
});
const selectEmail = document.getElementById("email");
const selectedEmail = selectEmail.options[selectEmail.selectedIndex].text;
const replaceEmail = document.getElementById("replaceEmail");
replaceEmail.textContent = selectedEmail;
const selectPassword = document.getElementById("password");
const selectedPassword = selectPassword.options[selectPassword.selectedIndex].text;
const replacePassword = document.getElementById("replacePassword");
replacePassword.textContent = selectedPassword;
const selectLocation = document.getElementById("locate");
const selectedLocation = selectLocation.options[selectLocation.selectedIndex].text;
const replaceLocation = document.getElementById("replaceCity");
replaceLocation.textContent = selectedLocation;
}
function openModal(modalId) {
const modal = document.getElementById(modalId);
modal.style.display = "block";
}
function closeModal(modalId) {
const modal = document.getElementById(modalId);
modal.style.display = "none";
}
// For handling Login....POST Method
function handlerLogin() {
// Check if email and password are filled
if (!email.value || !password.value) {
console.error("Please fill in all required fields.");
return;
}
// Send a POST request to the login endpoint
fetch("https://reach-0hh4.onrender.com/authenticationRoute/login", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
email: email.value,
password: password.value
})
})
.then((res) => res.json()) // Parse response as JSON
.then((data) => {
console.log(data);
localStorage.setItem('userDate', JSON.stringify(data)); // Store data in local storage
if (data.user) {
window.location.href = 'index2.html'; // Redirect to index2.html if login is successful
} else {
errorMessage.innerHTML = data; // Display error message
}
})
.catch((err) => {
console.error(err);
});
const loginModal = document.getElementById("loginModal");
loginModal.style.display = "none";
}
// For handling Availability
function handlerAvailability() {
const selectedFromCity = document.getElementById('from');
const selectedToCity = document.getElementById('toCity');
const selectedDepartureDate = document.getElementById('departureDate');
if (!selectedFromCity.value || !selectedToCity.value || !selectedDepartureDate.value) {
alert("Please fill in all required fields.");
return;
}
console.log(selectedFromCity, selectedToCity, selectedDepartureDate);
fetch("https://reach-0hh4.onrender.com/reservationRoute", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
from: selectedFromCity.value,
to: selectedToCity.value,
date: selectedDepartureDate.value,
}),
})
.then((res) => res.json())
.then((data) => {
console.log(data);
})
.catch((error) => console.log(err));
const selectElement = document.getElementById("from");
const selectedOption = selectElement.options[selectElement.selectedIndex].text;
const replaceFrom = document.getElementById("replaceFrom");
replaceFrom.textContent = selectedOption;
const selectElement2 = document.getElementById("toCity");
const selectedOption2 = selectElement2.options[selectElement2.selectedIndex].text;
const replaceTo = document.getElementById("replaceTo");
replaceTo.textContent = selectedOption2;
const selectElement3 = document.getElementById("departureDate");
const selectedOption3 = selectElement3.value;
const replaceDate = document.getElementById("replaceDate");
replaceDate.textContent = selectedOption3;
const selectElement4 = document.getElementById("departureDate");
const selectedOption4 = selectElement4.value;
const replaceDate2 = document.getElementById("replaceDate2");
replaceDate2.textContent = selectedOption4;
const selectElement5 = document.getElementById("departureDate");
const selectedOption5 = selectElement5.value;
const replaceDate3 = document.getElementById("replaceDate2");
replaceDate3.textContent = selectedOption5;
const selectElement6 = document.getElementById("departureDate");
const selectedOption6 = selectElement6.value;
const replaceDate4 = document.getElementById("replaceDate4");
replaceDate4.textContent = selectedOption6;
}
document.addEventListener("DOMContentLoaded", function() {
const carSelect = document.getElementById("changeCar");
const carDisplay = document.getElementById("car");
carSelect.addEventListener("change", function() {
const selectedOption = carSelect.options[carSelect.selectedIndex].text;
carDisplay.textContent = selectedOption;
});
});