Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
39 changes: 39 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"env": {
"browser": true
},
"extends": "eslint:recommended",
"rules": {
"indent": [
"error",
2
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"always"
],
"curly": [
"error",
"all"
],
"consistent-this": [
"error",
"that"
],
"no-console": 0,
"no-unused-vars": 0
},
"globals": {
"chrome": true,
"firebase": true,
"Promise": true
}
}
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
language: node_js
node_js:
- "7"
install:
- npm install -g lerna
- npm install -g eslint
- lerna bootstrap
script:
- ./scripts/test.sh
Copy link
Contributor

Choose a reason for hiding this comment

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

Add end of file line break

4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ Please read and follow the steps in the [CONTRIBUTING.md](CONTRIBUTING.md)

## License
See [LICENSE](LICENSE)

## Build Status

[![Build Status](https://travis-ci.org/firebase/quickstart-js.svg?branch=master)](https://travis-ci.org/firebase/quickstart-js)
32 changes: 17 additions & 15 deletions database/scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
'use strict';

var componentHandler = componentHandler || undefined;
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe add componentHandler to the globals instead ?


// Shortcuts to DOM Elements.
var messageForm = document.getElementById('message-form');
var messageInput = document.getElementById('new-post-message');
Expand Down Expand Up @@ -162,7 +164,7 @@ function createPostElement(postId, title, text, author, authorId, authorPic) {
// [END post_value_event_listener]

// Listen for the starred status.
var starredStatusRef = firebase.database().ref('posts/' + postId + '/stars/' + uid)
var starredStatusRef = firebase.database().ref('posts/' + postId + '/stars/' + uid);
starredStatusRef.on('value', function(snapshot) {
updateStarredByCurrentUser(postElement, snapshot.val());
});
Expand Down Expand Up @@ -273,21 +275,21 @@ function startDatabaseQueries() {
var author = data.val().author || 'Anonymous';
var containerElement = sectionElement.getElementsByClassName('posts-container')[0];
containerElement.insertBefore(
createPostElement(data.key, data.val().title, data.val().body, author, data.val().uid, data.val().authorPic),
containerElement.firstChild);
createPostElement(data.key, data.val().title, data.val().body, author, data.val().uid, data.val().authorPic),
containerElement.firstChild);
});
postsRef.on('child_changed', function(data) {
var containerElement = sectionElement.getElementsByClassName('posts-container')[0];
var postElement = containerElement.getElementsByClassName('post-' + data.key)[0];
postElement.getElementsByClassName('mdl-card__title-text')[0].innerText = data.val().title;
postElement.getElementsByClassName('username')[0].innerText = data.val().author;
postElement.getElementsByClassName('text')[0].innerText = data.val().body;
postElement.getElementsByClassName('star-count')[0].innerText = data.val().starCount;
postsRef.on('child_changed', function(data) {
var containerElement = sectionElement.getElementsByClassName('posts-container')[0];
var postElement = containerElement.getElementsByClassName('post-' + data.key)[0];
postElement.getElementsByClassName('mdl-card__title-text')[0].innerText = data.val().title;
postElement.getElementsByClassName('username')[0].innerText = data.val().author;
postElement.getElementsByClassName('text')[0].innerText = data.val().body;
postElement.getElementsByClassName('star-count')[0].innerText = data.val().starCount;
});
postsRef.on('child_removed', function(data) {
var containerElement = sectionElement.getElementsByClassName('posts-container')[0];
var post = containerElement.getElementsByClassName('post-' + data.key)[0];
post.parentElement.removeChild(post);
var containerElement = sectionElement.getElementsByClassName('posts-container')[0];
var post = containerElement.getElementsByClassName('post-' + data.key)[0];
post.parentElement.removeChild(post);
});
};

Expand Down Expand Up @@ -370,8 +372,8 @@ function newPostForCurrentUser(title, text) {
var username = (snapshot.val() && snapshot.val().username) || 'Anonymous';
// [START_EXCLUDE]
return writeNewPost(firebase.auth().currentUser.uid, username,
firebase.auth().currentUser.photoURL,
title, text);
firebase.auth().currentUser.photoURL,
title, text);
// [END_EXCLUDE]
});
// [END single_value_read]
Expand Down
8 changes: 8 additions & 0 deletions firestore/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"globals": {
"Navigo": true,
"mdc": true,
"importScripts": true,
"FriendlyEats": true
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Add missing End of file line break :)

1 change: 0 additions & 1 deletion firestore/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"description": "Finished code for the Cloud Firestore codelab",
"main": ".",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"fmt": "prettier --write scripts/* styles/*"
},
"author": "",
Expand Down
35 changes: 19 additions & 16 deletions firestore/scripts/FriendlyEats.Data.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,25 @@
'use strict';

