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
}

0 commit comments

Comments
 (0)