Combine user add- and edit-form

pull/1/head
Florian Hartwich 2017-05-14 16:35:44 +02:00
parent b196cce61f
commit 78aa2cdfe8
9 changed files with 35 additions and 103 deletions

View File

@ -91,8 +91,11 @@ users.route('/')
return next(err); return next(err);
} }
res.status(codes.created); res.status(codes.created);
res.locals.items = user; getExtendedUser(user, next, (extUser) => {
next(); res.locals.items = extUser;
res.locals.processed = true;
return next();
})
}); });
}) })

View File

@ -10,17 +10,15 @@ import {ArmyService} from "../services/army-service/army.service";
}) })
export class ArmyComponent { export class ArmyComponent {
army: Army = {NATO: {squads: [], memberCount: 0}, CSAT: {squads: [], memberCount:0}}; army: Army = {NATO: {squads: [], memberCount: 0}, CSAT: {squads: [], memberCount: 0}};
constructor(private armyService: ArmyService) { constructor(private armyService: ArmyService) {
} }
ngOnInit() { ngOnInit() {
this.armyService.getArmy() this.armyService.getArmy()
.subscribe(army => { .subscribe(army => {
this.army = army; this.army = army;
console.log(army)
}); });
}; };
} }

View File

@ -1,27 +0,0 @@
<form #form="ngForm" class="overview">
<h3>Neuen Teilnehmer hinzufügen</h3>
<div class="form-group">
<label for="title">Name</label>
<input type="text" class="form-control"
[(ngModel)]="user.username"
name="title"
id="title"
required maxlength="50"/>
<show-error text="Name" path="title"></show-error>
</div>
<button id="cancel"
(click)="cancel()"
class="btn btn-default">
Abbrechen
</button>
<button id="save"
(click)="saveUser()"
class="btn btn-default"
[disabled]="!form.valid">
Teilnehmer hinzufügen
</button>
</form>

View File

@ -1,50 +0,0 @@
import {Component, ViewChild} from "@angular/core";
import {ActivatedRoute, Router} from "@angular/router";
import {NgForm} from "@angular/forms";
import {User} from "../../models/model-interfaces";
import {UserService} from "../../services/user-service/user.service";
@Component({
templateUrl: './new-user.component.html',
styleUrls: ['./new-user.component.css', '../../style/new-entry-form.css']
})
export class CreateUserComponent {
user: User = {};
saved = false;
@ViewChild(NgForm) form: NgForm;
constructor(private route: ActivatedRoute,
private router: Router,
private userService: UserService) {
}
ngOnInit() {
}
saveUser() {
this.userService.submitUser(this.user)
.subscribe(user => {
this.saved = true;
this.router.navigate(['../overview', user._id], {relativeTo: this.route});
})
}
cancel() {
//this.location.back();
this.router.navigate(['/cc-users']);
return false;
}
canDeactivate(): boolean {
if (this.saved || !this.form.dirty) {
return true;
}
return window.confirm(`Ihr Formular besitzt ungespeicherte Änderungen, möchten Sie die Seite wirklich verlassen?`);
}
}

View File

@ -48,7 +48,8 @@ export class UserListComponent implements OnInit {
} }
openNewUserForm() { openNewUserForm() {
this.router.navigate([{outlets: {'right': ['new']}}], {relativeTo: this.route}); this.selectedUserId = null;
this.router.navigate([{outlets: {'right': ['overview']}}], {relativeTo: this.route});
} }
selectUser(userId: string) { selectUser(userId: string) {

View File

@ -1,5 +1,6 @@
<form #form="ngForm" class="overview"> <form #form="ngForm" class="overview">
<h3>Teilnehmer editieren</h3> <h3 *ngIf="user._id">Teilnehmer editieren</h3>
<h3 *ngIf="!user._id">Neuen Teilnehmer hinzufügen</h3>
<div class="form-group"> <div class="form-group">
<label for="title">Name</label> <label for="title">Name</label>

View File

@ -1,6 +1,5 @@
import {Component, ViewChild} from "@angular/core"; import {Component, ViewChild} from "@angular/core";
import {ActivatedRoute, Router} from "@angular/router"; import {ActivatedRoute, Router} from "@angular/router";
import * as model from "../../models/model-interfaces";
import {Rank, Squad, User} from "../../models/model-interfaces"; import {Rank, Squad, User} from "../../models/model-interfaces";
import {UserService} from "../../services/user-service/user.service"; import {UserService} from "../../services/user-service/user.service";
import {SquadService} from "../../services/squad-service/squad.service"; import {SquadService} from "../../services/squad-service/squad.service";
@ -23,7 +22,7 @@ export class UserOverviewComponent {
ranksDisplay = 'none'; ranksDisplay = 'none';
user: User = {squad: {}}; user: User = {username: '', squad: '0', rank: {level: 0}};
squads: Squad[] = []; squads: Squad[] = [];
@ -45,7 +44,6 @@ export class UserOverviewComponent {
.filter(id => id != undefined) .filter(id => id != undefined)
.flatMap(id => this.userService.getUser(id)) .flatMap(id => this.userService.getUser(id))
.subscribe(user => { .subscribe(user => {
console.log(user.squad)
if (!user.squad) { if (!user.squad) {
user.squad = "0"; user.squad = "0";
this.ranksDisplay = 'none'; this.ranksDisplay = 'none';
@ -90,6 +88,8 @@ export class UserOverviewComponent {
if (this.user.squad._id !== '0') { if (this.user.squad._id !== '0') {
updateObject.squadId = this.user.squad._id updateObject.squadId = this.user.squad._id
} }
if (this.user._id) {
this.userService.updateUser(updateObject) this.userService.updateUser(updateObject)
.subscribe(user => { .subscribe(user => {
if (!user.squad) { if (!user.squad) {
@ -101,11 +101,18 @@ export class UserOverviewComponent {
this.showSuccessLabel = false; this.showSuccessLabel = false;
}, 2000) }, 2000)
}) })
} else {
this.userService.submitUser(updateObject)
.subscribe(user => {
this.router.navigate(['..'], {relativeTo: this.route});
return true;
})
}
} }
cancel() { cancel() {
this.router.navigate(['../..'], {relativeTo: this.route}); this.router.navigate([this.user._id ? '../..' : '..'], {relativeTo: this.route});
return false; return false;
} }

View File

@ -2,7 +2,6 @@ import {Routes} from "@angular/router";
import {UsersComponent} from "./users.component"; import {UsersComponent} from "./users.component";
import {UserOverviewComponent} from "./user-overview/user-overview.component"; import {UserOverviewComponent} from "./user-overview/user-overview.component";
import {UserListComponent} from "./user-list/user-list.component"; import {UserListComponent} from "./user-list/user-list.component";
import {CreateUserComponent} from "./new-user/new-user.component";
import {UserAwardComponent} from "./user-award/user-award.component"; import {UserAwardComponent} from "./user-award/user-award.component";
export const usersRoutes: Routes = [{ export const usersRoutes: Routes = [{
@ -15,8 +14,8 @@ export const usersRoutes: Routes = [{
] ]
}, },
{ {
path: 'new', path: 'overview',
component: CreateUserComponent, component: UserOverviewComponent,
outlet: 'right' outlet: 'right'
}, },
{ {
@ -31,4 +30,4 @@ export const usersRoutes: Routes = [{
} }
]; ];
export const usersRoutingComponents = [UsersComponent, UserListComponent, UserOverviewComponent, CreateUserComponent, UserAwardComponent]; export const usersRoutingComponents = [UsersComponent, UserListComponent, UserOverviewComponent, UserAwardComponent];