Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
287fa74
Model and Collections created, base started.
SesameSeeds Nov 28, 2017
f25c5e5
Trip list is now viewable.
SesameSeeds Dec 1, 2017
bec7c41
After much debugging, added urlRoot to models to get trips to display…
SesameSeeds Dec 2, 2017
2a3eb1f
Add trip form created.
SesameSeeds Dec 3, 2017
84c0b86
Trip add logic there, something is not working right just yet.
SesameSeeds Dec 3, 2017
25f9699
Fixed form for adding trip.
SesameSeeds Dec 3, 2017
e71077c
Adds trip, trying to get it to fetch to automatically re-populate wit…
SesameSeeds Dec 4, 2017
76a1cfd
Trip successfully adds AND repopulates.
SesameSeeds Dec 4, 2017
4c72f53
Reservation form created.
SesameSeeds Dec 4, 2017
59f00cd
Trip form moved so you can see properly.
SesameSeeds Dec 4, 2017
5bd18f6
Simple display complete so you can read things now.
SesameSeeds Dec 4, 2017
e592472
Colour added to sort function in css, so user can see which column th…
SesameSeeds Dec 4, 2017
c99dc7c
Well it sorts now, but no idea why Sloop is first.
SesameSeeds Dec 4, 2017
5e664b1
Working on reservations and pissing off the syntax gods who give me n…
SesameSeeds Dec 4, 2017
1163234
Created validations, well tried, sort of working. Still working on cr…
SesameSeeds Dec 4, 2017
734e92d
Cleaned up a bit of the code, still trying to submit a reservation.
SesameSeeds Dec 4, 2017
c3e0479
Cleaned up a bit, uncommented out an important part, changed my mind …
SesameSeeds Dec 4, 2017
870ffef
Apparently it does successfully make reservations, though I can't for…
SesameSeeds Dec 4, 2017
9d82d83
Fixed reservation requirements. Sorting out the puts vs. post.
SesameSeeds Dec 4, 2017
db96606
Actually fixed the age debacle, now posting, now getting 500 error. …
SesameSeeds Dec 4, 2017
4f7ca4b
Officially takes reservations.
SesameSeeds Dec 4, 2017
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
125 changes: 108 additions & 17 deletions dist/index.html
Original file line number Diff line number Diff line change
@@ -1,20 +1,111 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>My JavaScript App</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<header>

</header>
<main>

</main>
<footer>

</footer>
<script src="app.bundle.js" type="text/javascript"></script>
</body>

<head>
<meta charset="utf-8">
<title>Trekkin'</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>

<header>
<h1>Trekkin' - Go Global!</h1>

<section id="status-messages">
<ul>
</ul>
</section>
</header>

<aside class="columns large-6 small-12">
<section id="trip-description"></section>

<h3>Reserve a Trip!</h3>

<form id="reservation-trip-form" method="post">
<label for="name">Full Name</label>
<input type="text" name="name"></input>
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This isn't being selected by app.js Because it's having trouble with $('input[name="name"]') Instead an alternative way of being selected would be appropriate, an id etc.


<label for="email">E-Mail</label>
<input type="text" name="email"></input>

<label for="age">Age</label>
<input type="number" name="age"></input>

<input type="submit" value="Reserve Trip!" class="button"></input>
</form>

<h3>Add a Trip!</h3>

<form id="add-trip-form">
<label for="name">Trip Name</label>
<input type="text" name="name"></input>

<label for="category">Category</label>
<input type="text" name="category"></input>

<label for="continent">Continent</label>
<input type="text" name="continent"></input>

<label for="weeks">Weeks</label>
<input type="number" name="weeks"></input>

<label for="cost">Cost</label>
<input type="number" name="cost"></input>

<label for="about">About</label>
<input type="text" name="about"></input>

<input type="submit" value="Add Trip!" class="button"></input>
</form>

</aside>

<main class="row">
<section class="trips column large-4 small-12">
<table>
<thead>
<th class="sort name">Name</th>
<th class="sort id">Trip ID</th>
<th class="sort category">Category</th>
<th class="sort continent">Continent</th>
<th class="sort weeks">Weeks</th>
<th class="sort cost">Cost</th>
</thead>
<tbody id="trips-list"></tbody>
</table>
</section>

</main>

<footer>
&copy; 2017, Stef - Keeping it simple.
</footer>

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

<script type = "text/template" id="trip-details-template">
<h2><%- name %></h2>
<p><%- id %></p>
<p><%- category %></p>
<p><%- continent %></p>
<p><%- weeks %></p>
<p><%- cost %></p>
<p><%- about %></p>
</script>

<script src="app.bundle.js" type="text/javascript"></script>

</body>

</html>
199 changes: 195 additions & 4 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,204 @@
// Vendor Modules
// Vendor Modules.
import $ from 'jquery';
import _ from 'underscore';

// CSS
// CSS.
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';

// DOM Selectors.
const $tripsList = $('#trips-list')
const $tripDescription = $('#trip-description')
const $reservationTripForm = $('#reservation-trip-form')

// Templates for trip list and details.
let tripTemplate;
let tripDetailsTemplate;

// Start of trip list.
const tripList = new TripList();

