Skip to content
This repository has been archived by the owner on Sep 3, 2021. It is now read-only.

Commit

Permalink
#3 Error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ollilinde committed Aug 13, 2019
1 parent bbbaa3f commit 7c9f5a9
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/api/ormconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"type": "mysql",
"host": "localhost",
"host": "mysql",
"port": 3306,
"username": "root",
"password": "test",
Expand Down
13 changes: 9 additions & 4 deletions src/api/src/teams/teams.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable, Inject } from '@nestjs/common';
import { Injectable, Inject, BadRequestException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Team } from './team.entity';
Expand Down Expand Up @@ -52,8 +52,13 @@ export class TeamsService {
where: [{ email: email }],
});

userToAdd.team = actionUser.team;

return this.usersRepository.save(userToAdd);
if (!!userToAdd && !userToAdd.team) {
userToAdd.team = actionUser.team;
return this.usersRepository.save(userToAdd);
} else {
throw new BadRequestException(
"user isn't registered or already member of a team",
);
}
}
}
2 changes: 1 addition & 1 deletion src/frontend/src/app/me/effects/me.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class MeEffects {
map(result => {
return new AddEmailToTeamSucceededAction(result);
}),
catchError(err => of(new AddEmailToTeamFailedAction(err.message)))
catchError(err => of(new AddEmailToTeamFailedAction(err.error)))
)
)
);
Expand Down
8 changes: 7 additions & 1 deletion src/frontend/src/app/me/overview/overview.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ <h3>Teammitglied hinzufügen</h3>
<label for="addEmail">E-Mail</label>
<input id="addEmail" type="email" formControlName="email" required />
</fieldset>
<input type="submit" class="btn" [disabled]="!addMemberForm.valid" value="Hinzufügen" />
<input type="submit" class="btn" [disabled]="!addMemberForm.valid || (addEmailToTeamInProgress$ | async)"
value="Hinzufügen" />
<p *ngIf="(addEmailToTeamError$ | async)?.statusCode === 400" class="error">Das Mitglied ist nicht registriert oder
spielt bereits in einem Team.</p>
<p *ngIf="(addEmailToTeamError$ | async) && (addEmailToTeamError$ | async).statusCode !== 400" class="error">Ein
unbekannter Fehler ist aufgetreten:
{{ (addEmailToTeamError$ | async).message }}</p>
</form>
<hr />
<h3>Du hast gespielt?</h3>
Expand Down
5 changes: 5 additions & 0 deletions src/frontend/src/app/me/overview/overview.component.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@import '../../../scss/settings';

p.error {
color: $cl-error;
}
14 changes: 13 additions & 1 deletion src/frontend/src/app/me/overview/overview.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { Component, OnInit } from '@angular/core';
import { Store } from '@ngrx/store';
import { State, selectAuthUser, selectMeTeams } from 'src/app/reducers';
import {
State,
selectAuthUser,
selectMeTeams,
selectAddByEmailToTeamInProgress,
selectAddByEmailToTeamError,
} from 'src/app/reducers';
import { LoadTeamsAction, AddEmailToTeamAction } from '../actions/team.actions';
import { FormGroup, FormControl, Validators } from '@angular/forms';

Expand All @@ -12,6 +18,8 @@ import { FormGroup, FormControl, Validators } from '@angular/forms';
export class OverviewComponent implements OnInit {
user$;
teams$;
addEmailToTeamInProgress$;
addEmailToTeamError$;

addMemberForm = new FormGroup({
email: new FormControl('', [Validators.email]),
Expand All @@ -22,6 +30,10 @@ export class OverviewComponent implements OnInit {
ngOnInit() {
this.user$ = this._store.select(selectAuthUser);
this.teams$ = this._store.select(selectMeTeams);
this.addEmailToTeamInProgress$ = this._store.select(
selectAddByEmailToTeamInProgress
);
this.addEmailToTeamError$ = this._store.select(selectAddByEmailToTeamError);

this._store.dispatch(new LoadTeamsAction());
}
Expand Down
10 changes: 10 additions & 0 deletions src/frontend/src/app/reducers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,13 @@ export const selectSaveResultsError = createSelector(
selectResults,
(state: ResultsState) => state.saveResultsError
);

export const selectAddByEmailToTeamInProgress = createSelector(
selectMe,
(state: MeState) => state.addByEmailToTeamInProgress
);

export const selectAddByEmailToTeamError = createSelector(
selectMe,
(state: MeState) => state.addByEmailToTeamError
);

0 comments on commit 7c9f5a9

Please sign in to comment.