-
Notifications
You must be signed in to change notification settings - Fork 44
Mariana-Pipes-BackTreck #21
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?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -8,6 +8,167 @@ import './css/style.css'; | |
|
|
||
| console.log('it loaded!'); | ||
|
|
||
| import TripList from './app/collections/trip_list'; | ||
|
|
||
| import Trip from './app/models/trip'; | ||
| import Reservation from './app/models/reservation'; | ||
|
|
||
| const tripList = new TripList(); | ||
|
|
||
| const TRIP_FIELDS = ['name', 'continent', 'about', 'category', 'weeks', 'cost']; | ||
|
|
||
| const RESERVATION_FIELDS = ['name', 'trip_id', 'email' ]; | ||
|
|
||
| const render = function render(tripList) { | ||
| $('.clear').hide(); | ||
| const tripTableElement = $('#trip-list'); | ||
| tripTableElement.html(''); | ||
|
|
||
| tripList.forEach((trip) => { | ||
| const generatedHTML = tripTemplate(trip.attributes); | ||
| tripTableElement.append(generatedHTML); | ||
| $(`#trip${trip.attributes.id}`).on('click', (event) => { | ||
| let tripDetails = new Trip({id: `${trip.attributes.id}`}); | ||
| tripDetails.fetch({ | ||
| success: renderDetails | ||
| }); | ||
| }); | ||
| $('th.sort').removeClass('current-sort-field'); | ||
| $(`th.sort.${ tripList.comparator }`).addClass('current-sort-field'); | ||
| }); | ||
| }; | ||
|
|
||
| const renderDetails = function renderDetails(tripDetails){ | ||
| const $detailsElement = $('#oneTrip'); | ||
| console.log('found detailsElement?', $detailsElement) | ||
| $detailsElement.html(''); | ||
|
|
||
| const generatedHTML = detailsTemplate(tripDetails.attributes); | ||
| $detailsElement.append(generatedHTML); | ||
|
|
||
| $('#goReservationForm').on('click', function(event) { | ||
| renderReservationForm(event, tripDetails); | ||
| }); | ||
| }; | ||
|
|
||
| const renderReservationForm = function renderReservationForm(event, tripDetails){ | ||
| $('#goReservationForm').hide(); | ||
| const tripId = tripDetails.attributes.id | ||
| const $spaceReservationForm = $('#spaceReservationForm'); | ||
| $spaceReservationForm.html(''); | ||
| const generatedHTML = reservationTemplate({tripId: tripId}); | ||
| // console.log(generatedHTML); | ||
| $spaceReservationForm.append(generatedHTML); | ||
| // nuevas cosas | ||
| event.preventDefault(); | ||
| $('#submitForm').on('click', reserveTripHandler); | ||
| }; | ||
|
|
||
| const reserveTripHandler = function reserveTripHandler(event){ | ||
| event.preventDefault(); | ||
| console.log('inside reserveTripHandler'); | ||
| let reservationData = {}; | ||
|
|
||
| RESERVATION_FIELDS.forEach((field) =>{ | ||
| const inputElement = $(`#reservation-form input[name="${ field }"]`); | ||
| const value = inputElement.val(); | ||
| reservationData[field] = value; | ||
| inputElement.val(''); | ||
| }); | ||
| const reservationEnterUser = new Reservation(reservationData); | ||
| console.log(`reservationEnterUser ${reservationEnterUser}`); | ||
| console.log(reservationEnterUser); | ||
| reservationEnterUser.save({}, { success: (model, response) => { | ||
| reportStatus('success', 'Make your bags. You reserve this trips Successfully!'); | ||
| console.log("reservation went great"); | ||
| }, error: (model, response) => { | ||
|
|
||
| const errors = response.responseJSON["errors"]; | ||
| for (let field in errors) { | ||
| for (let problem of errors[field]) { | ||
| reportStatus('error', `${field}: ${problem}`); | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
| const addTripHandler = function(event) { | ||
| event.preventDefault(); | ||
|
|
||
| const tripData = {}; | ||
| TRIP_FIELDS.forEach((field) => { | ||
| // select the input corresponding to the field we want | ||
| const inputElement = $(`#add-trip-form input[name="${ field }"]`); | ||
| const value = inputElement.val(); | ||
| tripData[field] = value; | ||
|
|
||
| inputElement.val(''); | ||
| }); | ||
| const tripEnterUser = new Trip(tripData); | ||
|
|
||
| tripEnterUser.save({}, { | ||
| success: (model, response) => { | ||
| console.log('Successfully saved trip!'); | ||
| reportStatus('success', 'Successfully saved trip!'); | ||
| }, | ||
| error: (model, response) => { | ||
| const errors = response.responseJSON["errors"]; | ||
| for (let field in errors) { | ||
| for (let problem of errors[field]) { | ||
| reportStatus('error', `${field}: ${problem}`); | ||
| } | ||
| } | ||
| }, | ||
| }); | ||
| }; | ||
|
|
||
|
|
||
| let tripTemplate; | ||
| let detailsTemplate; | ||
| let reservationTemplate; | ||
|
|
||
| // Clear status messages | ||
| const clearStatus = function clearStatus() { | ||
| $('.clear').hide(); | ||
| $('#status-messages ul').html(''); | ||
| $('#status-messages').hide(); | ||
| }; | ||
|
|
||
| // Add a new status message | ||
| const reportStatus = function reportStatus(status, message) { | ||
|
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. This function should also clear the container of error messages. Now they never go away. |
||
| console.log(`Reporting ${ status } status: ${ message }`); | ||
| $('.clear').show(); | ||
|
|
||
| const statusHTML = `<li class="${ status }">${ message }</li>`; | ||
|
|
||
| $('#status-messages ul').append(statusHTML); | ||
| $('#status-messages').show(); | ||
| }; | ||
|
|
||
| $(document).ready( () => { | ||
| $('main').html('<h1>Hello World!</h1>'); | ||
|
|
||
| tripTemplate = _.template($('#trip-template').html()); | ||
| detailsTemplate = _.template($('#details-template').html()); | ||
| reservationTemplate = _.template($('#reservationFormTemplate').html()); | ||
|
|
||
| tripList.on('update', render); | ||
|
|
||
| tripList.fetch(); | ||
|
|
||
| tripList.on('sort', render); | ||
|
|
||
| $('#add-trip-form').on('submit', addTripHandler); | ||
|
|
||
| TRIP_FIELDS.forEach((field) => { | ||
| const headerElement = $(`th.sort.${ field }`); | ||
| headerElement.on('click', (event) => { | ||
| console.log(`Sorting table by ${ field }`); | ||
| tripList.comparator = field; | ||
| tripList.sort(); | ||
| }); | ||
| }); | ||
|
|
||
| $('#status-messages').on('click', clearStatus); | ||
|
|
||
| }); | ||
| 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/', | ||
| comparator: 'name', | ||
|
|
||
| }); | ||
|
|
||
| export default TripList; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import Backbone from 'backbone'; | ||
|
|
||
| const Details = Backbone.Model.extend({ | ||
| url: `https://ada-backtrek-api.herokuapp.com/trips/1` | ||
| }); | ||
|
|
||
| export default Details; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import Backbone from 'backbone'; | ||
|
|
||
| const Reservation = Backbone.Model.extend({ | ||
| url: function() { | ||
| console.log('https://ada-backtrek-api.herokuapp.com/trips/'+this.trip_id+'/reservations'); | ||
|
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. It might make more sense to use It's also preferred style to use interpolation. |
||
| // return `https://ada-backtrek-api.herokuapp.com/trips/${this.trip_id}/reservations`; | ||
| return 'https://ada-backtrek-api.herokuapp.com/trips/'+this.get('trip_id')+'/reservations'; | ||
| } | ||
| }); | ||
|
|
||
| export default Reservation; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import Backbone from 'backbone'; | ||
|
|
||
| const Trip = Backbone.Model.extend({ | ||
| url: function() { | ||
| return `https://ada-backtrek-api.herokuapp.com/trips/` + (this.id || '') | ||
| } | ||
|
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. You should be handling client-side validations here and in |
||
| // url: `https://ada-backtrek-api.herokuapp.com/trips/${id}` | ||
| // url: `https://ada-backtrek-api.herokuapp.com/trips/` | ||
| }); | ||
|
|
||
| export default Trip; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,38 @@ | ||
|
|
||
| /* Your styles go here! */ | ||
| .hero-section { | ||
| background: url('https://i.pinimg.com/originals/df/d0/00/dfd0000f193a9aa0864277462c6b063e.jpg') 50% no-repeat; | ||
| background-size: cover; | ||
| height: 300px; | ||
| text-align: center; | ||
| display: flex; | ||
| align-items: center; | ||
| justify-content: center; | ||
| } | ||
|
|
||
| th.sort { | ||
| cursor: pointer; | ||
| } | ||
|
|
||
| .current-sort-field { | ||
| background-color: darkgrey; | ||
| color: white; | ||
| } | ||
|
|
||
| .success { | ||
| background-color: #ade9ff; | ||
| font-size: 1.2em; | ||
| } | ||
|
|
||
| .error { | ||
| background-color: #fcc4d0; | ||
| font-size: 1.2em; | ||
| } | ||
|
|
||
| li { | ||
| list-style-type: none; | ||
| width: 100%; | ||
| } | ||
|
|
||
| #status-messages { | ||
| margin: 0%; | ||
| } |
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.
Right here you're clearing all the field values, including the hidden field where you're hiding the trip id. Instead wait to clear the values until a save is successful. If someone fails validations, the trip Id is being cleared and thus no subsequent reservation will work.