Skip to content
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

Amy Lee -- Carets #40

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
111 changes: 111 additions & 0 deletions index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
header {
text-align: center;
}

h1, h2, h3 {
font-family: 'Bevan', cursive;
}

ul {
list-style: none;
}

body {
background-color: #ffcf32;
width: 100%;
}

li {
padding: 20px;
border: green dotted .5px;
width: 80%;
margin: 0px auto;
}


#trip-details h4 {
-webkit-margin-before: 0.5em;
-webkit-margin-after: 0.5em;
}

#trip-details p {
-webkit-margin-before: 0;
-webkit-margin-after: 0;
}

.trip-details {
font-weight: lighter;
/*color: lightgreen;*/
}

#trip-details, #trip-list {
margin-top: 20px;
background-color: snow;
}


#message h3 {
color: red;
}

button {
color: black;
background-color: transparent;
box-sizing: border-box;
padding: 2px 6px 3px;
font-size: 1em;
border: black solid 1px;
text-align: center;
}

#new-trip-form {
width: 80%;
margin: 0px auto;
padding-bottom: 10px;
border-radius: 15px;
/*text-align: center;*/
}

#new-trip-form h3, #new-trip-form .button, .button {
text-align: center;
padding-top: 2%;
}

input {
width: 60%;
padding: 12px 20px;
/*margin: 8px 0;*/
display: block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
margin-top: 10px;
}

input:focus {
border: 3px solid #555;
}

label {
width: 20%;
text-align: right;
}

label, input {
display: inline-block;
}

#find-trip-btns {
-webkit-margin-before: 0.35em;
}

#welcome-img {
width: 100%;
text-align: center;
}

#welcome-img img {
margin: 15px auto;
height: 60%;
width: 60%;
}
56 changes: 56 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Trek</title>
<link href="index.css" media="screen" rel="stylesheet" type="text/css">
<link href="https://fonts.googleapis.com/css?family=Bevan" rel="stylesheet">
</head>
<body>
<header>
<h1>
TREK
</h1>
<section id = "main-buttons">

<button id = "all-trips">
See All Trips
</button>

<button id = "add-trip">
Add New Trip
</button>

</section>
</header>

<section id= "message">

</section>
<section id= "trip-details">

</section>
<section id= "trip-list">
<!-- <section class = "filter-buttons">
<p>Filter By:
<select id="filter-query">
<option selected value="default">Please Select</option>
<option value="continent">Continent</option>
<option value="budget">Budget</option>
<option value="weeks">Weeks</option>
<option value="category">Category</option>
</select>
</p>
</section> -->

<ul>
</ul>
</section>
<section id= "welcome-img">
<img src="travel.png" alt= "travel-graphic" />
</section>

