-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainLogic.js
262 lines (220 loc) · 8.31 KB
/
mainLogic.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
const baseUrl = "https://tarmeezacademy.com/api/v1";
function setupUI() {
const token = localStorage.getItem("token");
const logindiv = document.getElementById("login-div")
const logoutdiv = document.getElementById("logout-div");
const addBtn = document.getElementById("add-btn");
if (token == null) {
if (addBtn !== null) {
addBtn.style.setProperty("display", "none", "important");
}
logindiv.style.setProperty("display", "flex", "important");
logoutdiv.style.setProperty("display", "none", "important");
} else {// For Logged In User
if (addBtn !== null) {
addBtn.style.setProperty("display", "block", "important");
}
logindiv.style.setProperty("display", "none", "important");
logoutdiv.style.setProperty("display", "flex", "important");
const user = getCurrentUser();
document.getElementById("profile-name").innerHTML = `@${user.username}`;
document.getElementById("profile-pic").src = user.profile_image;
}
}
// ========= AUTH FUNCTIONS ======= //
function loginBtnClicked() {
const username = document.getElementById("username-input").value
const password = document.getElementById("password-input").value
const params = {
"username": username,
"password": password
}
const url = `${baseUrl}/login`;
toggleLoader(true)
axios.post(url, params)
.then((response) => {
localStorage.setItem("token", response.data.token)
localStorage.setItem("user", JSON.stringify(response.data.user));
const modal = document.getElementById("login-modal");
const modalInstance = bootstrap.Modal.getInstance(modal);
modalInstance.hide();
showAlert("Logged In Successfuly", 'success');
setupUI();
})
.catch((error) => {
showAlert(error.response.data.message, "danger")
})
.finally(() => {
toggleLoader(false)
})
}
function registerBtnClicked() {
const name = document.getElementById("registerName-input").value;
const username = document.getElementById("registerUsername-input").value;
const password = document.getElementById("registerPassword-input").value;
const image = document.getElementById("register-image-input").files[0];
const formData = new FormData();
formData.append("username", username);
formData.append("name", name);
formData.append("image", image);
formData.append("password", password);
const url = `${baseUrl}/register`;
toggleLoader(true)
axios.post(url, formData)
.then((response) => {
localStorage.setItem("token", response.data.token)
localStorage.setItem("user", JSON.stringify(response.data.user));
const modal = document.getElementById("register-modal");
const modalInstance = bootstrap.Modal.getInstance(modal);
modalInstance.hide();
showAlert("New User Registered Successfuly", 'success');
setupUI();
})
.catch((error) => {
const errors = error.response.data.message;
showAlert(errors, 'danger');
})
.finally(() => {
toggleLoader(false)
})
}
function logout() {
localStorage.removeItem("token");
localStorage.removeItem("user");
showAlert("Logged Out Successfully");
setupUI();
}
function showAlert(customMessage, type = 'success') {
const alerPlaceHolder = document.getElementById("success-alert");
const alert = (message, type) => {
const wrapper = document.createElement('div')
wrapper.innerHTML = [
`<div class="alert alert-${type} alert-dismissible" role="alert">`,
` <div>${message}</div>`,
' <button type="button" class ="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>',
'</div>'
].join('');
alerPlaceHolder.append(wrapper);
}
alert(customMessage, type);
// todo : hide alert
}
function getCurrentUser() {
let user = null;
const storageUser = localStorage.getItem("user");
if (storageUser !== null) {
user = JSON.parse(storageUser);
}
return user;
}
function addTags(tagsArray) {
let Tags = "";
for (tag of tagsArray) {
let content = `
<span class="bg-secondary rounded-pill p-2 mx-1 text-white">${tag.name}</span>
`
Tags += content;
}
return Tags;
}
//======== POSTS REQUEST========//
function editPostBtnClicked(postObject) {
let post = JSON.parse(decodeURIComponent(postObject))
document.getElementById("post-modal-submit-btn").innerHTML = "Update"
document.getElementById("post-id-input").value = post.id;
document.getElementById("newPost-modal-title").innerHTML = "Edit Post"
document.getElementById("post-title-input").value = post.title;
document.getElementById("post-body-input").value = post.body
let postModal = new bootstrap.Modal(document.getElementById("newPost-modal"), {})
postModal.toggle();
}
function deletePostBtnClicked(postObject) {
let post = JSON.parse(decodeURIComponent(postObject))
document.getElementById("delete-post-id-input").value = post.id
let postModal = new bootstrap.Modal(document.getElementById("delete-post-modal"), {})
postModal.toggle();
}
function confirmPostDelete() {
let token = localStorage.getItem("token");
let postId = document.getElementById("delete-post-id-input").value;
let url = `${baseUrl}/posts/${postId}`
let headers = {
"authorization": `Bearer ${token}`
}
toggleLoader(true)
axios.delete(url, { headers: headers })
.then(() => {
const modal = document.getElementById("delete-post-modal");
const modalInstance = bootstrap.Modal.getInstance(modal);
modalInstance.hide();
showAlert("Post Deleted Successfuly", 'success');
getPosts();
})
.catch((error) => {
showAlert(error.response.data.message, "danger");
})
.finally(() => {
toggleLoader(false)
})
}
function createNewPostClicked() {
let postId = document.getElementById("post-id-input").value;
let isCreate = postId === null || postId === "";
const title = document.getElementById("post-title-input").value
const body = document.getElementById("post-body-input").value
const image = document.getElementById("post-image-input").files[0];
const params = new FormData();
params.append("title", title)
params.append("body", body)
params.append("image", image)
let url = ``;
const headers = {
"authorization": `Bearer ${localStorage.getItem('token')}`
}
if (isCreate) {
url = `${baseUrl}/posts`
} else {
params.append("_method", "put")
url = `${baseUrl}/posts/${postId}`
}
toggleLoader(true)
axios.post(url, params, {
headers: headers
})
.then(() => {
const modal = document.getElementById("newPost-modal");
const modalInstance = bootstrap.Modal.getInstance(modal);
modalInstance.hide();
showAlert("Post Created Successfuly", 'success');
getPosts();
})
.catch((error) => {
showAlert(error.response.data.message, "danger");
})
.finally(() => {
toggleLoader(false)
})
}
function postClicked(postId) {
window.location = `postDetails.html?postId=${postId}`;
}
function addBtnClicked() {
document.getElementById("post-modal-submit-btn").innerHTML = "Create"
document.getElementById("post-id-input").value = "";
document.getElementById("newPost-modal-title").innerHTML = "Create New Post"
document.getElementById("post-title-input").value = "";
document.getElementById("post-body-input").value = ""
let postModal = new bootstrap.Modal(document.getElementById("newPost-modal"), {})
postModal.toggle();
}
function profileClicked() {
let user = getCurrentUser()
window.location = `profile.html?userid=${user.id}`
}
function toggleLoader(show = true) {
if (show) {
document.getElementById("loader").style.setProperty("visibility", "visible", "important")
} else {
document.getElementById("loader").style.setProperty("visibility", "hidden", "important")
}
}