Skip to content
Merged
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
3 changes: 3 additions & 0 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
<li routerLinkActive="active"><a routerLink="auth">Authentification</a></li>
<li routerLinkActive="active"><a routerLink="appareils">Appareils</a></li>
</ul>
<div class="navbar-right">
<p>Vous êtes connecté depuis {{ secondes }} secondes !</p>
</div>
</div>
</div>
</nav>
Expand Down
26 changes: 24 additions & 2 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,37 @@
import {Component, OnInit} from '@angular/core';
import { Component, OnInit, OnDestroy } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Subscription } from 'rxjs/Subscription';
import 'rxjs/add/observable/interval';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
export class AppComponent implements OnInit, OnDestroy {

secondes: number;
counterSubscription: Subscription;

constructor() {
}

ngOnInit() {
const counter = Observable.interval(1000);
this.counterSubscription = counter.subscribe(
(value) => {
this.secondes = value;
},
(error) => {
console.log('Uh-oh, an error occurred! : ' + error);
},
() => {
console.log('Observable complete!');
}
);
}

ngOnDestroy() {
this.counterSubscription.unsubscribe();
}
}
23 changes: 14 additions & 9 deletions src/app/appareil-view/appareil-view.component.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { Component, OnInit } from '@angular/core';
import { Component, OnInit, OnDestroy } from '@angular/core';
import { AppareilService } from '../services/appareil.service';
import { Subscription } from 'rxjs/Subscription';

@Component({
selector: 'app-appareil-view',
templateUrl: './appareil-view.component.html',
styleUrls: ['./appareil-view.component.scss']
})
export class AppareilViewComponent implements OnInit {
export class AppareilViewComponent implements OnInit, OnDestroy {

isAuth = false;
appareils: any[];
appareilSubscription: Subscription;

lastUpdate = new Promise((resolve, reject) => {
const date = new Date();
Expand All @@ -20,14 +21,15 @@ export class AppareilViewComponent implements OnInit {
);
});

constructor(private appareilService: AppareilService) {
setTimeout(() => {
this.isAuth = true;
}, 3000);
}
constructor(private appareilService: AppareilService) { }

ngOnInit() {
this.appareils = this.appareilService.appareils;
this.appareilSubscription = this.appareilService.appareilsSubject.subscribe(
(appareils: any[]) => {
this.appareils = appareils;
}
);
this.appareilService.emitAppareilSubject();
}

onAllumer() {
Expand All @@ -42,4 +44,7 @@ export class AppareilViewComponent implements OnInit {
}
}

ngOnDestroy() {
this.appareilSubscription.unsubscribe();
}
}
11 changes: 10 additions & 1 deletion src/app/services/appareil.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { Subject } from 'rxjs/Subject';

export class AppareilService {
appareils = [

appareilsSubject = new Subject<any[]>();

private appareils = [
{
id: 1,
name: 'Machine à laver',
Expand All @@ -17,6 +22,10 @@ export class AppareilService {
}
];

emitAppareilSubject() {
this.appareilsSubject.next(this.appareils.slice());
}

switchOnAll() {
for (let appareil of this.appareils) {
appareil.status = 'allumé';
Expand Down