Skip to content
87 changes: 87 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="styles/foundation.css">
<title>Treeeekkkkk</title>
</head>
<body>
<section id="errors">

</section>
<section id="message">

</section>
<button class="button" id="load"> Take me away! </button>
<div id="trips-list"></div>
<script id="trips-template" type="text/template">
<section class="individual-trip" id="<%- data.id %>">
<h1>
<%- data.name %>
</h1>
<p>
<strong>Continent:</strong>
<%- data.continent %>
</p>
<p>
<strong>Weeks:</strong>
<%- data.weeks %>
</p>
</section>
</script>


<div id="trip" class="row"></div>

<script id="trip-template" type="text/template">
<section class="column large-6 small-12">
<h1 class="trip-name">
<%- data.name %> - TripID: <%- data.id %>
</h1>
<h3>
<%- data.continent %>
</h3>
<h3>
Weeks: <%- data.weeks %>
</h3>
<h3>
Category: <%- data.category %>
</h3>
<h3>
Price: <%- data.cost %>
</h3>
<p>
<%- data.about %>
</p>

</section>
<section class="column large-5 small-12" id="reserve-form">
<h2>Reserve this trip!</h2>
<form action="https://trektravel.herokuapp.com/trips/<%- data.id %>/reserve" method="post">
<section>
<label>Name</label>
<input type="text" id="name" name="name"></input>
</section>
<section>
<label>Age</label>
<input type="text" id="age" name="age"></input>
</section>
<section>
<label>E-mail</label>
<input type="text" id="email" name="email"></input>
</section>

<button class="button" type="submit">Reserve Trip</button>
</form>
</section>
</script>





<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="index.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js"></script>
</body>
</html>
72 changes: 72 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
var buildUrl = function() {
return "https://trektravel.herokuapp.com/trips";
};

var allTripsCallback = function(response) {
console.log("Success!");
console.log(response);

$("#trips-list").empty();
$("#trip").empty();

var tripsTemplate = _.template($('#trips-template').html());
for (var i = 0; i < response.length; i++) {

var generatedHtml = tripsTemplate({
data: response[i]
});

$('#trips-list').append($(generatedHtml));
}

$(".individual-trip").click(clickHandler2);
};

var tripCallback = function(response) {
console.log("Success!");
console.log(response);

$("#trips-list").empty();
$("#trip").empty();

var tripTemplate = _.template($('#trip-template').html());
var generatedHtml = tripTemplate({
data: response
});
$('#trip').append($(generatedHtml));

$('form').submit(function(e) {
// By default, the form will attempt to do it's own local POST so we want to prevent that default behavior
e.preventDefault();

var url = $(this).attr("action"); // Retrieve the action from the form
var formData = $(this).serialize();

$.post(url, formData, function(response){
$('#reserve-form').html('<p> Trip reserved for ' + this.name + '! </p>');
console.log(response);
}).fail(function() {
$("#errors").html('<p>Reservation failed! Please check you have filled out all fields</p>');
});
});
};

var failureCallback = function() {
console.log("Didn't work :(");
$("#errors").html("<h1>AJAX request failed!</h1>");
};

var clickHandler2 = function() {
var url2 = "https://trektravel.herokuapp.com/trips/" + this.id;
$.get(url2, tripCallback).fail(failureCallback);
};

var tripsClickHandler = function() {
$.get(url, allTripsCallback).fail(failureCallback);
};

$(document).ready(function() {
url = buildUrl();

$('#load').click(tripsClickHandler);
});
3 changes: 3 additions & 0 deletions styles/app.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
html {
color: green;
}
Loading