-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add UI full error and loading handling
- Loading branch information
Showing
8 changed files
with
77 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
<app-weight *ngIf="$sync | async"></app-weight> | ||
<app-weight *ngIf="syncState.isReady"></app-weight> | ||
|
||
<h1 *ngIf="syncState.hasFailed">Error occured</h1> | ||
|
||
<p *ngIf="syncState.isLoading">Loading....</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,21 @@ | ||
import { Component } from '@angular/core'; | ||
import { Component, OnInit } from '@angular/core'; | ||
import { subscribeToRequestState, initialHttpRequestState } from './utils'; | ||
import { WithingsService } from './withings.service'; | ||
import { HttpRequestState } from './types'; | ||
|
||
@Component({ | ||
selector: 'app-root', | ||
templateUrl: './app.component.html', | ||
styleUrls: ['./app.component.css'] | ||
styleUrls: ['./app.component.css'], | ||
}) | ||
export class AppComponent { | ||
export class AppComponent implements OnInit { | ||
constructor(private withingsService: WithingsService) {} | ||
|
||
$sync = this.withingsService.sync(); | ||
syncState: HttpRequestState<void> = initialHttpRequestState; | ||
|
||
ngOnInit(): void { | ||
subscribeToRequestState(this.withingsService.sync(), (newState) => { | ||
this.syncState = newState; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,13 @@ | ||
import { HttpErrorResponse } from "@angular/common/http"; | ||
|
||
export interface HttpRequestState<T> { | ||
isLoading: boolean; | ||
hasFailed: boolean; | ||
isReady: boolean; | ||
value?: T; | ||
error?: HttpErrorResponse | Error; | ||
} | ||
|
||
export interface WeightResponse { | ||
weight?: number; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Observable, catchError, map, of, startWith } from 'rxjs'; | ||
import { HttpRequestState } from './types'; | ||
|
||
export const initialHttpRequestState: HttpRequestState<any> = { | ||
isLoading: false, | ||
isReady: false, | ||
hasFailed: false, | ||
}; | ||
|
||
export function subscribeToRequestState<T>( | ||
$target: Observable<T>, | ||
setState: (newState: HttpRequestState<T>) => void | ||
): void { | ||
$target | ||
.pipe( | ||
map((value) => ({ | ||
isLoading: false, | ||
isReady: true, | ||
hasFailed: false, | ||
value, | ||
})), | ||
catchError((error) => { | ||
console.log(error); | ||
return of({ isLoading: false, isReady: false, hasFailed: true, error }); | ||
}), | ||
startWith({ isLoading: true, isReady: false, hasFailed: false }) | ||
) | ||
.subscribe(setState); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
<p>{{$weight | async}}</p> | ||
<p *ngIf="weightState.isReady">{{ weightState.value || '?' }}</p> | ||
|
||
<h1 *ngIf="weightState.hasFailed">Error occured</h1> | ||
|
||
<p *ngIf="weightState.isLoading">Loading....</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,21 @@ | ||
import { Component } from '@angular/core'; | ||
import { map } from 'rxjs'; | ||
import { Component, OnInit } from '@angular/core'; | ||
import { HttpRequestState } from '../types'; | ||
import { initialHttpRequestState, subscribeToRequestState } from '../utils'; | ||
import { WeightService } from '../weight.service'; | ||
|
||
@Component({ | ||
selector: 'app-weight', | ||
templateUrl: './weight.component.html', | ||
styleUrls: ['./weight.component.css'], | ||
}) | ||
export class WeightComponent { | ||
export class WeightComponent implements OnInit { | ||
constructor(private weightService: WeightService) {} | ||
|
||
$weight = this.weightService | ||
.getWeight() | ||
.pipe(map((weight) => weight?.toString() ?? '?')); | ||
weightState: HttpRequestState<number | undefined> = initialHttpRequestState; | ||
|
||
ngOnInit(): void { | ||
subscribeToRequestState(this.weightService.getWeight(), newState => { | ||
this.weightState = newState | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters