-
Notifications
You must be signed in to change notification settings - Fork 0
/
community.js
199 lines (176 loc) · 6.26 KB
/
community.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
const parentElement = document.querySelector(".heroine");
function createPost() {
// Create the post section element with the class "post"
const postSection = document.createElement("section");
postSection.className = "post";
// Create the info div element with the class "info p-1"
const infoDiv = document.createElement("div");
infoDiv.className = "info p-1";
// Create the profile image element
const profileImage = document.createElement("img");
profileImage.src = "Assets/man.png";
profileImage.alt = "";
infoDiv.appendChild(profileImage);
// Create the username paragraph element
const usernameParagraph = document.createElement("p");
usernameParagraph.textContent = "Keshav Aneja";
infoDiv.appendChild(usernameParagraph);
// Append the info div to the post section
postSection.appendChild(infoDiv);
// Create the description div element with the class "description p-2"
const descriptionDiv = document.createElement("div");
descriptionDiv.className = "description p-2";
// Create the description paragraph element
const descriptionParagraph = document.createElement("p");
descriptionParagraph.innerHTML = `${thread}<br /><span class="hashtags">#PanelDiscussion #InterSchoolDebate #LearningTogether</span>`;
descriptionDiv.appendChild(descriptionParagraph);
// Append the description div to the post section
postSection.appendChild(descriptionDiv);
// Create the image div element with the class "image"
const imageDiv = document.createElement("div");
imageDiv.className = "image";
// Create the post image element
const postImage = document.createElement("img");
postImage.src = newPostSrc;
postImage.alt = "";
imageDiv.appendChild(postImage);
// Append the image div to the post section
postSection.appendChild(imageDiv);
// Create the controls div element with the class "controls"
const controlsDiv = document.createElement("div");
controlsDiv.className = "controls";
// Create the unfavourite image element
const unfavouriteImage = document.createElement("img");
unfavouriteImage.src = "Assets/unfavourite.png";
unfavouriteImage.alt = "";
unfavouriteImage.className = "like";
controlsDiv.appendChild(unfavouriteImage);
// Create the chat image element
const chatImage = document.createElement("img");
chatImage.src = "Assets/chat.png";
chatImage.alt = "";
controlsDiv.appendChild(chatImage);
// Create the share image element
const shareImage = document.createElement("img");
shareImage.src = "Assets/share.png";
shareImage.alt = "";
controlsDiv.appendChild(shareImage);
// Append the controls div to the post section
postSection.appendChild(controlsDiv);
// Select the parent element where you want to append the post section
// Append the post section to the parent element
parentElement.prepend(postSection);
}
let thread;
const postDesc = document.getElementById("post-desc");
postDesc.addEventListener("change", function () {
thread = postDesc.value;
});
const postBtn = document.getElementById("post");
postBtn.addEventListener("click", function () {
createPost();
closePlayer();
});
const man = document.getElementById("man");
const inpFile = document.getElementById("inpFile");
const previewImage = document.getElementById("previewImage");
const imageSection = document.getElementById("imageSection");
const textUpload = document.querySelector(".text-upload");
let newPostSrc;
inpFile.addEventListener("change", (e) => {
const file = e.target.files[0];
const reader = new FileReader();
reader.onload = (event) => {
newPostSrc = event.target.result;
console.log(newPostSrc);
// previewImage.src = event.target.result;
// previewImage.style.display = "block";
// Set the background image of the section
// imageSection.style.backgroundImage = `url('${event.target.result}')`;
};
reader.readAsDataURL(file);
});
//Functionality to enlarge the post section
let isOpen = false;
// let open = document.getElementById("openUp");
const screen = document.querySelector(".post-method");
const openPlayer = function () {
// console.log(window.innerWidth );
screen.classList.toggle("overlay-player");
document.querySelector(".overlay").classList.toggle("hidden");
if (window.innerWidth > 600) {
screen.style.width = "60vw";
} else {
screen.style.width = "80vw";
}
screen.style.height = "70vh";
screen.style.borderRadius = "50px";
man.style.display = "none";
postDesc.style.width = "90%";
postDesc.style.height = "40%";
textUpload.style.display = "inline-block";
postBtn.style.width = "50%";
// postDesc.widt;
isOpen = true;
};
const closePlayer = function () {
screen.classList.toggle("overlay-player");
document.querySelector(".overlay").classList.toggle("hidden");
screen.style.width = "100%";
screen.style.height = "120px";
postDesc.style.width = "55%";
postDesc.style.height = "50px";
man.style.display = "inherit";
textUpload.style.display = "none";
postBtn.style.width = "";
postDesc.value = "";
isOpen = false;
};
postDesc.addEventListener("click", function () {
if (!isOpen) {
openPlayer();
}
});
document.addEventListener("keydown", function (e) {
if (e.key === "Escape" && isOpen) {
closePlayer();
}
});
const like = document.querySelectorAll(".like");
for (let i = 0; i < like.length; i++) {
like[i].addEventListener("click", function () {
like[i].src = "Assets/favourite.png";
like[i].style.filter = "invert(0)";
});
}
const textBox = document.getElementById("text-box");
const btn = document.querySelector(".btn-chat");
btn.addEventListener("click", async () => {
const text = textBox.value;
console.log(text);
const content = await getOpenAIResponse(text);
const textrep = document.createElement("p");
textrep.textContent = content;
const chatbox = document.querySelector(".chat-box");
chatbox.appendChild(textrep);
});
async function getOpenAIResponse(text) {
const response = await fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: ``,
},
body: JSON.stringify({
model: "gpt-3.5-turbo",
messages: [
{
role: "user",
content: text,
},
],
}),
});
const data = await response.json();
return data.choices[0].message.content;
}