GitLab steht wegen Wartungsarbeiten am Montag, den 10. Mai, zwischen 17:00 und 19:00 Uhr nicht zur Verfügung.

nickname-select.component.ts 4.5 KB
Newer Older
1
import { Component, Inject, OnDestroy, OnInit, PLATFORM_ID, SecurityContext } from '@angular/core';
2
import { DomSanitizer } from '@angular/platform-browser';
3
import { Router } from '@angular/router';
4
import { SimpleMQ } from 'ng2-simple-mq';
5 6 7 8
import { MemberEntity } from '../../../lib/entities/member/MemberEntity';
import { StorageKey } from '../../../lib/enums/enums';
import { MessageProtocol, StatusProtocol } from '../../../lib/enums/Message';
import { IMessage } from '../../../lib/interfaces/communication/IMessage';
9
import { MemberApiService } from '../../../service/api/member/member-api.service';
10
import { AttendeeService } from '../../../service/attendee/attendee.service';
11
import { CustomMarkdownService } from '../../../service/custom-markdown/custom-markdown.service';
12
import { FooterBarService } from '../../../service/footer-bar/footer-bar.service';
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
13
import { QuizService } from '../../../service/quiz/quiz.service';
14
import { UserService } from '../../../service/user/user.service';
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
15 16 17 18

@Component({
  selector: 'app-nickname-select',
  templateUrl: './nickname-select.component.html',
19
  styleUrls: ['./nickname-select.component.scss'],
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
20 21
})
export class NicknameSelectComponent implements OnInit, OnDestroy {
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
22
  public static TYPE = 'NicknameSelectComponent';
23
  public isLoggingIn: string;
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
24

25 26
  private _nicks: Array<string> = [];

Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
27 28 29 30
  get nicks(): Array<string> {
    return this._nicks;
  }

31
  private _messageSubscriptions: Array<string> = [];
32

Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
33
  constructor(
34
    @Inject(PLATFORM_ID) private platformId: Object,
35
    private sanitizer: DomSanitizer,
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
36 37 38
    private footerBarService: FooterBarService,
    private router: Router,
    private attendeeService: AttendeeService,
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
39
    private userService: UserService,
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
40
    private quizService: QuizService,
41 42 43
    private memberApiService: MemberApiService,
    private messageQueue: SimpleMQ,
    private customMarkdownService: CustomMarkdownService,
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
44 45 46
  ) {

    this.footerBarService.TYPE_REFERENCE = NicknameSelectComponent.TYPE;
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
47
    footerBarService.replaceFooterElements([
48
      this.footerBarService.footerElemBack,
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
49 50 51 52 53 54
    ]);
    this.footerBarService.footerElemBack.onClickCallback = () => {
      this.router.navigate(['/']);
    };
  }

Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
55
  public async joinQuiz(nickname: any): Promise<void> {
56 57 58 59 60
    if (this.isLoggingIn) {
      return;
    }

    this.isLoggingIn = nickname;
61 62
    if (nickname.changingThisBreaksApplicationSecurity) {
      nickname = nickname.changingThisBreaksApplicationSecurity.match(/:[\w\+\-]+:/g)[0];
63
    }
64
    nickname = nickname.toString();
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
65 66 67

    const token = await this.memberApiService.generateMemberToken(nickname, this.quizService.quiz.name).toPromise();

68
    sessionStorage.setItem(StorageKey.QuizToken, token);
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
69

70
    const promise = new Promise(async (resolve, reject) => {
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
71 72 73
      this.memberApiService.putMember(new MemberEntity({
        currentQuizName: this.quizService.quiz.name,
        name: nickname,
74
        groupName: sessionStorage.getItem(StorageKey.CurrentMemberGroupName),
75
        ticket: this.userService.casTicket,
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
76 77
      })).subscribe((data: IMessage) => {
        if (data.status === StatusProtocol.Success && data.step === MessageProtocol.Added) {
78 79 80 81
          this._messageSubscriptions.push(this.messageQueue.subscribe(MessageProtocol.Added, payload => {
            this.attendeeService.addMember(payload.member);
            resolve();
          }));
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
82 83 84 85 86 87 88 89
        } else {
          reject();
        }
      }, () => {
        reject();
      });
    });
    promise.then(() => {
90
      this.attendeeService.ownNick = nickname;
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
91 92
      this.router.navigate(['/quiz', 'flow', 'lobby']);
    }, (err) => {
93
      console.log('NicknameSelectComponent: PutMember failed', err);
94
      this.router.navigate(['/']);
95
      this.isLoggingIn = null;
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
96 97 98
    });
  }

99
  public sanitizeHTML(value: string): string {
100
    return this.sanitizer.sanitize(SecurityContext.HTML, `${value}`);
101 102
  }

103
  public parseAvailableNick(name: string): string {
104
    return name.match(/:[\w\+\-]+:/g) ? this.sanitizeHTML(this.customMarkdownService.parseGithubFlavoredMarkdown(name)) : name;
105 106
  }

107
  public ngOnInit(): void {
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
108
    if (this.attendeeService.ownNick) {
109 110
      this.router.navigate(['/']);
    }
111

112 113 114 115
    this.quizService.loadDataToPlay(sessionStorage.getItem(StorageKey.CurrentQuizName)).then(() => {
      this.memberApiService.getAvailableNames(this.quizService.quiz.name).subscribe(data => {
        this._nicks = this._nicks.concat(data);
      });
116
    });
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
117 118
  }

119
  public ngOnDestroy(): void {
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
120
    this.footerBarService.footerElemBack.restoreClickCallback();
121
    this._messageSubscriptions.forEach(sub => this.messageQueue.unsubscribe(sub));
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
122 123
  }
}