Skip to content

Commit 948e287

Browse files
authored
Merge pull request #3 from masteropen/angular_routing
finalize routing chapter
2 parents d61bc1e + 8b05bf8 commit 948e287

22 files changed

+244
-50
lines changed

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"@angular/router": "~9.1.6",
2222
"bootstrap": "^3.3.7",
2323
"rxjs": "~6.5.4",
24+
"rxjs-compat": "^6.5.5",
2425
"tslib": "^1.10.0",
2526
"zone.js": "~0.10.2"
2627
},

src/app/app.component.html

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
1+
<nav class="navbar navbar-default">
2+
<div class="container-fluid">
3+
<div class="navbar-collapse">
4+
<ul class="nav navbar-nav">
5+
<li routerLinkActive="active"><a routerLink="auth">Authentification</a></li>
6+
<li routerLinkActive="active"><a routerLink="appareils">Appareils</a></li>
7+
</ul>
8+
</div>
9+
</div>
10+
</nav>
111
<div class="container">
212
<div class="row">
313
<div class="col-xs-12">
4-
<h2>Mes appareils</h2>
5-
<p>Mis à jour le : {{ lastUpdate | async | date: 'd MMMM y' | uppercase }}</p>
6-
<ul class="list-group">
7-
<app-appareil *ngFor="let appareil of appareils; let i = index"
8-
[appareilName]="appareil.name"
9-
[appareilStatus]="appareil.status"
10-
[index]="i"></app-appareil>
11-
</ul>
12-
<button class="btn btn-success"
13-
[disabled]="!isAuth"
14-
(click)="onAllumer()">Tout allumer</button>
15-
<button class="btn btn-danger"
16-
[disabled]="!isAuth"
17-
(click)="onEteindre()">Tout éteindre</button>
14+
<router-outlet></router-outlet>
1815
</div>
1916
</div>
2017
</div>

src/app/app.component.ts

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {Component, OnInit} from '@angular/core';
2-
import { AppareilService } from './services/appareil.service';
32

