forked from AdaGold/trek
-
Notifications
You must be signed in to change notification settings - Fork 47
Shelan's Trek assignment #45
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
sheland
wants to merge
7
commits into
Ada-C10:master
Choose a base branch
from
sheland: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
7 commits
Select commit
Hold shift + click to select a range
83de9f9
can show trip's details
sheland 1422236
created a non-functioning form
sheland 6b06487
erased function
sheland 838dbb7
minor changes
sheland c9a42f8
form complete
sheland b8a4114
html change
sheland 8b3d670
added comments
sheland 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
Empty file.
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,42 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en" dir="ltr"> | ||
| <head> | ||
| <meta charset="utf-8"> | ||
| <title>Trips with axios</title> | ||
| <script | ||
| src="https://code.jquery.com/jquery-3.3.1.js"></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> | ||
| <section id="status-message"></section> | ||
|
|
||
| <main> | ||
| <section class="alltrips"> | ||
| <h1>List of Trips</h1> | ||
| <button id="load">Load Trips!</button> | ||
| <ul id="trip-list"></ul> | ||
| <ul id="trip-details"></ul> | ||
| </section> | ||
|
|
||
| <form id="form"> | ||
| </form> | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| <!-- <section class="form"> | ||
| <form id="trip-form"> | ||
| <div> | ||
| <label for="name">name</label> | ||
| <input type="text"></label> | ||
| </div> | ||
|
|
||
| <div> | ||
|
|
||
|
|
||
| </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,211 @@ | ||
| const URL = 'https://trektravel.herokuapp.com/trips'; | ||
|
|
||
| const reportStatus = (message) => { | ||
| $('#status-message').html(message); | ||
| }; | ||
|
|
||
| const loadTrips = () => { | ||
| const tripList = $('#trip-list'); | ||
| tripList.empty(); //starts as empty till button is clicked | ||
|
|
||
| reportStatus('Loading trips...'); //place before axios call, shows before error and success msg | ||
|
|
||
|
|
||
| // Make an Axios request to get the array with the main list of trips. | ||
| // It's a long array of objects representing trips, all in this form: | ||
| // { | ||
| // "id": 1, | ||
| // "name": "Cairo to Zanzibar", | ||
| // "continent": "Africa", | ||
| // "category": "everything", | ||
| // "weeks": 5, | ||
| // "cost": 9599.99 | ||
| // } | ||
|
|
||
| axios.get(URL) | ||
| .then((response) => { | ||
| // This function runs to handle a successful response | ||
| reportStatus(`It loaded ${response.data.length} fricking trips!`); | ||
| // Now we step through each of the trip in the array given by the API, | ||
| // and add them to our interface. | ||
|
|
||
| // For each trip ... | ||
| response.data.forEach((trip) => { | ||
| // Create a new list item element containing the name of the trip. | ||
| const tag = $(`<li>${trip.name}</li>`); | ||
| // Append our new list item to the trip list element. Now it will appear on the screen. | ||
| tripList.append(tag); | ||
| // Set up the event handler to run when the user clicks on this list item. | ||
| tag.click(() => { | ||
| // (Now we're in the event handler, so this code will only run once the list item | ||
| // has been clicked.) | ||
|
|
||
| // call tripDetails() and pass in the trip object to show the user the details about the trip. | ||
| tripDetails(trip); | ||
| // Now call reservationForm() to build the reservation form for this specific | ||
| // trip and add it to the page. We only pass in the trip ID, since that is all | ||
| // it needs to know in order to build the form. | ||
| reservationForm(trip.id); | ||
| }) | ||
|
|
||
| }) | ||
| }) | ||
| .catch((error) => { //all .catch(callback) to do something when the response fails | ||
| reportStatus(`Encountered an error while loading trips: ${error.message}`); | ||
| console.log(error); | ||
| }); | ||
| }; | ||
|
|
||
| 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); | ||
| }; | ||
|
|
||
| const tripDetails = (trip) => { | ||
| const tripDetailsList = $('#trip-details'); | ||
| tripDetailsList.empty(); | ||
|
|
||
| // Look up this specific trip through the API... | ||
| axios.get(URL + '/' + trip.id) //gets a trip | ||
| .then((response) => { | ||
|
|
||
| const tripData = response.data; | ||
| console.log(trip) | ||
| tripDetailsList.append(`<li>Name: ${tripData.id}</li>`); | ||
| tripDetailsList.append(`<li>Name: ${tripData.name}</li>`); | ||
| tripDetailsList.append(`<li>Category: ${tripData.category}</li>`); | ||
| tripDetailsList.append(`<li>Continent: ${tripData.continent}</li>`); | ||
| tripDetailsList.append(`<li>Cost: ${tripData.cost}</li>`); | ||
| tripDetailsList.append(`<li>Weeks: ${tripData.weeks}</li>`); | ||
| tripDetailsList.append(`<li>About: ${tripData.about}</li>`); | ||
| }) | ||
| } | ||
| const reservationForm = (trip_id) => { | ||
| console.log(trip_id) | ||
| const form =$('#form') | ||
| form.empty(); | ||
|
|
||
| // The "Reserve a Trip" form has 3 inputs: | ||
| // Name (comes from the user) | ||
| // Email (comes from the user) | ||
| // Trip ID (a hidden input that we auto-generate based on the trip | ||
| // that was chosen) | ||
| // | ||
| // So we populate the inside of the form using a template string | ||
| // that is mostly just the raw HTML of the form, but we also inject | ||
| // the trip ID into the code for the hidden input. | ||
|
|
||
|
|
||
| form.append(` | ||
| <h2>Reserve a Trip</h2> | ||
| <div> | ||
| <label for="name">name</label> | ||
| <input type="text" name="name" /> | ||
| </div> | ||
| <div> | ||
| <label for="email">Email</label> | ||
| <input type="text" name="email" /> | ||
| </div> | ||
| <div> | ||
| <input type="hidden" name="trip_id" value="${trip_id}" /> | ||
| </div> | ||
| <input type="submit" name="reserve" value="Reserve" /> | ||
| `); | ||
| } | ||
|
|
||
|
|
||
| // With this function, you take the values from the form (name, email and trip ID) | ||
| // and condense them into an object in the format expected by the API, | ||
| // with the keys name, email and trip_id. | ||
| // For example: | ||
| // { | ||
| // name: "Amy Testington", | ||
| // email: "amy@testington.com", | ||
| // trip_id: 10 | ||
| // } | ||
| //reserving trip | ||
| const readFormData = () => { | ||
| // Initialize an empty object | ||
| const parsedFormData = {}; | ||
|
|
||
| // Get the value from the form input with the name "trip_id" | ||
| // and add it to the output object under the key "trip_id" | ||
|
|
||
| const tripsForm = $(`#form input[name="trip_id"]`).val(); | ||
| parsedFormData['trip_id'] = tripsForm ? tripsForm : undefined; | ||
|
|
||
| const formName = $(`#form input[name="name"]`).val(); | ||
| parsedFormData['name'] = formName ? formName : undefined; | ||
|
|
||
| const formEmail = $(`#form input[name="email"]`).val(); | ||
| parsedFormData['email'] = formEmail ? formEmail : undefined; | ||
|
|
||
| return parsedFormData; | ||
| }; | ||
|
|
||
| const clearForm = () => { | ||
| $(`#form input[name="name"]`).val(''); | ||
| $(`#form input[name="email"]`).val(''); | ||
| $(`#form input[name="trip"]`).val(''); | ||
| } | ||
|
|
||
| const reserveTrip = (event) => { //handler for a `submit` | ||
| console.log(event); | ||
| event.preventDefault(); //prevents page from reloading | ||
|
|
||
| // readFormData takes the values from the form (name, email and trip ID) | ||
| // and condenses them into an object in the format expected by the API, | ||
| // with the keys name, email and trip_id. | ||
| // For example: | ||
| // { | ||
| // name: "Sally Testington", | ||
| // email: "sally@testington.com", | ||
| // trip_id: 10 | ||
| // } | ||
|
|
||
| const tripInfo = readFormData(); //values from the form | ||
| console.log(tripInfo); | ||
|
|
||
| // The URL alone does not have enough information for the API to know | ||
| // what we want, so we also send an object in the POST data with the complete | ||
| // data about the trip (see above). | ||
|
|
||
| axios.post(URL + '/' + tripInfo.trip_id+ '/reservations', tripInfo) | ||
| .then((response) => { | ||
| reportStatus(`Successfully reserved ${response.data.id}!`); | ||
| clearForm(); | ||
| }) | ||
| .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}`); | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
| // When we call $(document).ready() and pass it a callback, | ||
| // then everything inside that callback will run only when the | ||
| // page is fully loaded and ready for jQuery to add stuff. | ||
| $(document).ready(() => { | ||
|
|
||
| // When the button is clicked, loadTrips will be called to download and | ||
| // display the list of trips | ||
| $('#load').click(loadTrips); | ||
|
|
||
| // When "Reserve Trip" button is clicked. | ||
| // reserveTrip() will be called to perform the reservation | ||
|
|
||
| $('#form').submit(reserveTrip); | ||
| }); | ||
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.
You also need a
catchto handle errors here!