Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
3bfd3b9
Built basic structure of site. Added button to show a list of trips w…
dev-elle-up Jun 2, 2019
f9713f3
Mid-troubleshooting. Can't click a td elememnt. Committing now for a …
dev-elle-up Jun 2, 2019
a1c71dd
Finished the function to show details of individual trip when it is c…
dev-elle-up Jun 3, 2019
9f2c858
Cleaned up comments etc.
dev-elle-up Jun 3, 2019
5649ee0
Adjusted grid layout and HTML to accomodate form. Began implementatio…
dev-elle-up Jun 3, 2019
da74fe3
Implemented functionality to submit a reservation.
dev-elle-up Jun 3, 2019
8e1f922
Rearranged error message handling so it is all done by one function. …
dev-elle-up Jun 8, 2019
561bd67
Fixed clearing reservation form upon clicking submit. Submit button a…
dev-elle-up Jun 8, 2019
0070e66
Started implementing a get request for each trip upon click. Not fini…
dev-elle-up Jun 9, 2019
9e5177f
Clear status message after making reservation and then clicking on an…
dev-elle-up Jun 9, 2019
04f6550
Set price to display two decimal places. Investigated how to get trip…
dev-elle-up Jun 9, 2019
7e36b49
Added a GET request for each individual trip in order to display its …
dev-elle-up Jun 9, 2019
146151a
Cleaned up a few little HTML and CSS things.
dev-elle-up Jun 9, 2019
b912740
Fixed display of reservation details in success status message. Clean…
dev-elle-up Jun 9, 2019
1b54272
Added unbind to initial build of table to remove double, triple, etc …
dev-elle-up Jun 9, 2019
ba0e6e5
Cleaned up comments.
dev-elle-up Jun 9, 2019
50ee636
Moved form to HTML. Working to make components hide and show at appro…
dev-elle-up Jun 9, 2019
c8b0ec4
Fixed duplicate list showing when clicking show all trips button.
dev-elle-up Jun 9, 2019
e6fae9b
Fixed duplicate list showing when clicking show all trips button.
dev-elle-up Jun 9, 2019
c4634c7
List of trips, trip details, and form each show at the right times no…
dev-elle-up Jun 9, 2019
7b4f87a
Merge branch 'refactorForm'
dev-elle-up Jun 9, 2019
a141692
Fixed the form by adding divs back in. Cleaned up all comments. Did a…
dev-elle-up Jun 9, 2019
56dc50f
Fixed details ESLint noticed upon running eslint in terminal.
dev-elle-up Jun 9, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
body {
background-color: #f0f0f0;
color: rgb(34, 34, 34);
margin: 30px 50px;
padding: 10px 10px;
}

h2 {
font-size: 1.5rem;
}

main {
display: grid;
grid-template: 1fr 1fr / 1fr 1fr;
margin-top: 1.5em;
grid-column-gap: 1.5em;
}
#status-message {
margin-bottom: 2em;
}

#trips-list-area {
grid-area: 1 / 1 / span 1 / span 1;
}

#trip-details-area {
grid-area: 1 / 2 / span 1 / span 1;
display: flex;
}

#sign-up-form {
margin-top: 3rem;
}

.bold {
font-weight: bold;
}
55 changes: 55 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<!DOCTYPE html>
<html lang="en" dir="ltr"></html>
<html>
<head>
<title>Trek</title>
<meta charset="utf-8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link href="index.css" media="screen" rel="stylesheet" type="text/css"/>

</head>

<body id='body'>

<section id="status-message"></section>
<h1>Trek</h1>
<button id="btn-show-trips"class="btn btn-outline-info">Show All Trips</button>

<main>
<section id='trips-list-area'>
<table class='table table-striped table-hover '>
<thead></thead>
<tbody id='list-of-trips'></tbody>
</table>
</section>

<section id='details-and-form' >
<section id="trip-details" ></section>

<section id="sign-up-form" hidden>
<hr>
<h2>Reserve your spot!</h2>
<form id='reservation-form' >

<div><label for="guestname">Name</label> <input type="text" name="guestname" required/></div>
<div><label for="email">Email Address</label> <input type="text" name="email" required/></div>
<div><label for="age">Age</label> <input type="number" min="0" name="age"/></div>
<input type="submit" class="btn btn-outline-info" name="make-reservation" value="Make Reservation"/>

</form>
</section>
</section>
</main>




<script src="https://code.jquery.com/jquery-3.4.1.js"
integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
crossorigin="anonymous"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="index.js"></script>


</body>
</html>
155 changes: 155 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
const BASEURL = "https://trektravel.herokuapp.com/trips";

const reportStatus = (message) => {
$('#status-message').html(message);
};

const handleApiError = (error, customMessage) => {
// console.log(error);
// *** need help with figuring out how to test this is working ***

console.log(error.response);
if (error.response.data && error.response.data.errors) {
reportStatus(`${customMessage} Details: ${error.message}, ${error.response.data.errors}`);
} else {
reportStatus(`${customMessage} Details: ${error.message}`);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should respond by also reporting any validation errors. You can loop through the content like we did in Rails.

}
};

