opt-cc/static/src/app/users/user-list/user-list.component.ts

112 lines
3.3 KiB
TypeScript
Raw Normal View History

2018-03-07 11:56:50 +01:00
import {Component, OnInit} from '@angular/core';
import {Location} from '@angular/common';
2017-05-10 11:04:06 +02:00
2018-03-07 11:56:50 +01:00
import {FormControl} from '@angular/forms';
import {ActivatedRoute, Router} from '@angular/router';
import {Observable} from 'rxjs/Observable';
import {UserService} from '../../services/army-management/user.service';
import {User} from '../../models/model-interfaces';
import {ADD, LOAD} from '../../services/stores/user.store';
import {Fraction} from '../../utils/fraction.enum';
import {MatButtonToggleGroup} from '@angular/material';
import {UIHelpers} from '../../utils/global.helpers';
2017-05-10 11:04:06 +02:00
@Component({
selector: 'squad-list',
templateUrl: './user-list.component.html',
2017-10-06 20:11:18 +02:00
styleUrls: ['./user-list.component.css', '../../style/select-list.css']
2017-05-10 11:04:06 +02:00
})
export class UserListComponent implements OnInit {
selectedUserId: string | number = null;
users$: Observable<User[]>;
searchTerm = new FormControl();
2018-07-05 21:57:47 +02:00
radioModel = '';
2017-05-10 11:04:06 +02:00
2017-10-14 15:26:05 +02:00
throttle = 300;
scrollDistance = 1;
offset = 0;
limit = 20;
2018-02-26 09:04:27 +01:00
2017-11-08 19:40:51 +01:00
readonly fraction = Fraction;
2017-10-14 15:26:05 +02:00
2017-05-10 11:04:06 +02:00
constructor(private userService: UserService,
private router: Router,
private route: ActivatedRoute,
private location: Location) {
}
ngOnInit() {
this.users$ = this.userService.users$;
const paramsStream = this.route.queryParams
2018-02-26 09:04:27 +01:00
.map(params => decodeURI(params['query'] || ''))
.do(query => this.searchTerm.setValue(query));
2017-05-10 11:04:06 +02:00
const searchTermStream = this.searchTerm.valueChanges
2018-02-26 09:04:27 +01:00
.debounceTime(400)
.do(query => this.adjustBrowserUrl(query));
2017-05-10 11:04:06 +02:00
Observable.merge(paramsStream, searchTermStream)
2018-02-26 09:04:27 +01:00
.distinctUntilChanged()
.switchMap(query => this.filterUsers())
.subscribe();
2017-05-10 11:04:06 +02:00
}
openNewUserForm() {
2017-05-14 16:35:44 +02:00
this.selectedUserId = null;
this.router.navigate([{outlets: {'right': ['new']}}], {relativeTo: this.route});
2017-05-10 11:04:06 +02:00
}
2017-05-13 14:57:40 +02:00
selectUser(userId: string) {
2017-05-10 11:04:06 +02:00
this.selectedUserId = userId;
this.router.navigate([{outlets: {'right': ['edit', userId]}}], {relativeTo: this.route});
2017-05-10 11:04:06 +02:00
}
2017-05-15 17:18:04 +02:00
awardUser(userId) {
2017-05-13 14:57:40 +02:00
this.selectedUserId = userId;
this.router.navigate([{outlets: {'right': ['award', userId]}}], {relativeTo: this.route});
}
deleteUser(user: User) {
if (confirm('Soll der Teilnehmer "' + user.username + '" wirklich gelöscht werden?')) {
this.userService.deleteUser(user)
2018-02-26 09:04:27 +01:00
.subscribe((res) => {
2018-03-07 11:56:50 +01:00
});
}
}
2018-07-05 21:57:47 +02:00
filterUsers(action?, group?: MatButtonToggleGroup) {
2017-10-14 17:47:55 +02:00
if (!action || action === LOAD) {
2017-10-14 15:26:05 +02:00
this.offset = 0;
this.limit = 20;
}
this.radioModel = UIHelpers.toggleReleaseButton(this.radioModel, group);
2017-10-14 15:26:05 +02:00
return this.users$ = this.userService.findUsers(this.searchTerm.value, this.radioModel,
null, this.limit, this.offset, action);
}
onScrollDown() {
if (this.offset + this.limit > this.userService.totalCount) {
this.limit = this.userService.totalCount - this.offset;
}
2018-03-07 11:56:50 +01:00
if (this.limit !== 0) {
2017-10-14 15:26:05 +02:00
this.offset += this.limit;
this.filterUsers(ADD);
}
2017-05-10 11:04:06 +02:00
}
adjustBrowserUrl(queryString = '') {
const absoluteUrl = this.location.path().split('?')[0];
2018-03-07 11:56:50 +01:00
const queryPart = queryString !== '' ? 'query=${queryString}' : '';
2017-05-10 11:04:06 +02:00
this.location.replaceState(absoluteUrl, queryPart);
}
}