clean code
This commit is contained in:
parent
17a00aa912
commit
8e48a3b3a3
4 changed files with 31 additions and 35 deletions
|
|
@ -40,7 +40,7 @@ export class LoginService {
|
|||
const userData = this.firebaseService.getUserDataFromUid(user.uid);
|
||||
if (userData.length > 0 && userData[0].id) {
|
||||
this.getUserIdInLocalStorage(userData[0].id);
|
||||
this.updateUserOnlineStatus(userData[0].id);
|
||||
this.updateUserOnlineStatus(userData[0].id, true);
|
||||
}
|
||||
this.sharedService.isBtnDisabled = false;
|
||||
})
|
||||
|
|
@ -107,31 +107,23 @@ export class LoginService {
|
|||
);
|
||||
getDocs(querySnapshot).then((snapshot) => {
|
||||
if (snapshot.empty) {
|
||||
const displayName = user.displayName || '';
|
||||
const [firstName = '', lastName = ''] = displayName.split(' ');
|
||||
this.createUserInFirestore({
|
||||
uId: user.uid,
|
||||
email: user.email || 'no mail',
|
||||
firstName: user.displayName
|
||||
? user.displayName.split(' ')[0].charAt(0).toUpperCase() +
|
||||
user.displayName.split(' ')[0].slice(1)
|
||||
: '',
|
||||
lastName: user.displayName
|
||||
? user.displayName.split(' ')[1].charAt(0).toUpperCase() +
|
||||
user.displayName.split(' ')[1].slice(1)
|
||||
: '',
|
||||
firstName: firstName.charAt(0).toUpperCase() + firstName.slice(1),
|
||||
lastName: lastName.charAt(0).toUpperCase() + lastName.slice(1),
|
||||
status: true,
|
||||
phone: '',
|
||||
initials: user.displayName
|
||||
? user.displayName.split(' ')[0].slice(0, 1) +
|
||||
user.displayName.split(' ')[1].slice(0, 1)
|
||||
: '',
|
||||
initials: firstName.slice(0, 1) + lastName.slice(0, 1),
|
||||
color: this.sharedService.generateRandomColor(),
|
||||
lastLogin: 0,
|
||||
});
|
||||
this.sharedService.isBtnDisabled = false;
|
||||
} else {
|
||||
this.ifExistUser(user.uid);
|
||||
this.sharedService.isBtnDisabled = false;
|
||||
}
|
||||
this.sharedService.isBtnDisabled = false;
|
||||
});
|
||||
})
|
||||
.catch((error) => {
|
||||
|
|
@ -167,13 +159,13 @@ export class LoginService {
|
|||
const userData = this.firebaseService.getUserDataFromUid(user);
|
||||
if (userData.length > 0 && userData[0].id) {
|
||||
this.getUserIdInLocalStorage(userData[0].id);
|
||||
this.updateUserOnlineStatus(userData[0].id);
|
||||
this.updateUserOnlineStatus(userData[0].id, true);
|
||||
}
|
||||
}
|
||||
|
||||
async updateUserOnlineStatus(userId: string) {
|
||||
async updateUserOnlineStatus(userId: string, status: boolean) {
|
||||
await updateDoc(doc(collection(this.firestore, 'users'), userId), {
|
||||
status: true,
|
||||
status: status,
|
||||
lastLogin: new Date().getTime(),
|
||||
}).catch((err) => {
|
||||
console.error(err);
|
||||
|
|
@ -197,4 +189,15 @@ export class LoginService {
|
|||
getUserIdInLocalStorage(userId: string) {
|
||||
localStorage.setItem('currentUserJOIN', JSON.stringify(userId));
|
||||
}
|
||||
|
||||
// LOGOUT
|
||||
|
||||
logout(userId: string) {
|
||||
this.deleteUserIdInLocalStorage();
|
||||
this.updateUserOnlineStatus(userId, false);
|
||||
}
|
||||
|
||||
deleteUserIdInLocalStorage() {
|
||||
localStorage.removeItem('currentUserJOIN');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -40,17 +40,6 @@ export class SharedService {
|
|||
this.removeResizeListener();
|
||||
}
|
||||
|
||||
// LOGOUT
|
||||
|
||||
logout() {
|
||||
this.deleteUserIdInLocalStorage();
|
||||
window.location.reload();
|
||||
}
|
||||
|
||||
deleteUserIdInLocalStorage() {
|
||||
localStorage.removeItem('currentUserJOIN');
|
||||
}
|
||||
|
||||
// RANDOM COLOR
|
||||
|
||||
generateRandomColor(): string {
|
||||
|
|
|
|||
|
|
@ -4,7 +4,8 @@ import { HeaderComponent } from '../header.component';
|
|||
import { LanguageService } from '../../../../services/language.service';
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { TranslateModule } from '@ngx-translate/core';
|
||||
import { SharedService } from '../../../../services/shared.service';
|
||||
import { LoginService } from '../../../../services/login.service';
|
||||
import { FirebaseService } from '../../../../services/firebase.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-navbar',
|
||||
|
|
@ -22,7 +23,8 @@ export class NavbarComponent {
|
|||
constructor(
|
||||
public langService: LanguageService,
|
||||
private router: Router,
|
||||
private sharedService: SharedService
|
||||
private loginService: LoginService,
|
||||
private firebaseService: FirebaseService
|
||||
) {}
|
||||
|
||||
ngOnInit() {
|
||||
|
|
@ -30,6 +32,6 @@ export class NavbarComponent {
|
|||
}
|
||||
|
||||
logout() {
|
||||
this.sharedService.logout();
|
||||
this.loginService.logout(this.firebaseService.getCurrentUserId());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,8 @@ import { Component, OnInit } from '@angular/core';
|
|||
import { NavigationEnd, Router, RouterModule } from '@angular/router';
|
||||
import { TranslateModule } from '@ngx-translate/core';
|
||||
import { LanguageService } from '../../../services/language.service';
|
||||
import { SharedService } from '../../../services/shared.service';
|
||||
import { LoginService } from '../../../services/login.service';
|
||||
import { FirebaseService } from '../../../services/firebase.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-sidebar',
|
||||
|
|
@ -18,7 +19,8 @@ export class SidebarComponent implements OnInit {
|
|||
constructor(
|
||||
private router: Router,
|
||||
public languageService: LanguageService,
|
||||
private sharedService: SharedService
|
||||
private loginService: LoginService,
|
||||
private firebaseService: FirebaseService
|
||||
) {}
|
||||
|
||||
ngOnInit(): void {
|
||||
|
|
@ -26,7 +28,7 @@ export class SidebarComponent implements OnInit {
|
|||
}
|
||||
|
||||
logout() {
|
||||
this.sharedService.logout();
|
||||
this.loginService.logout(this.firebaseService.getCurrentUserId());
|
||||
}
|
||||
|
||||
getCurrentPath() {
|
||||
|
|
|
|||
Loading…
Reference in a new issue