forked from Ada-C11/trek
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
90 lines (76 loc) · 2.5 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
88
89
90
const URL = 'https://trektravel.herokuapp.com/trips';
function reportStatus(message) {
$('#status-message').html(message);
}
let trips = {};
function loadTrips() {
reportStatus('Loading trips...');
const tripList = $('#trip-list');
tripList.empty();
axios.get(URL)
.then((response) => {
reportStatus(`Successfully loaded ${response.data.length} trips`);
response.data.forEach((trip) => {
const listItem = $(`<li>${trip.name}</li>`).appendTo(tripList);
listItem.click(() => {
clickTrip(trip.name);
})
trips[trip.name] = trip;
});
})
.catch((error) => {
reportStatus(`Encountered an error while loading trips: ${error.message}`);
console.log(error);
});
}
function clickTrip(tripName) {
const trip = trips[tripName];
const details = $('#details');
details.show();
details.empty();
details.append(`<p><b>Name</b>: ${trip.name}</p>`);
details.append(`<p>Continent: ${trip.continent}</p>`);
details.append(`<p>Category: ${trip.category}</p>`);
details.append(`<p>Weeks: ${trip.weeks}</p>`);
details.append(`<p>Cost: $${trip.cost}</p>`);
details.append(`<p>About:</p>`);
details.append(`<p>This is the trip of your dreams!</p>`);
const form = $('#registration');
form.show();
$('#registration-submit').click((event) => {
reserveTrip(event, trip.id);
});
}
// function createTrip(event) {
// event.preventDefault();
// reportStatus('Sending trip data...');
// axios.post(URL, tripData)
// .then((response) => {
// reportStatus(`Successfully added a trip with ID ${response.data.id}!`);
// clearForm();
// })
// .catch((error) => {
// console.log(error.response);
// reportStatus(`Encountered an error while loading trips: ${error.message}`)
// });
// }
function reserveTrip(event, tripId) {
//prevents page from reloading
event.preventDefault();
//https://developer.mozilla.org/en-US/docs/Web/API/FormData
const reservationData = new FormData($('#registration')[0]);
reportStatus('Reserving trip...');
const URL = `https://trektravel.herokuapp.com/trips/${tripId}/reservations`;
axios.post(URL, reservationData)
.then((response) => {
reportStatus(`Successfully reserved a trip with ID ${response.data.id}!`);
})
.catch((error) => {
console.log(error.response);
reportStatus(`Encountered an error while reserving trips: ${error.message}`)
});
}
$(document).ready(() => {
$('#load').click(loadTrips);
// $('#trip-form').submit(createTrip);
});