Fixed profile navigation #255

This commit is contained in:
Sven Heidemann 2023-03-24 14:15:21 +01:00
parent 24d5bbf4d8
commit e6c614dfdc

View File

@ -1,4 +1,4 @@
import { Component, OnInit } from "@angular/core"; import { Component, OnDestroy, OnInit } from "@angular/core";
import { ActivatedRoute, Router } from "@angular/router"; import { ActivatedRoute, Router } from "@angular/router";
import { Queries } from "../../../../models/graphql/queries.model"; import { Queries } from "../../../../models/graphql/queries.model";
import { UserListQuery } from "../../../../models/graphql/query.model"; import { UserListQuery } from "../../../../models/graphql/query.model";
@ -10,17 +10,21 @@ import { AuthService } from "src/app/services/auth/auth.service";
import { ToastService } from "src/app/services/toast/toast.service"; import { ToastService } from "src/app/services/toast/toast.service";
import { TranslateService } from "@ngx-translate/core"; import { TranslateService } from "@ngx-translate/core";
import { Server } from "../../../../models/data/server.model"; import { Server } from "../../../../models/data/server.model";
import { Subject } from "rxjs";
import { takeUntil } from "rxjs/operators";
@Component({ @Component({
selector: "app-profile", selector: "app-profile",
templateUrl: "./profile.component.html", templateUrl: "./profile.component.html",
styleUrls: ["./profile.component.scss"] styleUrls: ["./profile.component.scss"]
}) })
export class ProfileComponent implements OnInit { export class ProfileComponent implements OnInit, OnDestroy {
user: User = { createdAt: "", modifiedAt: "" }; user: User = { createdAt: "", modifiedAt: "" };
private server: Server = {}; private server: Server = {};
private unsubscriber = new Subject<void>();
constructor( constructor(
private route: ActivatedRoute, private route: ActivatedRoute,
private router: Router, private router: Router,
@ -32,42 +36,47 @@ export class ProfileComponent implements OnInit {
) { ) {
} }
ngOnInit() { public ngOnInit(): void {
this.data.getServerFromRoute(this.route).then(async (server) => { this.route.params.pipe(takeUntil(this.unsubscriber)).subscribe(params => {
if (!this.route.snapshot.params["memberId"] || this.route.snapshot.params["memberId"] == "undefined") { this.data.getServerFromRoute(this.route).then(async (server) => {
this.router.navigate([`/server/${server.id}`]); if (!params["memberId"] || params["memberId"] == "undefined") {
return; await this.router.navigate([`/server/${server.id}`]);
} return;
this.server = server; }
this.server = server;
let authUser = await this.auth.getLoggedInUser(); let authUser = await this.auth.getLoggedInUser();
this.spinner.showSpinner(); this.spinner.showSpinner();
let user: UserDTO | null = authUser?.users?.find(u => u.server == server.id) ?? null; let user: UserDTO | null = authUser?.users?.find(u => u.server == server.id) ?? null;
if (!user || user?.id != this.route.snapshot.params["memberId"] && !user?.isModerator) { if (!user || user?.id != params["memberId"] && !user?.isModerator) {
this.toast.error(this.translate.instant("view.server.profile.permission_denied"), this.translate.instant("view.server.profile.permission_denied_d")); this.toast.error(this.translate.instant("view.server.profile.permission_denied"), this.translate.instant("view.server.profile.permission_denied_d"));
this.spinner.hideSpinner(); this.spinner.hideSpinner();
this.router.navigate(["/server", server.id]); await this.router.navigate(["/server", server.id]);
return; return;
} }
this.data.query<UserListQuery>(Queries.usersQuery, { this.data.query<UserListQuery>(Queries.usersQuery, {
serverId: this.server.id, serverId: this.server.id,
filter: { filter: {
id: this.route.snapshot.params["memberId"] id: params["memberId"]
}
},
(x: { servers: Server[] }) => {
return x.servers[0];
} }
}, ).subscribe(users => {
(x: { servers: Server[] }) => { if (!users.users[0]) {
return x.servers[0]; this.router.navigate([`/server/${server.id}`]);
} }
).subscribe(users => { this.user = users.users[0];
if (!users.users[0]) { this.spinner.hideSpinner();
this.router.navigate([`/server/${server.id}`]); });
}
this.user = users.users[0];
this.spinner.hideSpinner();
}); });
}); });
}
public ngOnDestroy(): void {
this.unsubscriber.next();
this.unsubscriber.complete();
} }
} }