<script src="https://code.jquery.com/jquery-3.2.1.js" integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE=" crossorigin="anonymous"></script>
<script src="index.js" type= "text/javascript"></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 @@
/* eslint-disable */
const BASE_URL = 'https://trektravel.herokuapp.com/trips';
const continents = [];
const getTrips = (url) => {
$.get(url, (response) => {
console.log('success!');
console.log(response);
response.forEach((trip) => {
const tripInfo = `<li data-id= ${trip.id} id = "trip-${trip.id}"><strong>${trip.name}</strong> - ${trip.weeks} week(s) in ${trip.continent} </li>`;
$('#trip-list ul').append(tripInfo);
if (!continents.includes(trip.continent.toLowerCase()) && trip.continent) {
Copy link

Choose a reason for hiding this comment

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

if trip.continent is null, the code breaks. Surprisingly enough, if you change the order of this conditional, then it'll work:

if (trip.continent && !continents.includes(trip.continent.toLowerCase())) {

In a conditional with an &&, if trip.continent is falsey, it won't bother evaluating the rest of the conditional

continents.push((trip.continent).toLowerCase());
};
});
}).fail(() => {
$('#message').html('<h3> No trips found :( )</h3>');
}).always(() => {
console.log('Adenture awaits you!');
});
};
const addTrip = () => {
// TODO: Add "Cancel" button to clear add new trip form
const newTripForm = `
<form id= "new-trip-form" action="https://trektravel.herokuapp.com/trips" method="post">
<h3> ADD NEW TRIP </h3>
<section>
<div class = "form-input">
<label>Name:</label>
<input type="text" id="name" name="name"></input>
</div>
<div class = "form-input">
<label>Continent:</label>
<input type="text" id="continent" name="continent"></input>
</div>
<div class = "form-input">
<label>About:</label>
<input type="textfield" id="about" name="about"></input>
</div>
<div class = "form-input">
<label>Category:</label>
<input type="text" id="category" name="category"></input>
</div>
<div class = "form-input">
<label>Weeks:</label>
<input type="integer" id="weeks" name="weeks"></input>
</div>
<div class = "form-input">
<label>Cost:</label>
<input type="float" id="cost" name="cost"></input>
</section>
</div>
<section class="button">
<button type="submitReservation">Add This Trip</button>
</section>
</form>`;
$('#trip-list').prepend(newTripForm);
Copy link

Choose a reason for hiding this comment

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

This particular form doesn't have any HTML that needs to be dynamically generated. Since the form will look the same in all cases, you may consider having this on the html already, and just using jQuery to hide()/show() it when appropriate

$('form').on('submit', function(e) {
e.preventDefault();
const formData = $(this).serialize();
const url = $(this).attr('action');
$.post(url, formData, (response) => {
$('#message').html(`<h3 id= "status-message"> Successfully added new trip: ${response.name}</h3>`);
console.log(`Success! New Trip added`);
// $('#trip-details').empty();
}).fail(() => {
$('#message').html('<h3 id= "status-message"> Error: Unable to create new trip</h3>');
console.log(`Try again.`);
});
});
}
const hideDetails = () => {
console.log('you hid the details');
$('#trip-details').empty();
};
const reserveTrip = (tripID) => {
console.log(`you are reserving this trip with id ${tripID}`);
const reservationForm = `<form id= "reservation-form" action="https://trektravel.herokuapp.com/trips/${tripID}/reservations" method="post">
<section>
<label>Name</label>
<input type="text" id="name" name="name"></input>
</section>

<section class="button">
<button type="submitReservation">Reserve My Spot</button>
</section>
</form>`;
$('#trip-details').append(reservationForm);
$('form').submit(function(e) {
e.preventDefault();
const formData = $(this).serialize();
const url = $(this).attr('action');
$.post(url, formData, (response) => {
$('#message').html(`<h3 id= "status-message"> Successfully reserved this trip for ${response.name}</h3>`);
console.log(`Success! You're on the list.`);
$('#trip-info').toggle();
$('#trip-details form:last-child').empty();
}).fail(() => {
$('#message').html('<h3 id= "status-message"> Sorry, there are no spots left for this trip.</h3>');
console.log(`Sorry, no spots left.`);
});
});
};
const findTrip = (tripID) => {
$.get(`https://trektravel.herokuapp.com/trips/${tripID}`, (response) => {
console.log(response);
const tripInfo = `
<section id= "trip-info">
<h3>${response.name.toUpperCase()}</h3>
<p>Trip ID: ${response.id}
</p>
<p>Trip Destination: ${response.continent}</p>
<p>Duration(in weeks): ${response.weeks}</p>
<p>Category: ${response.category}</p>
<p>Cost: $${(response.cost).toFixed(2)}</p>
<h4>About:</h4>
<p>${response.about}</p>
<div id = "find-trip-btns">
<button id= "hide-details">Hide Details</button>
<button id= "reserve-trip">Reserve this Trip</button>
</div>
</section>
`;
$('#trip-details').append(tripInfo);
$('#hide-details').on('click', () => {
hideDetails();
});
$('#reserve-trip').on('click', () => {
reserveTrip(response.id);
$('#trip-info').toggle();
});
});
};

$(document).ready(() => {
// events
$('#all-trips').on('click', () => {
$('#trip-details').empty();
$('#message').empty();
$('#trip-list form').empty();
$('#welcome-img').toggle();
getTrips(BASE_URL);
});
$('#trip-list ul').on('click', 'li', function() {
const tripID = $(this).attr('data-id');
// $('#trip-details').toggle();
findTrip(tripID);
$('#trip-list ul').empty();
});
$('#add-trip').on('click', () => {
$('#trip-details').empty();
$('#trip-list ul').empty();
console.log('adding a trip');
addTrip();
});
});
Binary file added travel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.