Skip to content

Commit

Permalink
fix: Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
andreroggeri committed Sep 12, 2020
1 parent ee7ff9b commit 92d37ed
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 28 deletions.
3 changes: 0 additions & 3 deletions src/app/finance/store/effects/transaction-type.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export class TransactionTypeEffects {

refreshClinic$ = createEffect(() => this.action$.pipe(
ofType(loadClinics),
tap(() => console.log('passou aqui refreshClinic')),
switchMap(() => {
return this.clinicService.getAll().pipe(
map(clinics => loadClinicsSuccess({ clinics: clinics.results })),
Expand All @@ -40,7 +39,6 @@ export class TransactionTypeEffects {

refreshTransactionType$ = createEffect(() => this.action$.pipe(
ofType(clinicSelected),
tap(() => console.log('passou aqui refreshTransactionType')),
switchMap((v) => {
return this.transactionTypeService.getAll(v.clinic.id).pipe(
map(transactionTypes => loadTransactionTypesSuccess({ transactionTypes })),
Expand All @@ -51,7 +49,6 @@ export class TransactionTypeEffects {

transactionTypeListPageChange$ = createEffect(() => this.action$.pipe(
ofType(transactionTypesPageChanged),
tap(() => console.log('passou aqui transactionTypeListPageChange')),
switchMap((data, clinic) => {
const offset = data.event.pageSize * data.event.pageIndex;
const filter = new TransactionTypeFilter();
Expand Down
17 changes: 15 additions & 2 deletions src/app/finance/store/reducers/transaction-type.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
loadClinics,
loadClinicsError,
loadClinicsSuccess,
loadTransactionTypesError,
loadTransactionTypesSuccess,
} from '../actions/transaction-type.actions';

Expand Down Expand Up @@ -33,10 +34,10 @@ const _transactionTypeReducer = createReducer(initialState,
return { ...state, clinic: { ...state.clinic, all: action.clinics, empty: action.clinics.length === 0, error: false, loading: false } };
}),
on(loadClinicsError, (state) => {
return { ...state, error: true, clinics: [], empty: false };
return { ...state, clinic: { all: [], error: true, empty: false, loading: false } };
}),
on(clinicSelected, (state, action) => {
return { ...state, clinic: { ...state.clinic, selected: action.clinic }, transactionTypes: { ...state.transactionTypes, loading: true } };
return { ...state, clinic: { ...state.clinic, selected: action.clinic }, transactionTypes: { ...state.transactionTypes, loading: true, error: false, empty: false } };
}),
on(loadTransactionTypesSuccess, (state, action) => {
return {
Expand All @@ -50,6 +51,18 @@ const _transactionTypeReducer = createReducer(initialState,
},
};
}),
on(loadTransactionTypesError, (state) => {
return {
...state,
transactionTypes: {
all: [],
count: 0,
error: true,
empty: false,
loading: false,
},
};
}),
);

export function transactionTypeReducer(state, action) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@
</ng-container>

<app-empty-state image="empty-box" *ngIf="empty$ | async">
<p>Você não possui nenhum tipo de despesa cadastrado</p>
<p>Você ainda não possui nenhum tipo de despesa cadastrado</p>
<p>Cadastre uma agora para começar a gestão financeira</p>
</app-empty-state>

<app-empty-state image="empty-box" *ngIf="error$ | async">
<p>Ocorreu um erro ao recuperar os tipos de despesas</p>
</app-empty-state>
<p>Ocorreu um erro ao recuperar os dados</p>
<p>Tente novamente mais tarde</p>
</app-empty-state>

<mat-progress-bar mode="query" *ngIf="loading$ | async"></mat-progress-bar>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AfterViewInit, Component, OnInit, ViewChild } from '@angular/core';
import { Component, ViewChild } from '@angular/core';
import { MatPaginator, PageEvent } from '@angular/material';
import { Store } from '@ngrx/store';
import { map } from 'rxjs/operators';
Expand All @@ -10,7 +10,7 @@ import { transactionTypesPageChanged } from '../../store/actions/transaction-typ
selector: 'app-transaction-type-list',
templateUrl: './transaction-type-list.component.html',
})
export class TransactionTypeListComponent implements AfterViewInit {
export class TransactionTypeListComponent {
state$ = this.store.select(m => m.finance.transactionTypes);
empty$ = this.state$.pipe(map(v => v.transactionTypes.empty));
error$ = this.state$.pipe(map(v => v.transactionTypes.error));
Expand All @@ -23,13 +23,12 @@ export class TransactionTypeListComponent implements AfterViewInit {
displayedColumns = ['code', 'label'];

@ViewChild(MatPaginator, { static: false })
paginator: MatPaginator;

constructor(private readonly store: Store<{ finance: { transactionTypes: ITransactionTypeState } }>) { }

ngAfterViewInit() {
this.paginator.page.subscribe((pageEvent: PageEvent) => {
set(paginator: MatPaginator) {
paginator.page.subscribe((pageEvent: PageEvent) => {
this.store.dispatch(transactionTypesPageChanged({ event: pageEvent }));
});
}

constructor(private readonly store: Store<{ finance: { transactionTypes: ITransactionTypeState } }>) { }

}
34 changes: 22 additions & 12 deletions src/app/finance/transaction-type/transaction-type.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,29 @@
<span>Tipos de Despesas </span>
</mat-card-title>

<mat-form-field>
<mat-select placeholder="Clinica" [value]="selectedClinic$ | async" (selectionChange)="clinicSelected($event)">
<mat-option [value]="clinic" *ngFor="let clinic of clinic$ | async">{{clinic.name}}</mat-option>
</mat-select>
</mat-form-field>
<div fxLayout="row" fxLayoutAlign="end end">
<ng-container *ngIf="hasClinics$ | async">
<mat-form-field>
<mat-select placeholder="Clinica" [value]="selectedClinic$ | async"
(selectionChange)="clinicSelected($event)">
<mat-option [value]="clinic" *ngFor="let clinic of clinic$ | async">{{clinic.name}}</mat-option>
</mat-select>
</mat-form-field>
<div fxLayout="row" fxLayoutAlign="end end">

</div>
<h1 *ngIf="empty$ | async">SEM RESULTADOS</h1>
<h1 *ngIf="error$ | async">ERROOO</h1>
<h1 *ngIf="loading$ | async">CARREGANDOOOO</h1>
</div>
<app-transaction-type-list></app-transaction-type-list>
</ng-container>

<app-transaction-type-list>
<app-empty-state image="empty-box" *ngIf="empty$ | async">
<p>Você ainda não tem clinicas cadastradas.</p>
<p>Cadastre uma clinica para começar a gestão financeira</p>
</app-empty-state>

<app-empty-state image="error-cloud" *ngIf="error$ | async">
<p>Ocorreu um erro ao recuperar os dados</p>
<p>Tente novamente mais tarde</p>
</app-empty-state>

<mat-progress-bar mode="query" *ngIf="loading$ | async"></mat-progress-bar>

</app-transaction-type-list>
</mat-card>
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class TransactionTypeComponent implements OnInit {
empty$ = this.state$.pipe(map(v => v.clinic.empty));
error$ = this.state$.pipe(map(v => v.clinic.error));
loading$ = this.state$.pipe(map(v => v.clinic.loading));
hasClinics$ = this.state$.pipe(map(v => !v.clinic.empty && !v.clinic.error && !v.clinic.loading));

constructor(private readonly store: Store<{ finance: { transactionTypes: ITransactionTypeState } }>) {
}
Expand Down
Binary file added src/assets/images/error-cloud.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 92d37ed

Please sign in to comment.