Skip to content

Commit 170986e

Browse files
authored
Merge pull request #6 from masteropen/angular_httpClient
interaction with firebase by angular HttpClient
2 parents d96fd25 + 9544aa7 commit 170986e

File tree

4 files changed

+47
-17
lines changed

4 files changed

+47
-17
lines changed

src/app/app.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { EditAppareilComponent } from './edit-appareil/edit-appareil.component';
1515
import { UserService } from './services/user.service';
1616
import { UserListComponent } from './user-list/user-list.component';
1717
import { NewUserComponent } from './new-user/new-user.component';
18+
import { HttpClientModule } from '@angular/common/http';
1819

1920
const appRoutes: Routes = [
2021
{ path: 'appareils', canActivate: [AuthGuard], component: AppareilViewComponent },
@@ -43,6 +44,7 @@ const appRoutes: Routes = [
4344
imports: [
4445
BrowserModule,
4546
FormsModule,
47+
HttpClientModule,
4648
ReactiveFormsModule,
4749
RouterModule.forRoot(appRoutes)
4850
],

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ <h2>Mes appareils</h2>
1414
(click)="onAllumer()">Tout allumer</button>
1515
<button class="btn btn-danger"
1616
(click)="onEteindre()">Tout éteindre</button>
17+
<button class="btn btn-primary"
18+
(click)="onSave()">Enregistrer les appareils</button>
19+
<button class="btn btn-primary"
20+
(click)="onFetch()">Récupérer les appareils</button>
1721
</div>
1822
</div>
1923
</div>

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,12 @@ export class AppareilViewComponent implements OnInit, OnDestroy {
4747
ngOnDestroy() {
4848
this.appareilSubscription.unsubscribe();
4949
}
50+
51+
onSave() {
52+
this.appareilService.saveAppareilsToServer();
53+
}
54+
55+
onFetch() {
56+
this.appareilService.getAppareilsFromServer();
57+
}
5058
}

src/app/services/appareil.service.ts

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,15 @@
11
import { Subject } from 'rxjs/Subject';
2+
import { Injectable } from '@angular/core';
3+
import { HttpClient } from '@angular/common/http';
24

5+
@Injectable()
36
export class AppareilService {
47

8+
constructor(private httpClient: HttpClient) { }
9+
510
appareilsSubject = new Subject<any[]>();
611

7-
private appareils = [
8-
{
9-
id: 1,
10-
name: 'Machine à laver',
11-
status: 'éteint'
12-
},
13-
{
14-
id: 2,
15-
name: 'Frigo',
16-
status: 'allumé'
17-
},
18-
{
19-
id: 3,
20-
name: 'Ordinateur',
21-
status: 'éteint'
22-
}
23-
];
12+
private appareils = [];
2413

2514
emitAppareilSubject() {
2615
this.appareilsSubject.next(this.appareils.slice());
@@ -66,4 +55,31 @@ export class AppareilService {
6655
this.appareils.push(appareilObject);
6756
this.emitAppareilSubject();
6857
}
58+
59+
saveAppareilsToServer() {
60+
this.httpClient
61+
.put('https://angular-training-8da8c.firebaseio.com/appareils.json', this.appareils)
62+
.subscribe(
63+
() => {
64+
console.log('Enregistrement terminé !');
65+
},
66+
(error) => {
67+
console.log('Erreur ! : ' + error);
68+
}
69+
);
70+
}
71+
72+
getAppareilsFromServer() {
73+
this.httpClient
74+
.get<any[]>('https://angular-training-8da8c.firebaseio.com/appareils.json')
75+
.subscribe(
76+
(response) => {
77+
this.appareils = response;
78+
this.emitAppareilSubject();
79+
},
80+
(error) => {
81+
console.log('Erreur ! : ' + error);
82+
}
83+
);
84+
}
6985
}

0 commit comments

Comments
 (0)