-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
161 lines (150 loc) · 5.66 KB
/
app.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
let baliseVols = document.getElementById('vols')
let chambres = document.getElementById('chambres')
let submitHotelForm = document.getElementById('hotelForm')
let dateArrive = document.getElementById('dateArrive')
let dateDepart = document.getElementById('dateDepart')
let idSelectedRoom = null
let maxRoom = null
let onClickFirst = []
const serverIP = "host.docker.internal"
const serverName = "java-restserver"
let headers = new Headers()
headers.append('Content-Type', 'application/json')
headers.append('Accept', 'application/json')
headers.append('Access-Control-Allow-Origin', '*')
headers.append('Access-Control-Allow-Credentials', 'true')
headers.append('GET', 'POST', 'OPTIONS')
function initKeycloak() {
var keycloak = new Keycloak();
keycloak.init({
onLoad:'login-required'
}).then(function(authenticated) {
headers.append('Authorization', 'Bearer ' + keycloak.token)
initSecurised()
}).catch(function(err) {
initNotSecurised()
});
}
function initSecurised() {
initPlaces("http://"+serverIP+":8090/" + serverName + "/api/securised/compagnies/company_1/vols/17/places")
initChambres("http://"+serverIP+":8090/" + serverName + "/api/securised/rooms")
initForm("securised")
}
function initNotSecurised() {
initPlaces("http://"+serverIP+":8090/" + serverName + "/api/notsecurised/compagnies/company_1/vols/17/places")
initChambres("http://"+serverIP+":8090/" + serverName + "/api/notsecurised/rooms")
initForm("notsecurised")
}
function initForm(isSecurised) {
dateDepart.setAttribute('disabled', 'disabled')
dateArrive.addEventListener('change', function() {
if(dateArrive.value != "") {
dateDepart.removeAttribute('disabled')
dateDepart.setAttribute('min', dateArrive.value)
}
else {
dateDepart.setAttribute('disabled', 'disabled')
}
})
submitHotelForm.addEventListener('submit', function(event) {
event.preventDefault()
if(idSelectedRoom == null) {
alert("Veuillez sélectionner une chambre")
}
else {
let url = "http://"+serverIP+":8090/" + serverName + "/api/" + isSecurised + "/" + "rooms/" + idSelectedRoom
fetch(url, {
method: 'POST',
headers: headers,
body: JSON.stringify({dateDepart: dateDepart.value, dateArrive: dateArrive.value})})
.then(function(response) {
response.json().then(function(response) {
response ? alert('Réservation réussis') : alert('Impossible de réserver à ces dates')
location.reload()
})
})
}
})
}
function initPlaces(url) {
fetch(url, {
method: 'GET',
headers: headers})
.then(function(response) {
response.json().then(function(response) {
response.forEach(element => {
let button = document.createElement('a')
button.innerHTML = element.numPlace
button.className = element.available ? 'btn btn-success' : 'btn btn-dark disabled'
button.setAttribute('role', 'button')
if(element.available) {
button.addEventListener('click', function(event) {
fetch(url + "/" + element.numPlace, {
method: 'POST',
headers: headers
})
.then(function(response) {
response.json().then(function(response) {
button.className = response ? 'btn btn-dark disabled' : 'btn btn-warning disabled'
response ? alert('Réservation réussis') : alert('Impossible de réserver cette place')
location.reload()
})
})
.catch(err => console.error(err))
})
}
baliseVols.appendChild(button)
});
}
)})
.catch(err => console.error(err))
}
function initChambres(url) {
fetch(url, {
method: 'GET',
headers: headers})
.then(function(response) {
response.json().then(function(response) {
let i = 1
response.forEach(element => {
let button = document.createElement('a')
button.innerHTML = element.roomNumber
button.className = 'btn btn-success'
button.id = i
button.setAttribute('role', 'button')
onClickFirst[i] = listenerFirstClick.bind(null, element.roomNumber)
button.addEventListener('click', onClickFirst[i])
chambres.appendChild(button)
i++
})
maxRoom = i
})
})
.catch(err => console.error(err))
}
function listenerFirstClick(index) {
idSelectedRoom = index
for(j = 1; j < maxRoom; j++) {
btn = document.getElementById(j)
btn.removeEventListener('click', onClickFirst[j])
if(j != idSelectedRoom) {
btn.className = 'btn btn-dark disabled'
}
else {
btn.className = 'btn btn-warning'
btn.addEventListener('click', listenerSecondClick)
}
}
}
function listenerSecondClick() {
for(j = 1; j < maxRoom; j++) {
btn = document.getElementById(j)
if(j == idSelectedRoom) {
btn.removeEventListener('click', listenerSecondClick)
}
btn.className = 'btn btn-success'
onClickFirst[j] = listenerFirstClick.bind(null, j)
btn.addEventListener('click', onClickFirst[j])
}
idSelectedRoom = null
}