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);
}
res.status(codes.created);
res.locals.items = user;
next();
getExtendedUser(user, next, (extUser) => {
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 {
army: Army = {NATO: {squads: [], memberCount: 0}, CSAT: {squads: [], memberCount:0}};
army: Army = {NATO: {squads: [], memberCount: 0}, CSAT: {squads: [], memberCount: 0}};
constructor(private armyService: ArmyService) {
}
ngOnInit() {
this.armyService.getArmy()
.subscribe(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() {
this.router.navigate([{outlets: {'right': ['new']}}], {relativeTo: this.route});
this.selectedUserId = null;
this.router.navigate([{outlets: {'right': ['overview']}}], {relativeTo: this.route});
}
selectUser(userId: string) {

View File

@ -1,5 +1,6 @@
<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">
<label for="title">Name</label>

View File

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

View File

@ -2,7 +2,6 @@ import {Routes} from "@angular/router";
import {UsersComponent} from "./users.component";
import {UserOverviewComponent} from "./user-overview/user-overview.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";
export const usersRoutes: Routes = [{
@ -15,8 +14,8 @@ export const usersRoutes: Routes = [{
]
},
{
path: 'new',
component: CreateUserComponent,
path: 'overview',
component: UserOverviewComponent,
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];