-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Follow users dev #53
Follow users dev #53
Changes from all commits
2d41b4e
532b57a
72493e9
944394b
29d3224
4ba1a0f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import {ActivatedRoute, Router} from '@angular/router'; | ||
import {AngularFirestore} from '@angular/fire/firestore'; | ||
import {Component, OnInit} from '@angular/core'; | ||
import {AngularFireAuth} from '@angular/fire/auth'; | ||
import {Component, OnInit, NgZone} from '@angular/core'; | ||
import {User} from '../user'; | ||
import {Converter} from '../converter'; | ||
import { templateJitUrl } from '@angular/compiler'; | ||
|
||
@Component({ | ||
selector: 'app-user-page', | ||
|
@@ -11,16 +13,33 @@ import {Converter} from '../converter'; | |
}) | ||
export class UserPageComponent implements OnInit { | ||
username = ''; | ||
currentPageUid = ''; | ||
loggedIn = false; | ||
displayName = ''; | ||
picUrl = 'assets/images/blank-profile.png'; | ||
profileLoaded = false; | ||
userFollowing = false; | ||
currentUserUid!: string; | ||
currentUserData: User | undefined = undefined; | ||
|
||
constructor( | ||
private router: Router, | ||
private activatedRoute: ActivatedRoute, | ||
private afs: AngularFirestore | ||
) {} | ||
private afs: AngularFirestore, | ||
private fAuth: AngularFireAuth, | ||
private zone: NgZone | ||
) { | ||
this.fAuth.onAuthStateChanged(auth => { | ||
if (auth) { | ||
this.loggedIn = true; | ||
this.currentUserUid = auth.uid; | ||
} else { | ||
this.loggedIn = false; | ||
this.currentUserData = undefined; | ||
this.userFollowing = false; | ||
} | ||
}); | ||
} | ||
|
||
ngOnInit() { | ||
const username = this.activatedRoute.snapshot.paramMap.get('username'); | ||
|
@@ -38,10 +57,12 @@ export class UserPageComponent implements OnInit { | |
.ref.withConverter(new Converter().usernameConverter) | ||
.get(); | ||
if (postUsername !== null && postUsername.data() !== undefined) { | ||
this.currentPageUid = postUsername.data()?.uid!; | ||
const postUser = await this.afs | ||
.doc('/users/' + postUsername.data()?.uid + '/') | ||
.doc('/users/' + this.currentPageUid + '/') | ||
.ref.withConverter(new Converter().userConverter) | ||
.get(); | ||
this.userFollowing = await this.isUserFollowing(); | ||
if (postUser !== null && postUser.data() !== undefined) { | ||
const user: User = postUser.data()!; | ||
this.displayName = user.displayName !== null ? user.displayName : ''; | ||
|
@@ -55,4 +76,61 @@ export class UserPageComponent implements OnInit { | |
} | ||
} | ||
} | ||
|
||
async isUserFollowing() { | ||
if (this.currentPageUid && this.loggedIn && this.currentUserUid) { | ||
const postUser = await this.afs.firestore | ||
.doc('/users/' + this.currentUserUid + '/') | ||
.withConverter(new Converter().userConverter) | ||
.get(); | ||
if (postUser) { | ||
this.currentUserData = postUser.data(); | ||
if ( | ||
this.currentUserData && | ||
this.currentUserData.following.indexOf(this.currentPageUid) > -1 | ||
) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
setFollowing(follow: boolean){ | ||
if (this.currentUserData && this.currentUserData.following) { | ||
if(follow){ | ||
this.currentUserData.following.push(this.currentPageUid); | ||
} else { | ||
const index = this.currentUserData.following.indexOf(this.currentPageUid); | ||
if (index > -1) { | ||
this.currentUserData.following.splice(index, 1); | ||
} | ||
} | ||
this.fAuth.currentUser.then(user => { | ||
if (user) { | ||
this.afs | ||
.collection('users') | ||
.doc(this.currentUserUid) | ||
.ref.withConverter(new Converter().userConverter) | ||
.update({following: this.currentUserData!.following}) | ||
.then(() => { | ||
this.userFollowing = follow; | ||
}); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
follow() { | ||
this.zone.run(() => { | ||
this.setFollowing(true); | ||
}) | ||
} | ||
|
||
|
||
unfollow() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be a bit cleaner to have one setFollowing(bool) method that had all the storage logic in it, and turn follow() and unfollow() into one-line methods that call setFollowing(true/false). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the idea, it definitely made the readability better and condensed the code. |
||
this.zone.run(() => { | ||
this.setFollowing(false) | ||
}) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's really nice to limit visibility as much as possible up front! Thanks for switching a bunch of these away from public