Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Answer: 31 #1153

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { Component } from '@angular/core';
import { RouterLink, RouterOutlet } from '@angular/router';

@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet, RouterLink],
template: `
<div class="flex gap-2">
<button
Expand All @@ -20,7 +23,8 @@ import { Component } from '@angular/core';
User
</button>
</div>
<router-outlet></router-outlet>

<router-outlet />
`,
host: {
class: 'flex flex-col p-4 gap-3',
Expand Down
11 changes: 0 additions & 11 deletions apps/angular/31-module-to-standalone/src/app/app.module.ts

This file was deleted.

13 changes: 8 additions & 5 deletions apps/angular/31-module-to-standalone/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
import { provideToken } from '@angular-challenges/module-to-standalone/core/providers';
import { appRoutes } from '@angular-challenges/module-to-standalone/shell';
import { bootstrapApplication } from '@angular/platform-browser';
import { provideRouter } from '@angular/router';
import { AppComponent } from './app/app.component';

platformBrowserDynamic()
.bootstrapModule(AppModule)
.catch((err) => console.error(err));
bootstrapApplication(AppComponent, {
providers: [provideRouter(appRoutes), provideToken('main-shell-token')],
}).catch((err) => console.error(err));
2 changes: 1 addition & 1 deletion libs/module-to-standalone/admin/feature/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './lib/admin-feature.module';
export { ADMIN_ROUTES } from './lib/admin-feature.routes';

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Routes } from '@angular/router';

export const ADMIN_ROUTES: Routes = [
{
path: '',
loadComponent: () =>
import('./dashboard/dashboard.component').then(
(m) => m.DashboardComponent,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use the default keyword is you want to avoid the then close. But that's only a proposal, some people prefer it more explicit

),
},
{
path: 'create-user',
loadComponent: () =>
import('./create-user/create-user.component').then(
(m) => m.CreateUserComponent,
),
},
];
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Component, NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { Component } from '@angular/core';
import { RouterLink } from '@angular/router';

@Component({
selector: 'lib-create-user',
standalone: true,
imports: [RouterLink],
template: `
Create User Form

<button
routerLink=".."
class="ml-5 rounded-lg border bg-gray-700 p-2 text-white">
Expand All @@ -14,11 +15,3 @@ import { RouterModule } from '@angular/router';
`,
})
export class CreateUserComponent {}

@NgModule({
imports: [
RouterModule.forChild([{ path: '', component: CreateUserComponent }]),
],
declarations: [CreateUserComponent],
})
export class CreateUserModule {}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Component, NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { Component } from '@angular/core';
import { RouterLink } from '@angular/router';

@Component({
selector: 'lib-dashboard',
standalone: true,
imports: [RouterLink],
template: `
Dashboard

<button
routerLink="create-user"
class="ml-10 rounded-lg border bg-gray-700 p-2 text-white">
Expand All @@ -14,11 +15,3 @@ import { RouterModule } from '@angular/router';
`,
})
export class DashboardComponent {}

@NgModule({
imports: [
RouterModule.forChild([{ path: '', component: DashboardComponent }]),
],
declarations: [DashboardComponent],
})
export class DashboardModule {}
30 changes: 11 additions & 19 deletions libs/module-to-standalone/admin/shared/src/lib/authorized.guard.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
import { CanActivate, Router, UrlTree } from '@angular/router';

import { AuthorizationService } from '@angular-challenges/module-to-standalone/core/service';
import { Injectable } from '@angular/core';
import { inject } from '@angular/core';
import { Router, UrlTree } from '@angular/router';
import { Observable, map } from 'rxjs';

@Injectable({
providedIn: 'root',
})
export class IsAuthorizedGuard implements CanActivate {
constructor(
private authorizationService: AuthorizationService,
private router: Router,
) {}
export const isAuthorizedGuard = (): Observable<boolean | UrlTree> => {
const authorizationService = inject(AuthorizationService);
const router = inject(Router);

canActivate(): Observable<boolean | UrlTree> {
return this.authorizationService.isAuthorized$.pipe(
map((isAuthorized) =>
isAuthorized ? true : this.router.createUrlTree(['forbidden']),
),
);
}
}
return authorizationService.isAuthorized$.pipe(
map((isAuthorized) =>
isAuthorized ? true : router.createUrlTree(['forbidden']),
),
);
};
2 changes: 1 addition & 1 deletion libs/module-to-standalone/forbidden/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './lib/forbidden.module';
export { ForbiddenComponent } from './lib/forbidden.component';
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Component } from '@angular/core';

