-
Notifications
You must be signed in to change notification settings - Fork 1
/
user_page.html
285 lines (245 loc) · 8.38 KB
/
user_page.html
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User Page</title>
<link rel="stylesheet" href="styles.css">
<style>
body {
display: grid;
grid-template-rows: auto 1fr;
grid-template-columns: 1fr 2fr;
gap: 20px;
height: 100vh;
margin: 0;
}
#top {
grid-row: 1;
grid-column: 1 / span 2;
display: flex;
justify-content: space-between;
align-items: center;
padding: 20px;
background-color: #f0f0f0;
}
#left {
grid-row: 2;
grid-column: 1;
padding: 20px;
background-color: #f9f9f9;
}
#right {
grid-row: 2;
grid-column: 2;
padding: 20px;
background-color: #ffffff;
}
.logout-btn {
padding: 10px 20px;
background-color: #3498db;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s ease;
}
.logout-btn:hover {
background-color: #2980b9;
}
#newsfeed {
margin-bottom: 20px;
width: 200px;
}
.post {
margin-bottom: 20px;
border: 1px solid #ccc;
padding: 10px;
border-radius: 5px;
}
.post-title {
font-weight: bold;
}
.post-body {
margin-top: 5px;
width: 500px;
}
</style>
</head>
<body>
<div id="top">
<div class="user-box">
<h1 id="welcome-message"></h1>
<p id="user-email"></p>
</div>
<button class="logout-btn" id="logout-btn">로그아웃</button>
</div>
<div id="left">
<h2>뉴스피드</h2>
<div id="posts-list"></div>
</div>
<div id="right">
<h2>게시물 작성</h2>
<form id="post-form">
<label for="post-body">POST</label><br>
<textarea id="post-body" name="post-body"></textarea><br><br>
<button type="submit" id="post-submit">게시</button>
</form>
<h2>Image Generation</h2>
<form id="image-form">
<label for="user-input">Enter Your Text:</label><br>
<input type="text" id="user-input" name="user-input"><br>
<label for="style-option">Select Image Style:</label><br>
<select id="style-option" name="style-option">
<option value="Japanese Cartoon">Japanese Cartoon</option>
<option value="Realistic">Realistic</option>
<option value="3D modeling">3D modeling</option>
<option value="Animation">Animation</option>
<option value="Pencil Drawing">Pencil Drawing</option>
<option value="Watercolor">Watercolor</option>
</select><br><br>
<button type="submit" id="generate-image">Generate Image</button>
</form>
<div id="generated-image"></div>
</div>
<script type="module">
// Import Firebase modules
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.9.0/firebase-app.js";
import { getAuth, onAuthStateChanged, signOut } from "https://www.gstatic.com/firebasejs/10.9.0/firebase-auth.js";
import { getFirestore, collection, addDoc, getDocs, deleteDoc, doc } from "https://www.gstatic.com/firebasejs/10.9.0/firebase-firestore.js";
import { getDatabase, ref, push, update } from "https://www.gstatic.com/firebasejs/10.9.0/firebase-database.js";
// Initialize Firebase
const firebaseConfig = {
apiKey: "AIzaSyA2aoOXeoe8KXbqzo_Du1yfilCQkqi03wM",
authDomain: "besteam-e5d45.firebaseapp.com",
projectId: "besteam-e5d45",
storageBucket: "besteam-e5d45.appspot.com",
messagingSenderId: "1007108211259",
appId: "1:1007108211259:web:24136c60d32ffc5261521e",
measurementId: "G-KV7E75RSQZ"
};
// Initialize Firebase app
const firebaseApp = initializeApp(firebaseConfig);
// Get Auth object
const auth = getAuth(firebaseApp);
// Get Firestore object
const db = getFirestore(firebaseApp);
// Get Realtime Database object
const database = getDatabase(firebaseApp);
// Function to fetch and display posts
// Function to fetch and display posts
async function fetchAndDisplayPosts() {
const postsList = document.getElementById("posts-list");
postsList.innerHTML = ""; // Clear existing posts
const querySnapshot = await getDocs(collection(db, "posts"));
querySnapshot.forEach((doc) => {
const postData = doc.data();
const postElement = document.createElement("div");
postElement.classList.add("post");
const postTime = postData.createdAt.toDate().toLocaleString(); // Convert Firebase timestamp to local time string
postElement.innerHTML = `
<div class="post-title">${postData.title}</div>
<div class="post-body">${postData.body}</div>
<div class="post-info">Posted by ${postData.author} at ${postTime} (${postData.authorEmail})</div>
<button class="remove-btn" data-post-id="${doc.id}">삭제</button>
`;
postsList.appendChild(postElement);
});
// Add event listener to remove buttons
const removeButtons = document.querySelectorAll(".remove-btn");
removeButtons.forEach((button) => {
button.addEventListener("click", async () => {
const postId = button.getAttribute("data-post-id");
await deletePost(postId);
fetchAndDisplayPosts(); // Refresh the newsfeed after removing the post
});
});
}
// Function to delete a post from the database
async function deletePost(postId) {
await deleteDoc(doc(db, "posts", postId));
}
// Listen for authentication state changes
onAuthStateChanged(auth, (user) => {
if (user) {
// User is signed in, display welcome message
const displayName = user.displayName;
const email = user.email;
const msg = email + " 계정으로 로그인 됨";
document.getElementById("user-email").innerText = msg;
} else {
// User is signed out
// Redirect to login page
window.location.href = "login.html";
}
});
// Add event listener to logout button
const logoutBtn = document.getElementById("logout-btn");
logoutBtn.addEventListener("click", () => {
signOut(auth).then(() => {
console.log("User signed out successfully.");
// Redirect to login page
window.location.href = "login.html";
}).catch((error) => {
console.error("Error signing out:", error);
});
});
// Add event listener to post form
const postForm = document.getElementById("post-form");
postForm.addEventListener("submit", async (e) => {
e.preventDefault();
const postBody = document.getElementById("post-body").value.trim();
if (postBody !== "") {
const user = auth.currentUser;
if (user) {
const userId = user.uid;
const userName = user.displayName;
// Call the writeNewPost function to add the post to the database
await writeNewPost(userId, userName, '', '', postBody);
// Clear the input field after posting
document.getElementById("post-body").value = "";
// Refresh the newsfeed to display the newly added post
fetchAndDisplayPosts();
} else {
console.error("User not logged in.");
}
} else {
console.error("Post body cannot be empty.");
}
});
// Function to write a new post to the database
async function writeNewPost(uid, username, picture, title, body) {
await addDoc(collection(db, "posts"), {
author: username,
uid: uid,
body: body,
title: title,
starCount: 0,
authorPic: picture,
createdAt: new Date()
});
}
// Initial fetch and display of posts
fetchAndDisplayPosts();
const imageForm = document.getElementById("image-form");
imageForm.addEventListener("submit", async (e) => {
e.preventDefault();
const userInput = document.getElementById("user-input").value.trim();
const styleOption = document.getElementById("style-option").value;
// Placeholder image URLs
const placeholderImages = [
"https://via.placeholder.com/150",
"https://via.placeholder.com/200",
"https://via.placeholder.com/250",
// Add more placeholder image URLs as needed
];
// Randomly select an image URL from the array
const randomImageUrl = placeholderImages[Math.floor(Math.random() * placeholderImages.length)];
// Create and append the image element to the news feed
const imageElement = document.createElement('img');
imageElement.src = randomImageUrl;
document.getElementById('posts-list').appendChild(imageElement);
});
</script>
</body>
</html>