0

I have found several questions similar to mine, but the big part are related to params, the answers didn't help me to solve my issue. Basically the problem is the following: Once the user logs in, the user is able to visualize his/her username in the navbar, when they click on it, the page takes the user to the profile page, the first time it works perfectly, when the user logs out and logs in with different username the information of the old user is still there unless the page is refreshed.

Service Account

getUserInformation(userId: number) {
if (!this.userInformation$) {
  this.userInformation$ = this.http.post<any>(this.baseUrlGetUserInfo, { userId }).pipe(shareReplay());
}
return this.userInformation$;
}

On init method

ngOnInit() {
this.route.params.subscribe(params => {
  this.loaderSpinner = true;
  // tslint:disable-next-line: radix
  // this.userId = this.accntService.currentUserId;
  // tslint:disable-next-line: radix
  this.accntService.currentUserId.subscribe(res => {
    this.userId = res;
  });
  // tslint:disable-next-line: radix
  this.userInformation$ = this.accntService.getUserInformation(parseInt(this.userId));
  this.userInformation$.subscribe(result => {
    this.userInformation = result.userInformation;
    console.log(this.userInformation);
    this.loaderSpinner = false;
  }, error => {
    this.loaderSpinner = false;
    console.error(error);
  });
});
}

Navbar HTML

<a [routerLink]="['/profile']" class="nav-link" *ngIf="(userName$ | async) as userName">
          {{userName}}
        </a>

Can somebody help me with this?

Thanks in advance.

HeyBaldur
  • 545
  • 1
  • 7
  • 16
  • 2
    Have you cleared userInformation$ values on log out operation? – Rajesh Jan 14 '20 at 03:37
  • Won't work because you should track the info on route event also. Something like this: `this.router.events.pipe(....).subscribe(...)`. Just search and you will find couple of useful examples. – k.vincent Jan 14 '20 at 07:23
  • try to make the userId veriable to empty. this.route.params.subscribe(params => { this.userId =null; your code here }); – Tharaka Jan 14 '20 at 09:18

1 Answers1

0

Here is the problem, I found the issue. Basically the error was in the onDestroy.

private getUserInfo(): void {
this.loaderSpinner = true;
this.accntService.currentUserId.pipe(takeUntil(this.$ondestroy)).subscribe(res => {
  this.userId = res;
});

this.accntService.getUserInformation(this.userId).pipe(takeUntil(this.$ondestroy)).subscribe(response => {
  this.userInformation = response.userInformation;
  this.loaderSpinner = false;
}, () => {
  this.loaderSpinner = false;
});
}

ngOnInit() {
this.getUserInfo(); }

ngOnDestroy() {
this.$ondestroy.next(true);
}
HeyBaldur
  • 545
  • 1
  • 7
  • 16