/
LikePost.js
62 lines (55 loc) · 1.76 KB
/
LikePost.js
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
56
57
58
59
60
61
62
import Utils from './../../services/Utils.js'
let likePost = async (post_id) => {
const payload = {
"post_id": post_id,
}
const options = {
method: 'POST',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(payload)
};
try {
const response = await fetch(`http://localhost:3000/like_post`, options)
const json = await response.json();
console.log(json)
return json
} catch (err) {
console.log('Error getting documents', err)
}
}
let LikePost = {
// Only add shareable state here so that any other component can query the current value of state
state : {
selectedTags: []
},
render: async function (count) {
let view = /*html*/`
<a class="button is-danger is-outlined" id="like_post_btn">
<span class="icon is-small">
<i class="far fa-heart"></i>
${count ? count : 0}
</span>
</a>
`
return view
},
control: async function () {
// Get the post id
let postId = Utils.parseRequestURL().id
// Handle the event when user types in the input and show the matched tags in the dropdown
document.getElementById("like_post_btn").addEventListener ("click", async () => {
let result = await likePost(postId)
if (result.status == 'success') {
console.log('Post liked')
} else if (result.status == 401) {
console.log("401 came")
} else {
console.log (`Like Failed: ${result.message}`)
}
})
}
}
export default LikePost;