-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
260 lines (238 loc) · 9.71 KB
/
main.js
File metadata and controls
260 lines (238 loc) · 9.71 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
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
const BASE_URL = "http://localhost:3000"; // Backend base URL
let currentUser;
async function login(){//get all users and see if given login matches
let usernam = document.getElementById("username").value;
let pass = document.getElementById("password").value;
try {
const response = await fetch(`${BASE_URL}/users`);
const users = await response.json();
let userFound = false;
users.forEach(user => {
//check if the given username and pass match any of the stored users (caps sensitive)
if(user.password == pass && user.username == usernam){
localStorage.setItem("Current User", user.username);
userFound = true;
window.location.href = "home.html"; //only go to home page if found
}
});
if(!userFound)
alert("User Not Found. Register or try again.");
} catch (error) {
console.error('Error fetching data:', error);
}
}
async function addUser(){
//get input
let email = document.getElementById("email").value;
let username = document.getElementById("username").value;
let password = document.getElementById("password").value;
let passCheck = document.getElementById("checkPass").value;
//invalid input check
arr = [email, username, password, passCheck];
if(arr.includes("") || password != passCheck){
alert("Please fill all fields and make sure passwords match");
return;
}
//add to database
arr = {email, username, password}
try {
console.log(JSON.stringify(arr));
const response = await fetch(`${BASE_URL}/adduser`,{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(arr),
});
if(response.ok) //only go to next page if it acutally worked
window.location.href = "login.html";
else{
alert("Error, user may already exist.");
console.log("response not ok");
}
} catch (error) {
console.log('Error:', error);
}
}
async function fetchAndDisplayData() { //list songs
try {
const response = await fetch(`${BASE_URL}/songs`);
const songs = await response.json();
songs.forEach(song => {
//create a song row element for each song found using the database info
const container = document.createElement("div");
container.className = "container";
container.dataset.id = song.id;
const newDiv = document.createElement("div");
const pic = document.createElement("span");
const text = document.createElement("span");
let title = document.createElement("h4");
title.innerHTML = song.title + " - " + song.artist;
let user = document.createElement("small");
user.innerHTML = " | Posted By: " + song.user;
title.appendChild(user);
let cover = document.createElement("img");
cover.src = song.image;
let desc = document.createElement("p");
desc.innerHTML = song.description;
let aud = document.createElement("audio");
aud.controls = true;
aud.src = song.audio;
newDiv.className = "row";
pic.className = "col-2";
text.className = "col-10";
newDiv.appendChild(pic);
newDiv.appendChild(text);
newDiv.appendChild(document.createElement("br"));
newDiv.appendChild(aud);
pic.appendChild(cover);
if(song.link != null){ //only add link if it was given
let link = document.createElement("a");
link.href = song.link;
link.target = "_blank";
link.appendChild(title);
text.appendChild(link);
}
else{
text.appendChild(title);
}
text.appendChild(document.createElement("hr"));
text.appendChild(desc);
//add to the feed in order of newest first
const currentDiv = document.getElementById("feed");
currentDiv.innerText = ""; //get rid of empty feed message
document.body.insertBefore(newDiv, currentDiv.nextSibling);
});
} catch (error) {
console.error('Error fetching data:', error);
}
}
async function newPost(){ //save a song
//readers to store actual files in the database rather than file locations
const imgReader = new FileReader();
const picReader = new FileReader();
let titl = document.getElementById("song title").value;
let arti = document.getElementById("artist").value;
let bod = document.getElementById("desc").value;
let imag;
let user = localStorage.getItem("Current User");
let link = document.getElementById("link").value;
//convert image and music to base64 then submit to database
imgReader.readAsDataURL(document.querySelector("input[type=file]").files[0]);
imgReader.addEventListener('load', () => { imag = imgReader.result; });
picReader.readAsDataURL(document.querySelectorAll("input[type=file]")[1].files[0]);
picReader.addEventListener('load', () => {
let aud = picReader.result;
if([titl, bod, user, imag, aud, arti].includes("")) {
alert("Please fill all fields");
return;
}
else{
if(link != "")
addSong({titl, bod, user, imag, aud, arti, link});
else//only submit link if one was given
addSong({titl, bod, user, imag, aud, arti})
}
});
}
async function addSong(arr){
try {
console.log(JSON.stringify(arr));
const response = await fetch(`${BASE_URL}/add`,{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(arr),
});
if(response.ok) //only go to next page if song was actually added
window.location.href = "home.html";
else{ //files that are too large can't be stored in the database in base64
alert("Upload failed, please use smaller files.");
console.log("response not ok");
}
} catch (error) {
console.log('Error:', error);
}
}
async function newMsg(){ //save message, same as save song but without image
const reader = new FileReader();
let topic = document.getElementById("topic").value;
let body = document.getElementById("desc").value;
let reciever = document.getElementById("reciever").value
let aud;
let sender = localStorage.getItem("Current User");
if(document.querySelector("input[type=file]").files[0] != null){
reader.readAsDataURL(document.querySelector("input[type=file]").files[0]);
reader.addEventListener('load', () => {
let aud = reader.result;
let arr = [sender, topic, body, aud];
console.log(arr);
if(arr.includes("")) {
alert("Please fill all fields");
return;
}
else{
addMessage({sender, topic, body, reciever, aud});
}
});
}
else{
addMessage({sender, topic, body, reciever});
}
}
async function addMessage(arr){
try {
console.log(JSON.stringify(arr));
const response = await fetch(`${BASE_URL}/addmsg`,{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(arr),
});
if(response.ok)
window.location.href = "dms.html";
else
console.log("response not ok");
} catch (error) {
console.log('Error:', error);
}
}
async function loadDms() { //list messages for the user currently logged in
try {
const response = await fetch(`${BASE_URL}/messages`);
const messages = await response.json();
console.log("loading dms");
messages.forEach(message => {
//only list the messages for the current user in local storage
if(message.reciever == localStorage.getItem("Current User")){
//create the message row element
const container = document.createElement("div");
container.className = "container";
const newDiv = document.createElement("div");
const text = document.createElement("span");
let title = document.createElement("h4");
title.innerHTML = message.topic;
let user = document.createElement("small");
user.innerHTML = " | Sent By: " + message.sender;
title.appendChild(user);
let body = document.createElement("p");
body.innerHTML = message.body;
newDiv.className = "row";
text.className = "col-12";
newDiv.appendChild(text);
newDiv.appendChild(document.createElement("br"));
if(message.audio != null){ //only add the audio to the end if it was given
let aud = document.createElement("audio");
aud.controls = true;
aud.src = message.audio;
newDiv.appendChild(aud);
}
text.appendChild(title);
text.appendChild(document.createElement("hr"));
text.appendChild(body);
const currentDiv = document.getElementById("feed");
currentDiv.innerText = ""; //get rid of no posts found message
document.body.insertBefore(newDiv, currentDiv.nextSibling);
}
});
} catch (error) {
console.error('Error fetching data:', error);
}
}
//npx kill-port 3000