forked from Ada-C7/trek
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
156 lines (124 loc) · 4.33 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// single-page app with jQuery + AJAX
var failureCallback = function(response) {
console.log(response);
console.log("Fail :(");
$("#errors").removeClass().addClass("alert callout");
$("#errors").html("<h1>Your AJAX request failed!</h1>");
};
// INDEX ~ callback for all trips + template
var indexCallback = function(response) {
console.log("Success!");
for ( var j = 0; j < response.length; j ++ ) {
console.log(response[j]);
}
var tripsTemplate = _.template($('#index-trip-template').html());
var all_trips = tripsTemplate({
trips: response
});
$('#index-page').html(all_trips);
};
// SHOW ~ callback for each trip + template
var showCallback = function(response) {
console.log("success!");
console.log(response);
var tripData = response;
var showTemplate = _.template($('#show-trip-template').html());
var showHtml = showTemplate( { data: tripData } );
$('#show-page').html(showHtml);
$('#show-page').data("id", response.id); // attach trip id to show page
};
// NEW ~ template for adding a new trip
var newTemplate = _.template($('#new-trip-template').html());
$('#new-page').append(newTemplate);
// BY BUDGET
//https://trektravel.herokuapp.com/trips/budget?query=5000
var budgetCallback = function(response) {
console.log("success!");
console.log(response);
};
//***********************ONCE DOM IS READY************************
$(document).ready(function() {
var baseUrl = "https://trektravel.herokuapp.com/trips"; // Trek API
// INDEX ~ event handler for button "All Trips"
$("#index-page").toggle(false); // start with trips toggled to 'off'
$("#index-button").click(function(event) {
$("#errors").html("").removeClass();
console.log("Alerts cleared!");
$.get(baseUrl, indexCallback).fail(failureCallback);
$("#index-page").toggle();
$("#new-page").hide();
$("#show-page").hide();
});
// SHOW ~ event handler for click on trip card
$("#index-page").on("click", "a", function(event) {
console.log("success!");
event.preventDefault();
var tripId = $(this).data().id;
var showUrl = baseUrl + "/" + tripId.toString();
console.log(showUrl);
$.get(showUrl, showCallback).fail(failureCallback);
$("#show-page").show();
$("#index-page").toggle(false); // hide all trips
return false;
});
// RESERVE ~ event handler for form
$('#show-page').on("submit", "form", (function(event) {
event.preventDefault();
console.log("success!");
// console.log($('#show-page').data().id); //shows 1
var tripId = $('#show-page').data().id;
var reserveUrl = baseUrl + "/" + tripId.toString() + "/reserve";
console.log(reserveUrl);
var formData = $(this).serialize();
console.log(formData);
$.post(reserveUrl, formData, function(response) {
console.log("success!");
console.log(response);
alert("Reservation added!");
console.log("Form submitted!");
$('#show-page').hide();
}).fail(failureCallback);
this.reset();
return false;
}));
// NEW ~ template and event handler for button
$("#new-page").toggle(false); // start with trips toggled to 'off'
$("#new-button").click(function(event) {
console.log("success!");
$('#show-page').hide();
$('#index-page').hide();
$("#new-page").toggle();
});
// NEW ~ event handler for form
$('#new-page').on("submit", "form", (function(event) {
event.preventDefault();
console.log("success!");
var newData = $(this).serialize();
var newUrl = this.action;
console.log(newUrl);
console.log(newData);
$.post(newUrl, newData, function(response) {
console.log(response);
console.log("success!");
alert("New trip added");
console.log("Trip added!");
}).fail(failureCallback);
this.reset();
return false;
}));
// BY BUDGET
$('#budget-search').on("submit", "form", (function(event) {
event.preventDefault();
console.log("success!");
var queryText = $(this).serialize();
var budgetUrl = this.action + "?" + queryText;
$.get(budgetUrl, indexCallback).fail(failureCallback);
$("#index-page").show();
// alert user when search is restricted by budget
var budgetMessage = "Now showing trips up to $" + queryText.substring(6);
$("#errors").html(budgetMessage);
$("#errors").removeClass().addClass("success callout");
this.reset();
return false;
}));
});