8000 Merge pull request #30 from aruld/google-profile-photo-patch-new · iambryansanders/quickstart-js@94765eb · GitHub
[go: up one dir, main page]

Skip to content

Commit 94765eb

Browse files
Merge pull request firebase#30 from aruld/google-profile-photo-patch-new
Issue firebase#24 Google profile photo patch new
2 parents cdffb60 + 8ec0ef1 commit 94765eb

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

database/scripts/main.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,15 @@ var myTopPostsMenuButton = document.getElementById('menu-my-top-posts');
3434
* Saves a new post to the Firebase DB.
3535
*/
3636
// [START write_fan_out]
37-
function writeNewPost(uid, username, title, body) {
37+
function writeNewPost(uid, username, picture, title, body) {
3838
// A post entry.
3939
var postData = {
4040
author: username,
4141
uid: uid,
4242
body: body,
4343
title: title,
44-
starCount: 0
44+
starCount: 0,
45+
authorPic: picture
4546
};
4647

4748
// Get a key for a new Post.
@@ -82,7 +83,7 @@ function toggleStar(postRef, uid) {
8283
/**
8384
* Creates a post element.
8485
*/
85-
function createPostElement(postId, title, text, author, authorId) {
86+
function createPostElement(postId, title, text, author, authorId, authorPic) {
8687
var uid = firebase.auth().currentUser.uid;
8788

8889
var html =
@@ -128,7 +129,8 @@ function createPostElement(postId, title, text, author, authorId) {
128129
// Set values.
129130
postElement.getElementsByClassName('text')[0].innerText = text;
130131
postElement.getElementsByClassName('mdl-card__title-text')[0].innerText = title;
131-
postElement.getElementsByClassName('username')[0].innerText = author;
132+
postElement.getElementsByClassName('username')[0].innerText = author || 'Anonymous';
133+
postElement.getElementsByClassName('avatar')[0].style.backgroundImage = `url("${authorPic || './silhouette.jpg'}")`;
132134

133135
// Listen for comments.
134136
// [START child_event_listener_recycler]
@@ -218,7 +220,7 @@ function addCommentElement(postElement, id, text, author) {
218220
comment.classList.add('comment-' + id);
219221
comment.innerHTML = '<span class="username"></span><span class="comment"></span>';
220222
comment.getElementsByClassName('comment')[0].innerText = text;
221-
comment.getElementsByClassName('username')[0].innerText = author;
223+
comment.getElementsByClassName('username')[0].innerText = author || 'Anonymous';
222224

223225
var commentsContainer = postElement.getElementsByClassName('comments-container')[0];
224226
commentsContainer.appendChild(comment);
@@ -256,9 +258,10 @@ function startDatabaseQueries() {
256258

257259
var fetchPosts = function(postsRef, sectionElement) {
258260
postsRef.on('child_added', function(data) {
261+
var author = data.val().author || 'Anonymous';
259262
var containerElement = sectionElement.getElementsByClassName('posts-container')[0];
260263
containerElement.insertBefore(
261-
createPostElement(data.key, data.val().title, data.val().body, data.val().author, data.val().uid),
264+
createPostElement(data.key, data.val().title, data.val().body, author, data.val().uid, data.val().authorPic),
262265
containerElement.firstChild);
263266
});
264267
};
@@ -272,10 +275,11 @@ function startDatabaseQueries() {
272275
* Writes the user's data to the database.
273276
*/
274277
// [START basic_write]
275-
function writeUserData(userId, name, email) {
278+
function writeUserData(userId, name, email, imageUrl) {
276279
firebase.database().ref('users/' + userId).set({
277280
username: name,
278-
email: email
281+
email: email,
282+
profile_picture : imageUrl
279283
});
280284
}
281285
// [END basic_write]
@@ -292,7 +296,7 @@ window.addEventListener('load', function() {
292296
firebase.auth().onAuthStateChanged(function(user) {
293297
if (user) {
294298
splashPage.style.display = 'none';
295-
writeUserData(user.uid, user.displayName, user.email);
299+
writeUserData(user.uid, user.displayName, user.email, user.photoURL);
296300
startDatabaseQueries();
297301
} else {
298302
splashPage.style.display = '';
@@ -311,6 +315,7 @@ window.addEventListener('load', function() {
311315
var username = snapshot.val().username;
312316
// [START_EXCLUDE]
313317
writeNewPost(firebase.auth().currentUser.uid, firebase.auth().currentUser.displayName,
318+
firebase.auth().currentUser.photoURL,
314319
titleInput.value, postText).then(function() {
315320
myPostsMenuButton.click();
316321
});

0 commit comments

Comments
 (0)
0