const render = function render(tripList) {
console.log(tripList);
const $trips = $('#trips-list')

// Remember to empty, so things don't repeat.
$trips.empty();
tripList.forEach((trip) => {
$trips.append(tripTemplate(trip.attributes));
console.log(trip.attributes);
console.log('It loaded!');
});
};

const fields = ['name', 'id', 'category', 'continent', 'weeks', 'cost', 'about'];

const reservationFields = ['name', 'email', 'age'];

// const updateStatusMessageFrom = (messageHash) => {
// $('#status-messages ul').empty();
// for(let messageType in messageHash) {
// messageHash[messageType].forEach((message) => {
// $('#status-messages ul').append($(`<li>${messageType}: ${message}</li>`));
// console.log(`<li>${messageType}: ${message}</li>`);
// })
// }
// $('#status-messages').show();
// }
//
// const updateStatusMessageWith = (message) => {
// $('#status-messages ul').empty();
// $('#status-messages ul').append(`<li>${message}</li>`);
// $('#status-messages').show();
// }

const events = {
// Add a bloody trip
addTrip(event) {
event.preventDefault();
const tripData = {};
fields.forEach( (field) => {
const val = $(`input[name=${field}]`).val();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This isn't working for the name field.

if (val != '') {
tripData[field] = val;
}
});

const trip = new Trip(tripData);

if (trip.isValid()) {
// tripList.add(trip);
trip.save({}, {
success: events.successfullSave,
error: events.failedSave,
});
} else {
console.log('Trip is invalid, sad trombone.');
console.log(trip.validationError);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It's good that you're checking validation errors, but this needs to be reported to the user as well.


// updateStatusMessageFrom(trip.validationError);
}
},

successfullSave(trip, response) {
console.log('Success!');
console.log(trip);
console.log(response);
$('#status-messages ul').empty();
$('#status-messages ul').append(`<li>${trip.get('title')} added to ze listy list list!</li>`);
$('#status-messages').show();
tripList.fetch();
},

failedSave(trip, response) {
console.log('ERROR!');
console.log(trip);
console.log(response);
$('#status-messages ul').empty();
console.log(response.responseJSON.errors);

for(let key in response.responseJSON.errors) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good, good to use for letting the user know the validation error issues.

response.responseJSON.errors[key].forEach((error) => {
$('#status-messages ul').append(`<li>${key}: ${error}</li>`);
})
}
$('#status-messages').show();
trip.destroy();
},

// Make a reservation



addReservation(event){
event.preventDefault();
let reserveData = {};

reservationFields.forEach((field) => {
console.log(`assigning ${field}`);
const value = $(`#reservation-trip-form input[name=${field}]`).val();
console.log(value);
reserveData[field] = value;
});

console.log('Your reservation has been added!');
console.log(reserveData);

const reservation = new Reservation(reserveData);
reservation.urlRoot = `https://ada-backtrek-api.herokuapp.com/trips/1/reservations`;

reservation.save();

},

// Sort Trips
sortTrips(event) {
console.log(event);
console.log(this);
// Get the class list of the selected element
const classes = $(this).attr('class').split(/\s+/);

classes.forEach((className) => {
if (fields.includes(className)) {
if (className === tripList.comparator) {
tripList.models.reverse();
tripList.trigger('sort', tripList);
}
else {
tripList.comparator = className;
tripList.sort();
}
}
});

$('.sort-field').removeClass('sort-field');
$(this).addClass('sort-field');
},
};

$(document).ready( () => {
$('main').html('<h1>Hello World!</h1>');

// $('#reservation-trip-form').submit((event) => {
// event.preventDefault();
//
// const url = $('form').attr('action');
// console.log(url);
// const id = ($('form').attr('tripid'));
// const reservationUrl = `${url}${id}/reservations`;
// const formData = $('form').serialize();
//
// $.post(reservationUrl, formData, (response) => {
// $('#message').html('<p> Trip Reserved! </p>');
// console.log(response);
// });
// });

// Get those trip deets.
$tripsList.on('click', 'tr', function getTrip() {
const trip = new Trip({ id: $(this).attr('data-id') })
$tripDescription.empty();

trip.fetch().done(() => {
$tripDescription.append(tripDetailsTemplate(trip.attributes));
});
});


tripTemplate = _.template($('#trip-template').html());
tripList.on('update', render, tripList);
tripList.fetch();

tripDetailsTemplate = _.template($('#trip-details-template').html());

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

$('#reservation-trip-form').submit(events.addReservation);

$('.sort').click(events.sortTrips);
tripList.on('sort', render, tripList);
});
15 changes: 15 additions & 0 deletions src/app/collections/trip_list.js
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;
}
});

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

const Reservation = Backbone.Model.extend({

validate: function(attributes) {
const error = {};

if (!attributes.name) {
error['name'] = 'Full Name cannot be blank!';
}

if (!attributes.email) {
error['email'] = 'E-Mail cannot be blank!';
}

if (!attributes.age) {
error['age'] = 'Age cannot be blank!';
} else if (isNaN(attributes.age)) {
error['age'] = 'Age must be a number!';
}

console.log('Error!');
console.log(error);
if (Object.keys(error).length > 0) {
return error;
} else {
return false;
}
}
});

export default Reservation;
Loading