-
Notifications
You must be signed in to change notification settings - Fork 47
LEANNE - NODES #43
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?
LEANNE - NODES #43
Changes from all commits
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,7 @@ | ||
| .hidden { | ||
| display: none; | ||
| } | ||
|
|
||
| article:hover { | ||
| cursor: pointer; /*for indicating that it can be clicked...*/ | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <title>Go Trekking!</title> | ||
| <meta charset="utf-8"> | ||
| <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous"> <!--bootstrap--> | ||
| <link href="index.css" media="screen" rel="stylesheet" type="text/css"/> | ||
| </head> | ||
|
|
||
| <body> | ||
| <header> | ||
| <h1> Trek </h1> | ||
| <nav> | ||
| <a href="index.html">HOME </a> | ||
| </nav> | ||
| </header> | ||
| <main> | ||
| <button id="trip-view" class="btn">View Trips</button> | ||
| <section id="trip-info"> | ||
|
|
||
| </section> | ||
| </main> | ||
| <footer> | ||
| </footer> | ||
|
|
||
| <script src="https://unpkg.com/axios/dist/axios.min.js"></script> | ||
| <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script> <!-- external link to jQuery library--> | ||
| <script src="index.js" type="text/javascript"></script> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| const URL = "https://trektravel.herokuapp.com/trips/" //travel trip API | ||
|
|
||
| // =======STATUS MESSAGES======== | ||
| const reportStatus = (message) => { | ||
| $('#status-message').html(message); | ||
| }; | ||
|
|
||
| // =======HIDDEN TOGGLE========= | ||
| const toggleMe = (id) => { //hide and show items | ||
| $(`article[id$=${id}] h3`).click(() => { //when article ending with id is clicked | ||
| $(`summary[id=trip-${id}]`).toggleClass("hidden"); //unhide loaded trip information | ||
| $(`form`).toggleClass("hidden"); //unhide form info | ||
| clearForm() ; | ||
| $(`form`).submit( {"trip": id}, submitReservation); //FORM SUBMISSION FUNCTION, PASS ON DATA TO BE USED IN FORM, only submits per each trip | ||
| $(`article[id!=trip-info-${id}]`).toggleClass("hidden"); //hide trips that do not equal this article's id | ||
| }); | ||
| } | ||
|
|
||
| // =======FORM DATA======== | ||
| const readFormData = () => { | ||
| const parsedFormData = {}; | ||
|
|
||
| const nameFromForm = $( `input[name='name']`).val(); | ||
| parsedFormData['name'] = nameFromForm ? nameFromForm : undefined; | ||
| console.log(nameFromForm); | ||
|
|
||
| const emailFromForm = $(`input[name='email']`).val(); | ||
| console.log(emailFromForm); | ||
| parsedFormData['email'] = emailFromForm ? emailFromForm : undefined; | ||
|
|
||
| console.log() | ||
| console.log(parsedFormData); | ||
| return parsedFormData; | ||
|
|
||
| }; | ||
|
|
||
| const clearForm = () => { //clear out form after submission | ||
| $('form')[0].reset(); | ||
| } | ||
|
|
||
| // =======FORM SUBMISSIONS======= | ||
| const submitReservation = (event) => { | ||
| const tripID = event.data.trip | ||
| event.preventDefault(); //prevents reload | ||
| const reserveData = readFormData(); | ||
| const sendData = { | ||
| 'name': reserveData["name"], | ||
| 'email': reserveData["email"] | ||
| }; | ||
| console.log(tripID); | ||
|
|
||
| const postURL = URL+tripID+"/reservations"; | ||
|
|
||
| axios.post(postURL, sendData) | ||
| .then((response) => { | ||
| clearForm; | ||
|
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 is a function reference, not a function call, so the form isn't getting cleared. |
||
| reportStatus(`Successfully submitted reservation ${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 |
||
| }) | ||
| .catch((error) => { | ||
| console.log(error.response); | ||
| if (error.response.data && error.response.data.errors) { | ||
| reportStatus( | ||
|
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. Again, report status isn't able to access an html element here, so nothing gets rendered. |
||
| `Encountered an error: ${error.message}`, | ||
| error.response.data.errors | ||
| ); | ||
| } else { | ||
| reportStatus(`Encountered an error: ${error.message}`); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| // =======TRIPS LIST VIEW======== | ||
| const allTripView = () => { | ||
| let tripInfo = []; | ||
| const tripsSection = (data) => {return $(` | ||
| <article id='trip-info-${data.id}'> | ||
| <h3 class='name'> ${data.name}, ${data.continent} </h3> | ||
| <summary id='trip-${data.id}' class='hidden'> | ||
|
|
||
| </summary> | ||
| </article> | ||
| `) | ||
| }; | ||
|
|
||
| axios.get(URL) | ||
| .then((response) => { | ||
| tripInfo = response.data; | ||
| console.log(tripInfo); | ||
| tripInfo.forEach((trip) => { | ||
| $("#trip-info").append(tripsSection(trip)); | ||
| singleTripInfo(trip.id); //DOWNLOAD TRIP INFORMATION | ||
| toggleMe(trip.id); //TOGGLE FUNCTION WHEN ARTICLE CLICKED | ||
| }); | ||
| }) | ||
| .catch((error) => { | ||
| reportStatus(`Encountered an error while loading trips: ${error.message}`); | ||
| console.log(error); | ||
| }); | ||
| } | ||
|
|
||
| // ========CREATE FORM =============== | ||
| const createForm = () => { | ||
|
|
||
| return $(` | ||
| <form class='hidden'> | ||
| <label for="name">Name</label> | ||
| <input type="text" name="name" /> | ||
| <label for="email">eMail</label> | ||
| <input type="text" name="email" /> | ||
|
|
||
| <input type="submit" name="reservation" value="Reserve Your Space Now!"/> | ||
| </form> | ||
| `) | ||
| } | ||
|
|
||
| // ==========SINGLE TRIP INFO========= | ||
| const singleTripInfo = (id) => { | ||
| let singleTrip = []; | ||
| const idURL = URL+id; | ||
|
|
||
| const singleTripInfo = (data) => { return $(` | ||
| <ul> | ||
| <li> ${data.id} </li> | ||
| <li> ${data.category} </li> | ||
| <li> ${data.weeks} </li> | ||
| <li> ${data.cost} </li> | ||
|
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. Why isn't any of this data labeled? |
||
| </ul> | ||
| <p> ${data.about} </p> | ||
| `)}; | ||
|
|
||
| axios.get(idURL) | ||
| .then((response) => { | ||
| singleTrip = response.data; | ||
| $(`#trip-${id}`).append(singleTripInfo(singleTrip)); // APPEND TO SUMMARY WITH TRIP ID | ||
| }) | ||
| .catch((error) => { | ||
| reportStatus(`Encountered an error while loading trips: ${error.message}`); | ||
| console.log(error); | ||
| }); | ||
| }; | ||
|
|
||
|
|
||
| $(document).ready(() => { | ||
| $("#trip-view").click( () => { | ||
| allTripView(); | ||
| $("#trip-view").hide(); | ||
| }); | ||
| $('main').append(createForm); //only one form for everyone | ||
| }); | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| // | ||
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.
this is a slick one-liner!