forked from AdaGold/trek
-
Notifications
You must be signed in to change notification settings - Fork 48
Sockets - Pauline #32
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
Open
paulentine
wants to merge
6
commits into
Ada-C11:master
Choose a base branch
from
paulentine:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
16d00e5
Add function to load & display trips using axios; add skeleton for ot…
paulentine 927e85a
Clicking `load` twice won't cause duplication
paulentine 89328eb
Grid layout setup & HTML skeleton
paulentine dee4cac
Split load & display trips functions (controller vs view)
paulentine 1cfd6c3
Add functions to load & display trip's details on click
paulentine 2789b67
Add function to reserve trip, with readFormData & clearForm helpers
paulentine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
|
|
||
| body { | ||
| font-family: sans-serif; | ||
| } | ||
|
|
||
| main { | ||
| display: grid; | ||
| grid-template-columns: 1fr 1fr; | ||
| grid-template-rows: 1fr; | ||
| grid-template-areas: | ||
| "list details" | ||
| "list reserve" | ||
| } | ||
|
|
||
| #trips-list { | ||
| grid-area: list; | ||
| } | ||
|
|
||
| #trip-details { | ||
| grid-area: details; | ||
| } | ||
|
|
||
| #reserve-trip { | ||
| grid-area: reserve; | ||
| } | ||
|
|
||
| #status-message { | ||
|
|
||
| } | ||
|
|
||
| #trips-button { | ||
|
|
||
| } | ||
|
|
||
| #reservation-form { | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en" dir="ltr"> | ||
| <head> | ||
| <meta charset="utf-8"> | ||
| <title>Pets with axios</title> | ||
| <script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script> | ||
| <script src="https://unpkg.com/axios/dist/axios.min.js"></script> | ||
| <script type="text/javascript" src="index.js"></script> | ||
| <link rel="stylesheet" href="index.css"> | ||
| </head> | ||
| <body> | ||
|
|
||
| <main> | ||
| <section id="status-message"></section> | ||
|
|
||
| <section> | ||
| <h1>Trek</h1> | ||
| <button id="trips-button">See All Trips</button> | ||
| <ul id="trips-list"></ul> | ||
| </section> | ||
|
|
||
| <section> | ||
| <h1>Trip Details</h1> | ||
| <ul id="trip-details"></ul> | ||
| </section> | ||
|
|
||
| <section id="reserve-trip"> | ||
| <h1>Reserve a Spot on Example Trip</h1> | ||
| <form id="reservation-form"> | ||
| <div> | ||
| <label for="name">Name</label> | ||
| <input type="text" name="name" /> | ||
| </div> | ||
|
|
||
| <div> | ||
| <label for="email">Email</label> | ||
| <input type="text" name="email" /> | ||
| </div> | ||
|
|
||
| <input type="submit" value="Reserve" /> | ||
| </form> | ||
| </section> | ||
| </main> | ||
| </body> | ||
| </html> | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| const URL = 'https://trektravel.herokuapp.com/trips/' | ||
|
|
||
| // Status Management | ||
| const reportStatus = (message) => { | ||
| $('#status-message').html(message); | ||
| }; | ||
|
|
||
| const reportError = (message, errors) => { | ||
| let content = `<p>${message}</p><ul>`; | ||
| for (const field in errors) { | ||
| for (const problem of errors[field]) { | ||
| content += `<li>${field}: ${problem}</li>`; | ||
| } | ||
| } | ||
| content += "</ul>"; | ||
| reportStatus(content); | ||
| }; | ||
|
|
||
| // Wave 1 - Display | ||
| const displayTripsList = (tripsList) => { | ||
| const target = $('#trips-list'); | ||
| target.empty(); | ||
| tripsList.forEach(trip => { | ||
| target.append(`<li id="${trip.id}">${trip.name}</a></li>`); | ||
|
|
||
| const tripID = $(`#${trip.id}`); | ||
| tripID.click(() => loadTripDetails(trip)); | ||
| }); | ||
| } | ||
|
|
||
| // Wave 1 - Load | ||
| const loadTrips = () => { | ||
| reportStatus("loading trips..."); | ||
|
|
||
| axios.get(URL) | ||
| .then((response) => { | ||
| const trips = response.data; | ||
| displayTripsList(trips); | ||
| reportStatus(`Successfully loaded ${trips.length} trips`); | ||
| }) | ||
| .catch((error) => { | ||
| reportStatus(`Encountered an error while loading trips: ${error.message}`); | ||
| console.log(error); | ||
| }); | ||
| } | ||
|
|
||
| // Wave 2 - Display | ||
| const displayTripDetails = (trip) => { | ||
| const target = $('#trip-details'); | ||
| target.empty(); | ||
|
|
||
| target.append(`<h1>Trip Details</h1>`); | ||
| target.append(`<li>ID: ${trip.id}</li>`); | ||
| target.append(`<li>Name: ${trip.name}</li>`); | ||
| target.append(`<li>Continent: ${trip.continent}</li>`); | ||
| target.append(`<li>Category: ${trip.category}</li>`); | ||
| target.append(`<li>Weeks: ${trip.weeks}</li>`); | ||
| target.append(`<li>Cost: $${trip.cost.toFixed(2)}</li>`); | ||
| target.append(`<li>About: ${trip.about}</li>`); | ||
| } | ||
|
|
||
| // Wave 2 - Load | ||
| const loadTripDetails = (trip) => { | ||
| reportStatus(`loading details for trip ${trip.name}`); | ||
|
|
||
| axios.get(URL + trip.id) | ||
| .then((response) => { | ||
| const trip = response.data; | ||
| displayTripDetails(trip); | ||
|
|
||
| reportStatus(`Successfully loaded details for: ${trip.name}`); | ||
| $('#reservation-form').submit(() => reserveTrip(trip)) | ||
| }) | ||
| .catch((error) => { | ||
| reportStatus(`Encountered an error while loading trip: ${error.message}`); | ||
| console.log(error); | ||
| }); | ||
| } | ||
|
|
||
| // Wave 3 | ||
| const readFormData = () => { | ||
| const parsedFormData = {}; | ||
|
|
||
| // const fields = ['name', 'email']; | ||
| // for (let field in fields) { | ||
| // const dataFromForm = $(`#reservation-form input[name=${field}]`).val(); | ||
| // parsedFormData[field] = dataFromForm ? dataFromForm : undefined; | ||
| // } | ||
| parsedFormData.name = $("input[name='name']").val(); | ||
| parsedFormData.email = $("input[name='email']").val(); | ||
| return parsedFormData; | ||
| } | ||
|
|
||
| const clearForm = () => { | ||
| $(`#pet-form input[name="name"]`).val(''); | ||
| $(`#pet-form input[name="email"]`).val(''); | ||
| } | ||
|
|
||
| const reserveTrip = (trip) => { | ||
| event.preventDefault(); | ||
| const reservationData = readFormData(); | ||
|
|
||
| reportStatus(`Sending reservation data for: ${trip.name}`); | ||
|
|
||
| axios.post(URL + trip.id + '/reservations', reservationData) | ||
| .then((response) => { | ||
| reportStatus(`Successfully added a reservation with ID ${response.data.id}!`); | ||
| clearForm(); | ||
| }) | ||
| .catch((error) => { | ||
| console.log(error.response); | ||
| if (error.response.data && error.response.data.errors) { | ||
|
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. This should include: just so that if there's a network error the error can be reported. (no response object for a network error). |
||
| reportError( | ||
| `Encountered an error: ${error.message}`, | ||
| error.response.data.errors | ||
| ); | ||
| } else { | ||
| reportStatus(`Encountered an error: ${error.message}`); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| $(document).ready(() => { | ||
| $('#trips-button').on('click', loadTrips); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
So every time I click on a trip you add another event handler which will run when the form gets submitted. You should use
$('#reservation-form').off()to clear out any existing event handlers first.Oh and nice closure!