forked from firebase/functions-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
150 lines (129 loc) · 5.2 KB
/
main.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
/**
* Copyright 2017 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
function Demo() {
$(function() {
this.$signInButton = $('#demo-sign-in-button');
this.$signOutButton = $('#demo-sign-out-button');
this.$messageTextarea = $('#demo-message');
this.$createMessageButton = $('#demo-create-message');
this.$createMessageResult = $('#demo-create-message-result');
this.$messageListButtons = $('.message-list-button');
this.$messageList = $('#demo-message-list');
this.$messageDetails = $('#demo-message-details');
this.$signInButton.on('click', this.signIn.bind(this));
this.$signOutButton.on('click', this.signOut.bind(this));
this.$createMessageButton.on('click', this.createMessage.bind(this));
this.$messageListButtons.on('click', this.listMessages.bind(this));
firebase.auth().onAuthStateChanged(this.onAuthStateChanged.bind(this));
}.bind(this));
}
Demo.prototype.signIn = function() {
firebase.auth().signInWithPopup(new firebase.auth.GoogleAuthProvider());
};
Demo.prototype.signOut = function() {
firebase.auth().signOut();
};
Demo.prototype.onAuthStateChanged = function(user) {
if (user) {
// If we have a user, simulate a click to get all their messages.
// Material Design Lite will create a <span> child that we'll expect to be clicked
$('#message-list-button-all > span').click();
this.$messageTextarea.removeAttr('disabled');
this.$createMessageButton.removeAttr('disabled');
} else {
this.$messageTextarea.attr('disabled', true);
this.$createMessageButton.attr('disabled', true);
this.$createMessageResult.html('');
this.$messageList.html('');
this.$messageDetails.html('');
}
};
Demo.prototype.createMessage = function() {
var message = this.$messageTextarea.val();
if (message === '') return;
// Make an authenticated POST request to create a new message
this.authenticatedRequest('POST', '/api/messages', {message: message}).then(function(response) {
this.$messageTextarea.val('');
this.$messageTextarea.parent().removeClass('is-dirty');
this.$createMessageResult.html('Created <b>' + response.category + '</b> message: ' + response.message);
}.bind(this)).catch(function(error) {
console.log('Error creating message:', message);
throw error;
});
};
Demo.prototype.listMessages = function(event) {
this.$messageListButtons.removeClass('mdl-button--accent');
$(event.target).parent().addClass('mdl-button--accent');
this.$messageList.html('');
this.$messageDetails.html('');
// Make an authenticated GET request for a list of messages
// Optionally specifying a category (positive, negative, neutral)
var label = $(event.target).parent().text().toLowerCase();
var category = label === 'all' ? '' : label;
var url = category ? '/api/messages?category=' + category : '/api/messages';
this.authenticatedRequest('GET', url).then(function(response) {
var elements = response.map(function(message) {
return $('<li>')
.text(message.message)
.addClass('mdl-list__item')
.data('key', message.key)
.on('click', this.messageDetails.bind(this));
}.bind(this));
// Append items to the list and simulate a click to fetch the first message's details
this.$messageList.append(elements);
if (elements.length > 0) {
elements[0].click();
}
}.bind(this)).catch(function(error) {
console.log('Error listing messages.');
throw error;
});
};
Demo.prototype.messageDetails = function(event) {
$('li').removeClass('selected');
$(event.target).addClass('selected');
var key = $(event.target).data('key');
this.authenticatedRequest('GET', '/api/message/' + key).then(function(response) {
this.$messageDetails.text(JSON.stringify(response, null, 2));
}.bind(this)).catch(function(error) {
console.log('Error getting message details.');
throw error;
});
};
Demo.prototype.authenticatedRequest = function(method, url, body) {
if (!firebase.auth().currentUser) {
throw new Error('Not authenticated. Make sure you\'re signed in!');
}
// Get the Firebase auth token to authenticate the request
return firebase.auth().currentUser.getToken().then(function(token) {
var request = {
method: method,
url: url,
dataType: 'json',
beforeSend: function(xhr) { xhr.setRequestHeader('Authorization', 'Bearer ' + token); }
};
if (method === 'POST') {
request.contentType = 'application/json';
request.data = JSON.stringify(body);
}
console.log('Making authenticated request:', method, url);
return $.ajax(request).catch(function() {
throw new Error('Request error: ' + method + ' ' + url);
});
});
};
window.demo = new Demo();