Created
October 7, 2022 11:58
-
-
Save hieptlccc/2e2bd8cc32f03ffa975487a540a23b2d to your computer and use it in GitHub Desktop.
1 on 1 Angular Chat - profile.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit } from '@angular/core'; | |
import { NgForm } from '@angular/forms'; | |
import { AngularFireAuth } from '@angular/fire/auth'; | |
import { Router } from '@angular/router'; | |
import { CometChat } from '@cometchat-pro/chat'; | |
import { environment } from 'src/environments/environment' | |
@Component({ | |
selector: 'app-profile', | |
templateUrl: './profile.component.html', | |
styleUrls: ['./profile.component.css'] | |
}) | |
export class ProfileComponent implements OnInit { | |
loading: boolean = false; | |
user: any = null; | |
constructor(private auth: AngularFireAuth, private route: Router) { | |
this.auth.authState.subscribe((authState) => (this.user = authState)); | |
} | |
ngOnInit(): void { | |
} | |
public submit(form: NgForm): void { | |
if (form.valid) { | |
this.loading = true | |
const photoURL = form.value.avatar | |
const theme = form.value.theme | |
this.auth.authState.subscribe((authState) => { | |
authState.updateProfile({photoURL}) | |
.then(() => this.setProfile(photoURL, theme)) | |
.catch((error) => { | |
console.log(error.message) | |
alert(error.message) | |
this.loading = false | |
}) | |
}); | |
} | |
} | |
private setProfile(avatar: string, theme: string) { | |
const authKey = environment.AUTH_KEY; | |
const uid = this.user.uid; | |
var user = new CometChat.User(uid); | |
if (!avatar.includes('base64')) user.setAvatar(avatar); | |
user.setMetadata({avatar, theme}); | |
CometChat.updateUser(user, authKey) | |
.then(() => this.route.navigate([''])) | |
.catch((error) => { | |
console.log(error.message) | |
alert(error.message) | |
this.loading = false | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment