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

100 lines
2.8 KiB
TypeScript
Raw Normal View History

import {Component} from '@angular/core';
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_ARRAY, LOAD} from '../../../services/stores/generic-store';
import {Fraction} from '../../../utils/fraction.enum';
import {MatButtonToggleGroup} from '@angular/material';
import {UIHelpers} from '../../../utils/global.helpers';
2018-10-04 11:48:22 +02:00
import {TranslateService} from '@ngx-translate/core';
2017-05-10 11:04:06 +02:00
@Component({
selector: 'cc-user-list',
2017-05-10 11:04:06 +02:00
templateUrl: './user-list.component.html',
styleUrls: ['./user-list.component.scss']
2017-05-10 11:04:06 +02:00
})
export class UserListComponent {
2017-05-10 11:04:06 +02:00
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
readonly throttle = 300;
2017-10-14 15:26:05 +02:00
readonly scrollDistance = 1;
2017-10-14 15:26:05 +02:00
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,
2018-10-04 11:48:22 +02:00
private route: ActivatedRoute,
private translate: TranslateService) {
2017-05-10 11:04:06 +02:00
this.users$ = this.userService.users$;
}
2017-05-10 11:04:06 +02:00
initObservable(observables: any) {
Observable.merge(observables.params as Observable<string>, observables.searchTerm)
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) {
this.translate.get('users.list.delete.confirm', {name: user.username}).subscribe((confirmQuestion) => {
2018-10-04 11:48:22 +02:00
if (confirm(confirmQuestion)) {
this.userService.deleteUser(user)
.subscribe((res) => {
});
}
});
}
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);
return this.users$ = this.userService.findUsers({
query: this.searchTerm.value,
fraction: this.radioModel
}, this.limit, this.offset, action);
2017-10-14 15:26:05 +02:00
}
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_ARRAY);
2017-10-14 15:26:05 +02:00
}
2017-05-10 11:04:06 +02:00
}
}