From f5321d80cd4310ab6df164ec5d9d05fd5b9e24ea Mon Sep 17 00:00:00 2001 From: majid noureddine Date: Thu, 14 May 2020 18:17:36 +0100 Subject: [PATCH 1/3] init appareil service --- src/app/app.component.ts | 3 ++- src/app/app.module.ts | 5 ++++- src/app/services/appareil.service.ts | 2 ++ 3 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 src/app/services/appareil.service.ts diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 7e526d6..c5e68a0 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,4 +1,5 @@ import { Component } from '@angular/core'; +import { AppareilService } from './services/appareil.service'; @Component({ selector: 'app-root', @@ -31,7 +32,7 @@ export class AppComponent { } ]; - constructor() { + constructor(private appareilService: AppareilService) { setTimeout( () => { this.isAuth = true; diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 73e91b5..c9d2ca3 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -3,6 +3,7 @@ import { NgModule } from '@angular/core'; import { AppComponent } from './app.component'; import { AppareilComponent } from './appareil/appareil.component'; import { FormsModule } from '@angular/forms'; +import { AppareilService } from './services/appareil.service'; @NgModule({ declarations: [ @@ -13,7 +14,9 @@ import { FormsModule } from '@angular/forms'; BrowserModule, FormsModule ], - providers: [], + providers: [ + AppareilService + ], bootstrap: [AppComponent] }) export class AppModule { } diff --git a/src/app/services/appareil.service.ts b/src/app/services/appareil.service.ts new file mode 100644 index 0000000..017195e --- /dev/null +++ b/src/app/services/appareil.service.ts @@ -0,0 +1,2 @@ +export class AppareilService { +} From bdf2ed0fac1f881cfc2c678c8af236171b8d3472 Mon Sep 17 00:00:00 2001 From: majid noureddine Date: Thu, 14 May 2020 18:36:03 +0100 Subject: [PATCH 2/3] switch on or off all appareils --- src/app/app.component.html | 3 +++ src/app/app.component.ts | 33 ++++++++++++++-------------- src/app/services/appareil.service.ts | 26 ++++++++++++++++++++++ 3 files changed, 45 insertions(+), 17 deletions(-) diff --git a/src/app/app.component.html b/src/app/app.component.html index c220c79..ae42641 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -11,6 +11,9 @@

Mes appareils

+ diff --git a/src/app/app.component.ts b/src/app/app.component.ts index c5e68a0..846136d 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,4 +1,4 @@ -import { Component } from '@angular/core'; +import {Component, OnInit} from '@angular/core'; import { AppareilService } from './services/appareil.service'; @Component({ @@ -6,7 +6,7 @@ import { AppareilService } from './services/appareil.service'; templateUrl: './app.component.html', styleUrls: ['./app.component.scss'] }) -export class AppComponent { +export class AppComponent implements OnInit { isAuth = false; lastUpdate = new Promise((resolve, reject) => { @@ -17,20 +17,7 @@ export class AppComponent { }, 2000 ); }); - appareils = [ - { - name: 'Machine à laver', - status: 'éteint' - }, - { - name: 'Frigo', - status: 'allumé' - }, - { - name: 'Ordinateur', - status: 'éteint' - } - ]; + appareils: any[]; constructor(private appareilService: AppareilService) { setTimeout( @@ -40,7 +27,19 @@ export class AppComponent { ); } + ngOnInit() { + this.appareils = this.appareilService.appareils; + } + onAllumer() { - console.log('On allume tout !'); + 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/services/appareil.service.ts b/src/app/services/appareil.service.ts index 017195e..fe63c85 100644 --- a/src/app/services/appareil.service.ts +++ b/src/app/services/appareil.service.ts @@ -1,2 +1,28 @@ export class AppareilService { + appareils = [ + { + name: 'Machine à laver', + status: 'éteint' + }, + { + name: 'Frigo', + status: 'allumé' + }, + { + name: 'Ordinateur', + status: 'éteint' + } + ]; + + switchOnAll() { + for (let appareil of this.appareils) { + appareil.status = 'allumé'; + } + } + + switchOffAll() { + for (let appareil of this.appareils) { + appareil.status = 'éteint'; + } + } } From f2114842d17882de5114776b60d026239887eb0f Mon Sep 17 00:00:00 2001 From: majid noureddine Date: Thu, 14 May 2020 18:44:20 +0100 Subject: [PATCH 3/3] switch on or off one appareill --- src/app/app.component.html | 5 +++-- src/app/appareil/appareil.component.html | 11 +++++++++-- src/app/appareil/appareil.component.ts | 13 ++++++++++++- src/app/services/appareil.service.ts | 8 ++++++++ 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/app/app.component.html b/src/app/app.component.html index ae42641..474e8aa 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -4,9 +4,10 @@

Mes appareils

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

    - + [appareilStatus]="appareil.status" + [index]="i">
+ + diff --git a/src/app/appareil/appareil.component.ts b/src/app/appareil/appareil.component.ts index 51b7a8f..75e5745 100644 --- a/src/app/appareil/appareil.component.ts +++ b/src/app/appareil/appareil.component.ts @@ -1,4 +1,5 @@ import { Component, Input, OnInit } from '@angular/core'; +import { AppareilService } from '../services/appareil.service'; @Component({ selector: 'app-appareil', @@ -10,8 +11,10 @@ export class AppareilComponent implements OnInit { @Input() appareilName: string; @Input() appareilStatus: string; + @Input() index: number; - constructor() { } + constructor(private appareilService: AppareilService) { + } ngOnInit() { } @@ -23,4 +26,12 @@ export class AppareilComponent implements OnInit { return 'red'; } } + + onSwitch() { + if (this.appareilStatus === 'allumé') { + this.appareilService.switchOffOne(this.index); + } else if (this.appareilStatus === 'éteint') { + this.appareilService.switchOnOne(this.index); + } + } } diff --git a/src/app/services/appareil.service.ts b/src/app/services/appareil.service.ts index fe63c85..a75f8c7 100644 --- a/src/app/services/appareil.service.ts +++ b/src/app/services/appareil.service.ts @@ -25,4 +25,12 @@ export class AppareilService { appareil.status = 'éteint'; } } + + switchOnOne(i: number) { + this.appareils[i].status = 'allumé'; + } + + switchOffOne(i: number) { + this.appareils[i].status = 'éteint'; + } }