43
@Component({
54
selector: 'app-root',
@@ -8,38 +7,9 @@ import { AppareilService } from './services/appareil.service';
87
})
98
export class AppComponent implements OnInit {
109

11-
isAuth = false;
12-
lastUpdate = new Promise((resolve, reject) => {
13-
const date = new Date();
14-
setTimeout(
15-
() => {
16-
resolve(date);
17-
}, 2000
18-
);
19-
});
20-
appareils: any[];
21-
22-
constructor(private appareilService: AppareilService) {
23-
setTimeout(
24-
() => {
25-
this.isAuth = true;
26-
}, 4000
27-
);
28-
}
29-
30-
ngOnInit() {
31-
this.appareils = this.appareilService.appareils;
32-
}
33-
34-
onAllumer() {
35-
this.appareilService.switchOnAll();
36-
}
10+
constructor() {
11+
}
3712

38-
onEteindre() {
39-
if (confirm('Etes-vous sûr de vouloir éteindre tous vos appareils ?')) {
40-
this.appareilService.switchOffAll();
41-
} else {
42-
return null;
13+
ngOnInit() {
4314
}
44-
}
4515
}

src/app/app.module.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,41 @@ import { AppComponent } from './app.component';
44
import { AppareilComponent } from './appareil/appareil.component';
55
import { FormsModule } from '@angular/forms';
66
import { AppareilService } from './services/appareil.service';
7+
import { AuthComponent } from './auth/auth.component';
8+
import { AppareilViewComponent } from './appareil-view/appareil-view.component';
9+
import { RouterModule, Routes } from '@angular/router';
10+
import { AuthService } from './services/auth.service';
11+
import { SingleAppareilComponent } from './single-appareil/single-appareil.component';
12+
import { FourOhFourComponent } from './four-oh-four/four-oh-four.component';
13+
import { AuthGuard } from './services/auth-guard.service';
14+
15+
const appRoutes: Routes = [
16+
{ path: 'appareils', canActivate: [AuthGuard], component: AppareilViewComponent },
17+
{ path: 'appareils/:id', canActivate: [AuthGuard], component: SingleAppareilComponent },
18+
{ path: 'auth', component: AuthComponent },
19+
{ path: '', component: AuthComponent },
20+
{ path: 'not-found', component: FourOhFourComponent },
21+
{ path: '**', redirectTo: 'not-found' }
22+
];
723

824
@NgModule({
925
declarations: [
1026
AppComponent,
11-
AppareilComponent
27+
AppareilComponent,
28+
AuthComponent,
29+
AppareilViewComponent,
30+
SingleAppareilComponent,
31+
FourOhFourComponent
1232
],
1333
imports: [
1434
BrowserModule,
15-
FormsModule
35+
FormsModule,
36+
RouterModule.forRoot(appRoutes)
1637
],
1738
providers: [
18-
AppareilService
39+
AppareilService,
40+
AuthService,
41+
AuthGuard
1942
],
2043
bootstrap: [AppComponent]
2144
})
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<div class="container">
2+
<div class="row">
3+
<div class="col-xs-12">
4+
<h2>Mes appareils</h2>
5+
<p>Mis à jour le : {{ lastUpdate | async | date: 'd MMMM y' | uppercase }}</p>
6+
<ul class="list-group">
7+
<app-appareil *ngFor="let appareil of appareils; let i = index"
8+
[appareilName]="appareil.name"
9+
[appareilStatus]="appareil.status"
10+
[index]="i"
11+
[id]="appareil.id"></app-appareil>
12+
</ul>
13+
<button class="btn btn-success"
14+
(click)="onAllumer()">Tout allumer</button>
15+
<button class="btn btn-danger"
16+
(click)="onEteindre()">Tout éteindre</button>
17+
</div>
18+
</div>
19+
</div>

src/app/appareil-view/appareil-view.component.scss

Whitespace-only changes.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { Component, OnInit } from '@angular/core';
2+
import { AppareilService } from '../services/appareil.service';
3+
4+
@Component({
5+
selector: 'app-appareil-view',
6+
templateUrl: './appareil-view.component.html',
7+
styleUrls: ['./appareil-view.component.scss']
8+
})
9+
export class AppareilViewComponent implements OnInit {
10+
11+
isAuth = false;
12+
appareils: any[];
13+
14+
lastUpdate = new Promise((resolve, reject) => {
15+
const date = new Date();
16+
setTimeout(
17+
() => {
18+
resolve(date);
19+
}, 2000
20+
);
21+
});
22+
23+
constructor(private appareilService: AppareilService) {
24+
setTimeout(() => {
25+
this.isAuth = true;
26+
}, 3000);
27+
}
28+
29+
ngOnInit() {
30+
this.appareils = this.appareilService.appareils;
31+
}
32+
33+
onAllumer() {
34+
this.appareilService.switchOnAll();
35+
}
36+
37+
onEteindre() {
38+
if (confirm('Etes-vous sûr de vouloir éteindre tous vos appareils ?')) {
39+
this.appareilService.switchOffAll();
40+
} else {
41+
return null;
42+
}
43+
}
44+
45+
}

src/app/appareil/appareil.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
'list-group-item-danger': appareilStatus === 'éteint'}">
44

55
<h4 [ngStyle]="{color: getColor()}">Appareil : {{ appareilName }} -- Statut : {{ appareilStatus }}</h4>
6+
<a [routerLink]="[id]">Détail</a>
67
<input type="text" class="form-control" [(ngModel)]="appareilName">
78

89
<button class="btn btn-sm btn-success"

