Skip to content

Commit

Permalink
feat: translate all error messages
Browse files Browse the repository at this point in the history
Pablito2020 committed Nov 21, 2023
1 parent 710076f commit ce40bb1
Showing 16 changed files with 271 additions and 167 deletions.
2 changes: 1 addition & 1 deletion app/src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -62,7 +62,7 @@
<ng-container *ngIf="isMapLoading$ | async as loading">
<ion-loading
isOpen="{{ loading }}"
message="{{ 'TODO_MAP_LOADING' | translate }}"
message="{{ 'MAP.LOADING' | translate }}"
></ion-loading>
</ng-container>
<ng-container *ngIf="hasConnectionError$ | async as error">
4 changes: 3 additions & 1 deletion app/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@ import { clearMessage } from './state/messages/message.actions';
import { MinorError } from './state/errors/error.reducer';
import { getBottomItems, getTopItems } from './state/auth/auth.selectors';
import { isMapLoading } from './state/map/map.selectors';
import { TranslateService } from '@ngx-translate/core';

@Component({
selector: 'app-root',
@@ -50,6 +51,7 @@ export class AppComponent implements OnInit {
constructor(
private store: Store<AppState>,
private toast: ToastController,
private translateService: TranslateService,
) {
this.minorErrors$
.pipe(
@@ -87,7 +89,7 @@ export class AppComponent implements OnInit {

private async showToast(message: Message, options: ToastOptions) {
const element = await this.toast.create({
message: message.message,
message: this.translateService.instant(message.message, message.props),
...options,
});
await element.present();
4 changes: 3 additions & 1 deletion app/src/app/pages/profile/profile-get/profile.page.ts
Original file line number Diff line number Diff line change
@@ -87,7 +87,9 @@ export class ProfilePage implements OnInit {
private async handleUnauthorizedError() {
await this.showError(async () => {
await this.showErrorMessage(
this.translateService.instant('UNAUTHORIZED_ERROR.MESSAGE'),
this.translateService.instant(
'MINOR_ERRORS.NOT_ALLOWED_OPERATION_FOR_USER',
),
).then();
});
}
Original file line number Diff line number Diff line change
@@ -100,7 +100,9 @@ export class ProfileUpdatePage implements OnInit {
private async handleUnauthorizedError() {
await this.showError(async () => {
await this.showErrorMessage(
this.translateService.instant('UNAUTHORIZED_ERROR.MESSAGE'),
this.translateService.instant(
'MINOR_ERRORS.NOT_ALLOWED_OPERATION_FOR_USER',
),
).then();
});
}
66 changes: 47 additions & 19 deletions app/src/app/pages/reservations/reservations.store.ts
Original file line number Diff line number Diff line change
@@ -22,6 +22,10 @@ import {
} from '../../schemas/reservations/create-reservation';
import { fromPromise } from 'rxjs/internal/observable/innerFrom';
import { match } from 'ts-pattern';
import {
CorrectDeleteReservation,
ErrorDeleteReservation,
} from '../../schemas/reservations/delete-reservation';

export interface ReservationsState {
reservations: RefugeReservationsRelations;
@@ -72,7 +76,7 @@ export class ReservationsComponentStore extends ComponentStore<ReservationsState
return this.getReservations(userId, (reservations) => {
this.store.dispatch(
showMessages({
message: 'TODO: Reservas cargadas correctamente',
message: 'RESERVATIONS.FETCH_OPERATION.CORRECT',
}),
);
});
@@ -130,20 +134,7 @@ export class ReservationsComponentStore extends ComponentStore<ReservationsState
this.patchState({ isLoading: true });
return this.reservationService.deleteReservation(reservationId).pipe(
tapResponse(
(reservations) => {
this.store.dispatch(
showMessages({ message: 'TODO: Reserva eliminada correctamente' }),
);
this.patchState((state) => {
return {
reservations: this.removeReservationWithId(
reservationId,
state.reservations,
),
isLoading: false,
};
});
},
(reservation) => this.onDeleteReservationResponse(reservation),
() => {
this.patchState({ isLoading: false });
this.store.dispatch(
@@ -154,6 +145,30 @@ export class ReservationsComponentStore extends ComponentStore<ReservationsState
);
}

private onDeleteReservationResponse(
reservation: CorrectDeleteReservation | ErrorDeleteReservation,
) {
match(reservation)
.with({ status: 'ok' }, (res) => {
this.store.dispatch(
showMessages({ message: 'RESERVATIONS.DELETE_OPERATION.CORRECT' }),
);
this.patchState((state) => {
return {
reservations: this.removeReservationWithId(
res.reservation.id,
state.reservations,
),
isLoading: false,
};
});
})
.with({ status: 'error' }, (err) => {
this.patchState({ isLoading: false });
this.store.dispatch(minorError({ error: err.error }));
});
}

readonly createReservation = this.effect(
(reservation: Observable<ReservationWithoutUserId>) =>
reservation.pipe(
@@ -193,7 +208,8 @@ export class ReservationsComponentStore extends ComponentStore<ReservationsState
.with({ status: 'ok' }, (res) => {
this.store.dispatch(
showMessages({
message: 'TODO: Reserva creada correctamente',
message: 'RESERVATIONS.CREATE_OPERATION.CORRECT',
props: { day: reservation.night.day },
}),
);
})
@@ -217,13 +233,25 @@ export class ReservationsComponentStore extends ComponentStore<ReservationsState
private handleCreateError(error: CreateReservationError) {
match(error)
.with(CreateReservationDataError.NTP_SERVER_IS_DOWN, () =>
this.store.dispatch(customMinorError({ error })),
this.store.dispatch(
customMinorError({
error: 'RESERVATIONS.CREATE_OPERATION.SERVER_ERROR',
}),
),
)
.with(CreateReservationDataError.INVALID_DATE, () =>
this.store.dispatch(customMinorError({ error })),
this.store.dispatch(
customMinorError({
error: 'RESERVATIONS.CREATE_OPERATION.INVALID_DATE',
}),
),
)
.with(CreateReservationDataError.REFUGE_OR_USER_NOT_FOUND, () =>
this.store.dispatch(customMinorError({ error })),
this.store.dispatch(
customMinorError({
error: 'RESERVATIONS.CREATE_OPERATION.REFUGE_OR_USER_NOT_FOUND',
}),
),
)
.otherwise((error) => this.store.dispatch(minorError({ error })));
}
2 changes: 2 additions & 0 deletions app/src/app/state/auth/auth.actions.ts
Original file line number Diff line number Diff line change
@@ -6,6 +6,8 @@ export const login = createAction(
props<{ token: Token }>(),
);

export const loginFailed = createAction('[Auth] Login Failed');

export const loginCompleted = createAction(
'[Auth] Login Completed',
props<{ userId: string }>(),
14 changes: 9 additions & 5 deletions app/src/app/state/auth/auth.effects.ts
Original file line number Diff line number Diff line change
@@ -11,10 +11,13 @@ import {
logOutCompleted,
logOutRequest,
login,
loginFailed,
} from './auth.actions';
import { combineLatest, map, switchMap, tap } from 'rxjs';
import { fromPromise } from 'rxjs/internal/observable/innerFrom';
import { Router } from '@angular/router';
import { fatalError } from '../errors/error.actions';
import { ServerErrors } from 'src/app/schemas/errors/server';

@Injectable()
export class AuthEffects {
@@ -45,11 +48,12 @@ export class AuthEffects {
fromPromise(this.authService.authenticate(action.token)),
),
switchMap(() => this.authService.getUserId()),
map((userId) => {
if (userId) return loginCompleted({ userId });
return loginCompleted({
userId: 'TODO: ERROR SAVING HERE',
});
switchMap((userId) => {
if (userId) return [loginCompleted({ userId })];
return [
loginFailed(),
fatalError({ error: ServerErrors.UNKNOWN_ERROR }),
];
}),
),
);
4 changes: 2 additions & 2 deletions app/src/app/state/auth/auth.reducer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createReducer, on } from '@ngrx/store';
import { loginCompleted, logOutRequest } from './auth.actions';
import { loginCompleted, logOutCompleted } from './auth.actions';

export type AuthState = {
userId?: string;
@@ -12,5 +12,5 @@ export const authReducer = createReducer(
on(loginCompleted, (state, action) => ({
userId: action.userId,
})),
on(logOutRequest, (state) => ({})),
on(logOutCompleted, (state) => ({})),
);
29 changes: 27 additions & 2 deletions app/src/app/state/errors/error.effects.ts
Original file line number Diff line number Diff line change
@@ -43,11 +43,36 @@ export class ErrorEffects {
createCustomMinorErrorFromNormalError$ = createEffect(() =>
this.actions$.pipe(
ofType(minorError),
// TODO: here translate the error!
map((error) => customMinorError({ error: error.error })),
map((error) =>
customMinorError({ error: this.getStringFor(error.error) }),
),
),
);

private getStringFor(error: AllErrors): string {
return match(error)
.with(ServerErrors.UNKNOWN_ERROR, () => 'MINOR_ERRORS.UNKNOWN')
.with(
ServerErrors.INCORRECT_DATA_FORMAT_OF_CLIENT,
() => 'MINOR_ERRORS.INCORRECT_DATA_FORMAT_OF_CLIENT',
)
.with(
ServerErrors.INCORRECT_DATA_FORMAT_OF_SERVER,
() => 'MINOR_ERRORS.INCORRECT_DATA_FORMAT_OF_SERVER',
)
.with(ResourceErrors.NOT_FOUND, () => 'MINOR_ERRORS.NOT_FOUND')
.with(
PermissionsErrors.NOT_AUTHENTICATED,
() => 'MINOR_ERRORS.NOT_AUTHENTICATED',
)
.with(
PermissionsErrors.NOT_ALLOWED_OPERATION_FOR_USER,
() => 'MINOR_ERRORS.NOT_ALLOWED_OPERATION_FOR_USER',
)
.with(DeviceErrors.NOT_CONNECTED, () => 'MINOR_ERRORS.NOT_CONNECTED')
.exhaustive();
}

private redirect(error: AllErrors) {
match(error)
.with(ServerErrors.UNKNOWN_ERROR, () =>
17 changes: 9 additions & 8 deletions app/src/app/state/language/language.effects.ts
Original file line number Diff line number Diff line change
@@ -18,6 +18,8 @@ import {
import { fromPromise } from 'rxjs/internal/observable/innerFrom';
import { LanguageStorage } from '../../services/translate/language-settings.service';
import { DeviceLanguageService } from '../../services/translate/device-language.service';
import { showMessages } from '../messages/message.actions';
import { customMinorError } from '../errors/error.actions';

const LANGUAGES_SUPPORTED = ['en', 'es', 'ca'];

@@ -106,14 +108,13 @@ export class LanguageEffects {
map(() =>
changeLanguageCorrect({ languageCode: createData.languageCode }),
),
catchError((error) =>
of(
changeLanguageError({
error: error,
languageCode: createData.languageCode,
}),
),
),
catchError((error) => [
changeLanguageError({
error: error,
languageCode: createData.languageCode,
}),
customMinorError({ error: 'SETTINGS.ERROR_CHANGING_LANGUAGE' }),
]),
),
),
),
2 changes: 1 addition & 1 deletion app/src/app/state/messages/message.actions.ts
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ import { createAction, props } from '@ngrx/store';

export const showMessages = createAction(
'[Messages] Show Messages',
props<{ message: string }>(),
props<{ message: string; props?: any }>(),
);

export const clearMessage = createAction(
11 changes: 8 additions & 3 deletions app/src/app/state/messages/message.reducer.ts
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ import { clearMessage, showMessages } from './message.actions';

export type Message = {
message: string;
props?: any;
id: number;
};

@@ -17,9 +18,13 @@ export const messageReducer = createReducer(
counter: 0,
},
on(showMessages, (state, action) => ({
message: [{ id: state.counter + 1, message: action.message }].concat(
state.message,
),
message: [
{
id: state.counter + 1,
message: action.message,
props: action.props,
} as Message,
].concat(state.message),
counter: state.counter + 1,
})),
on(clearMessage, (state, action) => ({
Loading

0 comments on commit ce40bb1

Please sign in to comment.