1 | import {Component, OnInit} from '@angular/core';
|
---|
2 | import {QuestionService} from "../question.service";
|
---|
3 | import {QuestionInterface} from "../QuestionInterface";
|
---|
4 | import {ActivatedRoute, Router} from "@angular/router";
|
---|
5 | import {AnswerInterface} from "../AnswerInterface";
|
---|
6 | import {LoginService} from "../login.service";
|
---|
7 | import {ActiveUserInterface} from "../ActiveUserInterface";
|
---|
8 |
|
---|
9 | @Component({
|
---|
10 | selector: 'app-question-details',
|
---|
11 | templateUrl: './question-details.component.html',
|
---|
12 | styleUrls: ['./question-details.component.css']
|
---|
13 | })
|
---|
14 | export class QuestionDetailsComponent implements OnInit {
|
---|
15 | question: QuestionInterface | undefined
|
---|
16 | answers: AnswerInterface[] = []
|
---|
17 | answersLikes = new Map<number, number>()
|
---|
18 | activeUser: ActiveUserInterface | undefined;
|
---|
19 |
|
---|
20 | constructor(private questionService: QuestionService,
|
---|
21 | private route: ActivatedRoute,
|
---|
22 | private loginService: LoginService,
|
---|
23 | private router: Router) {
|
---|
24 | }
|
---|
25 |
|
---|
26 | ngOnInit(): void {
|
---|
27 | const id = Number(this.route.snapshot.paramMap.get('id'));
|
---|
28 | this.findQuestionById(id);
|
---|
29 | this.activeUser = this.loginService.activeUser;
|
---|
30 | if (!this.activeUser) {
|
---|
31 | this.router.navigate(['/'])
|
---|
32 | }
|
---|
33 | }
|
---|
34 |
|
---|
35 | findQuestionById(id: number) {
|
---|
36 | this.questionService.findQuestionById(id)
|
---|
37 | .subscribe(resp => {
|
---|
38 | this.question = resp;
|
---|
39 | this.questionService.findAnswersForQuestionWithId(id)
|
---|
40 | .subscribe(resp => {
|
---|
41 | this.answers = resp;
|
---|
42 | this.findLikesForAnswers()
|
---|
43 | })
|
---|
44 | });
|
---|
45 | }
|
---|
46 |
|
---|
47 | findLikesForAnswers() {
|
---|
48 | this.answers.forEach(answer =>
|
---|
49 | this.questionService.findLikesForAnswer(answer.id)
|
---|
50 | .subscribe(number => this.answersLikes.set(answer.id, +number))
|
---|
51 | )
|
---|
52 | }
|
---|
53 |
|
---|
54 | likeAnswer(id: number) {
|
---|
55 | // @ts-ignore
|
---|
56 | this.questionService.likeAnswer(id, this.activeUser!!.username, this.activeUser!!.userType)
|
---|
57 | .subscribe(number => {
|
---|
58 | this.answersLikes.delete(id);
|
---|
59 | this.answersLikes.set(id, +number);
|
---|
60 | })
|
---|
61 | }
|
---|
62 |
|
---|
63 | unlikeAnswer(id: number) {
|
---|
64 | // @ts-ignore
|
---|
65 | this.questionService.unlikeAnswer(id, this.activeUser!!.username, this.activeUser!!.userType)
|
---|
66 | .subscribe(number => {
|
---|
67 | this.answersLikes.delete(id);
|
---|
68 | this.answersLikes.set(id, +number);
|
---|
69 | })
|
---|
70 | }
|
---|
71 |
|
---|
72 | }
|
---|