diff --git a/index.html b/index.html new file mode 100644 index 00000000..8e80b03d --- /dev/null +++ b/index.html @@ -0,0 +1,47 @@ + + + + + Shamira's Trek + + + + + + +
+ +
+
+

List of Trips

+ + +
+ +
+ +
+ +
+

Reserve Trip!

+
+
+ + +
+
+ + +
+ + +
+
+ + + + +
+ + \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 00000000..05a88c4e --- /dev/null +++ b/index.js @@ -0,0 +1,129 @@ +const TRIPSURL = 'https://trektravel.herokuapp.com/trips/' + + +// error handling from panopto video + +const reportStatus = (message) => { + $('#status-message').html(message); +}; + +// const reportError = (message, errors) => { +// let content = `

${message}

" +// reportStatus(info); +// }; + +// Gets All Trips - from Ada Pets +const loadTrips = () => { + reportStatus('Loading trips...'); + + const tripList = $('#trip-list'); + tripList.empty(); + + + axios.get(TRIPSURL) + .then((response) => { + let listOfTrips = response.data + + reportStatus(`Successfully loaded ${listOfTrips.length} trips`); + console.log('Loading trips works!'); + //loops through response to print the trip name + listOfTrips.forEach((trip) => { + let currentTrip = $(`
  • ${trip.name}
  • `); + currentTrip.addClass(`${trip.id}`); + tripList.append(currentTrip); + }) + }) + .catch((error) => { + reportStatus(`Error while loading trips: ${error.message}`); + console.log(error); + }) +}; +// Trip Details +const detailsTrips = (event) => { + let tripID = event.target.className; + let tripDetailsURL = `https://trektravel.herokuapp.com/trips/${tripID}`; + reportStatus("Loading details..."); + + const tripDetails = $("#details"); + tripDetails.empty(); + + axios.get(tripDetailsURL) + .then((response) => { + reportStatus(`Successfully loaded trip for ${response.data.name}`); + console.log('Succefully loaded trip!'); + + tripDetails.html( + `

    ${response.data.name} in ${response.data.continent}

    +

    Cost: $${response.data.cost}

    +

    Category: ${response.data.category}

    +

    Weeks: ${response.data.weeks}

    +

    About:

    ${response.data.about}

    ` + ) + }) + + .catch((error) => { + reportStatus( + `Encountered an error while loading trips: ${tripID}: ${error.message}` + ); + console.log(error); + }); +}; + +// Make a reservation + +const readFormData = () => { + let parsedData = {}; + + parsedData.name = $("input[name='name']").val(); + parsedData.email = $("input[name='email']").val(); + + return parsedData; + }; + +const clearForm = () => { + $('#trip-form input[name="name"]').val(''); + $('#trip-form input[name="email"]').val(''); +} + +const reservation = (event) => { + + event.preventDefault(); + console.log('this works') + + + const tripID = event.target.className; + const createTripURL = `https://trektravel.herokuapp.com/trips/${tripID}/reservations`; + let tripData = readFormData(); + reportStatus('Sending trip data...'); + + console.log("About to post trip data", tripData) + // keep getting a 404 not found. The data isn't being read + axios.post(createTripURL, tripData) + .then((response) => { + console.log(response); + reportStatus(`Successfully added trip! ${response.data.id}`); + clearForm(); + }) + .catch((error) => { + console.log(error.response) + if(error.response.data && error.response.data.errors) { + reportStatus(`There was an error loading this trip: ${error.message}`, error.response.data.errors); + } else { + reportStatus(`There was an error loading this trip: ${error.message}`); + } + }); +}; + +$(document).on('click', 'li', detailsTrips); +$(document).on('submit', 'form', reservation); + +$(document).ready(() => { + $('#load-trips').click(loadTrips); + $('form').submit(readFormData); +}); diff --git a/style.css b/style.css new file mode 100644 index 00000000..f4161127 --- /dev/null +++ b/style.css @@ -0,0 +1,23 @@ +body { + background-color: #FFC2AD; +} + +main { + display: flex; +} + +h2 { + color: #CC6D4E; +} + +h3 { + color: #FF8960; +} + +li { + color: #CC6D4E +} + +ul { + list-style-type: none; +} \ No newline at end of file