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

Jocelyn Gonzalez -- Carets #39

Open
wants to merge 6 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
130 changes: 128 additions & 2 deletions dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,137 @@
</head>
<body>
<header>

<h1>BackTrek</h1>
</header>
<main>

<main class='row'>

<aside class='add-trip columns medium-12'>
<section class='row'>
<h2 class='add columns small-12'>Add a Trip</h2>
</section>
<section id="status-messages">
<ul class='columns medium-12'></ul>
<!-- <button class="clear float-right columns medium-2">
<img src="http://www.pvhc.net/img223/xeiwtpbbgpmyhtcnjvwt.png" alt="Clear">
</button> -->
</section>
<form id="add-trip-form">
<section class='row'>
<section class='columns medium-6 small-12'>
<label for="name">name</label>
<input type="text" name="name"></input>
</section>
<section class='columns medium-3 small-12'>
<label for="continent">continent</label>
<input type="text" name="continent"></input>
</section>
<section class='columns medium-3 small-12'>
<label for="category">category</label>
<input type="text" name="category"></input>
</section>
<section class='columns medium-6 small-12'>
<label for="about">about</label>
<input type="text" name="about"></input>
</section>
<section class='columns medium-3 small-12'>
<label for="weeks">weeks</label>
<input type="number" name="weeks"></input>
</section>
<section class='columns medium-3 small-12'>
<label for="cost">cost</label>
<input type="number" name="cost"></input>
</section>
<input type="submit" value="Add It!" class="columns large-12 button"></input>
</section>
</form>
</aside>

<!-- <main class='row'> -->
<section id="summary" class='columns medium-5 small-12'>
<section class='info'>
<p>Summary goes here</p>
</section>
</section>

<section class='trips-table columns medium-7 small-12'>
<button class='all-trips'>View All Trips</button>

<table>
<thead>
<th class="sort name">name</th>
<th class="sort id">id</th>
<th class="sort continent">continent</th>
<th class="sort category">category</th>
<th class="sort weeks">weeks</th>
</thead>
<tbody id="trip-list">
</tbody>
</table>
<!-- <button id='top'>Return to Top</button> -->
</section>


</main>

<script type="text/template" id="trip-template">
<tr class="trip" data-id="<%- id %>">
<td><%- name %></td>
<td><%- id %></td>
<td> <%- continent %></td>
<td>
<%- category %>
</td>
<td>
<%- weeks %>
</td>
</tr>
</script>

<script type="text/template" id="info-template">
<section class="info" name="info" data-id=<%- id %>>
<p><%- name %></p>
<p>
<%- continent %>
</p>
<p>
<%- about %>
</p>
<p>
<%- category %>
</p>
<p>
<%- weeks %>
</p>
<p>
<%- cost %>
</p>
<p>
<%- id %>
</p>
</section>
<section id='message'></section>

<form id ='add-reservation-form' dataid=<%- id %>>
<section>
<label>Name</label>
<input type="text" name="name"></input>
</section>
<section>
<label>Age</label>
<input type="text" name="age"></input>
</section>
<section>
<label>Email</label>
<input type="text" name="email"></input>
</section>
<section>
<input type='submit' value='Reserve Now'></input>
</section>
</form>
</section>
</script>

<footer>

</footer>
Expand Down
161 changes: 159 additions & 2 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,165 @@ import _ from 'underscore';
import './css/foundation.css';
import './css/style.css';

console.log('it loaded!');
import Trip from './app/models/trip';
import TripList from './app/collections/trip_list'
import Reservation from './app/models/reservation'


const tripList = new TripList();
let trip;
let tripTemplate;

const render = function render(tripList) {
const $tripList = $('#trip-list');
$tripList.empty();
tripList.forEach((trip) => {
$tripList.append(tripTemplate(trip.attributes));
});
};

const loadTrips = function loadTrips(){
tripList.on('update', render, tripList);
tripList.fetch();
};

let infoTemplate;

const loadTripDetails = function loadTripDetails(id) {
trip = tripList.get(id);
trip.fetch( {
success: events.successfulRender,
error: events.failedRender,
});
};

const updateStatusMessageWith = (message) => {
$('#status-messages ul').empty();
$('#status-messages ul').append(`<li>${message}</li>`);
$('#status-messages').show();
}