src/app/appareil/appareil.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export class AppareilComponent implements OnInit {
1212
@Input() appareilName: string;
1313
@Input() appareilStatus: string;
1414
@Input() index: number;
15+
@Input() id: number;
1516

1617
constructor(private appareilService: AppareilService) {
1718
}

src/app/auth/auth.component.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<h2>Authentification</h2>
2+
<button class="btn btn-success" *ngIf="!authStatus" (click)="onSignIn()">Se connecter</button>
3+
<button class="btn btn-danger" *ngIf="authStatus" (click)="onSignOut()">Se déconnecter</button>

src/app/auth/auth.component.scss

Whitespace-only changes.

src/app/auth/auth.component.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Component, OnInit } from '@angular/core';
2+
import { AuthService } from '../services/auth.service';
3+
import { Router } from '@angular/router';
4+
5+
@Component({
6+
selector: 'app-auth',
7+
templateUrl: './auth.component.html',
8+
styleUrls: ['./auth.component.scss']
9+
})
10+
export class AuthComponent implements OnInit {
11+
12+
authStatus: boolean;
13+
14+
constructor(private authService: AuthService, private router: Router) { }
15+
16+
ngOnInit() {
17+
this.authStatus = this.authService.isAuth;
18+
}
19+
20+
onSignIn() {
21+
this.authService.signIn().then(
22+
() => {
23+
console.log('Sign in successful!');
24+
this.authStatus = this.authService.isAuth;
25+
this.router.navigate(['appareils']);
26+
}
27+
);
28+
}
29+
30+
onSignOut() {
31+
this.authService.signOut();
32+
this.authStatus = this.authService.isAuth;
33+
}
34+
35+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>four-oh-four works!</p>

src/app/four-oh-four/four-oh-four.component.scss

Whitespace-only changes.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Component, OnInit } from '@angular/core';
2+
3+
@Component({
4+
selector: 'app-four-oh-four',
5+
templateUrl: './four-oh-four.component.html',
6+
styleUrls: ['./four-oh-four.component.scss']
7+
})
8+
export class FourOhFourComponent implements OnInit {
9+
10+
constructor() { }
11+
12+
ngOnInit(): void {
13+
}
14+
15+
}

src/app/services/appareil.service.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
export class AppareilService {
22
appareils = [
33
{
4+
id: 1,
45
name: 'Machine à laver',
56
status: 'éteint'
67
},
78
{
9+
id: 2,
810
name: 'Frigo',
911
status: 'allumé'
1012
},
1113
{
14+
id: 3,
1215
name: 'Ordinateur',
1316
status: 'éteint'
1417
}
@@ -33,4 +36,12 @@ export class AppareilService {
3336
switchOffOne(i: number) {
3437
this.appareils[i].status = 'éteint';
3538
}
39+
40+
getAppareilById(id: number) {
41+
return this.appareils.find(
42+
(s) => {
43+
return s.id === id;
44+
}
45+
);
46+
}
3647
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router';
2+
import { Observable } from 'rxjs/Observable';
3+
import { AuthService } from './auth.service';
4+
import { Injectable } from '@angular/core';
5+
6+
@Injectable()
7+
export class AuthGuard implements CanActivate {
8+
9+
constructor(private authService: AuthService,
10+
private router: Router) { }
11+
12+
canActivate(
13+
route: ActivatedRouteSnapshot,
14+
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
15+
if (this.authService.isAuth) {
16+
return true;
17+
} else {
18+
this.router.navigate(['/auth']);
19+
}
20+
}
21+
}

src/app/services/auth.service.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export class AuthService {
2+
isAuth = false;
3+
4+
signIn() {
5+
return new Promise(
6+
(resolve, reject) => {
7+
setTimeout(
8+
() => {
9+
this.isAuth = true;
10+
resolve(true);
11+
}, 2000
12+
);
13+
}
14+
);
15+
}
16+
17+
signOut() {
18+
this.isAuth = false;
19+
}
20+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<h2>{{ name }}</h2>
2+
<p>Statut : {{ status }}</p>
3+
<a routerLink="/appareils">Retour à la liste</a>

src/app/single-appareil/single-appareil.component.scss

Whitespace-only changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Component, OnInit } from '@angular/core';
2+
import { AppareilService } from '../services/appareil.service';
3+
import { ActivatedRoute } from '@angular/router';
4+
5+
@Component({
6+
selector: 'app-single-appareil',
7+
templateUrl: './single-appareil.component.html',
8+
styleUrls: ['./single-appareil.component.scss']
9+
})
10+
export class SingleAppareilComponent implements OnInit {
11+
12+
name = 'Appareil';
13+
status = 'Statut';
14+
15+
constructor(private appareilService: AppareilService, private route: ActivatedRoute) { }
16+
17+
ngOnInit() {
18+
const id = this.route.snapshot.params['id'];
19+
this.name = this.appareilService.getAppareilById(+id).name;
20+
this.status = this.appareilService.getAppareilById(+id).status;
21+
}
22+
23+
}

0 commit comments

Comments
 (0)