-
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 - Kate Evans-Spitzer - BackTREK #40
base: master
Are you sure you want to change the base?
Changes from all commits
537f604
ade1b58
3bca65b
20cc153
11487c1
b31f49b
d2d4fd2
0161dac
e99533d
bfd2896
32c1a74
0f04cd2
895b69f
dac3cc7
1da09e4
e221abe
d5fbfb6
456a1bf
d5cb2d8
b2adb68
3479b08
b206b8e
da1837c
e2f9a62
db11e7f
5e4a433
33322ce
b34b0d1
feb9f5a
bb6dec3
0590e8e
5d08bfd
6abc4da
d73d4e2
137644c
a6d39cb
7142c94
eb3b965
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,208 @@ 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 TripList from './app/collections/trip-list'; | ||
import Reservation from './app/models/reservation'; | ||
|
||
let tripTemplate; | ||
let emptyTripTemplate; | ||
let tripDetailTemplate; | ||
let reserveModalTemplate; | ||
let addTripModalTemplate; | ||
|
||
// Trip List | ||
|
||
const tripList = new TripList(); | ||
|
||
const render = function render(tripList) { | ||
$('.trip-row').remove(); | ||
const tripListElement = $('#trip-list ul'); | ||
if (tripList.length > 0) { | ||
tripList.forEach((trip) => { | ||
const generatedHTML = $(tripTemplate(trip.attributes)); | ||
tripListElement.append(generatedHTML); | ||
}); | ||
} else { | ||
const generatedHTML = $(emptyTripTemplate()); | ||
tripListElement.append(generatedHTML); | ||
} | ||
}; | ||
|
||
const show = function show(e) { | ||
// don't minimize trip details when the reserve button is clicked | ||
if ($(e.target).is('#reserve-btn')) return; | ||
const id = parseInt(findElementTripID(e)); | ||
const tripElement = $(`#${id}`); | ||
if (tripElement.hasClass('show')) { | ||
clearShow(); | ||
} else { | ||
const trip = tripList.findWhere({id: id}); | ||
trip.fetch({ | ||
success: () => { | ||
clearShow(); | ||
$('.trip-row').removeClass('show'); | ||
$('.trip-details').remove(); | ||
const generatedHTML = $(tripDetailTemplate(trip.attributes)); | ||
const reserveBtn = ('#reserve-btn'); | ||
tripElement.append(generatedHTML).addClass('show'); | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
const clearShow = function clearShow() { | ||
$('.trip-row').removeClass('show'); | ||
$('.trip-detail-holder').remove(); | ||
}; | ||
|
||
const sort = function sort(e) { | ||
$('.current-sort').removeClass('current-sort'); | ||
let targetElement = $(e.target); | ||
let field = targetElement[0].id; | ||
tripList.comparator = field; | ||
targetElement.addClass('current-sort'); | ||
tripList.sort(); | ||
render(tripList); | ||
}; | ||
|
||
// Filtering | ||
|
||
const filter = function filter(e) { | ||
e.preventDefault(); | ||
const form = $('#trip-search'); | ||
const searchData = getFormData(form, ['search-type', 'query']); | ||
const newList = tripList.filterBy(searchData['search-type'], searchData['query']); | ||
render(newList); | ||
}; | ||
|
||
// Modals | ||
|
||
const addTripModal = function addTripModal() { | ||
$('body').append(addTripModalTemplate()); | ||
}; | ||
|
||
const reserveModal = function reserveModal(e) { | ||
const id = findElementTripID(e); | ||
const generatedHTML = $(reserveModalTemplate({'id': id})); | ||
const form = $('#reservation-form'); | ||
// form.on('submit', submitReservation); | ||
$('body').append(generatedHTML); | ||
}; | ||
|
||
const clearModal = function clearModal(e) { | ||
if ($(e.target).hasClass('modal-close')) { | ||
$('.modal').remove(); | ||
} | ||
}; | ||
|
||
// Forms | ||
|
||
const submitTrip = function submitTrip(e) { | ||
e.preventDefault(); | ||
clearErrors(); | ||
const form = $(e.target); | ||
const formData = getFormData(form, ['name', 'continent', 'category', 'weeks', 'cost', 'about']); | ||
const newTrip = new Trip(formData); | ||
saveIfValid(newTrip, form, 'trip'); | ||
}; | ||
|
||
const submitReservation = function submitReservation(e) { | ||
e.preventDefault(); | ||
clearErrors(); | ||
$('.form-messages').html(''); | ||
const form = $('#reservation-form'); | ||
const id = form.data('id'); | ||
const formData = getFormData(form, ['name', 'email']); | ||
formData['tripID'] = id; | ||
const newReservation = new Reservation(formData); | ||
saveIfValid(newReservation, form, 'reservation'); | ||
}; | ||
|
||
const getFormData = function getFormData(target, values) { | ||
const formData = {}; | ||
values.forEach((value) => { | ||
let targetElement = target.find(`[name="${ value }"]`); | ||
formData[value] = targetElement.val(); | ||
}); | ||
return formData; | ||
}; | ||
|
||
const saveIfValid = function saveIfValid(object, form, type) { | ||
if (object.isValid()) { | ||
object.save({}, { | ||
success: (response) => { | ||
formSuccess(type, form) | ||
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. Note, for trips, this won't allow a newly created trip to show up in a displayed list. The user would have to refresh the browser to see it. On a successful trip save, you should execute a I do however really like how you abstracted out the |
||
}, | ||
error: (status, response) => { | ||
const errors = ($.parseJSON(response.responseText))['errors']; | ||
printErrors(errors, type); | ||
}, | ||
}); | ||
} else { | ||
printErrors(object.validationError, type); | ||
} | ||
}; | ||
|
||
const formSuccess = function formSuccess(item, form) { | ||
const messageBox = $('#form-messages'); | ||
messageBox.html(`<p class="success">Successfully created ${item}!</p>`); | ||
form[0].reset(); | ||
} | ||
|
||
const printErrors = function printErrors(errors, type) { | ||
for(let field in errors) { | ||
let errorElementID = `#${type}-${field} label` | ||
let errorElement = $(errorElementID); | ||
errorElement.addClass('has-errors'); | ||
errorElement.append(`<p class="error">${errors[field]}</p>`); | ||
} | ||
}; | ||
|
||
const clearErrors = function clearErrors() { | ||
$('.has-errors').removeClass('has-errors'); | ||
$('.error').remove(); | ||
}; | ||
|
||
const findElementTripID = function findElementTripID(e) { | ||
return $(e.target).closest('li.trip-row').attr('id'); | ||
}; | ||
|
||
$(document).ready( () => { | ||
$('main').html('<h1>Hello World!</h1>'); | ||
$('.on-load').hide(); | ||
tripTemplate = _.template($('#trip-template').html()); | ||
emptyTripTemplate = _.template($('#empty-trip-template').html()); | ||
tripDetailTemplate = _.template($('#trip-detail-template').html()); | ||
reserveModalTemplate = _.template($('#reserve-modal-template').html()); | ||
addTripModalTemplate = _.template($('#add-trip-modal-template').html()); | ||
|
||
tripList.on('update', render); | ||
|
||
$('#intro-button').on('click', (e) => { | ||
$('#intro-button').hide(200); | ||
$('#loading').show(500); | ||
tripList.fetch({ | ||
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 also have an error callback, just in case the API is down to alert the user. |
||
success: () => { | ||
$('#loading').hide(200); | ||
$('#trip-list').show(500); | ||
}, | ||
}); | ||
$('.on-load').show(500); | ||
}); | ||
|
||
$('#trip-list').on('click', 'li', show); | ||
|
||
$('.sort').on('click', sort); | ||
|
||
$('#add-trip').on('click', addTripModal); | ||
$('body').on('click', '.modal-close', clearModal); | ||
|
||
$('body').on('keyup', '#trip-search', filter); | ||
$('#trip-search').on('submit', e => e.preventDefault()); | ||
|
||
$('#trip-list').on('click', '#reserve-btn', reserveModal); | ||
$(document).on('submit', '#reservation-form', submitReservation); | ||
$(document).on('submit', '#add-trip-form', submitTrip); | ||
|
||
}); |
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.
It's better stylistically to have render listening for a
sort
event.So adding
tripList.on('sort', render);