diff --git a/package-lock.json b/package-lock.json index 2a81204..5119fb4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10197,6 +10197,11 @@ "tslib": "^1.9.0" } }, + "rxjs-compat": { + "version": "6.5.5", + "resolved": "https://registry.npmjs.org/rxjs-compat/-/rxjs-compat-6.5.5.tgz", + "integrity": "sha512-F42sssVbUyWH4vJswEo6m+Eh02xHv3q93n8S7nUJO58R7sbc3CvJIOts605zdaBhWa1xMB9aVSyqPqhQ5q3eXg==" + }, "safe-buffer": { "version": "5.1.2", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", diff --git a/package.json b/package.json index 23d4d96..4cb687b 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "@angular/router": "~9.1.6", "bootstrap": "^3.3.7", "rxjs": "~6.5.4", + "rxjs-compat": "^6.5.5", "tslib": "^1.10.0", "zone.js": "~0.10.2" }, diff --git a/src/app/app.component.html b/src/app/app.component.html index 474e8aa..5bd872a 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -1,20 +1,17 @@ +
-

Mes appareils

-

Mis à jour le : {{ lastUpdate | async | date: 'd MMMM y' | uppercase }}

-
    - -
- - +
diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 846136d..6576cce 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,5 +1,4 @@ import {Component, OnInit} from '@angular/core'; -import { AppareilService } from './services/appareil.service'; @Component({ selector: 'app-root', @@ -8,38 +7,9 @@ import { AppareilService } from './services/appareil.service'; }) export class AppComponent implements OnInit { - isAuth = false; - lastUpdate = new Promise((resolve, reject) => { - const date = new Date(); - setTimeout( - () => { - resolve(date); - }, 2000 - ); - }); - appareils: any[]; - - constructor(private appareilService: AppareilService) { - setTimeout( - () => { - this.isAuth = true; - }, 4000 - ); - } - - ngOnInit() { - this.appareils = this.appareilService.appareils; - } - - onAllumer() { - this.appareilService.switchOnAll(); - } + constructor() { + } - onEteindre() { - if (confirm('Etes-vous sûr de vouloir éteindre tous vos appareils ?')) { - this.appareilService.switchOffAll(); - } else { - return null; + ngOnInit() { } - } } diff --git a/src/app/app.module.ts b/src/app/app.module.ts index c9d2ca3..5d529b9 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -4,18 +4,41 @@ import { AppComponent } from './app.component'; import { AppareilComponent } from './appareil/appareil.component'; import { FormsModule } from '@angular/forms'; import { AppareilService } from './services/appareil.service'; +import { AuthComponent } from './auth/auth.component'; +import { AppareilViewComponent } from './appareil-view/appareil-view.component'; +import { RouterModule, Routes } from '@angular/router'; +import { AuthService } from './services/auth.service'; +import { SingleAppareilComponent } from './single-appareil/single-appareil.component'; +import { FourOhFourComponent } from './four-oh-four/four-oh-four.component'; +import { AuthGuard } from './services/auth-guard.service'; + +const appRoutes: Routes = [ + { path: 'appareils', canActivate: [AuthGuard], component: AppareilViewComponent }, + { path: 'appareils/:id', canActivate: [AuthGuard], component: SingleAppareilComponent }, + { path: 'auth', component: AuthComponent }, + { path: '', component: AuthComponent }, + { path: 'not-found', component: FourOhFourComponent }, + { path: '**', redirectTo: 'not-found' } +]; @NgModule({ declarations: [ AppComponent, - AppareilComponent + AppareilComponent, + AuthComponent, + AppareilViewComponent, + SingleAppareilComponent, + FourOhFourComponent ], imports: [ BrowserModule, - FormsModule + FormsModule, + RouterModule.forRoot(appRoutes) ], providers: [ - AppareilService + AppareilService, + AuthService, + AuthGuard ], bootstrap: [AppComponent] }) diff --git a/src/app/appareil-view/appareil-view.component.html b/src/app/appareil-view/appareil-view.component.html new file mode 100644 index 0000000..11dfa70 --- /dev/null +++ b/src/app/appareil-view/appareil-view.component.html @@ -0,0 +1,19 @@ +
+
+
+

Mes appareils

+

Mis à jour le : {{ lastUpdate | async | date: 'd MMMM y' | uppercase }}

+
    + +
+ + +
+
+
diff --git a/src/app/appareil-view/appareil-view.component.scss b/src/app/appareil-view/appareil-view.component.scss new file mode 100644 index 0000000..e69de29 diff --git a/src/app/appareil-view/appareil-view.component.ts b/src/app/appareil-view/appareil-view.component.ts new file mode 100644 index 0000000..9c9867c --- /dev/null +++ b/src/app/appareil-view/appareil-view.component.ts @@ -0,0 +1,45 @@ +import { Component, OnInit } from '@angular/core'; +import { AppareilService } from '../services/appareil.service'; + +@Component({ + selector: 'app-appareil-view', + templateUrl: './appareil-view.component.html', + styleUrls: ['./appareil-view.component.scss'] +}) +export class AppareilViewComponent implements OnInit { + + isAuth = false; + appareils: any[]; + + lastUpdate = new Promise((resolve, reject) => { + const date = new Date(); + setTimeout( + () => { + resolve(date); + }, 2000 + ); + }); + + constructor(private appareilService: AppareilService) { + setTimeout(() => { + this.isAuth = true; + }, 3000); + } + + ngOnInit() { + this.appareils = this.appareilService.appareils; + } + + onAllumer() { + this.appareilService.switchOnAll(); + } + + onEteindre() { + if (confirm('Etes-vous sûr de vouloir éteindre tous vos appareils ?')) { + this.appareilService.switchOffAll(); + } else { + return null; + } + } + +} diff --git a/src/app/appareil/appareil.component.html b/src/app/appareil/appareil.component.html index de0a6c0..6463b62 100644 --- a/src/app/appareil/appareil.component.html +++ b/src/app/appareil/appareil.component.html @@ -3,6 +3,7 @@ 'list-group-item-danger': appareilStatus === 'éteint'}">

Appareil : {{ appareilName }} -- Statut : {{ appareilStatus }}

+ Détail + diff --git a/src/app/auth/auth.component.scss b/src/app/auth/auth.component.scss new file mode 100644 index 0000000..e69de29 diff --git a/src/app/auth/auth.component.ts b/src/app/auth/auth.component.ts new file mode 100644 index 0000000..d9d10e0 --- /dev/null +++ b/src/app/auth/auth.component.ts @@ -0,0 +1,35 @@ +import { Component, OnInit } from '@angular/core'; +import { AuthService } from '../services/auth.service'; +import { Router } from '@angular/router'; + +@Component({ + selector: 'app-auth', + templateUrl: './auth.component.html', + styleUrls: ['./auth.component.scss'] +}) +export class AuthComponent implements OnInit { + + authStatus: boolean; + + constructor(private authService: AuthService, private router: Router) { } + + ngOnInit() { + this.authStatus = this.authService.isAuth; + } + + onSignIn() { + this.authService.signIn().then( + () => { + console.log('Sign in successful!'); + this.authStatus = this.authService.isAuth; + this.router.navigate(['appareils']); + } + ); + } + + onSignOut() { + this.authService.signOut(); + this.authStatus = this.authService.isAuth; + } + +} diff --git a/src/app/four-oh-four/four-oh-four.component.html b/src/app/four-oh-four/four-oh-four.component.html new file mode 100644 index 0000000..8b6bafc --- /dev/null +++ b/src/app/four-oh-four/four-oh-four.component.html @@ -0,0 +1 @@ +

four-oh-four works!

diff --git a/src/app/four-oh-four/four-oh-four.component.scss b/src/app/four-oh-four/four-oh-four.component.scss new file mode 100644 index 0000000..e69de29 diff --git a/src/app/four-oh-four/four-oh-four.component.ts b/src/app/four-oh-four/four-oh-four.component.ts new file mode 100644 index 0000000..3844973 --- /dev/null +++ b/src/app/four-oh-four/four-oh-four.component.ts @@ -0,0 +1,15 @@ +import { Component, OnInit } from '@angular/core'; + +@Component({ + selector: 'app-four-oh-four', + templateUrl: './four-oh-four.component.html', + styleUrls: ['./four-oh-four.component.scss'] +}) +export class FourOhFourComponent implements OnInit { + + constructor() { } + + ngOnInit(): void { + } + +} diff --git a/src/app/services/appareil.service.ts b/src/app/services/appareil.service.ts index a75f8c7..c1bf2ef 100644 --- a/src/app/services/appareil.service.ts +++ b/src/app/services/appareil.service.ts @@ -1,14 +1,17 @@ export class AppareilService { appareils = [ { + id: 1, name: 'Machine à laver', status: 'éteint' }, { + id: 2, name: 'Frigo', status: 'allumé' }, { + id: 3, name: 'Ordinateur', status: 'éteint' } @@ -33,4 +36,12 @@ export class AppareilService { switchOffOne(i: number) { this.appareils[i].status = 'éteint'; } + + getAppareilById(id: number) { + return this.appareils.find( + (s) => { + return s.id === id; + } + ); + } } diff --git a/src/app/services/auth-guard.service.ts b/src/app/services/auth-guard.service.ts new file mode 100644 index 0000000..41cee48 --- /dev/null +++ b/src/app/services/auth-guard.service.ts @@ -0,0 +1,21 @@ +import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router'; +import { Observable } from 'rxjs/Observable'; +import { AuthService } from './auth.service'; +import { Injectable } from '@angular/core'; + +@Injectable() +export class AuthGuard implements CanActivate { + + constructor(private authService: AuthService, + private router: Router) { } + + canActivate( + route: ActivatedRouteSnapshot, + state: RouterStateSnapshot): Observable | Promise | boolean { + if (this.authService.isAuth) { + return true; + } else { + this.router.navigate(['/auth']); + } + } +} diff --git a/src/app/services/auth.service.ts b/src/app/services/auth.service.ts new file mode 100644 index 0000000..1cbe0f0 --- /dev/null +++ b/src/app/services/auth.service.ts @@ -0,0 +1,20 @@ +export class AuthService { + isAuth = false; + + signIn() { + return new Promise( + (resolve, reject) => { + setTimeout( + () => { + this.isAuth = true; + resolve(true); + }, 2000 + ); + } + ); + } + + signOut() { + this.isAuth = false; + } +} diff --git a/src/app/single-appareil/single-appareil.component.html b/src/app/single-appareil/single-appareil.component.html new file mode 100644 index 0000000..64d0e54 --- /dev/null +++ b/src/app/single-appareil/single-appareil.component.html @@ -0,0 +1,3 @@ +

{{ name }}

+

Statut : {{ status }}

+Retour à la liste diff --git a/src/app/single-appareil/single-appareil.component.scss b/src/app/single-appareil/single-appareil.component.scss new file mode 100644 index 0000000..e69de29 diff --git a/src/app/single-appareil/single-appareil.component.ts b/src/app/single-appareil/single-appareil.component.ts new file mode 100644 index 0000000..3b89261 --- /dev/null +++ b/src/app/single-appareil/single-appareil.component.ts @@ -0,0 +1,23 @@ +import { Component, OnInit } from '@angular/core'; +import { AppareilService } from '../services/appareil.service'; +import { ActivatedRoute } from '@angular/router'; + +@Component({ + selector: 'app-single-appareil', + templateUrl: './single-appareil.component.html', + styleUrls: ['./single-appareil.component.scss'] +}) +export class SingleAppareilComponent implements OnInit { + + name = 'Appareil'; + status = 'Statut'; + + constructor(private appareilService: AppareilService, private route: ActivatedRoute) { } + + ngOnInit() { + const id = this.route.snapshot.params['id']; + this.name = this.appareilService.getAppareilById(+id).name; + this.status = this.appareilService.getAppareilById(+id).status; + } + +}