forked from Redrecovery9/PugBoard_Client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.js
229 lines (209 loc) · 6.52 KB
/
user.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
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
var baseURL = 'https://vast-wildwood-40809.herokuapp.com/'
var localURL = 'http://localhost:3000/'
getMessages(baseURL)
showUserData()
$(document).on('click', '.homeButton', goHome)
$(document).on('click', '.signOut', signOut)
$(document).on('click', '.my-posts', displayUserMessages)
$(document).on('click', '.submit-new-message', addMessage)
$(document).on('click', '.delete', deleteMessage)
$(document).on('click', '.upvote', upVoteAMessage)
$(document).on('click', '.downvote', downVoteAMessage)
function getMessages(baseURL) {
$.get(baseURL)
.then(displayMessages)
}
function displayMessages(data) {
$('.message-section').empty()
data.forEach(function(data) {
$('.message-section').append(
`
<div class="card">
<div class="card-header post-by">Post by: ${data.username}</div>
<div class="row">
<div class="col-sm-9">
<div class="card-block">
<h4 class="card-title">${data.title}</h4>
<p class="card-text">${data.message}</p>
</div>
</div>
<div class="col-sm-3 card-block text-center">
<div class="rating rounded">
<p id="rating-${data.message_id}">${data.rating}</p>
<button type="button" id="${data.message_id}" class="btn btn-outline-success upvote"><i id="${data.message_id}" class="fa fa-hand-o-up fa-2x" aria-hidden="true"></i></button>
<button type="button" id="${data.message_id}" class="btn btn-outline-danger downvote"><i id="${data.message_id}" class="fa fa-hand-o-down fa-2x" aria-hidden="true"></i></button>
</div>
<div class ="delete hide">
<button type="button" id="${data.id}" class="btn btn-outline-danger"><i class="fa fa-trash" id="${data.id}" aria-hidden="true"></i>
</button>
</div>
</div>
</div>
<div class="card-footer text-muted card-footer-${data.message_id}">
<p>
<a id="${data.message_id}" class="seeComments btn btn-primary" data-toggle="collapse" href="#collapse-${data.message_id}" aria-expanded="false" aria-controls="collapse-${data.message_id}">
Comments
</a>
</p>
</div>
`
)
$('.seeComments').on('click', displayComments)
})
}
function displayComments(event){
$('.card-footer-' + event.target.id).append(
`
<div class="collapse" id="collapse-${event.target.id}">
<div class="card card-block">
<div class="emptyGuy"></div>
<div class="form-group">
<label for="comment-text">Add a Comment</label>
<textarea class="form-control" id="comment-text" rows="3"></textarea>
<button type="button" id="${event.target.id}" class="btn btn-success">Submit Comment</button>
</div>
</div>
</div>
</div>
`
)
}
function goHome(id){
$('.message-section').empty()
getMessages(baseURL)
}
function signOut(){
localStorage.removeItem('token')
location.href = '/'
}
function showUserData(){
var userData = getUserDataFromToken()
$('.username-info').append(
`
Username: ${userData.username}
`)
$('.email-info').append(
`
Email: ${userData.email}
`)
}
function getUserDataFromToken(){
var token = localStorage.getItem('token')
return parseJwt(token)
}
function parseJwt (token) {
var base64Url = token.split('.')[1];
var base64 = base64Url.replace('-', '+').replace('_', '/');
return JSON.parse(window.atob(base64));
}
function displayUserMessages(){
var userData = getUserDataFromToken()
$('.message-section').empty()
$.get(baseURL + userData.id)
.then(function(data){
if(data.length === 0){
$('.message-section').append(
`
<div class="card card-block">
<h3 class="text-center"> You have not created any messages yet! </h3>
</div>
`)
} else {
displayMessages(data)
$('.post-by').text("Post by: You")
addDeleteButtonToUserMessages(data)
}
})
}
function getNewMessageDetails(){
var userData = getUserDataFromToken()
var messageTitle = $('#message-title').val()
var messageText = $('#message-text').val()
return {userData, messageTitle, messageText}
}
function addMessage() {
var messageDetails = getNewMessageDetails()
var postBody = {
title: messageDetails.messageTitle,
message: messageDetails.messageText,
rating: 0,
user_id: messageDetails.userData.id
}
if(messageDetails.messageText && messageDetails.messageTitle) {
$.post(baseURL + messageDetails.userData.id, postBody)
.then(()=> {
$('.message-section').empty()
getMessages(baseURL)
})
clearAndCollapseMessageForm()
}
}
function clearAndCollapseMessageForm(){
var messageTitle = $('#message-title').val('')
var messageText = $('#message-text').val('')
$('#add-message').collapse('toggle')
}
function addDeleteButtonToUserMessages(data){
$('.delete').removeClass('hide')
}
function deleteMessage(event){
var messageId = event.target.id
$.ajax({
type: 'DELETE',
url: baseURL + 'message/' + messageId
})
.then(()=> {
displayUserMessages()
notifySuccessOfDeletion()
})
}
function notifySuccessOfDeletion(){
$('header').append(
`
<div class="alert alert-success alert-dismissible fade show" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
Your message was successfully deleted!
</div>
`
)
}
function upVoteAMessage(event){
var messageId = event.target.id
$.get(baseURL + 'message/' + messageId)
.then(function(data){
$.ajax({
type: 'PUT',
url: baseURL + messageId,
data: {
message: data[0].message,
rating: Number(data[0].rating) + 1,
title: data[0].title,
user_id: data[0].user_id
}
})
.then(function(data){
$('#rating-' + messageId).text(data[0].rating)
})
})
}
function downVoteAMessage(event){
var messageId = event.target.id
$.get(baseURL + 'message/' + messageId)
.then(function(data){
$.ajax({
type: 'PUT',
url: baseURL + messageId,
data: {
message: data[0].message,
rating: Number(data[0].rating) - 1,
title: data[0].title,
user_id: data[0].user_id
}
})
.then(function(data){
$('#rating-' + messageId).text(data[0].rating)
})
})
}