Skip to content

Answer:31 #1322

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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,9 @@
import { Component } from '@angular/core';
import { RouterLink, RouterOutlet } from '@angular/router';

@Component({
selector: 'app-root',
imports: [RouterLink, RouterOutlet],
template: `
<div class="flex gap-2">
<button
Expand All @@ -25,6 +27,5 @@ import { Component } from '@angular/core';
host: {
class: 'flex flex-col p-4 gap-3',
},
standalone: false,
})
export class AppComponent {}
7 changes: 7 additions & 0 deletions apps/angular/31-module-to-standalone/src/app/app.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { ApplicationConfig } from '@angular/core';
import { provideRouter } from '@angular/router';
import { routes } from './app.routes';

export const appConfig: ApplicationConfig = {
providers: [provideRouter(routes)],
};
11 changes: 0 additions & 11 deletions apps/angular/31-module-to-standalone/src/app/app.module.ts

This file was deleted.

12 changes: 12 additions & 0 deletions apps/angular/31-module-to-standalone/src/app/app.routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Routes } from '@angular/router';

export const routes: Routes = [
{
path: '',
loadChildren: () =>
import('@angular-challenges/module-to-standalone/shell').then(
(r) => r.appRoutes,
),
},
{ path: '**', redirectTo: '' },
];
11 changes: 6 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,7 @@
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app/app.component';
import { appConfig } from './app/app.config';

platformBrowserDynamic()
.bootstrapModule(AppModule)
.catch((err) => console.error(err));
bootstrapApplication(AppComponent, appConfig).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 * from './lib/admin-feature.routes';

This file was deleted.

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

export const adminFeatureRoutes: Routes = [
{
path: '',
loadComponent: () => import('./dashboard/dashboard.component'),
},
{
path: 'create-user',
loadComponent: () => import('./create-user/create-user.component'),
},
];
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
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',
imports: [RouterLink],
template: `
Create User Form

<button
routerLink=".."
class="ml-5 rounded-lg border bg-gray-700 p-2 text-white">
Back
</button>
`,
standalone: false,
})
export class CreateUserComponent {}

@NgModule({
imports: [
RouterModule.forChild([{ path: '', component: CreateUserComponent }]),
],
declarations: [CreateUserComponent],
})
export class CreateUserModule {}
export default class CreateUserComponent {}
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
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',
imports: [RouterLink],
template: `
Dashboard

<button
routerLink="create-user"
class="ml-10 rounded-lg border bg-gray-700 p-2 text-white">
Create User
</button>
`,
standalone: false,
})
export class DashboardComponent {}

@NgModule({
imports: [
RouterModule.forChild([{ path: '', component: DashboardComponent }]),
],
declarations: [DashboardComponent],
})
export class DashboardModule {}
export default class DashboardComponent {}
31 changes: 12 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,16 @@
import { CanActivate, Router, UrlTree } from '@angular/router';
import { CanMatchFn, Router } from '@angular/router';

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

@Injectable({
providedIn: 'root',
})
export class IsAuthorizedGuard implements CanActivate {
constructor(
private authorizationService: AuthorizationService,
private router: Router,
) {}
export const IsAuthorizedGuard: CanMatchFn = () => {
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']),
),
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
makeEnvironmentProviders,
} from '@angular/core';

export const TOKEN = new InjectionToken<string[]>('token');
export const TOKEN = new InjectionToken<string>('token');

export const provideToken = (token: string): EnvironmentProviders => {
return makeEnvironmentProviders([
Expand Down
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 { default } from './lib/forbidden.component';
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ import { Component } from '@angular/core';
template: `
Forbidden component
`,
standalone: false,
})
export class ForbiddenComponent {}
export default class ForbiddenComponent {}
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 { default } from './lib/home.component';
14 changes: 6 additions & 8 deletions libs/module-to-standalone/home/src/lib/home.component.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
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',
imports: [AsyncPipe],
template: `
Home component

<section class="flex items-center gap-5">
Authorization :
<button class="border p-2 " (click)="authorizeService.authorize()">
Expand All @@ -20,11 +21,8 @@ import { Component, Inject } from '@angular/core';

<section>LoadedToken {{ token }}</section>
`,
standalone: false,
})
export class HomeComponent {
constructor(
public authorizeService: AuthorizationService,
@Inject(TOKEN) public token: string,
) {}
export default class HomeComponent {
readonly authorizeService = inject(AuthorizationService);
readonly 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.

26 changes: 12 additions & 14 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,34 @@
import { IsAuthorizedGuard } from '@angular-challenges/module-to-standalone/admin/shared';
import { Route } from '@angular/router';
import { provideToken } from '@angular-challenges/module-to-standalone/core/providers';
import { Routes } from '@angular/router';

export const appRoutes: Route[] = [
{ path: '', redirectTo: 'home', pathMatch: 'full' },
export const appRoutes: Routes = [
{ path: '', pathMatch: 'full', redirectTo: 'home' },
{
path: 'home',
loadChildren: () =>
import('@angular-challenges/module-to-standalone/home').then(
(m) => m.ModuleToStandaloneHomeModule,
),
providers: [provideToken('main-shell-token')],
loadComponent: () =>
import('@angular-challenges/module-to-standalone/home'),
},
{
path: 'admin',
canActivate: [IsAuthorizedGuard],
canMatch: [IsAuthorizedGuard],
loadChildren: () =>
import('@angular-challenges/module-to-standalone/admin/feature').then(
(m) => m.AdminFeatureModule,
(r) => r.adminFeatureRoutes,
),
},
{
path: 'user',
loadChildren: () =>
import('@angular-challenges/module-to-standalone/user/shell').then(
(m) => m.UserShellModule,
(r) => r.userShellRoutes,
),
},

{
path: 'forbidden',
loadChildren: () =>
import('@angular-challenges/module-to-standalone/forbidden').then(
(m) => m.ForbiddenModule,
),
loadComponent: () =>
import('@angular-challenges/module-to-standalone/forbidden'),
},
];
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,12 @@
import { Routes } from '@angular/router';

export const contactFeatureRoutes: Routes = [
{
path: '',
loadComponent: () => import('./dashboard/dashboard.component'),
},
{
path: 'create-contact',
loadComponent: () => import('./create-contact/create-contact.component'),
},
];
Loading