Skip to content

Commit

Permalink
Update photo on browser when new profile photo is chosen
Browse files Browse the repository at this point in the history
  • Loading branch information
marvac committed Jan 23, 2019
1 parent 0299146 commit 6d632ab
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 6 deletions.
1 change: 1 addition & 0 deletions Client/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export class AppComponent implements OnInit {

if (user) {
this.authService.currentUser = user;
this.authService.changeProfilePhoto(user.photoUrl);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ <h3>Your Profile</h3>
<div class="row">
<div class="col-sm-4">
<div class="card">
<img class="card-img-top img-thumbnail" src="{{user.photoUrl}}" alt="{{user.knownAs}}" />
<img class="card-img-top img-thumbnail" src="{{photoUrl}}" alt="{{user.knownAs}}" />
<div class="card-body">
<div>
<strong>Location:</strong>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class MemberEditComponent implements OnInit {
}

user: User;
photoUrl: string;

constructor(
private route: ActivatedRoute,
Expand All @@ -33,6 +34,8 @@ export class MemberEditComponent implements OnInit {
this.route.data.subscribe(data => {
this.user = data['user'];
});

this.authService.currentPhotoUrl.subscribe(photoUrl => this.photoUrl = photoUrl);
}

updateUser() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ export class PhotoEditorComponent implements OnInit {
this.currentMainPhoto = this.photos.filter(p => p.isMain)[0];
this.currentMainPhoto.isMain = false;
photo.isMain = true;
this.getMemberPhotoChange.emit(photo.url);

this.authService.changeProfilePhoto(photo.url);
this.authService.currentUser.photoUrl = photo.url;
localStorage.setItem('user', JSON.stringify(this.authService.currentUser))
}, error => {
this.alertify.error(error);
})
Expand Down
2 changes: 1 addition & 1 deletion Client/src/app/components/nav-menu/nav-menu.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

<div class="dropdown" *ngIf="isAuthenticated()" dropdown>
<span class="mr-2">
<img src="{{ authService.currentUser.photoUrl }}" />
<img src="{{ photoUrl }}" />
</span>
<a class="dropdown-toggle text-light" dropdownToggle>
Welcome {{ authService.decodedToken?.unique_name | titlecase }}
Expand Down
5 changes: 4 additions & 1 deletion Client/src/app/components/nav-menu/nav-menu.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ import { Router } from '@angular/router';
})
export class NavMenuComponent implements OnInit {
model: any = {};
photoUrl: string;

constructor(public authService: AuthService, private alertify: AlertifyService, private router: Router) { }

ngOnInit() { }
ngOnInit() {
this.authService.currentPhotoUrl.subscribe(photoUrl => this.photoUrl = photoUrl);
}

login() {
this.authService.login(this.model).subscribe(next => {
Expand Down
9 changes: 8 additions & 1 deletion Client/src/app/services/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { map } from 'rxjs/operators';
import { Observable } from 'rxjs';
import { Observable, BehaviorSubject } from 'rxjs';
import { JwtHelperService } from '@auth0/angular-jwt'
import { User } from '../models/user';

Expand All @@ -16,6 +16,8 @@ export class AuthService {

public currentUser: User;
public decodedToken: any;
public photoUrl = new BehaviorSubject<string>('../../assets/placeholder.png');
public currentPhotoUrl = this.photoUrl.asObservable();

constructor(private http: HttpClient) { }

Expand All @@ -29,6 +31,7 @@ export class AuthService {
localStorage.setItem('user', JSON.stringify(user.userResource));
this.decodedToken = this.jwtHelper.decodeToken(user.token);
this.currentUser = user.userResource;
this.changeProfilePhoto(this.currentUser.photoUrl);
}
})
);
Expand All @@ -42,4 +45,8 @@ export class AuthService {
const token = localStorage.getItem('token');
return !this.jwtHelper.isTokenExpired(token);
}

changeProfilePhoto(photoUrl: string) {
this.photoUrl.next(photoUrl);
}
}
Binary file added Client/src/assets/placeholder.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6d632ab

Please sign in to comment.