-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhomepage.js
More file actions
230 lines (228 loc) · 8.32 KB
/
Copy pathhomepage.js
File metadata and controls
230 lines (228 loc) · 8.32 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
let app = new Vue ({
el: '#app',
data: {
loggedin: false,
displayvideo: false,
JWT: "",
user: "",
username: "",
token:"",
redbg: false,
greenbg: false,
displaycomment: false,
displaycomments: false,
comments: [],
devURL: "https://ga-project-three.herokuapp.com",
prodURL: null,
videos: [],
videoSource: null,
video_Id: null,
video_title: null,
video_likes: 0,
video_dislikes: 0,
is_liked: null,
newComment: "",
updateComment: "",
updateDivComment: "",
openEditDiv: 0,
openDeleteDiv: 0,
correctUser: 0,
notSignedIn: false,
noText: false,
noUpdateText: false,
noVotePermission: false
},
methods: {
handleLogout: function(event) {
this.loggedin = false
this.user = null
this.token = null
localStorage.clear();
//After logout, page is refreshed via href
},
displayVideo: function(event) {
this.displayvideo = true
this.video_Id = event.target.id
this.showVideo(this.video_Id)
this.getVideoStats(this.video_Id)
this.getComments()
},
displayHomepage: function(event) {
this.displayvideo = false
},
getComments: function() {
const URL = this.prodURL ? this.prodURL : this.devURL;
fetch(`${URL}/videos/${this.video_Id}/comments`, {
method: "get",
headers: {
"Content-Type": "application/json"
}
})
.then((response) => response.json())
.then((data) => {
this.comments = data
})
},
updateVideoLikes: function() {
// update like_count and dislike_count on videos table for one video
// Used to update thumbnail video stats
fetch(`${this.devURL}/video/${this.video_Id}/likes`, {
method: "get",
headers: {"Content-Type": "application/json"},
})
.then( res => res.json())
.then( data => {
// Data returns the like/dislike count in the likes table
// We use that data to update our videos table
fetch(`${this.devURL}/videos/${this.video_Id}`, {
method: "put",
headers: {"Content-Type" : "application/json"},
body: JSON.stringify({"like_count": data.likes, "dislike_count": data.dislikes})
})
.then(res => res.json())
})
},
createComment: function() {
if(this.loggedin) {
const URL = this.prodURL ? this.prodURL : this.devURL;
const textOfComment = {content: this.newComment}
if (this.newComment === "") {
this.noText = true
} else {
fetch(`${URL}/videos/${this.video_Id}/users/${this.user}/comments`, {
method: "post",
headers: {
"Content-Type": "application/json",
Authorization: `bearer ${this.token}`
},
body: JSON.stringify(textOfComment)
})
.then((response) => response.json())
.then((data) => {
this.newComment = ""
this.getComments()
this.noText = false
})
}
} else {
this.notSignedIn = true
}
},
updateAComment: function() {
const URL = this.prodURL ? this.prodURL : this.devURL;
const textOfComment = {content: this.updateComment}
const id = event.target.id
if (this.updateComment === "") {
this.noUpdateText = true
} else {
fetch(`${URL}/videos/${this.video_Id}/users/${this.user}/comments/${id}`, {
method: "put",
headers: {
"Content-Type": "application/json",
Authorization: `bearer ${this.token}`
},
body: JSON.stringify(textOfComment)
})
.then((response) => response.json())
.then((data) => {
this.updateComment = ""
this.getComments()
this.openEditDiv = 0
this.noUpdateText = false
})
}
},
deleteAComment: function(event) {
const URL = this.prodURL ? this.prodURL : this.devURL;
const id = event.target.id
fetch(`${URL}/videos/${this.video_Id}/users/${this.user}/comments/${id}`, {
method: "delete",
headers: {
Authorization: `bearer ${this.token}`
}
})
.then((response) => {
this.getComments()
})
},
getVideos: function() {
fetch(`${this.devURL}/videos`)
.then((response) => response.json())
.then((data) => {
this.videos = data.response
})
},
showVideo: function(id) {
fetch(`${this.devURL}/videos/${id}`, {
method: "get",
headers: {
"Content-type": "application/json"
}
})
.then((response) => response.json())
.then((data) => {
this.videoSource = "https://youtube.com/embed/" + data.data.videoID
this.video_title = data.data.title
})
},
sendVote: function(status) {
if (this.loggedin == false) {
this.noVotePermission = true
} else {
fetch(`${this.devURL}/likes/video/${this.video_Id}/users/${this.user}`, {
method: "post",
headers: {
"Content-Type": "application/json",
"Authorization": `bearer ${this.token}`
},
body: JSON.stringify({"is_liked": status})
})
.then((response) =>response.json())
.then((data) => {
this.updateVideoLikes()
this.getVideoStats(this.video_Id)
})
}
},
triggerDislike: function() {
const status = false
this.sendVote(status)
this.getVideoStats(this.video_Id)
},
triggerLike: function() {
const status = true
this.sendVote(status)
this.getVideoStats(this.video_Id)
},
getVideoStats: function() {
fetch(`${this.devURL}/video/${this.video_Id}/likes`)
.then((response) => response.json())
.then((data) => {
this.video_likes = data.likes
this.video_dislikes = data.dislikes
})
}
},
beforeMount(){
this.getVideos()
const checkIfLoggedIn = ()=> {
let isLoggedIn = localStorage.getItem("vLoggedIn");
//convert string to boolean
if (isLoggedIn == "true") {
//set variables that are passed in from local storage
this.username = localStorage.getItem("vUsername");
this.user = Number(localStorage.getItem("vUser"));
this.correctUser = Number(localStorage.getItem("vUser"));
this.token = localStorage.getItem("vToken");
//TODO: with this line, the user stays logged in unless they log out
//accourding to https://vuejs.org/v2/cookbook/avoiding-memory-leaks.html,
// users should not have to refresh their browser when using Single Page Application.
// localStorage.clear();
return true;
} else { // returned null, or undefined because login file has not run yet
return false;
}
}
this.loggedin = checkIfLoggedIn();
}
})