const fields = ['name', 'continent', 'about', 'category', 'weeks', 'cost'];
const resFields = ['name', 'age', 'email'];
const events = {
addReservation(event) {
event.preventDefault();
const resData = {};
resFields.forEach( (field) => {
const val = $(`#add-reservation-form input[name=${field}]`).val();
if (val != '') {
resData[field] = val;
}
});

const reservation = new Reservation(resData);
if (reservation.isValid()) {
// debugger;
let tripID = $(this).attr('dataid');
// const getTripNumber = $(event.currentTarget.attributes.tripId).val();
reservation.urlRoot = `${(new Trip()).urlRoot}${tripID}/reservations`;
reservation.save({}, {
success: events.successfulSave,
error: events.failedSave,
});
} else {
updateStatusMessageWith('reservation is invalid');
reservation.destroy();
}
},
addTrip(event) {
event.preventDefault();
const tripData = {};
fields.forEach( (field) => {
const val = $(`input[name=${field}]`).val();
if (val != '') {
tripData[field] = val;
}
});
const trip = new Trip(tripData);
if (trip.isValid()) {
trip.save({}, {
success: events.successfulSave,
error: events.failedSave,
});
$('#status-messages ul').empty();
$('#status-messages ul').append(`INCOMPLETE Client-side errors`);
$('#status-messages').show();
}
},
successfulSave(trip, response) {
tripList.add(trip);
console.log('successful save!');
console.log(trip);
console.log(response);
$('#status-messages ul').empty();
$('#status-messages ul').append(`<li>${trip.get('name')} added!</li>`);
$('#status-messages').show();
},
failedSave(trip, response) {
console.log('error');
console.log(trip);
console.log(response);
console.log(response.responseJSON.errors);
for (let key in response.responseJSON.errors) {
response.responseJSON.errors[key].forEach((error) => {
$('#status-messages ul').append(`<li>${key}: ${error}</li>`);

});
}},
successfulRender(trip, response) {
// console.log('success render::::');
console.log(response)
const $info = $('.info');
$info.empty();
$info.append(infoTemplate(trip.attributes));
// console.log('last line of renderTrip');
},
failedRender(trip, response) {
// console.log('failed render:::::');
// console.log(response);
}
};


$(document).ready( () => {
$('main').html('<h1>Hello World!</h1>');
tripTemplate = _.template($('#trip-template').html());
infoTemplate = _.template($('#info-template').html());
loadTrips();
$('.all-trips').on('click', function() {
$('table').toggleClass('active');
});

$('#trip-list').on('click', '.trip', function(){
let tripID = $(this).attr('data-id');
loadTripDetails(tripID);
$('#summary').get(0).scrollIntoView();
});

$('.add').on('click', function(){
$('#add-trip-form').toggle({'display': 'block'});
});

$('#add-trip-form').submit(events.addTrip);

// $('#add-reservation-form').submit( function(e {
// e.preventDefault();
// console.log('when you click submit...');
// console.log(this);
// let tripId = $(this).attr('data-id');
//
// const url = `https://ada-backtrek-api.herokuapp.com/trips/${tripId}/reservations`;
// const formData = $(this).serialize();
//
// $.post(url, formData, (response) => {
// $('#status-messages ul').append('<p>Spot reserved!</p>');
// console.log(response);
// document.getElementById("new-trip-form").reset();
// }).fail(() => {
// $("#message").html("<p>Unable to complete reservation</p>")
// });
// });
$(document).on('submit', '#add-reservation-form', events.addReservation)

Choose a reason for hiding this comment

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

While this is a totally valid way to dynamically bring in this event, it is always the preference to use an element more specific than document if at all possible.



});
11 changes: 11 additions & 0 deletions src/app/collections/trip_list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Backbone from 'backbone';
import Trip from '../models/trip';

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


});

export default TripList;
12 changes: 12 additions & 0 deletions src/app/models/reservation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Backbone from 'backbone';

const Reservation = Backbone.Model.extend({
initialize: function(attributes) {
console.log('attributes here:');
console.log(attributes);
},
// idAttribute: 'id',
// urlRoot: 'https://ada-backtrek-api.herokuapp.com/trips/',
});

export default Reservation;
8 changes: 8 additions & 0 deletions src/app/models/trip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Backbone from 'backbone';

const Trip = Backbone.Model.extend({
idAttribute: 'id',
urlRoot: 'https://ada-backtrek-api.herokuapp.com/trips/',
});

export default Trip;
Loading