This repository was archived by the owner on Nov 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread-idea.component.ts
More file actions
55 lines (47 loc) · 1.76 KB
/
read-idea.component.ts
File metadata and controls
55 lines (47 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { AuthService } from 'app/auth.service';
import { ModelService } from 'app/model.service';
import { Comment, Idea, Tag } from 'app/shared/types';
@Component({
selector: 'app-read-idea',
templateUrl: './read-idea.component.html',
styleUrls: ['./read-idea.component.scss']
})
export class ReadIdeaComponent implements OnInit {
public idea: Idea;
public ideaTags: Tag[];
public comments: Comment[];
public canEdit = false;
constructor(private auth: AuthService,
private model: ModelService,
private route: ActivatedRoute) { }
ngOnInit() {
this.route.data.subscribe(({ idea, ideaTags, comments }: { idea: Idea, ideaTags: Tag[], comments: Comment[] }) => {
// initialize data
this.idea = idea;
this.ideaTags = ideaTags;
this.comments = comments;
// check whether user can edit the idea
this.canEdit = this.auth.username === this.idea.creator.username;
});
}
async onVote(vote: number) {
// when vote doesn't exist, we add it
// when vote exists, we remove it
// and we update the idea.votes object
if (this.idea.votes.me === 0) {
// we add the vote
await this.model.vote({ to: { type: 'ideas', id: this.idea.id }, value: vote });
if (vote === 1) { this.idea.votes.up += 1; }
if (vote === -1) { this.idea.votes.down += 1; }
this.idea.votes.me = vote;
} else {
// we remove the vote
await this.model.vote({ to: { type: 'ideas', id: this.idea.id }, value: 0 });
if (this.idea.votes.me === 1) { this.idea.votes.up -= 1; }
if (this.idea.votes.me === -1) { this.idea.votes.down -= 1; }
this.idea.votes.me = 0;
}
}
}