Spaces:
Running
Running
File size: 12,587 Bytes
5565235 |
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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
// API Base URL
const API_BASE_URL = '/api';
// DOM Elements
const shareForm = document.getElementById('shareForm');
const linksContainer = document.getElementById('linksContainer');
const loadingSpinner = document.getElementById('loadingSpinner');
const successMessage = document.getElementById('successMessage');
const successText = document.getElementById('successText');
// Initialize app
document.addEventListener('DOMContentLoaded', function() {
loadLinks();
// Handle form submission
shareForm.addEventListener('submit', handleShareSubmit);
});
// Handle share form submission
async function handleShareSubmit(e) {
e.preventDefault();
const formData = new FormData(shareForm);
const data = {
name: formData.get('name'),
note: formData.get('note'),
youtube_link: formData.get('youtube_link')
};
// Validate YouTube URL
if (!isValidYouTubeURL(data.youtube_link)) {
alert('กรุณากรอกลิงก์ YouTube ที่ถูกต้อง');
return;
}
try {
showLoading(true);
const response = await fetch(`${API_BASE_URL}/posts`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data)
});
if (!response.ok) {
throw new Error('เกิดข้อผิดพลาดในการแชร์');
}
const result = await response.json();
// Show success message
showSuccessMessage('แชร์สำเร็จ!');
// Clear form
shareForm.reset();
// Reload links
await loadLinks();
} catch (error) {
console.error('Error sharing link:', error);
alert('เกิดข้อผิดพลาดในการแชร์: ' + error.message);
} finally {
showLoading(false);
}
}
// Load all shared links
async function loadLinks() {
try {
showLoading(true);
const response = await fetch(`${API_BASE_URL}/posts`);
if (!response.ok) {
throw new Error('ไม่สามารถโหลดลิงก์ได้');
}
const links = await response.json();
displayLinks(links);
} catch (error) {
console.error('Error loading links:', error);
linksContainer.innerHTML = `
<div style="text-align: center; color: #666; padding: 20px;">
<i class="fas fa-exclamation-triangle"></i>
<p>ไม่สามารถโหลดลิงก์ได้: ${error.message}</p>
</div>
`;
} finally {
showLoading(false);
}
}
// Display links in the container
function displayLinks(links) {
if (links.length === 0) {
linksContainer.innerHTML = `
<div style="text-align: center; color: #666; padding: 40px;">
<i class="fab fa-youtube" style="font-size: 3rem; margin-bottom: 15px;"></i>
<p>ยังไม่มีลิงก์ที่แชร์</p>
<p>เป็นคนแรกที่แชร์ลิงก์ YouTube!</p>
</div>
`;
return;
}
linksContainer.innerHTML = links.map(link => createLinkCard(link)).join('');
// Add event listeners for like and comment buttons
addLinkEventListeners();
}
// Create HTML for a single link card
function createLinkCard(link) {
const videoId = extractYouTubeVideoId(link.youtube_link);
const embedUrl = videoId ? `https://www.youtube.com/embed/${videoId}` : '';
const timeAgo = getTimeAgo(new Date(link.created_at));
return `
<div class="link-card" data-link-id="${link.id}">
<div class="link-header">
<span class="user-name">
<i class="fas fa-user"></i> ${escapeHtml(link.name)}
</span>
<span class="timestamp">${timeAgo}</span>
</div>
<div class="link-note">
${escapeHtml(link.note)}
</div>
${embedUrl ? `
<div class="youtube-embed">
<iframe src="${embedUrl}"
title="YouTube video player"
frameborder="0"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen>
</iframe>
</div>
` : `
<div class="youtube-link">
<a href="${link.youtube_link}" target="_blank" rel="noopener noreferrer">
<i class="fab fa-youtube"></i> ดูวิดีโอใน YouTube
</a>
</div>
`}
<div class="link-actions">
<button class="action-btn like-btn" data-link-id="${link.id}">
<i class="fas fa-heart"></i>
<span class="like-count">${link.likes}</span>
</button>
<button class="action-btn comment-btn" data-link-id="${link.id}">
<i class="fas fa-comment"></i>
คอมเมนต์
</button>
</div>
<div class="comments-section" id="comments-${link.id}" style="display: none;">
<div class="comment-form">
<input type="text" placeholder="เขียนคอมเมนต์..." class="comment-input">
<button type="button" class="add-comment-btn" data-link-id="${link.id}">
<i class="fas fa-paper-plane"></i>
</button>
</div>
<div class="comments-list" id="comments-list-${link.id}">
<!-- Comments will be loaded here -->
</div>
</div>
</div>
`;
}
// Add event listeners for link interactions
function addLinkEventListeners() {
// Like buttons
document.querySelectorAll('.like-btn').forEach(btn => {
btn.addEventListener('click', handleLike);
});
// Comment buttons
document.querySelectorAll('.comment-btn').forEach(btn => {
btn.addEventListener('click', toggleComments);
});
// Add comment buttons
document.querySelectorAll('.add-comment-btn').forEach(btn => {
btn.addEventListener('click', handleAddComment);
});
// Enter key for comment input
document.querySelectorAll('.comment-input').forEach(input => {
input.addEventListener('keypress', function(e) {
if (e.key === 'Enter') {
const linkId = this.closest('.comments-section').id.split('-')[1];
const btn = document.querySelector(`.add-comment-btn[data-link-id="${linkId}"]`);
btn.click();
}
});
});
}
// Handle like button click
async function handleLike(e) {
const linkId = e.currentTarget.dataset.linkId;
const likeBtn = e.currentTarget;
const likeCount = likeBtn.querySelector('.like-count');
try {
const response = await fetch(`${API_BASE_URL}/posts/${linkId}/like`, {
method: 'POST'
});
if (!response.ok) {
throw new Error('ไม่สามารถถูกใจได้');
}
const result = await response.json();
likeCount.textContent = result.likes;
// Add visual feedback
likeBtn.classList.add('liked');
setTimeout(() => likeBtn.classList.remove('liked'), 1000);
} catch (error) {
console.error('Error liking post:', error);
alert('เกิดข้อผิดพลาดในการถูกใจ');
}
}
// Toggle comments section
async function toggleComments(e) {
const linkId = e.currentTarget.dataset.linkId;
const commentsSection = document.getElementById(`comments-${linkId}`);
if (commentsSection.style.display === 'none') {
commentsSection.style.display = 'block';
await loadComments(linkId);
} else {
commentsSection.style.display = 'none';
}
}
// Load comments for a specific link
async function loadComments(linkId) {
try {
const response = await fetch(`${API_BASE_URL}/posts/${linkId}/comments`);
if (!response.ok) {
throw new Error('ไม่สามารถโหลดคอมเมนต์ได้');
}
const comments = await response.json();
const commentsList = document.getElementById(`comments-list-${linkId}`);
if (comments.length === 0) {
commentsList.innerHTML = '<p style="text-align: center; color: #666; padding: 10px;">ยังไม่มีคอมเมนต์</p>';
} else {
commentsList.innerHTML = comments.map(comment => `
<div class="comment">
<div class="comment-author">
<i class="fas fa-user"></i> ${escapeHtml(comment.name)}
</div>
<div class="comment-text">${escapeHtml(comment.comment)}</div>
</div>
`).join('');
}
} catch (error) {
console.error('Error loading comments:', error);
}
}
// Handle add comment
async function handleAddComment(e) {
const linkId = e.currentTarget.dataset.linkId;
const commentsSection = document.getElementById(`comments-${linkId}`);
const commentInput = commentsSection.querySelector('.comment-input');
const commentText = commentInput.value.trim();
if (!commentText) {
alert('กรุณาเขียนคอมเมนต์');
return;
}
// Simple name prompt (in real app, you'd have user authentication)
const name = prompt('กรุณากรอกชื่อของคุณ:');
if (!name) return;
try {
const response = await fetch(`${API_BASE_URL}/posts/${linkId}/comments`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
name: name,
comment: commentText
})
});
if (!response.ok) {
throw new Error('ไม่สามารถเพิ่มคอมเมนต์ได้');
}
// Clear input
commentInput.value = '';
// Reload comments
await loadComments(linkId);
showSuccessMessage('เพิ่มคอมเมนต์สำเร็จ!');
} catch (error) {
console.error('Error adding comment:', error);
alert('เกิดข้อผิดพลาดในการเพิ่มคอมเมนต์');
}
}
// Utility functions
function isValidYouTubeURL(url) {
const youtubeRegex = /^(https?:\/\/)?(www\.)?(youtube\.com\/watch\?v=|youtu\.be\/|youtube\.com\/embed\/)[a-zA-Z0-9_-]{11}/;
return youtubeRegex.test(url);
}
function extractYouTubeVideoId(url) {
const regex = /(?:youtube\.com\/watch\?v=|youtu\.be\/|youtube\.com\/embed\/)([a-zA-Z0-9_-]{11})/;
const match = url.match(regex);
return match ? match[1] : null;
}
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
function getTimeAgo(date) {
const now = new Date();
const diffInSeconds = Math.floor((now - date) / 1000);
if (diffInSeconds < 60) return 'เมื่อสักครู่';
if (diffInSeconds < 3600) return `${Math.floor(diffInSeconds / 60)} นาทีที่แล้ว`;
if (diffInSeconds < 86400) return `${Math.floor(diffInSeconds / 3600)} ชั่วโมงที่แล้ว`;
return `${Math.floor(diffInSeconds / 86400)} วันที่แล้ว`;
}
function showLoading(show) {
loadingSpinner.style.display = show ? 'block' : 'none';
}
function showSuccessMessage(message) {
successText.textContent = message;
successMessage.style.display = 'flex';
setTimeout(() => {
successMessage.style.display = 'none';
}, 3000);
}
|