Skip to content

Commit

Permalink
feat: type error handling on reservations
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablito2020 committed Nov 13, 2023
1 parent 201a979 commit 775b822
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
4 changes: 2 additions & 2 deletions app/src/app/state/map/map.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ElementRef } from '@angular/core';
import { GoogleMapConfig } from '@capacitor/google-maps/dist/typings/definitions';
import { Refuge } from '../../schemas/refuge/refuge';
import { Coordinates } from '../../services/search/search.service';
import { GetAllRefugesErrors } from '../../schemas/refuge/get-all-refuges-schema';

export const loadMap = createAction(
'[Map] Loading Map',
Expand All @@ -20,10 +21,9 @@ export const moveMapTo = createAction(
props<{ coordinates: Coordinates }>(),
);

// TODO: This error shouldn't be any
export const loadRefugesError = createAction(
'[Map] Load All Refuges Error',
props<{ error: any }>(),
props<{ error: GetAllRefugesErrors }>(),
);

export const loadedRefuges = createAction(
Expand Down
24 changes: 22 additions & 2 deletions app/src/app/state/map/map.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
ofType,
ROOT_EFFECTS_INIT,
} from '@ngrx/effects';
import { combineLatest, map, switchMap, tap } from 'rxjs';
import { catchError, combineLatest, map, of, switchMap, tap } from 'rxjs';
import { fromPromise } from 'rxjs/internal/observable/innerFrom';
import { cloneDeep } from 'lodash';
import { openModal } from '../components/modal/modal.actions';
Expand All @@ -24,6 +24,9 @@ import {
moveMapTo,
} from './map.actions';
import { loadedMapLibrary } from '../init/init.actions';
import { programmingError, unknownError } from '../errors/error.actions';
import { match } from 'ts-pattern';
import { GetAllRefugesErrors } from '../../schemas/refuge/get-all-refuges-schema';

@Injectable()
export class MapEffects {
Expand Down Expand Up @@ -52,7 +55,10 @@ export class MapEffects {
actions[0].map,
cloneDeep(actions[0].config),
),
).pipe(map(() => loadedMap())),
).pipe(
map(() => loadedMap()),
catchError(() => of(unknownError())),
),
),
),
);
Expand Down Expand Up @@ -100,4 +106,18 @@ export class MapEffects {
),
{ dispatch: false },
);

getAllRefugesErrors$ = createEffect(() =>
this.actions$.pipe(
ofType(loadRefugesError),
map((action) => {
return match(action.error)
.with(GetAllRefugesErrors.SERVER_INCORRECT_DATA_FORMAT_ERROR, () =>
programmingError(),
)
.with(GetAllRefugesErrors.UNKNOWN_ERROR, () => unknownError())
.exhaustive();
}),
),
);
}
14 changes: 10 additions & 4 deletions app/src/app/state/reservations/reservations.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ import {
ReservationWithoutUserId,
} from '../../schemas/reservations/reservation';
import { RefugeReservationsRelations } from '../../services/reservations/grouped-by/refuge';
import {
CreateReservationError,
ErrorCreateReservation,
} from '../../schemas/reservations/create-reservation';
import {
DeleteReservationError,
ErrorDeleteReservation,
} from '../../schemas/reservations/delete-reservation';

export const addReservation = createAction(
'[Reservations] Add Reservation',
Expand All @@ -22,8 +30,7 @@ export const fetchReservations = createAction(

export const errorAddingReservation = createAction(
'[Reservations] Error Adding Reservation',
// TODO: This error shouldn't be any
props<{ error: any }>(),
props<{ error: CreateReservationError }>(),
);

export const deleteReservation = createAction(
Expand All @@ -38,6 +45,5 @@ export const deletedReservation = createAction(

export const errorDeletingReservation = createAction(
'[Reservations] Error Deleting Reservation',
// TODO: This error shouldn't be any
props<{ error: any }>(),
props<{ error: DeleteReservationError }>(),
);
2 changes: 1 addition & 1 deletion app/src/app/state/reservations/reservations.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ofType,
ROOT_EFFECTS_INIT,
} from '@ngrx/effects';
import { combineLatest, map, mergeMap, switchMap, tap } from 'rxjs';
import { combineLatest, map, mergeMap, switchMap } from 'rxjs';
import { loginCompleted } from '../auth/auth.actions';
import { UserReservationService } from '../../services/reservations/user-reservation.service';
import {
Expand Down

0 comments on commit 775b822

Please sign in to comment.