forked from aws-samples/aws-serverless-workshops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathride.js
112 lines (99 loc) · 3.73 KB
/
ride.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
/*global WildRydes _config*/
var WildRydes = window.WildRydes || {};
WildRydes.map = WildRydes.map || {};
(function rideScopeWrapper($) {
var authToken;
WildRydes.authToken.then(function setAuthToken(token) {
if (token) {
authToken = token;
} else {
window.location.href = '/signin.html';
}
}).catch(function handleTokenError(error) {
alert(error);
window.location.href = '/signin.html';
});
function requestUnicorn(pickupLocation) {
$.ajax({
method: 'POST',
url: _config.api.invokeUrl + '/ride',
headers: {
Authorization: authToken
},
data: JSON.stringify({
PickupLocation: {
Latitude: pickupLocation.latitude,
Longitude: pickupLocation.longitude
}
}),
contentType: 'application/json',
success: completeRequest,
error: function ajaxError(jqXHR, textStatus, errorThrown) {
console.error('Error requesting ride: ', textStatus, ', Details: ', errorThrown);
console.error('Response: ', jqXHR.responseText);
alert('An error occured when requesting your unicorn:\n' + jqXHR.responseText);
}
});
}
function completeRequest(result) {
var unicorn;
var pronoun;
console.log('Response received from API: ', result);
unicorn = result.Unicorn;
pronoun = unicorn.Gender === 'Male' ? 'his' : 'her';
displayUpdate(unicorn.Name + ', your ' + unicorn.Color + ' unicorn, is on ' + pronoun + ' way.');
animateArrival(function animateCallback() {
displayUpdate(unicorn.Name + ' has arrived. Giddy up!');
WildRydes.map.unsetLocation();
$('#request').prop('disabled', 'disabled');
$('#request').text('Set Pickup');
});
}
// Register click handler for #request button
$(function onDocReady() {
$('#request').click(handleRequestClick);
$('#signOut').click(function() {
WildRydes.signOut();
alert("You have been signed out.");
window.location = "signin.html";
});
$(WildRydes.map).on('pickupChange', handlePickupChanged);
WildRydes.authToken.then(function updateAuthMessage(token) {
if (token) {
displayUpdate('You are authenticated. Click to see your <a href="#authTokenModal" data-toggle="modal">auth token</a>.');
$('.authToken').text(token);
}
});
if (!_config.api.invokeUrl) {
$('#noApiMessage').show();
}
});
function handlePickupChanged() {
var requestButton = $('#request');
requestButton.text('Request Unicorn');
requestButton.prop('disabled', false);
}
function handleRequestClick(event) {
var pickupLocation = WildRydes.map.selectedPoint;
event.preventDefault();
requestUnicorn(pickupLocation);
}
function animateArrival(callback) {
var dest = WildRydes.map.selectedPoint;
var origin = {};
if (dest.latitude > WildRydes.map.center.latitude) {
origin.latitude = WildRydes.map.extent.minLat;
} else {
origin.latitude = WildRydes.map.extent.maxLat;
}
if (dest.longitude > WildRydes.map.center.longitude) {
origin.longitude = WildRydes.map.extent.minLng;
} else {
origin.longitude = WildRydes.map.extent.maxLng;
}
WildRydes.map.animate(origin, dest, callback);
}
function displayUpdate(text) {
$('#updates').append($('<li>' + text + '</li>'));
}
}(jQuery));