From 7d450a12acf5475339a38f3d5f07288440c5dad2 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 21 Feb 2023 11:50:30 +0100 Subject: [PATCH] Improved server loading by route #216 --- kdb-web/package.json | 2 +- .../auto-roles-rules.component.ts | 58 ++++++++--------- .../auto-roles/auto-roles.component.ts | 36 +++++------ .../view/server/members/members.component.ts | 34 +++++----- .../view/server/profile/profile.component.ts | 62 +++++++++---------- .../server-dashboard.component.ts | 4 +- kdb-web/src/app/services/data/data.service.ts | 38 +++++++----- kdb-web/src/assets/config.json | 4 +- 8 files changed, 122 insertions(+), 116 deletions(-) diff --git a/kdb-web/package.json b/kdb-web/package.json index 80c20063..13ddc1d0 100644 --- a/kdb-web/package.json +++ b/kdb-web/package.json @@ -1,6 +1,6 @@ { "name": "kdb-web", - "version": "1.0.dev217", + "version": "1.0.0", "scripts": { "ng": "ng", "update-version": "ts-node-esm update-version.ts", diff --git a/kdb-web/src/app/modules/view/server/auto-role/components/auto-roles-rules/auto-roles-rules.component.ts b/kdb-web/src/app/modules/view/server/auto-role/components/auto-roles-rules/auto-roles-rules.component.ts index 383586a2..2b6a0251 100644 --- a/kdb-web/src/app/modules/view/server/auto-role/components/auto-roles-rules/auto-roles-rules.component.ts +++ b/kdb-web/src/app/modules/view/server/auto-role/components/auto-roles-rules/auto-roles-rules.component.ts @@ -84,36 +84,38 @@ export class AutoRolesRulesComponent implements OnInit { } public ngOnInit(): void { - this.data.getServerFromRoute(this.route); - this.spinner.showSpinner(); - if (!this.route.snapshot.params["autoRoleId"]) { - this.spinner.hideSpinner(); - this.router.navigate(["../"]); - return; - } - this.autoRoleId = +this.route.snapshot.params["autoRoleId"]; - - this.spinner.showSpinner(); - this.data.query(Queries.guildsQuery, { - filter: { - id: this.sidebar.server$.value?.discordId - } - } - ).subscribe(data => { - this.guild = data.guilds[0]; - this.emojis = this.guild.emojis - .map(x => { - return { label: x.name, value: x }; - }); - this.roles = this.guild.roles - .map(x => { - return { label: x.name, value: x }; - }); - this.spinner.hideSpinner(); - }); this.setFilterForm(); - this.loadNextPage(); + this.data.getServerFromRoute(this.route).then(server => { + + this.spinner.showSpinner(); + if (!this.route.snapshot.params["autoRoleId"]) { + this.spinner.hideSpinner(); + this.router.navigate(["../"]); + return; + } + this.autoRoleId = +this.route.snapshot.params["autoRoleId"]; + + this.spinner.showSpinner(); + this.data.query(Queries.guildsQuery, { + filter: { + id: server.discordId + } + } + ).subscribe(data => { + this.guild = data.guilds[0]; + this.emojis = this.guild.emojis + .map(x => { + return { label: x.name, value: x }; + }); + this.roles = this.guild.roles + .map(x => { + return { label: x.name, value: x }; + }); + this.spinner.hideSpinner(); + }); + this.loadNextPage(); + }); } public loadNextPage(): void { diff --git a/kdb-web/src/app/modules/view/server/auto-role/components/auto-roles/auto-roles.component.ts b/kdb-web/src/app/modules/view/server/auto-role/components/auto-roles/auto-roles.component.ts index 78b27de9..03d6b2f8 100644 --- a/kdb-web/src/app/modules/view/server/auto-role/components/auto-roles/auto-roles.component.ts +++ b/kdb-web/src/app/modules/view/server/auto-role/components/auto-roles/auto-roles.component.ts @@ -76,26 +76,26 @@ export class AutoRolesComponent implements OnInit { } public ngOnInit(): void { - this.data.getServerFromRoute(this.route); - - this.spinner.showSpinner(); - this.data.query(Queries.guildsQuery, { - filter: { - id: this.sidebar.server$.value?.discordId - } - } - ).subscribe(data => { - this.guild = data.guilds[0]; - this.channels = this.guild.channels - .filter(x => x.type === ChannelType.text) - .map(x => { - return { label: x.name, value: x }; - }); - this.spinner.hideSpinner(); - }); this.setFilterForm(); - this.loadNextPage(); + this.data.getServerFromRoute(this.route).then(server => { + this.spinner.showSpinner(); + this.data.query(Queries.guildsQuery, { + filter: { + id: server?.discordId + } + } + ).subscribe(data => { + this.guild = data.guilds[0]; + this.channels = this.guild.channels + .filter(x => x.type === ChannelType.text) + .map(x => { + return { label: x.name, value: x }; + }); + this.spinner.hideSpinner(); + }); + this.loadNextPage(); + }); } public loadNextPage(): void { diff --git a/kdb-web/src/app/modules/view/server/members/members.component.ts b/kdb-web/src/app/modules/view/server/members/members.component.ts index 51bf7cba..19ddeebe 100644 --- a/kdb-web/src/app/modules/view/server/members/members.component.ts +++ b/kdb-web/src/app/modules/view/server/members/members.component.ts @@ -14,7 +14,6 @@ import { LevelListQuery, UserListQuery } from "../../../../models/graphql/query. import { DataService } from "../../../../services/data/data.service"; import { Page } from "../../../../models/graphql/filter/page.model"; import { Sort, SortDirection } from "../../../../models/graphql/filter/sort.model"; -import { SidebarService } from "../../../../services/sidebar/sidebar.service"; import { Mutations } from "../../../../models/graphql/mutations.model"; import { throwError } from "rxjs"; import { UpdateUserMutationResult } from "../../../../models/graphql/result.model"; @@ -88,32 +87,31 @@ export class MembersComponent implements OnInit { private fb: FormBuilder, private translate: TranslateService, private data: DataService, - private sidebar: SidebarService, private route: ActivatedRoute ) { } ngOnInit(): void { - this.data.getServerFromRoute(this.route); - - this.spinner.showSpinner(); - this.data.query(Queries.levelQuery, { - filter: { - server: { id: this.sidebar.server$.value?.id } - } - } - ).subscribe(data => { - this.levels = data.levels.map(level => { - return { label: level.name, value: level }; - }); - this.spinner.hideSpinner(); - }); - this.setFilterForm(); - this.loadNextPage(); + + this.data.getServerFromRoute(this.route).then(server => { + this.spinner.showSpinner(); + this.data.query(Queries.levelQuery, { + filter: { + server: { id: server.id } + } + } + ).subscribe(data => { + this.levels = data.levels.map(level => { + return { label: level.name, value: level }; + }); + }); + this.loadNextPage(); + }); } loadNextPage() { + this.spinner.showSpinner(); this.loading = true; this.data.query(Queries.usersQuery, { filter: this.filter, page: this.page, sort: this.sort diff --git a/kdb-web/src/app/modules/view/server/profile/profile.component.ts b/kdb-web/src/app/modules/view/server/profile/profile.component.ts index eb67181b..80909967 100644 --- a/kdb-web/src/app/modules/view/server/profile/profile.component.ts +++ b/kdb-web/src/app/modules/view/server/profile/profile.component.ts @@ -7,7 +7,6 @@ import { DataService } from "../../../../services/data/data.service"; import { User } from "../../../../models/data/user.model"; import { UserDTO } from "../../../../models/auth/auth-user.dto"; import { AuthService } from "src/app/services/auth/auth.service"; -import { SidebarService } from "../../../../services/sidebar/sidebar.service"; import { ToastService } from "src/app/services/toast/toast.service"; import { TranslateService } from "@ngx-translate/core"; @@ -18,13 +17,12 @@ import { TranslateService } from "@ngx-translate/core"; }) export class ProfileComponent implements OnInit { - user!: User; + user: User = { createdAt: "", modifiedAt: "" }; constructor( private route: ActivatedRoute, private router: Router, private spinner: SpinnerService, - private sidebar: SidebarService, private data: DataService, private auth: AuthService, private toast: ToastService, @@ -32,35 +30,37 @@ export class ProfileComponent implements OnInit { ) { } - async ngOnInit() { - this.data.getServerFromRoute(this.route); - - if (!this.route.snapshot.params["memberId"]) { - this.router.navigate(["/dashboard"]); - return; - } - - let authUser = await this.auth.getLoggedInUser(); - this.spinner.showSpinner(); - let user: UserDTO | null = authUser?.users?.find(u => u.server == this.sidebar.server$.value?.id) ?? null; - if (!user || user?.id != this.route.snapshot.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.spinner.hideSpinner(); - this.router.navigate(["/server", this.sidebar.server$.value?.id]); - return; - } - - this.data.query(Queries.singleUserQuery, { - filter: { - id: this.route.snapshot.params["memberId"] - } - }, - function(data: UserListQuery) { - return data.users.length > 0 ? data.users[0] : null; + ngOnInit() { + this.data.getServerFromRoute(this.route).then(async (server) => { + if (!this.route.snapshot.params["memberId"] || this.route.snapshot.params["memberId"] == "undefined") { + this.router.navigate([`/server/${server.id}`]); + return; } - ).subscribe(user => { - this.user = user; - this.spinner.hideSpinner(); + + let authUser = await this.auth.getLoggedInUser(); + this.spinner.showSpinner(); + let user: UserDTO | null = authUser?.users?.find(u => u.server == server.id) ?? null; + if (!user || user?.id != this.route.snapshot.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.spinner.hideSpinner(); + this.router.navigate(["/server", server.id]); + return; + } + + this.data.query(Queries.singleUserQuery, { + filter: { + id: this.route.snapshot.params["memberId"] + } + }, + function(data: UserListQuery) { + return data.users.length > 0 ? data.users[0] : null; + } + ).subscribe(user => { + this.user = user; + this.spinner.hideSpinner(); + }); }); + + } } diff --git a/kdb-web/src/app/modules/view/server/server-dashboard/server-dashboard.component.ts b/kdb-web/src/app/modules/view/server/server-dashboard/server-dashboard.component.ts index 651b8d56..642e0f7c 100644 --- a/kdb-web/src/app/modules/view/server/server-dashboard/server-dashboard.component.ts +++ b/kdb-web/src/app/modules/view/server/server-dashboard/server-dashboard.component.ts @@ -25,7 +25,9 @@ export class ServerDashboardComponent implements OnInit { } ngOnInit(): void { - this.data.getServerFromRoute(this.route); + this.data.getServerFromRoute(this.route).then(server => { + this.server = server; + }); this.sidebar.server$.subscribe(server => { if (!server) { diff --git a/kdb-web/src/app/services/data/data.service.ts b/kdb-web/src/app/services/data/data.service.ts index fbcb7c45..6112d96e 100644 --- a/kdb-web/src/app/services/data/data.service.ts +++ b/kdb-web/src/app/services/data/data.service.ts @@ -25,24 +25,28 @@ export class DataService { ) { } - public getServerFromRoute(route: ActivatedRoute) { - this.spinner.showSpinner(); - if (!route.snapshot.params["serverId"]) { - this.spinner.hideSpinner(); - this.router.navigate(["/dashboard"]); - return; - } - - this.query(Queries.serversQuery, { - filter: { id: route.snapshot.params["serverId"] } - }, - function(data: Query) { - return data.servers.length > 0 ? data.servers[0] : null; + public getServerFromRoute(route: ActivatedRoute): Promise { + return new Promise((resolve, reject) => { + this.spinner.showSpinner(); + if (!route.snapshot.params["serverId"]) { + this.spinner.hideSpinner(); + this.router.navigate(["/dashboard"]); + reject(); } - ).subscribe(server => { - this.sidebar.setServer(server); - this.spinner.hideSpinner(); + + this.query(Queries.serversQuery, { + filter: { id: route.snapshot.params["serverId"] } + }, + function(data: Query) { + return data.servers.length > 0 ? data.servers[0] : null; + } + ).subscribe(server => { + this.sidebar.setServer(server); + this.spinner.hideSpinner(); + resolve(server); + }); }); + } public query(query: string, variables?: Variables, f?: Function): Observable { @@ -53,7 +57,7 @@ export class DataService { }) .pipe(map(d => { if (d.errors && d.errors.length > 0) { - throw new Error(d.errors.map((x: {message: String}) => x.message).toString()); + throw new Error(d.errors.map((x: { message: String }) => x.message).toString()); } return d.data; })) diff --git a/kdb-web/src/assets/config.json b/kdb-web/src/assets/config.json index e129a65a..c055d5cb 100644 --- a/kdb-web/src/assets/config.json +++ b/kdb-web/src/assets/config.json @@ -3,7 +3,7 @@ "WebVersion": { "Major": "1", "Minor": "0", - "Micro": "dev217" + "Micro": "0" }, "Themes": [ { @@ -23,4 +23,4 @@ "Name": "sh-edraft-dark-theme" } ] -} +} \ No newline at end of file -- 2.45.2