Browse Source

Implement proper path segment routing for user detail dialog

- Restore path segment routing: /admin/users/{userId}
- Add runGuardsAndResolvers to handle component reuse
- Move route parameter handling to ngOnInit for proper timing
- Make fetchUsers return Promise to handle async data loading
- Ensure dialog opens correctly even when component is reused
- Follows original requirement: query params -> path segments
pull/5839/head
HarjobandeepSingh 3 days ago
parent
commit
d7ea931391
  1. 72
      apps/client/src/app/components/admin-users/admin-users.component.ts
  2. 6
      apps/client/src/app/pages/admin/admin-page.routes.ts

72
apps/client/src/app/components/admin-users/admin-users.component.ts

@ -133,13 +133,6 @@ export class GfAdminUsersComponent implements OnDestroy, OnInit {
]; ];
} }
this.route.queryParams
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe((params) => {
if (params['userId']) {
this.openUserDetailDialog(params['userId']);
}
});
this.userService.stateChanged this.userService.stateChanged
.pipe(takeUntil(this.unsubscribeSubject)) .pipe(takeUntil(this.unsubscribeSubject))
@ -169,6 +162,23 @@ export class GfAdminUsersComponent implements OnDestroy, OnInit {
public ngOnInit() { public ngOnInit() {
this.fetchUsers(); this.fetchUsers();
// Handle route parameter changes when component is reused
this.route.params
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe((params) => {
if (params['userId']) {
// If data is already loaded, open dialog immediately
if (this.dataSource.data.length > 0) {
this.openUserDetailDialog(params['userId']);
} else {
// If data is not loaded yet, wait for it to load
this.fetchUsers().then(() => {
this.openUserDetailDialog(params['userId']);
});
}
}
});
} }
public formatDistanceToNow(aDateString: string) { public formatDistanceToNow(aDateString: string) {
@ -245,10 +255,7 @@ export class GfAdminUsersComponent implements OnDestroy, OnInit {
} }
public onOpenUserDetailDialog(userId: string) { public onOpenUserDetailDialog(userId: string) {
this.router.navigate([], { this.router.navigate(['./', userId], { relativeTo: this.route });
relativeTo: this.route,
queryParams: { userId }
});
} }
public ngOnDestroy() { public ngOnDestroy() {
@ -256,27 +263,30 @@ export class GfAdminUsersComponent implements OnDestroy, OnInit {
this.unsubscribeSubject.complete(); this.unsubscribeSubject.complete();
} }
private fetchUsers({ pageIndex }: { pageIndex: number } = { pageIndex: 0 }) { private fetchUsers({ pageIndex }: { pageIndex: number } = { pageIndex: 0 }): Promise<void> {
this.isLoading = true; this.isLoading = true;
if (pageIndex === 0 && this.paginator) { if (pageIndex === 0 && this.paginator) {
this.paginator.pageIndex = 0; this.paginator.pageIndex = 0;
} }
this.adminService return new Promise((resolve) => {
.fetchUsers({ this.adminService
skip: pageIndex * this.pageSize, .fetchUsers({
take: this.pageSize skip: pageIndex * this.pageSize,
}) take: this.pageSize
.pipe(takeUntil(this.unsubscribeSubject)) })
.subscribe(({ count, users }) => { .pipe(takeUntil(this.unsubscribeSubject))
this.dataSource = new MatTableDataSource(users); .subscribe(({ count, users }) => {
this.totalItems = count; this.dataSource = new MatTableDataSource(users);
this.totalItems = count;
this.isLoading = false;
this.isLoading = false;
this.changeDetectorRef.markForCheck();
}); this.changeDetectorRef.markForCheck();
resolve();
});
});
} }
private openUserDetailDialog(userId: string) { private openUserDetailDialog(userId: string) {
@ -285,10 +295,7 @@ export class GfAdminUsersComponent implements OnDestroy, OnInit {
}); });
if (!userData) { if (!userData) {
this.router.navigate([], { this.router.navigate(['../'], { relativeTo: this.route });
relativeTo: this.route,
queryParams: {}
});
return; return;
} }
@ -308,10 +315,7 @@ export class GfAdminUsersComponent implements OnDestroy, OnInit {
.pipe(takeUntil(this.unsubscribeSubject)) .pipe(takeUntil(this.unsubscribeSubject))
.subscribe(() => { .subscribe(() => {
this.fetchUsers(); this.fetchUsers();
this.router.navigate([], { this.router.navigate(['../'], { relativeTo: this.route });
relativeTo: this.route,
queryParams: {}
});
}); });
} }
} }

6
apps/client/src/app/pages/admin/admin-page.routes.ts

@ -38,6 +38,12 @@ export const routes: Routes = [
path: internalRoutes.adminControl.subRoutes.users.path, path: internalRoutes.adminControl.subRoutes.users.path,
component: GfAdminUsersComponent, component: GfAdminUsersComponent,
title: internalRoutes.adminControl.subRoutes.users.title title: internalRoutes.adminControl.subRoutes.users.title
},
{
path: `${internalRoutes.adminControl.subRoutes.users.path}/:userId`,
component: GfAdminUsersComponent,
title: internalRoutes.adminControl.subRoutes.users.title,
runGuardsAndResolvers: 'paramsOrQueryParamsChange'
} }
], ],
component: AdminPageComponent, component: AdminPageComponent,

Loading…
Cancel
Save