source: Sources/frontend/src/app/question-details/question-details.component.ts@ 0e4e3d1

Last change on this file since 0e4e3d1 was 8423429, checked in by AngelNasev <angel.nasev@…>, 15 months ago

Add backend and frontend projects

  • Property mode set to 100644
File size: 2.2 KB
Line 
1import {Component, OnInit} from '@angular/core';
2import {QuestionService} from "../question.service";
3import {QuestionInterface} from "../QuestionInterface";
4import {ActivatedRoute, Router} from "@angular/router";
5import {AnswerInterface} from "../AnswerInterface";
6import {LoginService} from "../login.service";
7import {ActiveUserInterface} from "../ActiveUserInterface";
8
9@Component({
10 selector: 'app-question-details',
11 templateUrl: './question-details.component.html',
12 styleUrls: ['./question-details.component.css']
13})
14export 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}
Note: See TracBrowser for help on using the repository browser.