forked from AdaGold/BackTREK
-
Notifications
You must be signed in to change notification settings - Fork 44
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 - Sairagul - BackTrek #45
Open
sairagula
wants to merge
8
commits into
Ada-C8:master
Choose a base branch
from
sairagula:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
471d6b5
Wave1, gives all trips
sairagula 7fd4759
Added trip to DB
sairagula dc9d6b8
Reservation has been added
sairagula 417045e
Sorting added
sairagula b48f709
Add trip form attached to button
sairagula bb08560
Working code before validations
sairagula b47fc5b
Modal added, reservation saves, but add trip stopped working
sairagula 5e3ab44
Basic requirements are met
sairagula File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,220 @@ import _ from 'underscore'; | |
import './css/foundation.css'; | ||
import './css/style.css'; | ||
|
||
// Our components | ||
import Trip from './app/models/trip'; | ||
import Reservation from './app/models/reservation'; | ||
import TripList from './app/collections/trip_list'; | ||
console.log('it loaded!'); | ||
|
||
$(document).ready( () => { | ||
$('main').html('<h1>Hello World!</h1>'); | ||
const tripList = new TripList() | ||
|
||
|
||
const TRIP_FIELDS = ['name', 'continent', 'about', 'category', 'weeks', 'cost']; | ||
const RESERVATION_FIELDS = ['name', 'email', 'age']; | ||
|
||
let tripsTemplate; | ||
let tripTemplate; | ||
|
||
// Clear status messages | ||
const clearStatus = function clearStatus() { | ||
$('#status-messages ul').html(''); | ||
$('#status-messages').hide(); | ||
}; | ||
|
||
// Add a new status message | ||
const reportStatus = function reportStatus(status, message) { | ||
console.log(`Reporting ${ status } status: ${ message }`); | ||
|
||
const statusHTML = `<li class="${ status }">${ message }</li>`; | ||
|
||
// note the symetry with clearStatus() | ||
$('#status-messages ul').append(statusHTML); | ||
$('#status-messages').show(); | ||
}; | ||
|
||
const render = function render(tripList){ | ||
const $tripsTableElement = $('#trip-list'); | ||
$tripsTableElement.html(''); | ||
|
||
tripList.forEach((trip) => { | ||
const generatedHTML = $(tripsTemplate(trip.attributes)); | ||
generatedHTML.on('click',(event) => { | ||
$('#trips').hide(); | ||
$('#trip').show(); | ||
trip.fetch({ | ||
success (model, response){ | ||
renderTrip(model); | ||
} | ||
}); | ||
}) //.on | ||
$tripsTableElement.append(generatedHTML); | ||
}); //tripList | ||
}; // render | ||
|
||
|
||
const renderTrip = function renderTrip(trip){ | ||
const $tripTableElement = $('#trip'); | ||
$tripTableElement.html(''); | ||
console.log(trip.attributes); | ||
|
||
const generatedHTML = tripTemplate(trip.attributes); | ||
$tripTableElement.html(generatedHTML); | ||
|
||
$('#reservation-form').on('submit', addReservHandler); | ||
|
||
$('th.sort').removeClass('current-sort-field'); | ||
$(`th.sort.${ tripList.comparator }`).addClass('current-sort-field'); | ||
}; | ||
|
||
const loadTrips = function loadTrips() { | ||
tripList.fetch({ | ||
success(model, response) { | ||
render(model); | ||
} | ||
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. Consider adding an |
||
}); | ||
}; | ||
|
||
const readTripFormData = function readTripFormData() { | ||
const tripData = {}; | ||
TRIP_FIELDS.forEach((field) => { | ||
const $inputElement = $(`#add-trip-form input[name="${ field }"]`); | ||
const value = $inputElement.val(); | ||
|
||
// Don't take empty strings, so that Backbone can | ||
// fill in default values | ||
if (value != '') { | ||
tripData[field] = value; | ||
} | ||
|
||
$inputElement.val(''); | ||
}); | ||
|
||
console.log(tripData); | ||
return tripData; | ||
}; | ||
|
||
// Reserving a trip | ||
const readReservFormData = function readReservFormData() { | ||
const reservData = {}; | ||
RESERVATION_FIELDS.forEach((field) => { | ||
const $inputReservElement = $(`#reservation-form input[name="${ field }"]`); | ||
const value = $inputReservElement.val(); | ||
|
||
if (value != '') { | ||
reservData[field] = value; | ||
} | ||
|
||
$inputReservElement.val(''); | ||
}); | ||
|
||
console.log("Read reserve data"); | ||
console.log(reservData); | ||
return reservData; | ||
}; | ||
|
||
const handleValidationFailures = function handleValidationFailures(errors) { | ||
for (let field in errors) { | ||
for (let problem of errors[field]) { | ||
reportStatus('error', `${field}: ${problem}`); | ||
} | ||
} | ||
}; | ||
|
||
// Add trip | ||
const addTripHandler = function(event) { | ||
event.preventDefault(); | ||
// $('#status-messages').html('') | ||
|
||
const trip = new Trip(readTripFormData()); | ||
console.log(trip); | ||
|
||
|
||
if (!trip.isValid()) { | ||
handleValidationFailures(trip.validationError); | ||
return; | ||
} | ||
|
||
trip.save({}, { | ||
success: (model, response) => { | ||
tripList.add(model); | ||
console.log('Yay, trip was saved successfully!'); | ||
reportStatus('success', 'Yay, trip was saved successfully!'); | ||
}, | ||
error: (model, response) => { | ||
console.log('Failed to save trip! Server response:'); | ||
console.log(response); | ||
|
||
// After server-side validations failed, we have to remove this bad | ||
// trip from the list | ||
tripList.remove(model); | ||
|
||
handleValidationFailures(response.responseJSON["errors"]); | ||
}, | ||
}); | ||
}; | ||
|
||
// Add Reservation | ||
const addReservHandler = function addReservHandler(event) { | ||
event.preventDefault(); | ||
const reserve = new Reservation(readReservFormData()); | ||
reserve.set('trip_id', $(this).data('tripId')); | ||
|
||
|
||
if (!reserve.isValid()) { | ||
handleValidationFailures(reserve.validationError); | ||
return; | ||
} | ||
|
||
reserve.save({}, { | ||
success: (model, response) => { | ||
console.log('Congratulations, you successfully reserved a trip!'); | ||
|
||
reportStatus('success', 'Congratulations, you successfully reserved a trip!'); | ||
}, | ||
error: (model, response) => { | ||
console.log('Failed to save reservation. Server response: '); | ||
console.log(response); | ||
|
||
handleValidationFailures(response.responseJSON["errors"]); | ||
} | ||
}) | ||
} | ||
|
||
$(document).ready(() => { | ||
tripsTemplate = _.template($('#trips-template').html()); | ||
tripTemplate = _.template($('#trip-template').html()); | ||
|
||
// $('#trips').hide(); | ||
|
||
// Retrieving all trips by clicking the button | ||
$('#load').on('click', function(){ | ||
$('#trips').show(); | ||
loadTrips() | ||
$('#show_form').hide(); | ||
}); | ||
|
||
$('header').on('click', '#add_trip', function() { | ||
$('#status-messages').hide(); | ||
$('#show_form').show(); | ||
|
||
}); | ||
|
||
$('#add-trip-form').on('submit', addTripHandler); | ||
|
||
$('#status-messages button.clear').on('click', (event) => { | ||
$('#status-messages ul').html(''); | ||
$('#status-messages').hide(); | ||
$('#show-form').hide(); | ||
}) | ||
// sorting by header element | ||
tripList.on('sort', render); | ||
TRIP_FIELDS.forEach((field) => { | ||
const headerData = $(`th.sort.${ field }`); | ||
headerData.on('click', (event) => { | ||
tripList.comparator = field; | ||
tripList.sort(); | ||
}); | ||
}); | ||
$('#status-messages button.clear').on('click', clearStatus); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
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; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Consider adding an
error
event handler to deal with times that the API doesn't work and let the user know.