@Component({
selector: 'lib-home',
standalone: true,
template: `
Forbidden component
`,
Expand Down
13 changes: 0 additions & 13 deletions libs/module-to-standalone/forbidden/src/lib/forbidden.module.ts

This file was deleted.

2 changes: 1 addition & 1 deletion libs/module-to-standalone/home/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './lib/home.module';
export { HomeComponent } from './lib/home.component';
15 changes: 8 additions & 7 deletions libs/module-to-standalone/home/src/lib/home.component.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { TOKEN } from '@angular-challenges/module-to-standalone/core/providers';
import { AuthorizationService } from '@angular-challenges/module-to-standalone/core/service';
import { Component, Inject } from '@angular/core';
import { AsyncPipe } from '@angular/common';
import { Component, inject } from '@angular/core';

@Component({
selector: 'lib-home',
standalone: true,
imports: [AsyncPipe],
template: `
Home component

<section class="flex items-center gap-5">
Authorization :
<button class="border p-2 " (click)="authorizeService.authorize()">
<button class="border p-2" (click)="authorizeService.authorize()">
Authorize
</button>
<button class="border p-2 " (click)="authorizeService.forbid()">
<button class="border p-2" (click)="authorizeService.forbid()">
Forbid
</button>
(isAuthorized: {{ authorizeService.isAuthorized$ | async }})
Expand All @@ -22,8 +25,6 @@ import { Component, Inject } from '@angular/core';
`,
})
export class HomeComponent {
constructor(
public authorizeService: AuthorizationService,
@Inject(TOKEN) public token: string,
) {}
protected authorizeService = inject(AuthorizationService);
protected token = inject(TOKEN);
}
13 changes: 0 additions & 13 deletions libs/module-to-standalone/home/src/lib/home.module.ts

This file was deleted.

2 changes: 1 addition & 1 deletion libs/module-to-standalone/shell/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './lib/main-shell.module';
export * from './lib/main-shell.routes';
11 changes: 0 additions & 11 deletions libs/module-to-standalone/shell/src/lib/main-shell.module.ts

This file was deleted.

17 changes: 8 additions & 9 deletions libs/module-to-standalone/shell/src/lib/main-shell.routes.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
import { IsAuthorizedGuard } from '@angular-challenges/module-to-standalone/admin/shared';
import { Route } from '@angular/router';
import { isAuthorizedGuard } from 'libs/module-to-standalone/admin/shared/src/lib/authorized.guard';

export const appRoutes: Route[] = [
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{
path: 'home',
loadChildren: () =>
loadComponent: () =>
import('@angular-challenges/module-to-standalone/home').then(
(m) => m.ModuleToStandaloneHomeModule,
(m) => m.HomeComponent,
),
},
{
path: 'admin',
canActivate: [IsAuthorizedGuard],
canActivate: [isAuthorizedGuard],
loadChildren: () =>
import('@angular-challenges/module-to-standalone/admin/feature').then(
(m) => m.AdminFeatureModule,
(r) => r.ADMIN_ROUTES,
),
},
{
path: 'user',
loadChildren: () =>
import('@angular-challenges/module-to-standalone/user/shell').then(
(m) => m.UserShellModule,
(m) => m.USER_ROUTES,
),
},

{
path: 'forbidden',
loadChildren: () =>
loadComponent: () =>
import('@angular-challenges/module-to-standalone/forbidden').then(
(m) => m.ForbiddenModule,
(m) => m.ForbiddenComponent,
),
},
];
2 changes: 1 addition & 1 deletion libs/module-to-standalone/user/contact/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './lib/contact-feature.module';
export * from './lib/contact-feature.routes';

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Route } from '@angular/router';
import { CreateContactComponent } from './create-contact/create-contact.component';
import { ContactDashboardComponent } from './dashboard/dashboard.component';

export const contactFeatureRoutes: Route[] = [
{
path: '',
component: ContactDashboardComponent,
children: [
{
path: 'create-contact',
component: CreateContactComponent,
},
],
},
];
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Component, NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { Component } from '@angular/core';
import { RouterLink } from '@angular/router';

@Component({
selector: 'lib-create-contact',
standalone: true,
imports: [RouterLink],
template: `
Create Contact Form

Expand All @@ -14,11 +16,3 @@ import { RouterModule } from '@angular/router';
`,
})
export class CreateContactComponent {}

@NgModule({
imports: [
RouterModule.forChild([{ path: '', component: CreateContactComponent }]),
],
declarations: [CreateContactComponent],
})
export class CreateContactModule {}
Loading
Loading