// send GET request
// data comes back, store response object in a variable
// loop through the object and display each item on the page

// let allTrips; // not sure this is needed

const getTripDetails = (tripID) => {
axios.get(`${BASEURL}/${tripID}`)
.then((response) => {
const trip = response.data;
$("#trip-details").empty();

$("#trip-details").append(`<h2>${trip.name}</h2>`);
$("#trip-details").append(`<p><span class="bold">ID:</span> ${trip.id}</p>`);
$("#trip-details").append(`<p><span class="bold">Continent:</span> ${trip.continent}</p>`);
$("#trip-details").append(`<p><span class="bold">Category:</span> ${trip.category}</p>`);
$("#trip-details").append(`<p><span class="bold">Weeks:</span> ${trip.weeks}</p>`);
$("#trip-details").append(`<p><span class="bold">Summary:</span><br> ${trip.about}</p>`);
$("#trip-details").append(`<p><span class="bold">Price:</span> $${trip.cost.toFixed(2)}</p>`);

$('#reservation-form').unbind('submit');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good idea to use unbind to remove other event handlers from the form.

$('#reservation-form').submit(reserveTrip);
})
}

const loadTrips = () => {
reportStatus('Loading trips ...');
const listOfTrips = $('#list-of-trips');
// listOfTrips.empty; // this does not work, must use below syntax
$("#list-of-trips").empty();

// clear details and form sections upon clicking Show Trips button
$('#trip-details').empty();
$("#sign-up-form").hide();

axios.get(BASEURL)
.then((response) => {
const allTrips = response.data;
reportStatus(`There are ${allTrips.length} trips available.`);

$('thead').html('<tr><td><h2>AVAILABLE TRIPS</h2></td></tr>')

allTrips.forEach((trip) => {
listOfTrips.append(`<tr id=${trip.id}><td>${trip.name}</td></tr>`);
// onClick(getTripDetails($(`${trip.id}`)); // <-- START HERE FOR REFACTOR TO .ON() INSTEAD OF .BIND()

$(`#${trip.id}`).unbind()
$(`#${trip.id}`).bind('click', {thisTrip:trip}, function(event){
// Chris: I realized when I was just about done with this project that .bind() is deprecated. D: This should be changed to a .on() method instead. I will refactor if I get some more time, or perhaps we could talk through it.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, this still works fine, the better way is to use .on like we did in class.


// {thisTrip:trip} --> thisTrip variable stores the current trip object, whose details/object/payload/hash becomes available through event.data
// console.log("******* trip is: ********");
// console.log(JSON.stringify(event.data));
// {"thisTrip":{"id":74,"name":"Best of New Zealand","continent":"Australasia","category":"everything","weeks":3,"cost":1952.77}}

const tripID = event.data.thisTrip.id;

//remove class='selected' from any other table row
$(".selected").removeClass('selected');

//set class='selected' on this table row
$(`#${tripID}`).addClass('selected');
$('#status-message').empty();

getTripDetails(`${tripID}`);

$("#sign-up-form").removeAttr('hidden');
$("#sign-up-form").show();
});

});//END Each loop

})
.catch((error) => {
// reportStatus(`Error loading trips: ${error.message}`);
// console.log(error);
handleApiError(error, "We encountered a problem loading the list of available trips.");
});
};

const readFormData = () => {
const formData = {};

// Validations are being done in the HTML. Left these here just in case.

const nameFromForm = $(`#reservation-form input[name="guestname"]`).val();
formData['name'] = nameFromForm ? nameFromForm : undefined;

const emailFromForm = $(`#reservation-form input[name="email"]`).val();
formData['email'] = emailFromForm ? emailFromForm : undefined;

const ageFromForm = $(`#reservation-form input[name="age"]`).val();
formData['age'] = ageFromForm ? ageFromForm : 'not provided';

return formData;
};


const clearForm = () => {
$(`#reservation-form input`).not('.btn').val('');
};

const reserveTrip = (event) => {
event.preventDefault();

// gets the HTML attribute called id (#id)
const tripID = $(`.selected`).attr('id');

let reservationData = readFormData();

console.log(reservationData);

reportStatus('Submitting reservation...');

axios.post(`${BASEURL}/${tripID}/reservations`, reservationData)
.then((response) => {
const tripIdNum = $('.selected').attr('id');
reportStatus(`Successfully reserved your trip.
(Trip ID: ${tripIdNum}. Your name: ${response.data.name} Email: ${response.data.email}
Age: ${reservationData.age})`);
clearForm();
})
.catch((error) => {
handleApiError(error, "We encountered a problem submitting a reservation.");
clearForm();
});

// accepted params:
// name (string) required
// age (integer)
// email (string) required
};


$(document).ready(() => {
$('#btn-show-trips').click(loadTrips);
});