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

Pipes - Sara Frandsen -Back Trek #31

Open
wants to merge 46 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
39f3f99
create collections/trip_list
Nov 28, 2017
48b0081
create models/trip
Nov 28, 2017
81211ce
begin basic HTML template and import model/collection
Nov 28, 2017
efe63e3
correct parse function
Nov 28, 2017
4ea8c6c
show list of trips from the API on page
Nov 28, 2017
897a943
add form to create a trip
Nov 28, 2017
0907947
create template for single trip view
Nov 29, 2017
a2e2af5
change the name of the template id for list of trips
Nov 29, 2017
3535e4c
commented out code not currently being worked on to make it easier to…
Nov 29, 2017
a327903
add <%- id %> to data-id in trip list tr
Nov 29, 2017
2c22c32
can now show the trip id in the console
Nov 29, 2017
44744d6
added root url to Trip model
Nov 29, 2017
12c25da
used urlRoot to generate a url with an id (currently only in console …
Nov 29, 2017
3ab1432
made changes to function names for clarity, fixed render function for…
Nov 30, 2017
10df1a9
alternate way to show trip's 'about' using its own click event
Nov 30, 2017
51f250c
add basic form to html
Nov 30, 2017
58c6fba
make html form into a template #trips-reservation
Dec 1, 2017
3fe9bdd
changed 'reservation' to 'new trip'
Dec 1, 2017
05d4c5a
button for creating new trip, currently shows list of all trips??
Dec 1, 2017
08e48a6
when add new btn is clicked, form appears on page
Dec 1, 2017
f2dc549
SUCCESSFULLY ADD NEW TRIP
Dec 1, 2017
62cfd3a
create html template for form to make reservation
Dec 1, 2017
05c8f88
add footer
Dec 1, 2017
065478f
add template to app.js for making reservation
Dec 1, 2017
193f141
add color to headers of trip table
Dec 1, 2017
fc01263
added lots of comments across code to clarify what i'm doing
Dec 1, 2017
af54499
reservation form appears when button is clicked inside 'about'
Dec 1, 2017
f75aac1
add comments to organize tackling reservations
Dec 1, 2017
8ddbc7a
reservation form submits SOMETHING using .post
Dec 1, 2017
a5ab1c0
found missing '#' in form submit: new reservation
Dec 2, 2017
1044821
use trip-about data id to set trip_id in reservation
Dec 2, 2017
8a5ee59
adjust spacing and comments
Dec 2, 2017
84dcd99
create button to show list of trips
Dec 2, 2017
e4c440f
add responsive header with image, set 'about' to aside
Dec 3, 2017
9443ac4
shrink banner image when 'show trips' is clicked
Dec 3, 2017
a210658
try to insert 'about' into rows, so far works except for losing ID wh…
Dec 3, 2017
2ad48bd
change font in hero
Dec 3, 2017
a5915ef
make changes to font styling and table styling--colors
Dec 3, 2017
a6fa79b
more color changes made to table
Dec 3, 2017
5d3ded4
changed size of hero
Dec 3, 2017
ef54304
fit list to page, overflow scroll
Dec 4, 2017
1da9c95
'about' shows on the right hand side
Dec 4, 2017
1d23b4d
fixed vh for 'about'
Dec 4, 2017
b7f52d1
'about' hidden until clicked, overflow scroll
Dec 4, 2017
ddc05a9
scroll to bottom of section when making reservation
Dec 4, 2017
ff0e020
remove unnecessary comments
Dec 4, 2017
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
118 changes: 115 additions & 3 deletions dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,131 @@
<html>
<head>
<meta charset="utf-8">
<title>My JavaScript App</title>
<title>Back Trekking</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css?family=Abril+Fatface|Roboto" rel="stylesheet">
</head>
<body>
<header>

<header class="hero">
<div class="hero-section-text">
<h1>Back Trekking</h1>
<span><h5>
<button id="show-trips" class="button secondary hollow">Show Trips</button>
<button id="trip-create-new-btn" class="button secondary hollow">Create New Trip</button>
</h5></span>
</div>
</header>

<main>
<!-- MESSAGES -->
<span id="status-msg"></span>
<span id="confirmation-msg"></span>

<!-- ADD TRIP #trip-create-new -->
<article id="trip-create-new" class="columns large-12 small-12">
<!-- create new trip form -->
</article>

<!-- SINGLE TRIP #trip-about -->
<article id="trip-about" class="columns large-4 small-4 hidden">
</article>
</main>
<!-- TRIP LIST #trip-list -->
<div id="list">
<section>
<table id="trip-list-table" class="hover stack">
<thead>
<th class="sort name" id="trip-name">Name</th>
<th class="sort continent">Continent</th>
<th class="sort category">Category</th>
<th class="sort weeks">Trip Length</th>
<th class="sort cost">Cost (in USD)</th>
</thead>
<tbody id="trip-list">
</tbody>
</table>
</section>
</div>

<!--
<footer>
&copy; 2017 Sara Frandsen
</footer> -->

<!-------------------------->
<!-------------------------->

<!-- STATUS MESSAGE -->
<script type="text/template" id="status-msg-template">

</script>

<!-- SINGLE TRIP TEMPLATE -->
<script type="text/template" id="trip-template">
<h2><%- name %></h2>
<p><%- about %></p>
<button id="new-reservation-btn" class="button secondary hollow">Reserve This Trip</button>
<section id="new-reservation"><!-- reservation form --></section>
</script>

<!-- TRIPS TEMPLATE -->
<script type="text/template" id="trips-template">
<tr data-id="<%- id %>" id="<%- id %>" class="trips">
<td>
<%- name %>
</td>
<td>
<%- continent %>
</td>
<td>
<%- category %>
</td>
<td>
<%- weeks %> week(s)
</td>
<td>
$<%- cost %>
</td>
</tr>
</script>

<!-- CREATE NEW TRIP TEMPLATE -->
<script type="text/template" id="create-new-trip-template">
<h2>Add a Trip</h2>
<form id="add-trip-form">
<label for="name">Name</label>
<input type="text" name="name"></input>
<label for="continent">Continent</label>
<input type="text" name="continent"></input>
<label for="about">About</label>
<input type="text" name="about"></input>
<label for="category">Category</label>
<input type="text" name="category"></input>
<label for="weeks">Weeks</label>
<input type="number" name="weeks"></input>
<label for="cost">Cost</label>
<input type="number" name="cost"></input>

<input type="submit" value="Create New Trip" class="button"></input>
</form>
</script>

<!-- NEW RESERVATION TEMPLATE -->
<script type="text/template" id="new-reservation-template">
<h2>Make Reservation</h2>
<form id="reservation-form">
<label for="name" id="reservation-name">Name</label>
<input type="text" name="name"></input>
<label for="age" id="reservation-age">Age</label>
<input type="number" name="age"></input>
<label for="email" id="reservation-email">Email</label>
<input type="text" name="email"></input>

<input type="submit" value="Make Reservation" class="button"></input>
</form>
</script>

</footer>
<script src="app.bundle.js" type="text/javascript"></script>
</body>
</html>
183 changes: 179 additions & 4 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,188 @@
// Vendor Modules
import $ from 'jquery';
import _ from 'underscore';

// CSS
import './css/foundation.css';
import './css/style.css';
// OUR COMPONENTS
import TripList from './collections/trip_list';
import Trip from './models/trip';
import Reservation from './models/reservation'

// TRIP FIELDS
const TRIP_FIELDS = ['id', 'name', 'about', 'continent', 'category', 'weeks', 'cost'];
const RESERVATION_FIELDS = ['name', 'age', 'email'];

// Trip COLLECTION
const tripList = new TripList();

// TEMPLATES
///// still confused why /////
let tripsTemplate;
let aboutTemplate;
let createNewTripTemplate;
let newReservationTemplate;

// RENDER LIST OF TRIPS
const loadTrips = function loadTrips(tripList) {
const tripsTableElement = $('#trip-list');
tripsTableElement.html('');

tripList.forEach((trip) => {
const generatedHTML = $(tripsTemplate(trip.attributes));
tripsTableElement.append(generatedHTML);
});

// ADD COLOR TO HEADERS
$('th.sort').removeClass('current-sort-field');
$(`th.sort.${ tripList.comparator }`).addClass('current-sort-field');
};

const createNewTripHandler = function(event) {
event.preventDefault();

const tripData = {};
TRIP_FIELDS.forEach((field) => {
const inputElement = $(`#trip-create-new input[name="${ field }"]`);
const value = inputElement.val();
tripData[field] = value;
// clears form after submitted
inputElement.val('');
});

const trip = tripList.add(tripData);
trip.save({}, {
success: (model, response) => {
console.log('Create new trip: success');
console.log('Server response:');
console.log(response);
},
error: (model, response) => {
console.log('Create new trip: failure');
console.log('Server response:');
console.log(response);
},
});
};

///// RESERVATION FORM /////
const newReservationHandler = function(event) {
event.preventDefault();

const reservationData = {};
RESERVATION_FIELDS.forEach((field) => {
const inputElement = $(`#reservation-form input[name="${ field }"]`);
const value = inputElement.val();
reservationData[field] = value;
// clears form after submitted
inputElement.val('');
});

// take reservation trip_id from #trip-about data id
reservationData.trip_id = $('#trip-about').data('id')
const reservation = new Reservation(reservationData);
reservation.save({}, {
success: (model, response) => {
console.log(reservation.url());
console.log('Create new reservation: success');
},
error: (model, response) => {
console.log('Create new reservation: failure');
console.log('Server response:');
console.log(response);
},
});
};

//////////////////////////
///// DOCUMENT READY /////
$(document).ready(() => {
// TEMPLATES
tripsTemplate = _.template($('#trips-template').html());
aboutTemplate = _.template($('#trip-template').html());
createNewTripTemplate = _.template($('#create-new-trip-template').html());
newReservationTemplate = _.template($('#new-reservation-template').html());

$('#show-trips').on('click', function() {
console.log('show trips: clicked');
/// need clarification on this /////
tripList.on('update', loadTrips);
tripList.on('sort', loadTrips);
tripList.fetch({
success: () => {
$('#trip-list-table').show();
console.log('show list table: success');
$('.hero').animate({height:'40vh'});
},
});
});

// RENDER SINGLE TRIP DETAILS TO DOM
$('#trip-list').on('click', 'tr', function() {
$('.hidden').show();
const aboutElement = $('#trip-about');
aboutElement.html('');
console.log('about: clicked');
// uses 'data' from html data-id
let tripID = $(this).data('id');
// set the id onto #trip-about to be used on reservation form
$('#trip-about').data('id', tripID);
console.log(tripID);
// uses tripID to find api address for single trip
let singleTrip = new Trip({id: tripID});
console.log(singleTrip.url());

// model refers to singleTrip
// model.fetch(): takes urlRoot and adds id?
//https://stackoverflow.com/questions/16544984/how-backbone-js-model-fetch-method-works
singleTrip.fetch({
success: (model) => {
const generatedHTML = $(aboutTemplate(model.attributes));
aboutElement.html(generatedHTML);

// RENDER 'RESERVATION' FORM TO DOM
$('#new-reservation-btn').on('click', function() {

$('#trip-about').animate({
scrollTop: $('#trip-about')[0].scrollHeight}, 2000);

const newReservationElement = $('#new-reservation');
newReservationElement.html('');
const generatedHTML = $(newReservationTemplate());
newReservationElement.html(generatedHTML);

console.log('load new reservation form: clicked');
});
},
error: (model) => {
console.log('singleTrip fetch: failure');
console.log(response);
},
});
})

// RENDER 'ADD NEW' FORM TO DOM
$('#trip-create-new-btn').on('click', function() {
const newTripElement = $('#trip-create-new');
newTripElement.html('');
const generatedHTML = $(createNewTripTemplate());
newTripElement.html(generatedHTML);

console.log('load add new form: clicked');
});
})

console.log('it loaded!');
// submit new reservation
$('#trip-about').on('submit', '#reservation-form', newReservationHandler);
// submit new trip
$('#trip-create-new').on('submit', createNewTripHandler);

$(document).ready( () => {
$('main').html('<h1>Hello World!</h1>');
// SORT BY CLICKED FIELD
TRIP_FIELDS.forEach((field) => {
const headerElement = $(`th.sort.${ field }`);
headerElement.on('click', (event) => {
console.log(`Sorting table by ${ field }`);
tripList.comparator = field;
tripList.sort();
});
});
14 changes: 14 additions & 0 deletions src/collections/trip_list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Backbone from 'backbone';
import Trip from '../models/trip';

const TripList = Backbone.Collection.extend({
model: Trip,
url: 'https://ada-backtrek-api.herokuapp.com/trips',

parse: function(response) {
return response;
},
comparator: 'name',
});

export default TripList;
Loading