From 5f1cf2911f1c1ec3023be3d8f51d1cd273efc376 Mon Sep 17 00:00:00 2001 From: rubikscraft Date: Wed, 16 Mar 2022 22:27:03 +0100 Subject: [PATCH] change child route behaviour --- frontend/src/app/app.component.ts | 18 ++++++++- frontend/src/app/app.module.ts | 3 -- frontend/src/app/app.routing.module.ts | 40 ++++++++++++++++++- .../components/footer/footer.component.html | 2 +- .../app/components/header/header.component.ts | 2 +- frontend/src/app/guards/permission.guard.ts | 2 +- frontend/src/app/models/picsur-routes.ts | 1 + .../src/app/routes/errors/401.component.ts | 9 +++++ .../src/app/routes/errors/404.component.ts | 9 +++++ .../src/app/routes/errors/errors.module.ts | 11 +++++ .../routes/errors/errors.routing.module.ts | 22 ++++++++++ .../pagenotfound/pagenotfound.component.html | 2 - .../pagenotfound/pagenotfound.component.scss | 0 .../pagenotfound/pagenotfound.component.ts | 8 ---- .../pagenotfound/pagenotfound.module.ts | 10 ----- .../processing/processing.routing.module.ts | 2 +- frontend/src/app/routes/routes.ts | 15 ------- .../settings-general.component.html} | 0 .../settings-general.component.ts | 10 +++++ .../settings-general/settings-home.module.ts | 13 ++++++ .../settings-home.routing.module.ts} | 8 ++-- .../settings-home.component.scss | 0 .../settings-home/settings-home.component.ts | 18 --------- .../settings-sidebar.component.html | 32 +++++++++++++-- .../settings-sidebar.component.ts | 34 ++++++++++++++-- .../app/routes/settings/settings.module.ts | 5 +-- .../settings/settings.routing.module.ts | 10 ++--- .../app/routes/user/login/login.component.ts | 2 +- .../user/register/register.component.ts | 2 +- .../app/routes/view/view.routing.module.ts | 2 +- 30 files changed, 204 insertions(+), 88 deletions(-) create mode 100644 frontend/src/app/routes/errors/401.component.ts create mode 100644 frontend/src/app/routes/errors/404.component.ts create mode 100644 frontend/src/app/routes/errors/errors.module.ts create mode 100644 frontend/src/app/routes/errors/errors.routing.module.ts delete mode 100644 frontend/src/app/routes/pagenotfound/pagenotfound.component.html delete mode 100644 frontend/src/app/routes/pagenotfound/pagenotfound.component.scss delete mode 100644 frontend/src/app/routes/pagenotfound/pagenotfound.component.ts delete mode 100644 frontend/src/app/routes/pagenotfound/pagenotfound.module.ts delete mode 100644 frontend/src/app/routes/routes.ts rename frontend/src/app/routes/settings/{settings-home/settings-home.component.html => settings-general/settings-general.component.html} (100%) create mode 100644 frontend/src/app/routes/settings/settings-general/settings-general.component.ts create mode 100644 frontend/src/app/routes/settings/settings-general/settings-home.module.ts rename frontend/src/app/routes/{pagenotfound/processing.routing.module.ts => settings/settings-general/settings-home.routing.module.ts} (61%) delete mode 100644 frontend/src/app/routes/settings/settings-home/settings-home.component.scss delete mode 100644 frontend/src/app/routes/settings/settings-home/settings-home.component.ts diff --git a/frontend/src/app/app.component.ts b/frontend/src/app/app.component.ts index 509fe15..adaa0ac 100644 --- a/frontend/src/app/app.component.ts +++ b/frontend/src/app/app.component.ts @@ -34,7 +34,19 @@ export class AppComponent implements OnInit { ) {} private get routeData(): PRouteData { - return this.activatedRoute.firstChild?.snapshot.data ?? {}; + let currentRoute: ActivatedRoute | null = this.activatedRoute; + let accumulate: PRouteData = {}; + while (currentRoute !== null) { + const data = currentRoute.snapshot.data; + if (data !== undefined) { + accumulate = { + ...accumulate, + ...data, + }; + } + currentRoute = currentRoute.firstChild; + } + return accumulate; } ngOnInit() { @@ -67,13 +79,15 @@ export class AppComponent implements OnInit { private async onNavigationError(event: NavigationError) { const error: Error = event.error; if (error.message.startsWith('Cannot match any routes')) - this.router.navigate(['/pagenotfound']); + this.router.navigate(['/error/404'], { replaceUrl: true }); } private async onNavigationEnd(event: NavigationEnd) { const data = this.routeData; this.containerWrap = !data.noContainer; + console.log(data); + if (data.sidebar !== undefined) { this.sidebarPortal?.detach(); this.sidebarPortal = new ComponentPortal(data.sidebar); diff --git a/frontend/src/app/app.module.ts b/frontend/src/app/app.module.ts index 2ec2894..a288ec9 100644 --- a/frontend/src/app/app.module.ts +++ b/frontend/src/app/app.module.ts @@ -8,7 +8,6 @@ import { AppRoutingModule } from './app.routing.module'; import { FooterModule } from './components/footer/footer.module'; import { HeaderModule } from './components/header/header.module'; import { GuardsModule } from './guards/guards.module'; -import { routes } from './routes/routes'; @NgModule({ declarations: [AppComponent], @@ -23,8 +22,6 @@ import { routes } from './routes/routes'; HeaderModule, FooterModule, - - ...routes, ], providers: [], bootstrap: [AppComponent], diff --git a/frontend/src/app/app.routing.module.ts b/frontend/src/app/app.routing.module.ts index 23578fb..e91dedd 100644 --- a/frontend/src/app/app.routing.module.ts +++ b/frontend/src/app/app.routing.module.ts @@ -1,8 +1,44 @@ import { NgModule } from '@angular/core'; import { RouterModule } from '@angular/router'; import { PRoutes } from './models/picsur-routes'; - -const routes: PRoutes = []; +import { ErrorsRouteModule } from './routes/errors/errors.module'; +import { ProcessingRouteModule } from './routes/processing/processing.module'; +import { SettingsRouteModule } from './routes/settings/settings.module'; +import { UploadRouteModule } from './routes/upload/upload.module'; +import { UserRouteModule } from './routes/user/user.module'; +import { ViewRouteModule } from './routes/view/view.module'; +// PageNotFoundRouteModule, +// UploadRouteModule, +// ProcessingRouteModule, +// ViewRouteModule, +// UserRouteModule, +// SettingsRouteModule, +const routes: PRoutes = [ + { + path: '', + loadChildren: () => UploadRouteModule, + }, + { + path: 'processing', + loadChildren: () => ProcessingRouteModule, + }, + { + path: 'view', + loadChildren: () => ViewRouteModule, + }, + { + path: 'user', + loadChildren: () => UserRouteModule, + }, + { + path: 'settings', + loadChildren: () => SettingsRouteModule, + }, + { + path: 'error', + loadChildren: () => ErrorsRouteModule, + }, +]; @NgModule({ imports: [ diff --git a/frontend/src/app/components/footer/footer.component.html b/frontend/src/app/components/footer/footer.component.html index 41f6e48..d62bcfd 100644 --- a/frontend/src/app/components/footer/footer.component.html +++ b/frontend/src/app/components/footer/footer.component.html @@ -2,7 +2,7 @@

Made with 🤍 by RubiksCraftRubikscraft - ; diff --git a/frontend/src/app/routes/errors/401.component.ts b/frontend/src/app/routes/errors/401.component.ts new file mode 100644 index 0000000..4b57a22 --- /dev/null +++ b/frontend/src/app/routes/errors/401.component.ts @@ -0,0 +1,9 @@ +import { Component } from '@angular/core'; + +@Component({ + template: ` +

401 - Permission Denied

+

You do not have access to this page

+ `, +}) +export class E401Component {} diff --git a/frontend/src/app/routes/errors/404.component.ts b/frontend/src/app/routes/errors/404.component.ts new file mode 100644 index 0000000..63c64b2 --- /dev/null +++ b/frontend/src/app/routes/errors/404.component.ts @@ -0,0 +1,9 @@ +import { Component } from '@angular/core'; + +@Component({ + template: ` +

404 - Page not found

+

The page you are looking for does not exist.

+ `, +}) +export class E404Component {} diff --git a/frontend/src/app/routes/errors/errors.module.ts b/frontend/src/app/routes/errors/errors.module.ts new file mode 100644 index 0000000..4b1bf05 --- /dev/null +++ b/frontend/src/app/routes/errors/errors.module.ts @@ -0,0 +1,11 @@ +import { CommonModule } from '@angular/common'; +import { NgModule } from '@angular/core'; +import { E401Component } from './401.component'; +import { E404Component } from './404.component'; +import { ErrorsRoutingModule } from './errors.routing.module'; + +@NgModule({ + declarations: [E404Component, E401Component], + imports: [CommonModule, ErrorsRoutingModule], +}) +export class ErrorsRouteModule {} diff --git a/frontend/src/app/routes/errors/errors.routing.module.ts b/frontend/src/app/routes/errors/errors.routing.module.ts new file mode 100644 index 0000000..964ac99 --- /dev/null +++ b/frontend/src/app/routes/errors/errors.routing.module.ts @@ -0,0 +1,22 @@ +import { NgModule } from '@angular/core'; +import { RouterModule } from '@angular/router'; +import { PRoutes } from 'src/app/models/picsur-routes'; +import { E401Component } from './401.component'; +import { E404Component } from './404.component'; + +const routes: PRoutes = [ + { + path: '404', + component: E404Component, + }, + { + path: '401', + component: E401Component, + }, +]; + +@NgModule({ + imports: [RouterModule.forChild(routes)], + exports: [RouterModule], +}) +export class ErrorsRoutingModule {} diff --git a/frontend/src/app/routes/pagenotfound/pagenotfound.component.html b/frontend/src/app/routes/pagenotfound/pagenotfound.component.html deleted file mode 100644 index e6c06df..0000000 --- a/frontend/src/app/routes/pagenotfound/pagenotfound.component.html +++ /dev/null @@ -1,2 +0,0 @@ -

404 - Page not found

-

The page you are looking for does not exist.

diff --git a/frontend/src/app/routes/pagenotfound/pagenotfound.component.scss b/frontend/src/app/routes/pagenotfound/pagenotfound.component.scss deleted file mode 100644 index e69de29..0000000 diff --git a/frontend/src/app/routes/pagenotfound/pagenotfound.component.ts b/frontend/src/app/routes/pagenotfound/pagenotfound.component.ts deleted file mode 100644 index f449ec2..0000000 --- a/frontend/src/app/routes/pagenotfound/pagenotfound.component.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { Component } from '@angular/core'; - -@Component({ - selector: 'app-pagenotfound', - templateUrl: './pagenotfound.component.html', - styleUrls: ['./pagenotfound.component.scss'], -}) -export class PageNotFoundComponent {} diff --git a/frontend/src/app/routes/pagenotfound/pagenotfound.module.ts b/frontend/src/app/routes/pagenotfound/pagenotfound.module.ts deleted file mode 100644 index 3cb1f80..0000000 --- a/frontend/src/app/routes/pagenotfound/pagenotfound.module.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { CommonModule } from '@angular/common'; -import { NgModule } from '@angular/core'; -import { PageNotFoundComponent } from './pagenotfound.component'; -import { PageNotFoundRoutingModule } from './processing.routing.module'; - -@NgModule({ - declarations: [PageNotFoundComponent], - imports: [CommonModule, PageNotFoundRoutingModule], -}) -export class PageNotFoundRouteModule {} diff --git a/frontend/src/app/routes/processing/processing.routing.module.ts b/frontend/src/app/routes/processing/processing.routing.module.ts index 378703d..324f347 100644 --- a/frontend/src/app/routes/processing/processing.routing.module.ts +++ b/frontend/src/app/routes/processing/processing.routing.module.ts @@ -5,7 +5,7 @@ import { ProcessingComponent } from './processing.component'; const routes: PRoutes = [ { - path: 'processing', + path: '', component: ProcessingComponent, }, ]; diff --git a/frontend/src/app/routes/routes.ts b/frontend/src/app/routes/routes.ts deleted file mode 100644 index 1be710f..0000000 --- a/frontend/src/app/routes/routes.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { PageNotFoundRouteModule } from './pagenotfound/pagenotfound.module'; -import { ProcessingRouteModule } from './processing/processing.module'; -import { SettingsRouteModule } from './settings/settings.module'; -import { UploadRouteModule } from './upload/upload.module'; -import { UserRouteModule } from './user/user.module'; -import { ViewRouteModule } from './view/view.module'; - -export const routes = [ - PageNotFoundRouteModule, - UploadRouteModule, - ProcessingRouteModule, - ViewRouteModule, - UserRouteModule, - SettingsRouteModule, -]; diff --git a/frontend/src/app/routes/settings/settings-home/settings-home.component.html b/frontend/src/app/routes/settings/settings-general/settings-general.component.html similarity index 100% rename from frontend/src/app/routes/settings/settings-home/settings-home.component.html rename to frontend/src/app/routes/settings/settings-general/settings-general.component.html diff --git a/frontend/src/app/routes/settings/settings-general/settings-general.component.ts b/frontend/src/app/routes/settings/settings-general/settings-general.component.ts new file mode 100644 index 0000000..3d69789 --- /dev/null +++ b/frontend/src/app/routes/settings/settings-general/settings-general.component.ts @@ -0,0 +1,10 @@ +import { Component, OnInit } from '@angular/core'; + +@Component({ + templateUrl: './settings-general.component.html', +}) +export class SettingsGeneralComponent implements OnInit { + constructor() {} + + ngOnInit(): void {} +} diff --git a/frontend/src/app/routes/settings/settings-general/settings-home.module.ts b/frontend/src/app/routes/settings/settings-general/settings-home.module.ts new file mode 100644 index 0000000..9dc34a8 --- /dev/null +++ b/frontend/src/app/routes/settings/settings-general/settings-home.module.ts @@ -0,0 +1,13 @@ +import { CommonModule } from '@angular/common'; +import { NgModule } from '@angular/core'; +import { SettingsGeneralComponent } from './settings-general.component'; +import { SettingsGeneralRoutingModule } from './settings-home.routing.module'; + +@NgModule({ + declarations: [SettingsGeneralComponent], + imports: [ + CommonModule, + SettingsGeneralRoutingModule, + ], +}) +export class SettingsGeneralRouteModule {} diff --git a/frontend/src/app/routes/pagenotfound/processing.routing.module.ts b/frontend/src/app/routes/settings/settings-general/settings-home.routing.module.ts similarity index 61% rename from frontend/src/app/routes/pagenotfound/processing.routing.module.ts rename to frontend/src/app/routes/settings/settings-general/settings-home.routing.module.ts index 3f84c82..35844bf 100644 --- a/frontend/src/app/routes/pagenotfound/processing.routing.module.ts +++ b/frontend/src/app/routes/settings/settings-general/settings-home.routing.module.ts @@ -1,12 +1,12 @@ import { NgModule } from '@angular/core'; import { RouterModule } from '@angular/router'; import { PRoutes } from 'src/app/models/picsur-routes'; -import { PageNotFoundComponent } from './pagenotfound.component'; +import { SettingsGeneralComponent } from './settings-general.component'; const routes: PRoutes = [ { - path: 'pagenotfound', - component: PageNotFoundComponent, + path: '', + component: SettingsGeneralComponent, }, ]; @@ -14,4 +14,4 @@ const routes: PRoutes = [ imports: [RouterModule.forChild(routes)], exports: [RouterModule], }) -export class PageNotFoundRoutingModule {} +export class SettingsGeneralRoutingModule {} diff --git a/frontend/src/app/routes/settings/settings-home/settings-home.component.scss b/frontend/src/app/routes/settings/settings-home/settings-home.component.scss deleted file mode 100644 index e69de29..0000000 diff --git a/frontend/src/app/routes/settings/settings-home/settings-home.component.ts b/frontend/src/app/routes/settings/settings-home/settings-home.component.ts deleted file mode 100644 index e9bd91b..0000000 --- a/frontend/src/app/routes/settings/settings-home/settings-home.component.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { - Component, - OnInit -} from '@angular/core'; - -@Component({ - templateUrl: './settings-home.component.html', - styleUrls: ['./settings-home.component.scss'], -}) -export class SettingsHomeComponent implements OnInit { - - constructor( - ) {} - - ngOnInit(): void { - console.log('AdminComponent'); - } -} diff --git a/frontend/src/app/routes/settings/settings-sidebar/settings-sidebar.component.html b/frontend/src/app/routes/settings/settings-sidebar/settings-sidebar.component.html index 2fd8a00..0d426f7 100644 --- a/frontend/src/app/routes/settings/settings-sidebar/settings-sidebar.component.html +++ b/frontend/src/app/routes/settings/settings-sidebar/settings-sidebar.component.html @@ -1,6 +1,30 @@ -
- settings - General - + + Personal + + {{ route.icon }} + {{ route.name }} + + + + + + + + System + + {{ route.icon }} + {{ route.name }} + + diff --git a/frontend/src/app/routes/settings/settings-sidebar/settings-sidebar.component.ts b/frontend/src/app/routes/settings/settings-sidebar/settings-sidebar.component.ts index 6c8261c..6e9fdfb 100644 --- a/frontend/src/app/routes/settings/settings-sidebar/settings-sidebar.component.ts +++ b/frontend/src/app/routes/settings/settings-sidebar/settings-sidebar.component.ts @@ -1,14 +1,40 @@ import { Component, OnInit } from '@angular/core'; +type SideBarRoute = { + type: 'personal' | 'system'; + path: string; + name: string; + icon: string; +}; + +const SideBarRoutes: SideBarRoute[] = [ + { + type: 'personal', + path: '', + name: 'General', + icon: 'settings', + }, +]; + @Component({ templateUrl: './settings-sidebar.component.html', - styleUrls: ['./settings-sidebar.component.scss'] + styleUrls: ['./settings-sidebar.component.scss'], }) export class SettingsSidebarComponent implements OnInit { + personalRoutes: SideBarRoute[] = []; + systemRoutes: SideBarRoute[] = []; - constructor() { } + constructor() {} - ngOnInit(): void { + ngOnInit() { + const routes = SideBarRoutes.map((route) => { + return { + ...route, + path: `/settings/${route.path}`, + }; + }); + + this.personalRoutes = routes.filter((route) => route.type === 'personal'); + this.systemRoutes = routes.filter((route) => route.type === 'system'); } - } diff --git a/frontend/src/app/routes/settings/settings.module.ts b/frontend/src/app/routes/settings/settings.module.ts index f02f29c..7757de8 100644 --- a/frontend/src/app/routes/settings/settings.module.ts +++ b/frontend/src/app/routes/settings/settings.module.ts @@ -2,17 +2,14 @@ import { CommonModule } from '@angular/common'; import { NgModule } from '@angular/core'; import { MatIconModule } from '@angular/material/icon'; import { MatListModule } from '@angular/material/list'; -import { MatProgressSpinnerModule } from '@angular/material/progress-spinner'; -import { SettingsHomeComponent } from './settings-home/settings-home.component'; import { SettingsSidebarComponent } from './settings-sidebar/settings-sidebar.component'; import { SettingsRoutingModule } from './settings.routing.module'; @NgModule({ - declarations: [SettingsHomeComponent, SettingsSidebarComponent], + declarations: [SettingsSidebarComponent], imports: [ CommonModule, SettingsRoutingModule, - MatProgressSpinnerModule, MatListModule, MatIconModule, diff --git a/frontend/src/app/routes/settings/settings.routing.module.ts b/frontend/src/app/routes/settings/settings.routing.module.ts index ff66d1f..bc03de7 100644 --- a/frontend/src/app/routes/settings/settings.routing.module.ts +++ b/frontend/src/app/routes/settings/settings.routing.module.ts @@ -1,13 +1,13 @@ import { NgModule } from '@angular/core'; import { RouterModule } from '@angular/router'; import { PRoutes } from 'src/app/models/picsur-routes'; -import { SettingsHomeComponent } from './settings-home/settings-home.component'; +import { SettingsGeneralRouteModule } from './settings-general/settings-home.module'; import { SettingsSidebarComponent } from './settings-sidebar/settings-sidebar.component'; -const routes: PRoutes = [ +const SettingsRoutes: PRoutes = [ { - path: 'settings', - component: SettingsHomeComponent, + path: '', + loadChildren: () => SettingsGeneralRouteModule, data: { sidebar: SettingsSidebarComponent, }, @@ -15,7 +15,7 @@ const routes: PRoutes = [ ]; @NgModule({ - imports: [RouterModule.forChild(routes)], + imports: [RouterModule.forChild(SettingsRoutes)], exports: [RouterModule], }) export class SettingsRoutingModule {} diff --git a/frontend/src/app/routes/user/login/login.component.ts b/frontend/src/app/routes/user/login/login.component.ts index 5e68811..eebc040 100644 --- a/frontend/src/app/routes/user/login/login.component.ts +++ b/frontend/src/app/routes/user/login/login.component.ts @@ -67,7 +67,7 @@ export class LoginComponent implements OnInit { } async onRegister() { - this.router.navigate(['/register'], { + this.router.navigate(['/user/register'], { state: this.model.getRawData(), }); } diff --git a/frontend/src/app/routes/user/register/register.component.ts b/frontend/src/app/routes/user/register/register.component.ts index bdcfe61..2c06009 100644 --- a/frontend/src/app/routes/user/register/register.component.ts +++ b/frontend/src/app/routes/user/register/register.component.ts @@ -76,7 +76,7 @@ export class RegisterComponent implements OnInit { } async onLogin() { - this.router.navigate(['/login'], { + this.router.navigate(['/user/login'], { state: this.model.getRawData(), }); } diff --git a/frontend/src/app/routes/view/view.routing.module.ts b/frontend/src/app/routes/view/view.routing.module.ts index 1bef30c..27d581a 100644 --- a/frontend/src/app/routes/view/view.routing.module.ts +++ b/frontend/src/app/routes/view/view.routing.module.ts @@ -7,7 +7,7 @@ import { ViewComponent } from './view.component'; const routes: PRoutes = [ { - path: 'view/:hash', + path: ':hash', component: ViewComponent, canActivate: [PermissionGuard], data: { permissions: [Permission.ImageView] },