-
Notifications
You must be signed in to change notification settings - Fork 0
/
property.js
207 lines (162 loc) · 6.12 KB
/
property.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
var names = document.getElementById("property-names");
var result = document.getElementById("result");
var currURL = new URL(window.location.href);
var propId = currURL.searchParams.get("id");
var cid = currURL.searchParams.get("cid");
var cod = currURL.searchParams.get("cod");
var elemCid = document.getElementById("checkInDate")
var elemCod = document.getElementById("checkOutDate")
if (cid!="")elemCid.value=cid;
if (cod!="")elemCod.value=cod;
var check = document.getElementById("includePack")
var form = document.getElementById("book")
var packid;
function getFormData($form){
var unindexed_array = $form.serializeArray();
var indexed_array = {};
$.map(unindexed_array, function(n, i){
indexed_array[n['name']] = n['value'];
});
return indexed_array;
}
//Get by id
var getProperty = async ()=>{
var currURL = new URL(window.location.href);
var id = currURL.searchParams.get("id");
var url = 'http://localhost:3000/getpropertydetails/'+id;
var r = await fetch(url).then(function(response) {
return response.json();
})
.then(function(myJson) {
console.log(myJson[0])
var propName = document.getElementById("propName");
var propLoc = document.getElementById("propLocation");
var propSize = document.getElementById("propSize");
var propAmenities = document.getElementById("propAmenities");
var propDetails = document.getElementById("propDetails");
var ownerName = document.getElementById("ownerName");
var ownerDescription = document.getElementById("ownerDescription");
var price = document.getElementById("price");
var linktoprofile = document.getElementById("linkToOwnerProfile")
linktoprofile.href = "ownerprofile.html?id="+ myJson[0].owner_id
packid = myJson[0].packid;
if (packid==0 || packid==null){
var p = document.getElementById("ifpack");
p.style.display = "none"
}
else{
getPackage()
}
console.log(packid);
price.innerText = "Tk. " + myJson[0].price + "/night"
propName.innerText = myJson[0].propname;
propLoc.innerText = myJson[0].city+", " + myJson[0].country;
propDetails.innerText = myJson[0].prop_desc;
propAmenities.innerText = myJson[0].amenities;
propSize.innerText = "Guests " + myJson[0].no_of_guests + " Beds " + myJson[0].no_of_beds;
ownerName.innerText = myJson[0].owner_name;
ownerDescription.innerText = myJson[0].owner_desc;
})
.catch(error => result.innerText = "No properties found");
}
var getPackage = async ()=>{
var url = 'http://localhost:3000/getpackage/'+packid;
var r = await fetch(url).then(function(response) {
return response.json();
})
.then(function(myJson) {
var packPrice = document.getElementById("packPrice");
var pack = document.getElementById("package");
packPrice.innerText = "Tk. "+ myJson[0].price
pack.innerText = myJson[0].description
})
.catch(error => console.log(error));
}
var getPropertyReview = async ()=>{
var currURL = new URL(window.location.href);
var id = currURL.searchParams.get("id");
var url = 'http://localhost:3000/getpropertyreview/'+id;
var r = await fetch(url).then(function(response) {
return response.json();
})
.then(function(myJson) {
for (var i=0;i<myJson.length;i++){
console.log(myJson[i])
var li = document.createElement("li");
var span = document.createElement("span");
var rating = document.createElement("p");
var review = document.createElement("p");
li.className="collection-item avatar";
span.className = "title";
span.innerText = myJson[i].name;
rating.innerText = "Rating: " + myJson[i].rating;
review.innerText = myJson[i].description;
li.appendChild(span);
span.appendChild(rating);
span.appendChild(review);
var ul = document.getElementById("propReviews");
ul.appendChild(li);
}
})
.catch(error => console.log(error));
}
var E = document.getElementById("error");
async function getPhoto() {
var currURL = new URL(window.location.href);
var id = currURL.searchParams.get("id");
var imageContainer = document.getElementById("photos")
var url = 'http://localhost:3000/photos/'+id;
var r = await fetch(url).then(function(response) {
return response.json();
})
.then(function(myJson) {
for (var i=0;i<myJson.length;i++){
var img = document.createElement("img");
img.src = myJson[i].photo;
img.height = "200";
imageContainer.appendChild(img)
}
}).catch(error=>{
console.log(error);
})
}
var submit = async ()=>{
var data = getFormData($("#book"));
data["id"] =propId;
if(check.checked){
data["packid"]=packid
}
var currURL = new URL(window.location.href);
var url = 'http://localhost:3000/book/';
console.log(data)
var r = await fetch(url, {
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
}).then(function(response) {
return response.json();
})
.then(function(myJson) {
console.log(myJson);
if (myJson[0].bookguest){
E.innerText = "SUCCESSFULLY BOOKED!"
form.style.display = "none"
}
else{
E.innerText = "BOOKING FAILED"
}
})
.catch(error =>{
E.innerText=error;
});
}
form.addEventListener('submit', function (ev) {
ev.preventDefault();
submit();
})
getProperty();
getPropertyReview();
getPhoto();