-
Notifications
You must be signed in to change notification settings - Fork 0
/
restaurantApi.js
52 lines (45 loc) · 1.61 KB
/
restaurantApi.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
class RestaurantApi {
constructor(baseURL){
this.baseURL = baseURL;
};
deleteRestaurant(id){
const config = {
method: 'DELETE',
};
fetch(`${this.baseURL}/${id}`, config)
.then(resp => resp.json())
.then(json => alert(json.message))
};
submitForm(event){
event.preventDefault();
console.log("form submitted")
const myForm = document.getElementById('restaurant-form')
const restaurantName = document.getElementById('restaurant-name')
const restaurantNationality = document.getElementById('restaurant-nationality')
const restaurantRating = document.getElementById('restaurant-rating')
const selectCityDropdown = document.getElementById("cityRestaurant")
const formData = {
name: restaurantName.value,
nationality: restaurantNationality.value,
rating: restaurantRating.value,
city_id: selectCityDropdown.value,
}
const configObj = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify(formData)
}
fetch(this.baseURL, configObj)
.then(resp => resp.json())
.then(json => {
const newres = new Restaurant(json)
const city = document.getElementById(`city-${newres.cityId}`);
const appendedRes = newres.getRestaurant();
city.appendChild(appendedRes);
myForm.reset();
});
}
}