-
Notifications
You must be signed in to change notification settings - Fork 48
Sockets - Kirsten #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
06c02e0
825030a
f543025
ff5e68c
2eea7b3
91f3d95
785d9b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
|
|
||
|
|
||
| body { | ||
| display: grid; | ||
| margin-left: 25px; | ||
| margin-right: 25px; | ||
| grid-template: 100px fit-content(10%) 1fr 2fr / 1fr 2fr; | ||
| } | ||
|
|
||
| h1, h2, h3 { | ||
| font-family: 'Baloo Da', cursive; | ||
| } | ||
|
|
||
| h1 { | ||
| font-size: 4em; | ||
| color: white; | ||
| } | ||
|
|
||
| ul { | ||
| margin: 0; | ||
| padding: 0; | ||
| } | ||
|
|
||
| li { | ||
| list-style-type: none; | ||
| line-height: 1.5em; | ||
| } | ||
|
|
||
| #logo { | ||
| text-align: center; | ||
| background-color: lightblue; | ||
| grid-column: span 2; | ||
| } | ||
|
|
||
| #load { | ||
| margin-bottom: 25px; | ||
| } | ||
|
|
||
| #trips { | ||
| grid-row: span 2; | ||
| padding-right: 25px; | ||
| } | ||
|
|
||
| #status-message { | ||
| min-height: 10px; | ||
| grid-column: span 2; | ||
| } | ||
|
|
||
| #trip-details { | ||
| grid-row: 3 / span 1; | ||
| min-height: 100px; | ||
| grid-column: 2; | ||
| } | ||
|
|
||
| #form-section { | ||
| grid-column: 2; | ||
| grid-row: 4; | ||
| } | ||
|
|
||
| #submit-div { | ||
| padding-top: 15px; | ||
| } | ||
|
|
||
| .hidden { | ||
| display: none; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
|
|
||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <meta http-equiv="X-UA-Compatible" content="ie=edge"> | ||
| <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> | ||
| <script src="https://unpkg.com/axios/dist/axios.min.js"></script> | ||
| <script src="https://code.jquery.com/jquery-3.3.1.js"></script> | ||
| <script type="text/javascript" src="index.js"></script> | ||
| <link href="https://fonts.googleapis.com/css?family=Baloo+Da&display=swap" rel="stylesheet"> | ||
| <link rel="stylesheet" href="index.css"> | ||
| <title>TREK</title> | ||
| </head> | ||
|
|
||
| <body> | ||
|
|
||
| <header id="logo"> | ||
| <h1>TREK</h1> | ||
| </header> | ||
|
|
||
| <section id="status-message"></section> | ||
|
|
||
| <section id="trips"> | ||
|
|
||
| <button id="load" class="btn btn-primary form-control">Show All Trips</button> | ||
| <ul id="trips-list"></ul> | ||
| </section> | ||
|
|
||
| <section id="trip-details" class="hidden"> | ||
| <h2>Trip Details</h2> | ||
| <h3 id="name"></h3> | ||
| <p id="continent"></p> | ||
| <p id="category"></p> | ||
| <p id="weeks"></p> | ||
| <p id="cost"></p> | ||
| <p id="about"></p> | ||
| </section> | ||
|
|
||
| <section id="form-section" class="hidden"> | ||
| <h2></h2> | ||
| <form id="trip-form"> | ||
| <div id="name-div"> | ||
| <label for="name">Name (required): </label> | ||
| <input type="text" id="customer-name" class="form-control"></input> | ||
| </div> | ||
| <div id="age-div"> | ||
| <label for="age">Age: </label> | ||
| <input type="text" id="age" class="form-control"></input> | ||
| </div> | ||
| <div id="email-div"> | ||
| <label for="name">Email (required): </label> | ||
| <input type="text" id="email" class="form-control"></input> | ||
| </div> | ||
| <div id="hidden-input-div"> | ||
| <input type="hidden" id="tripID"></input> | ||
| </div> | ||
| <div id="submit-div"> | ||
| <input type="submit" value="Submit" class="btn btn-primary form-control"> | ||
| </div> | ||
| </form> | ||
| </section> | ||
| </body> | ||
|
|
||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| const URL = 'https://trektravel.herokuapp.com/trips'; | ||
|
|
||
|
|
||
| const reportStatus = (message, alertClass) => { | ||
| $('#status-message').removeClass(); | ||
| $('#status-message').addClass(alertClass); | ||
| $('#status-message').html(message); | ||
| }; | ||
|
|
||
| const reportError = (message, errors) => { | ||
| let content = `<p>${message}</p>` | ||
| content += "<ul>"; | ||
| for (const field in errors) { | ||
| for (const problem of errors[field]) { | ||
| content += `<li>${field}: ${problem}</li>`; | ||
| } | ||
| } | ||
| content += "</ul>"; | ||
| reportStatus(content, "alert alert-warning"); | ||
| }; | ||
|
|
||
| const clearForm = () => { | ||
| $('#trip-form').get(0).reset(); | ||
| }; | ||
|
|
||
|
|
||
|
|
||
| const loadTrips = () => { | ||
| reportStatus('Loading trips...'); | ||
|
|
||
| const tripList = $('#trips-list'); | ||
| tripList.empty(); | ||
|
|
||
| axios.get(URL) | ||
| .then((response) => { | ||
| reportStatus(`Successfully loaded ${response.data.length} trips`, "alert alert-success"); | ||
| response.data.forEach((trip) => { | ||
| tripList.append(`<li id='${trip.id}'><a href=#>${trip.name}</a></li><hr />`); | ||
| $(`#${trip.id}`).click(function () { | ||
| showTrip(trip.id); | ||
| }); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good use of a callback here to track the trip ID. |
||
| }); | ||
| }) | ||
| .catch((error) => { | ||
| reportStatus(`Encountered an error while loading trips: ${error.message}`); | ||
| console.log(error); | ||
| }); | ||
| }; | ||
|
|
||
| const showTrip = (index) => { | ||
| $('#status-message').removeClass(); | ||
| $('#status-message').html(''); | ||
|
|
||
| axios.get(URL + `/${index}`) | ||
| .then((response) => { | ||
| $('#form-section').removeClass('hidden'); | ||
| $('#trip-form').submit(function (event) { | ||
| $('#tripID').val(response.data.id); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The work being done here is complex enough that it might be worthwhile to break it out into its own function. |
||
| event.preventDefault(); | ||
| makeReservation(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When reserving a second trip, your site makes two POST requests. To reproduce:
The reason for this is that you reuse the same reservation form for each trip, but re-attach the |
||
| }); | ||
|
|
||
| $('#trip-details').removeClass('hidden'); | ||
| $('#form-section h2').html(`Make a Reservation: ${response.data.name}`); | ||
| $('#name').html(`Trip ${response.data.id}: ${response.data.name}`); | ||
| $('#continent').html(`<strong>Continent:</strong> ${response.data.continent}`) | ||
| $('#category').html(`<strong>Category:</strong> ${response.data.category}`) | ||
|
|
||
| let weeksString = 'week'; | ||
| if (response.data.weeks !== 1) { | ||
| weeksString += 's'; | ||
| } | ||
| $('#weeks').html(`<strong>Duration:</strong> ${response.data.weeks} ${weeksString}`) | ||
| $('#cost').html(`<strong>Cost:</strong> $${response.data.cost}`) | ||
| $('#about').html(`<strong>Description:</strong> ${response.data.about}`) | ||
| }) | ||
| .catch((error) => { | ||
| reportStatus(`Encountered an error while loading trip: ${error.message}`); | ||
| console.log(error); | ||
| }); | ||
| }; | ||
|
|
||
| const makeReservation = () => { | ||
| $('#status-message').html(''); | ||
| const tripID = $('#tripID').val(); | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using a hidden HTML field feels like an appropriate way to track the trip ID here. |
||
| const tripData = { | ||
| name: $('#customer-name').val(), | ||
| email: $('#email').val(), | ||
| age: $('#age').val(), | ||
| } | ||
|
|
||
| axios.post(URL+`/${tripID}/reservations`, tripData) | ||
| .then((response) => { | ||
| clearForm(); | ||
| console.log(response); | ||
| reportStatus('Successfully made reservation!', "alert alert-success"); | ||
| }) | ||
| .catch((error) => { | ||
| console.log(error.response); | ||
| if (error.response.data && error.response.data.errors) { | ||
| reportError( | ||
| `Encountered an error: ${error.message}`, | ||
| error.response.data.errors | ||
| ); | ||
| } else { | ||
| reportStatus(`Encountered an error: ${error.message}`, "alert alert-warning"); | ||
| } | ||
| }); | ||
|
|
||
| } | ||
|
|
||
| $(document).ready(() => { | ||
| $('#load').click(loadTrips); | ||
| }); | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like your editor is set up to make 4-space tabs in JavaScript. At Ada we recommend 2-spaces, which will be important when you start working on pair projects. Please update this setting.