Petites améliorations UI/UX.
This commit is contained in:
@@ -6,15 +6,21 @@
|
|||||||
[disabled]="isLoading$ | async">
|
[disabled]="isLoading$ | async">
|
||||||
Load the car
|
Load the car
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
|
<button mat-raised-button
|
||||||
|
(click)="loadCarViaAnObservable()"
|
||||||
|
[disabled]="isLoading$ | async">
|
||||||
|
Load the car
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
<div class="loading" *ngIf="isLoading$ | async">
|
<div class="loading" *ngIf="isLoading$ | async">
|
||||||
<h2>Loading...</h2>
|
<h2>Loading...</h2>
|
||||||
<mat-spinner></mat-spinner>
|
<mat-spinner></mat-spinner>
|
||||||
</div>
|
</div>
|
||||||
<ng-container *ngIf="isLoaded">
|
<div class="section" *ngIf="isLoaded">
|
||||||
<h2>Car data</h2>
|
<h2>Car data</h2>
|
||||||
<div class="centered">
|
<div class="centered">
|
||||||
<app-car [value]="car"></app-car>
|
<app-car [value]="car"></app-car>
|
||||||
</div>
|
</div>
|
||||||
</ng-container>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -1,4 +1,9 @@
|
|||||||
.component {
|
.component {
|
||||||
|
.actions {
|
||||||
|
display: flex;
|
||||||
|
gap: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
.loading {
|
.loading {
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
|
|||||||
@@ -28,7 +28,10 @@ export class NetworkCallComponent {
|
|||||||
|
|
||||||
loadCar(): void {
|
loadCar(): void {
|
||||||
this.loadThroughAPromise();
|
this.loadThroughAPromise();
|
||||||
// this.loadThroughAnObservable();
|
}
|
||||||
|
|
||||||
|
loadCarViaAnObservable(): void {
|
||||||
|
this.loadThroughAnObservable();
|
||||||
}
|
}
|
||||||
|
|
||||||
private loadThroughAPromise(): void {
|
private loadThroughAPromise(): void {
|
||||||
|
|||||||
@@ -1,4 +1,29 @@
|
|||||||
<div class="component">
|
<div class="component">
|
||||||
<h1>Les promesses</h1>
|
<h1>Les promesses</h1>
|
||||||
<app-car [value]="car"></app-car>
|
<div class="actions">
|
||||||
|
<button mat-raised-button (click)="showCarViaAPromise()" [disabled]="isLoading">
|
||||||
|
<mat-icon>visibility</mat-icon>
|
||||||
|
Show car via a Promise
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<button mat-raised-button (click)="showCarViaAsyncAwaitProcess()" [disabled]="isLoading">
|
||||||
|
<mat-icon>visibility</mat-icon>
|
||||||
|
Show car via an Async Await process
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="section">
|
||||||
|
Number of loadings: {{numberOfLoadings}}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="loading" *ngIf="isLoading">
|
||||||
|
<h2>Loading...</h2>
|
||||||
|
<mat-spinner></mat-spinner>
|
||||||
|
</div>
|
||||||
|
<div class="section" *ngIf="isLoaded">
|
||||||
|
<h2>Car data</h2>
|
||||||
|
<div class="centered">
|
||||||
|
<app-car [value]="car"></app-car>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
.component {
|
||||||
|
.actions {
|
||||||
|
display: flex;
|
||||||
|
gap: 1rem;
|
||||||
|
}
|
||||||
|
.loading {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,24 +1,95 @@
|
|||||||
import { Component } from '@angular/core';
|
import {Component} from '@angular/core';
|
||||||
import {Car} from "../core/model/car";
|
import {Car} from "../core/model/car";
|
||||||
|
import {MatSnackBar} from "@angular/material/snack-bar";
|
||||||
|
|
||||||
@Component({
|
const CAR: Car = {
|
||||||
selector: 'app-promises-example',
|
|
||||||
templateUrl: './promises-example.component.html',
|
|
||||||
styleUrls: ['./promises-example.component.scss']
|
|
||||||
})
|
|
||||||
export class PromisesExampleComponent {
|
|
||||||
car?: Car = {
|
|
||||||
id: 1,
|
id: 1,
|
||||||
brand: 'Toyota',
|
brand: 'Toyota',
|
||||||
model: 'Yaris',
|
model: 'Yaris',
|
||||||
power: 12,
|
power: 12,
|
||||||
numberOfSeats: 5
|
numberOfSeats: 5
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
@Component({
|
||||||
// Async / Await -> + comparaison avec les then
|
selector: 'app-promises-example',
|
||||||
// new Promise "à la main"
|
templateUrl: './promises-example.component.html',
|
||||||
// les then chaînés
|
styleUrls: ['./promises-example.component.scss']
|
||||||
// catch (avec 1 ou plusieurs then) -> try/catch avec un await
|
})
|
||||||
*/
|
export class PromisesExampleComponent {
|
||||||
|
car?: Car;
|
||||||
|
isLoading: boolean = false;
|
||||||
|
isLoaded: boolean = false;
|
||||||
|
numberOfLoadings: number = 0;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
private snackBar: MatSnackBar
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
// ******************************************
|
||||||
|
// Mode Promise avec les then/catch/finally
|
||||||
|
// ******************************************
|
||||||
|
showCarViaAPromise(): void {
|
||||||
|
this.loadCarViaAPromise();
|
||||||
|
}
|
||||||
|
|
||||||
|
private loadCarViaAPromise(): void {
|
||||||
|
this.isLoading = true;
|
||||||
|
this.isLoaded = false;
|
||||||
|
|
||||||
|
this.findViaPromise()
|
||||||
|
.then(car => {
|
||||||
|
this.car = car;
|
||||||
|
this.isLoaded = true;
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
this.snackBar.open(`Error while loading car. Cause : ${error}`);
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
this.isLoading = false;
|
||||||
|
this.numberOfLoadings++;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private findViaPromise(): Promise<Car> {
|
||||||
|
return new Promise<Car>((resolve, reject) => {
|
||||||
|
if (this.numberOfLoadings % 2 === 0) {
|
||||||
|
setTimeout(() => {
|
||||||
|
resolve(CAR);
|
||||||
|
}, 2000);
|
||||||
|
} else {
|
||||||
|
reject('It\'s an odd call number.');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// ******************************************
|
||||||
|
// Mode async/await
|
||||||
|
// ******************************************
|
||||||
|
showCarViaAsyncAwaitProcess(): void {
|
||||||
|
this.loadCarViaAsyncAwaitProcess();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async loadCarViaAsyncAwaitProcess(): Promise<void> {
|
||||||
|
this.isLoading = true;
|
||||||
|
this.isLoaded = false;
|
||||||
|
|
||||||
|
try {
|
||||||
|
this.car = await this.findViaAsyncAwaitProcess();
|
||||||
|
this.isLoaded = true;
|
||||||
|
} catch (error) {
|
||||||
|
this.snackBar.open(`Error while loading car. Cause : ${error}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.numberOfLoadings++;
|
||||||
|
this.isLoading = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async findViaAsyncAwaitProcess(): Promise<Car> {
|
||||||
|
if (this.numberOfLoadings % 2 !== 0) {
|
||||||
|
throw 'It\'s an odd call number.';
|
||||||
|
}
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 2000));
|
||||||
|
return CAR;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,4 +7,8 @@ body { margin: 0; font-family: Roboto, "Helvetica Neue", sans-serif; }
|
|||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: row;
|
flex-direction: row;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.section {
|
||||||
|
margin: 1rem 0;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user