-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessages.html
More file actions
181 lines (164 loc) · 8.8 KB
/
messages.html
File metadata and controls
181 lines (164 loc) · 8.8 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>MyScratchBlocks - Messages</title>
<script src="https://cdn.tailwindcss.com"></script>
<style>
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');
body {
font-family: 'Inter', sans-serif;
background-color: #f8fafc;
color: #334155;
}
.loading-spinner {
border: 4px solid #f3f3f3;
border-top: 4px solid #3498db;
border-radius: 50%;
width: 24px;
height: 24px;
animation: spin 1s linear infinite;
display: inline-block;
vertical-align: middle;
margin-left: 8px;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
/* Custom styling for messages for better readability and appearance */
.message-item {
background-color: #ffffff;
padding: 1rem;
border-radius: 0.5rem;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
margin-bottom: 1rem;
word-wrap: break-word; /* Ensure long messages wrap */
}
.message-item a {
color: #4f46e5; /* Tailwind indigo-600 */
text-decoration: none;
}
.message-item a:hover {
text-decoration: underline;
}
</style>
</head>
<body class="antialiased">
<header class="bg-white shadow-sm py-4">
<div class="container mx-auto px-4 flex justify-between items-center">
<a href="/" class="flex items-center space-x-2 text-2xl font-bold text-indigo-600">
<span>MyScratchBlocks</span>
</a>
<nav class="hidden md:flex space-x-6">
<a href="/" class="text-gray-600 hover:text-indigo-600 font-medium transition-colors duration-200">Home</a>
<a href="community-projects" class="text-gray-600 hover:text-indigo-600 font-medium transition-colors duration-200">Community Projects</a>
<a href="AI-Assistant.html" class="text-gray-600 hover:text-indigo-600 font-medium transition-colors duration-200">AI Assistant</a>
<a href="/messages" class="text-indigo-600 hover:text-indigo-600 font-medium transition-colors duration-200">Messages</a>
<a href="account" class="text-gray-600 hover:text-indigo-600 font-medium transition-colors duration-200">Account</a>
</nav>
<button id="mobile-menu-button" class="md:hidden text-gray-600 hover:text-indigo-600 focus:outline-none">
<svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16"></path>
</svg>
</button>
</div>
<div id="mobile-menu" class="hidden md:hidden bg-white py-2 shadow-md">
<nav class="flex flex-col items-center space-y-2">
<a href="/" class="block text-gray-600 hover:text-indigo-600 font-medium py-1">Home</a>
<a href="community-projects" class="block text-gray-600 hover:text-indigo-600 font-medium py-1">Community Projects</a>
<a href="AI-Assistant.html" class="block text-gray-600 hover:text-indigo-600 font-medium py-1">AI Assistant</a>
<a href="/messages" class="block text-indigo-600 hover:text-indigo-600 font-medium py-1">Messages</a>
<a href="account" class="block text-gray-600 hover:text-indigo-600 font-medium py-1">Account</a>
</nav>
</div>
</header>
<main class="container mx-auto px-4 py-8">
<h1 class="text-3xl font-bold text-gray-800 mb-6 text-center">Your Messages</h1>
<div id="messages-container" class="max-w-2xl mx-auto">
<p id="messages-loading" class="text-center text-gray-600">Loading messages... <span class="loading-spinner"></span></p>
<p id="messages-error" class="text-center text-red-500 hidden">Failed to load messages. Please try again later.</p>
<p id="no-messages" class="text-center text-gray-600 hidden">You have no new messages.</p>
</div>
</main>
<footer class="bg-gray-800 text-white py-8 px-4 rounded-t-lg shadow-inner mt-8">
<div class="container mx-auto text-center">
<p class="text-sm mb-4">© 2025 MyScratchBlocks. All rights reserved.</p>
<div class="flex justify-center space-x-6">
<a href="/privacy-policy" class="text-gray-400 hover:text-white transition-colors duration-200">Privacy Policy</a>
<a href="https://scratch.mit.edu/community_guidelines" class="text-gray-400 hover:text-white transition-colors duration-200">Terms of Service</a>
<a href="https://scratch.mit.edu/users/MyScratchedAccount/" class="text-gray-400 hover:text-white transition-colors duration-200">Contact Us</a>
</div>
</div>
</footer>
<script>
document.addEventListener('DOMContentLoaded', function () {
// Mobile Menu Toggle
const mobileMenuButton = document.getElementById('mobile-menu-button');
const mobileMenu = document.getElementById('mobile-menu');
if (mobileMenuButton && mobileMenu) {
mobileMenuButton.addEventListener('click', () => {
mobileMenu.classList.toggle('hidden');
});
mobileMenu.querySelectorAll('a').forEach(link => {
link.addEventListener('click', () => {
mobileMenu.classList.add('hidden');
});
});
}
const messagesContainer = document.getElementById('messages-container');
const messagesLoading = document.getElementById('messages-loading');
const messagesError = document.getElementById('messages-error');
const noMessages = document.getElementById('no-messages');
async function fetchMessages() {
messagesLoading.classList.remove('hidden');
messagesError.classList.add('hidden');
noMessages.classList.add('hidden');
messagesContainer.innerHTML = ''; // Clear previous messages
const username = localStorage.getItem('username');
if (!username) {
messagesLoading.classList.add('hidden');
messagesError.classList.remove('hidden');
messagesError.textContent = 'You must be logged in to view messages.';
return;
}
// Using corsproxy.io for fetching due to potential CORS issues with the API
const MESSAGES_API_URL = `https://corsproxy.io/?url=https://editor-compiler.onrender.com/users/${username}/messages`;
try {
const response = await fetch(MESSAGES_API_URL);
if (!response.ok) {
if (response.status === 404) { // Assuming 404 means no user or no messages
messagesLoading.classList.add('hidden');
noMessages.classList.remove('hidden');
return;
}
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
const messages = data.messages;
if (!Array.isArray(messages) || messages.length === 0) {
noMessages.classList.remove('hidden');
} else {
const messages2 = messages.slice().reverse();
messages2.forEach(message => {
const messageDiv = document.createElement('div');
messageDiv.className = 'message-item';
// Using innerHTML to render the <a> tags from the API response
messageDiv.innerHTML = message;
messagesContainer.appendChild(messageDiv);
});
}
} catch (error) {
console.error('Error fetching messages:', error);
messagesError.classList.remove('hidden');
messagesError.textContent = `Failed to load messages. Please try again later. (Error: ${error.message})`;
} finally {
messagesLoading.classList.add('hidden');
}
}
fetchMessages();
});
</script>
</body>
</html>