FriendlyEats.prototype.addRestaurant = function(data) {
const collection = firebase.firestore().collection('restaurants');
var collection = firebase.firestore().collection('restaurants');
return collection.add(data);
};

FriendlyEats.prototype.getAllRestaurants = function(render) {
const query = firebase.firestore()
.collection('restaurants')
.orderBy('avgRating', 'desc')
.limit(50);
var query = firebase.firestore()
.collection('restaurants')
.orderBy('avgRating', 'desc')
.limit(50);
this.getDocumentsInQuery(query, render);
};

FriendlyEats.prototype.getDocumentsInQuery = function(query, render) {
query.onSnapshot(snapshot => {
if (!snapshot.size) return render();
query.onSnapshot(function (snapshot) {
if (!snapshot.size) {
return render();
}

snapshot.docChanges.forEach(change => {
snapshot.docChanges.forEach(function(change) {
if (change.type === 'added') {
render(change.doc);
}
Expand All @@ -45,7 +47,7 @@ FriendlyEats.prototype.getRestaurant = function(id) {
};

FriendlyEats.prototype.getFilteredRestaurants = function(filters, render) {
let query = firebase.firestore().collection('restaurants');
var query = firebase.firestore().collection('restaurants');

if (filters.category !== 'Any') {
query = query.where('category', '==', filters.category);
Expand All @@ -62,21 +64,22 @@ FriendlyEats.prototype.getFilteredRestaurants = function(filters, render) {
if (filters.sort === 'Rating') {
query = query.orderBy('avgRating', 'desc');
} else if (filters.sort === 'Reviews') {
query = query.orderBy('numRatings', 'desc');
}

this.getDocumentsInQuery(query, render);
};

FriendlyEats.prototype.addRating = function(restaurantID, rating) {
const collection = firebase.firestore().collection('restaurants');
const document = collection.doc(restaurantID);
var collection = firebase.firestore().collection('restaurants');
var document = collection.doc(restaurantID);

return document.collection('ratings').add(rating).then(() => {
return firebase.firestore().runTransaction(transaction => {
return transaction.get(document).then(doc => {
const data = doc.data();
return document.collection('ratings').add(rating).then(function() {
return firebase.firestore().runTransaction(function(transaction) {
return transaction.get(document).then(function(doc) {
var data = doc.data();

let newAverage =
var newAverage =
(data.numRatings * data.avgRating + rating.rating) /
(data.numRatings + 1);

Expand Down
42 changes: 21 additions & 21 deletions firestore/scripts/FriendlyEats.Mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,29 @@
* Adds a set of mock Restaurants to the Cloud Firestore.
*/
FriendlyEats.prototype.addMockRestaurants = function() {
const promises = [];
var promises = [];

for (let i = 0; i < 20; i++) {
let name =
for (var i = 0; i < 20; i++) {
var name =
this.getRandomItem(this.data.words) +
' ' +
this.getRandomItem(this.data.words);
let category = this.getRandomItem(this.data.categories);
let city = this.getRandomItem(this.data.cities);
let price = Math.floor(Math.random() * 4) + 1;
let photoID = Math.floor(Math.random() * 22) + 1;
let photo = `https://storage.googleapis.com/firestorequickstarts.appspot.com/food_${photoID}.png`;
let numRatings = 0;
let avgRating = 0;
var category = this.getRandomItem(this.data.categories);
var city = this.getRandomItem(this.data.cities);
var price = Math.floor(Math.random() * 4) + 1;
var photoID = Math.floor(Math.random() * 22) + 1;
var photo = 'https://storage.googleapis.com/firestorequickstarts.appspot.com/food_' + photoID + '.png';
var numRatings = 0;
var avgRating = 0;

const promise = this.addRestaurant({
name,
category,
price,
city,
numRatings,
avgRating,
photo
var promise = this.addRestaurant({
name: name,
category: category,
price: price,
city: city,
numRating: numRatings,
avgRating: avgRating,
photo: photo
});

if (!promise) {
Expand All @@ -59,9 +59,9 @@ FriendlyEats.prototype.addMockRestaurants = function() {
* Adds a set of mock Ratings to the given Restaurant.
*/
FriendlyEats.prototype.addMockRatings = function(restaurantID) {
const ratingPromises = [];
for (let r = 0; r < 5*Math.random(); r++) {
const rating = this.data.ratings[
var ratingPromises = [];
for (var r = 0; r < 5*Math.random(); r++) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just noticed how amazing this is. The r < 5 * Math.random() part will execute on each loop so it's super, duper random.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's pretty funny. I wonder what the distribution of actual r's is lol.

var rating = this.data.ratings[
parseInt(this.data.ratings.length*Math.random())
];
rating.userName = 'Bot (Web)';
Expand Down
Loading