|
| 1 | +import {Component, OnInit} from '@angular/core'; |
| 2 | +import {FormControl} from '@angular/forms'; |
| 3 | +import {combineLatest, ReplaySubject, startWith, tap} from 'rxjs'; |
| 4 | +import {ActivatedRoute} from '@angular/router'; |
| 5 | +import {ApiService} from '../api.service'; |
| 6 | +import {MessageService} from '../message.service'; |
| 7 | +import {Card, User} from '../user.service'; |
| 8 | +import {StatusCode} from '../status-code'; |
| 9 | +import {V2Profile} from '../sega/chunithm/v2/model/V2Profile'; |
| 10 | +import {DisplayOngekiProfile} from '../sega/ongeki/model/OngekiProfile'; |
| 11 | +import {DisplayMaimai2Profile} from '../sega/maimai2/model/Maimai2Profile'; |
| 12 | +import {NgbModal} from '@ng-bootstrap/ng-bootstrap'; |
| 13 | +import {AuthenticationService} from '../auth/authentication.service'; |
| 14 | +import {TranslateService} from '@ngx-translate/core'; |
| 15 | + |
| 16 | +@Component({ |
| 17 | + selector: 'app-admin', |
| 18 | + templateUrl: './admin.component.html', |
| 19 | + styleUrls: ['./admin.component.css'] |
| 20 | +}) |
| 21 | +export class AdminComponent implements OnInit { |
| 22 | + private pageSubject = new ReplaySubject<number>(); |
| 23 | + currentPage = 1; |
| 24 | + totalElements = 0; |
| 25 | + loading = true; |
| 26 | + |
| 27 | + patternControl = new FormControl(''); |
| 28 | + |
| 29 | + userList: AdvancedUser[]; |
| 30 | + |
| 31 | + constructor( |
| 32 | + private api: ApiService, |
| 33 | + private messageService: MessageService, |
| 34 | + private route: ActivatedRoute, |
| 35 | + private translate: TranslateService, |
| 36 | + private authenticationService: AuthenticationService, |
| 37 | + protected modalService: NgbModal |
| 38 | + ) { |
| 39 | + |
| 40 | + } |
| 41 | + |
| 42 | + ngOnInit(): void { |
| 43 | + combineLatest([ |
| 44 | + this.pageSubject.pipe(startWith(0)), |
| 45 | + ]).subscribe(([page]) => { |
| 46 | + this.load(page, this.patternControl.value); |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + load(page: number, pattern: string) { |
| 51 | + this.currentPage = page + 1; |
| 52 | + const params: any = {page, size: 12}; |
| 53 | + if (pattern !== '') { |
| 54 | + params.pattern = pattern; |
| 55 | + } |
| 56 | + this.api.get('api/admin/advancedUserSearch', params).subscribe({ |
| 57 | + next: resp => { |
| 58 | + const statusCode: StatusCode = resp.status.code; |
| 59 | + if (statusCode === StatusCode.OK && resp.data) { |
| 60 | + this.userList = resp.data.content; |
| 61 | + this.totalElements = resp.data.totalElements; |
| 62 | + } |
| 63 | + else{ |
| 64 | + this.messageService.notice(resp.status.message, 'warning'); |
| 65 | + } |
| 66 | + this.loading = false; |
| 67 | + }, |
| 68 | + error: err => { |
| 69 | + this.messageService.notice(err.message, 'warning'); |
| 70 | + this.loading = false; |
| 71 | + } |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + search() { |
| 76 | + this.load(0, this.patternControl.value); |
| 77 | + } |
| 78 | + |
| 79 | + pageChanged(page: number) { |
| 80 | + this.pageSubject.next(page - 1); |
| 81 | + } |
| 82 | + |
| 83 | + loginAs(username: string){ |
| 84 | + this.authenticationService.loginAs(username) |
| 85 | + .subscribe( |
| 86 | + { |
| 87 | + next: (resp) => { |
| 88 | + if (resp?.status) { |
| 89 | + const statusCode: StatusCode = resp.status.code; |
| 90 | + if (statusCode === StatusCode.OK && resp.data) { |
| 91 | + this.messageService.notice(resp.status.message); |
| 92 | + location.reload(); |
| 93 | + } |
| 94 | + else if (statusCode === StatusCode.LOGIN_FAILED){ |
| 95 | + this.translate.get('SignInPage.LoginFailedMessage').subscribe((res: string) => { |
| 96 | + this.messageService.notice(res, 'danger'); |
| 97 | + }); |
| 98 | + } |
| 99 | + else{ |
| 100 | + this.messageService.notice(resp.status.message); |
| 101 | + } |
| 102 | + } |
| 103 | + }, |
| 104 | + error: (error) => { |
| 105 | + this.messageService.notice(error); |
| 106 | + console.warn('login fail', error); |
| 107 | + } |
| 108 | + } |
| 109 | + ); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +export interface AdvancedUser{ |
| 114 | + user: User; |
| 115 | + gameProfiles: GameProfile[]; |
| 116 | +} |
| 117 | + |
| 118 | +export interface GameProfile{ |
| 119 | + chusan: V2Profile; |
| 120 | + ongeki: DisplayOngekiProfile; |
| 121 | + maimai2: DisplayMaimai2Profile; |
| 122 | + card: Card; |
| 123 | +} |
0 commit comments