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

Beylul Kbreab #33

Open
wants to merge 2 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
6 changes: 6 additions & 0 deletions index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.trip-body {
text-align: center;
}
.title{
text-align: center;
}
35 changes: 35 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="index.css">
<title>Trek Website</title>
</head>
<body>
<h1 class="title">Welcome to Trek!</h1>
<button type="button" class="button" id="load-button">Load trips list</button>
<div class="row"><div class="callout secondary" id="single-trip"><section class="trip-details" ></section></div></div>

<section>
<form id="add-trip-form" >
<div class="row">
<fieldset class="large-6 columns fieldset">
<legend>Personal Info</legend>
<label> Enter Details Below To Book This Trip
<input type="text" name="name" placeholder="Name"></input>
<input type="text" name="email" placeholder="Email"></input>
<button class="button" type="submit">Submit</button>
</label>
</fieldset>
</div>
</form>
</section>
<table>
<tr class="trip-headers"></tr>
<tbody class="trip-body"></tbody>
</table>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/foundation/6.2.4-rc2/foundation.min.css">
<script src="https://code.jquery.com/jquery-3.1.1.js" type="text/javascript"></script>
<script src="trek.js" type="text/javascript"></script>
</body>
</html>
88 changes: 88 additions & 0 deletions trek.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
$(document).ready(function() {
var url = 'https://trektravel.herokuapp.com/trips';
$('#add-trip-form').toggle(false);
$('#single-trip').toggle(false);
var failCallback = function(xhr) {
var body = $('.trip-details');
body.empty();
body.append('Sorry can\'t load page!');
$('#add-trip-form').toggle(false);
};

var successCallback = function (response) {
var body = $('.trip-body');
body.empty();
var hrow = $('<tr></tr>');
var name = $('<th>Destination</th>');
hrow.append(name);
body.append(hrow);

$.each(response, function(index, trip){
if (trip.name != 'Person') {
var row = $('<tr></tr>');
var name = $('<td><a href="#" class="name-link" id=' + trip.id + '>' + trip.name + '</a></td>');
row.append(name);
body.append(row);
}
});
toggleTableView(true);
};

$('#load-button').click(function() {
$.get(url, successCallback)
.fail(failCallback);
});

var toggleTableView = function(boolean) {
$('.trip-details').toggle(!boolean);
$('#load-button').toggle(!boolean);
$('table').toggle(boolean);
$('#add-trip-form').toggle(!boolean);
$('#single-trip').toggle(!boolean);
};

var showSuccess = function(trip) {
var section = $('.trip-details');
var id = $('<b>Id:</b><span> ' + trip.id + '</span><br>');
var name = $('<b>Name:</b><span> ' + trip.name + '</span><br>');
var continent = $('<b>Continent:</b><span> ' + trip.continent + '</span><br><br>');
var about = $('<b>About:</b><br><pclass="large-text-justify"> ' + trip.about + '</p>');
var category = $('<b>Category:</b><span> ' + trip.category + '</span><br>');
var weeks = $('<b>Weeks:</b><span> ' + trip.weeks + '</span><br>');
var cost = $('<b>Cost:</b><span> ' +'$' + trip.cost + '</span><br>');

section.empty();
section.append(id, name, continent, about, category, weeks, cost);

toggleTableView(false);
$('#single-trip').toggle(true);

var postCallback = function() {
alert("The reservation was made!");
$('#add-trip-form').toggle(false);
};

var addtripcallback = function(e) {
e.preventDefault();
var reservationData = $(this).serialize();
var reservationUrl = url + '/' + trip.id + '/reserve';
$.post(reservationUrl, reservationData, postCallback)
.fail(showFailure);
};
$('#add-trip-form').submit(addtripcallback);
};

var showFailure = function(xhr) {
var section = $('.trip-details');
section.html('<strong>Error has occurred</strong>');

toggleTableView(false);
};
$('tbody').on('click', 'a', function(e) {
e.preventDefault();
var id = $(this).attr('id');
var showUrl = url + '/' + id;
$.get(showUrl, showSuccess)
.fail(showFailure);
});
});