-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
87 lines (73 loc) · 2.08 KB
/
index.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
const URL_API = "https://demo1340398.mockable.io/bedrooms";
function formatToReal(number) {
return number.toLocaleString("pt-BR", {
style: "currency",
currency: "BRL",
});
}
function openMap(bedroom) {
$("#myModal").modal({});
const locationBedroom = {
lat: bedroom.lat,
lng: bedroom.lng,
};
const map = new google.maps.Map(document.getElementById("map"), {
zoom: 6,
center: locationBedroom,
});
const marker = new google.maps.Marker({
position: locationBedroom,
map: map,
});
}
function render(bedrooms) {
const bedroomsElements = bedrooms.map((bedroom) => {
const div = document.createElement("div");
div.className = "bedroom";
div.onclick = () => openMap(bedroom);
div.innerHTML = `
<img
class="bedroom__image"
src="${bedroom.photo}"
alt="bedroom"
/>
<div class="bedroom__info">
<span class="bedroom__name">
<b>${bedroom.name}</b>
</span>
<div class="bedroom__location">
<img class="location-icon" src="./assets/icon-location.png" />
<span>${bedroom.location}</span>
</div>
<div class="bedroom__description">
<span>${bedroom.property_type}</span>
<span>${formatToReal(bedroom.price)}</span>
</div>
</div>`;
return div;
});
const containerBedrooms = document.querySelector(".main__bedrooms");
const paginationContainer = document.getElementById("pagination");
containerBedrooms.innerHTML = "";
containerBedrooms.append(...bedroomsElements);
paginationContainer.style.display = "block";
}
function onSubmit(e) {
e.preventDefault();
console.log(e);
}
function handlePagination(page) {
const BEDROOMS_PER_PAGE = 10;
const start = (page - 1) * BEDROOMS_PER_PAGE;
const end = start + 10 <= window.bedrooms.length ? start + 10 : undefined;
const elements = window.bedrooms.slice(start, end);
render(elements);
}
window.onload = function onLoad() {
fetch(URL_API)
.then((res) => res.json())
.then((bedrooms) => {
window.bedrooms = bedrooms;
render(bedrooms.slice(0, 10));
});
};