-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
126 lines (106 loc) · 3.14 KB
/
script.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
const local = "http://localhost:5000/"
const global ="https://tranquil-taiga-26020.herokuapp.com/"
let home = global;
function reload(){
location.reload();
}
window.onload = function (){
let params = new URLSearchParams(location.search);
var username = params.get('name');
console.log(username);
document.getElementById("xyz").innerHTML = "@"+username;
var btn = document.getElementById("tAdd");
var body = document.getElementById("block");
loadFeed();
btn.addEventListener("click",(event)=>{
let user = username;
event.preventDefault();
text = document.getElementById("tweetCnt").value;
img = document.getElementById("imgUrl").value;
console.log(img);
tweet = createTweet(user,text,img);
body.appendChild(tweet);
loadFeed();
storeTweet(user,text,img);
document.getElementById("tweetCnt").value = "";
document.getElementById("imgUrl").value = "";
})
};
function createTweet(username,content,img)
{
tweet = document.createElement('div');
tweet.setAttribute("class","tweet");
head = document.createElement("h1");
head.innerHTML = username;
hr = document.createElement("hr");
hr.style.size ="1";
hr.style.width= "90%";
hr.style.color = "lightskyblue"
br = document.createElement("br");
p = document.createElement("p");
p.innerHTML = content;
tweet.appendChild(head);
tweet.appendChild(hr);
tweet.appendChild(br);
tweet.appendChild(p);
if(img!=="")
{
image = document.createElement("img");
image.src = img;
tweet.appendChild(image);
}
return tweet;
}
async function storeTweet(username,content,img)
{
await axios.post(home+'addPost',
JSON.stringify({
"username" : username,
"content" : content,
"image" : img
}))
.then(function (response) {
console.log(response);
})
.catch(err => {
console.error(err);
});
}
async function loadFeed()
{
var body = document.getElementById("block");
body.innerHTML ="";
await axios.get(home+'getPosts')
.then(function (response) {
var data = response.data;
for(let i=data.length-1;i>=0;i--)
{
tweet = createTweet(data[i].username,data[i].content,data[i].image);
body.appendChild(tweet);
}
})
.catch(err => {
console.error(err);
});
}
async function mine()
{
var body = document.getElementById("block");
body.innerHTML ="";
let params = new URLSearchParams(location.search);
var username = params.get('name');
await axios.get(home+'getPosts')
.then(function (response) {
var data = response.data;
for(let i=data.length-1;i>=0;i--)
{
if(data[i].username == username){
tweet = createTweet(data[i].username,data[i].content,data[i].image);
}
body.appendChild(tweet);
}
})
.catch(err => {
console.error(err);
});
}