-
Notifications
You must be signed in to change notification settings - Fork 44
Amy Cash - Pipes #50
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
base: master
Are you sure you want to change the base?
Amy Cash - Pipes #50
Changes from all commits
fd47fc7
85f54f0
7a1b3ca
294754e
ae27560
7c44345
93423a1
987065d
12cb2bc
8eafe08
b766d74
154b3aa
862868d
618c41f
19c5829
8ad2df3
bba0984
b636eee
7b7e51a
cd53c65
15ee364
e0c6481
a3131b8
5e04bcf
91faa1e
8f2086d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,8 +6,193 @@ import _ from 'underscore'; | |
| import './css/foundation.css'; | ||
| import './css/style.css'; | ||
|
|
||
| console.log('it loaded!'); | ||
| // models and collections | ||
| import Trip from './app/models/trip'; | ||
| // import Reservation from './app/models/reservation'; | ||
| import TripList from './app/collections/trip_list'; | ||
|
|
||
|
|
||
| const TRIP_FIELDS = ['name', 'continent', 'category', 'weeks', 'price']; | ||
| const RES_FIELDS = ['name', 'email']; | ||
|
|
||
| let allTripsTemplate; | ||
| let showDetailsTemplate; | ||
| let statusMessageTemplate; | ||
|
|
||
| // Render Methods | ||
|
|
||
| const renderAll = function renderAll(tripList) { | ||
| const tripListElement = $('#trips-list'); | ||
| tripListElement.empty(); | ||
| $('th.sort').removeClass('current-sort-field'); | ||
| $(`th.sort.${ tripList.comparator }` ).addClass('current-sort-field'); | ||
|
|
||
| tripList.on('update', renderAll); | ||
| tripList.on('sort', renderAll); | ||
|
|
||
| tripList.forEach((trip) => { | ||
| let tripsHTML = allTripsTemplate(trip.attributes); | ||
| if (trip.id < 34) { | ||
| tripListElement.append( $(tripsHTML) ); | ||
| } | ||
| }); // for each | ||
| }; // renderAll function | ||
|
|
||
| // error messages | ||
|
|
||
| const reportStatus = function(status, message) { | ||
| console.log(`Reporting ${ status } message: ${ message } `); | ||
| const generatedHTML = statusMessageTemplate({ | ||
| status: status, | ||
| message: message, | ||
| }); | ||
|
|
||
| $('#status-messsages ul').append(generatedHTML); | ||
| $('status-messages').show() | ||
| } | ||
|
|
||
| const tripsApiErrorHandler = function(model, response) { | ||
| model.destroy(); | ||
|
|
||
| if (response.responseJSON['errors']) { | ||
| const errors = response.responseJSON['errors']; | ||
| for (const field in errors) { | ||
| for (const problem of errors[field]) { | ||
| reportStatus('error', ` ${ field }: ${ problem }`); | ||
| } // for problem | ||
| } // error | ||
|
|
||
| } else { | ||
| reportStatus('error', 'Could not save trip'); | ||
| console.log('Error response from server:') | ||
| console.log(response); | ||
| } // end else | ||
| } //handler | ||
|
|
||
| const renderDetails = function renderDetails(id) { | ||
| const tripDetails = $('#trip-details'); | ||
| tripDetails.empty(); | ||
| const oneTrip = new Trip({id: id}); | ||
| oneTrip.fetch({}).done(() => { | ||
| tripDetails.append(showDetailsTemplate(oneTrip.attributes)); | ||
| }); | ||
| }; // renderDetails | ||
|
|
||
| const makeReservation = function makeReservation(bookTripID, reservationFormData) { | ||
| let reserveURL = 'https://trektravel.herokuapp.com/trips/' + bookTripID + '/reservations'; | ||
| $.post(reserveURL, reservationFormData); | ||
| } | ||
|
|
||
| const readAddTripForm = function readAddTripForm() { | ||
| const addTripData = {}; | ||
| TRIP_FIELDS.forEach( (field) => { | ||
| const inputElement = $(`add-trip-form input[name="${ field }"]`); | ||
| addTripData[field] = inputElement.val(); | ||
| }); | ||
| return addTripData; | ||
| }; // read form function | ||
|
|
||
|
|
||
| const clearForm = function clearForm() { | ||
| $('#add-trip-form input[name]').val(''); | ||
| }; | ||
|
|
||
|
|
||
| // Document Ready | ||
| $(document).ready( () => { | ||
| $('main').html('<h1>Hello World!</h1>'); | ||
| }); | ||
|
|
||
|
|
||
| allTripsTemplate = _.template($('#all-trips-template').html() ); | ||
| showDetailsTemplate = _.template($('#show-details-template').html() ); | ||
| statusMessageTemplate = _.template($('#status-message-template').html() ); | ||
|
|
||
| $('#individual-trip').hide(); | ||
| $('#all-trips').hide(); | ||
| $('#reserve-trip').hide(); | ||
| $('#add-trip').hide(); | ||
| $('#status-messages').hide(); | ||
|
|
||
| const tripList = new TripList(); | ||
|
|
||
| tripList.on('sort', renderAll); | ||
|
|
||
| //Events | ||
|
|
||
| // show add trip form | ||
|
|
||
| $('#create-trip').on('click', function() { | ||
| console.log("You clicked to see the add trip form"); | ||
| $('#add-trip').show(); | ||
| }); | ||
|
|
||
| // add a new trip | ||
|
|
||
| $('#add-trip-form').on('submit', (event) => { | ||
| event.preventDefault(); | ||
|
|
||
| $('#individual-trip').hide(); | ||
| const newTrip = new Trip(readAddTripForm); | ||
| tripList.add(newTrip); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no checking for validations? Also the trip doesn't have an ID yet (gets the from the API), so you should wait until the save is successful and then execute |
||
| newTrip.save({}, { | ||
| success: clearForm, | ||
| error: tripsApiErrorHandler | ||
| }); | ||
| $('#status-messages').show | ||
| }); | ||
|
|
||
| // make reservations | ||
|
|
||
| $('#reservation-form').on('submit', (event) => { | ||
| event.preventDefault(); | ||
| const bookTripID = $(event.target).attr('data-id'); | ||
| let reservationFormData = | ||
| $('#reservation-form').serialize(); | ||
| makeReservation(reservationFormData); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here you're calling |
||
| }) //end book trip | ||
|
|
||
| // show trip reservation form | ||
| $('#trip-details').on('click', 'h4', function() { | ||
| console.log("You clicked to see the res form"); | ||
| console.log(this); | ||
| const bookTripID = $(this).attr('data-id'); | ||
| $('#reservation-form').attr("data-id", bookTripID); | ||
| $('#reserve-trip').show(); | ||
| }) | ||
|
|
||
|
|
||
| // show trip details | ||
| $('#trips-list').on('click', '.trips', (event) => { | ||
| const tripID = $(event.target).attr('data-id'); | ||
| $('#individual-trip').show(); | ||
| renderDetails(tripID); | ||
| window.scrollTo(0, 0); | ||
| }); | ||
|
|
||
| // show all the trips | ||
|
|
||
| const allTrips = $('#load-trips'); | ||
| allTrips.on('click', () => { | ||
|
|
||
| tripList.fetch(); | ||
| $('#all-trips').show(); | ||
| }) | ||
|
|
||
| // Clear error messages | ||
| $('#status-messages button.clear').on('click', (event) => { | ||
| $('#status-messages ul').html(''); | ||
| $('#status-messages').hide | ||
| }) | ||
|
|
||
| // Sort Fields | ||
| TRIP_FIELDS.forEach((field) => { | ||
| const headerElement = $(`.sort.${ field }`); | ||
|
|
||
| headerElement.on('click', () => { | ||
| console.log(`Sorting by ${ field }`); | ||
| tripList.comparator = field; | ||
| tripList.sort(); | ||
|
|
||
| }); // click event handler | ||
| }); // fields for each | ||
|
|
||
| }); // end document ready | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| 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; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // Defining the Model | ||
|
|
||
| // import Backbone from 'backbone'; | ||
| // | ||
| // const Reservation = Backbone.Model.extend({ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like you were on a good track! |
||
| // | ||
| // url: function() { | ||
| // console.log(this); | ||
| // return 'https://ada-backtrek-api.herokuapp.com/trips/' + this.get("id") + '/reservations'; | ||
| // } | ||
| // }); //end const Rervation | ||
| // | ||
| // export default Reservation; | ||
|
|
||
| // https://ada-backtrek-api.herokuapp.com/trips/1/reservations | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These should be in
$(document(.readythis will eventually cause lots of rendering when a new model is added or if it's been sorted multiple times.Only add the listener once, not every time it renders.