-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
246 lines (229 loc) · 8.97 KB
/
index.html
File metadata and controls
246 lines (229 loc) · 8.97 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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Loading...</title>
<style>
/* Add your styles here as needed */
.post { border: 1px solid #ccc; margin: 1em 0; padding: 1em; }
.post-title { font-weight: bold; font-size: 1.2em; cursor: pointer; }
.modal-bg { position: fixed; top:0; left:0; right:0; bottom:0; background:rgba(0,0,0,0.6); display:none; align-items:center; justify-content:center;}
.modal { background:#fff; padding:2em; border-radius:8px; max-width:600px; width:90%; }
.modal-close { float:right; cursor:pointer; }
</style>
</head>
<body>
<h1 id="blog-title">Loading...</h1>
<div class="controls">
<select id="filter-tags" multiple size="1" style="min-width:120px;">
<option value="">Filter by Tag</option>
</select>
<select id="filter-type">
<option value="">Filter by Type</option>
</select>
<select id="filter-author">
<option value="">Filter by Author</option>
</select>
<label>Date from: <input type="date" id="filter-date-from"></label>
<label>Date to: <input type="date" id="filter-date-to"></label>
<input type="text" id="search-input" placeholder="Search posts...">
<button id="search-btn">Search</button>
<button id="reset-btn">Reset</button>
<a href="editor.html" id="edit-link" style="margin-left:2em;">Edit Blog</a>
<span id="current-user"></span>
<button id="sign-out-btn" style="display:none;">Sign Out</button>
</div>
<div id="posts"></div>
<div class="modal-bg" id="modal-bg">
<div class="modal" id="modal-content">
<span class="modal-close" id="modal-close">×</span>
<div id="modal-body"></div>
</div>
</div>
<script>
let blog = { title: "", posts: [] };
let users = [];
let signedInUser = null;
let sessionId = localStorage.getItem('sessionId');
if (!sessionId) {
const array = new Uint8Array(16); // Generate 16 random bytes
window.crypto.getRandomValues(array);
sessionId = Array.from(array, byte => byte.toString(16).padStart(2, '0')).join('');
localStorage.setItem('sessionId', sessionId);
}
function fetchWithSession(url, opts = {}) {
opts.headers = opts.headers || {};
opts.headers['x-session-id'] = sessionId;
return fetch(url, opts);
}
async function loadBlogAndUsers() {
const [blogRes, usersRes, userRes] = await Promise.all([
fetchWithSession('/api/blog'),
fetchWithSession('/api/users'),
fetchWithSession('/api/currentUser')
]);
blog = await blogRes.json();
users = await usersRes.json();
const userData = await userRes.json();
signedInUser = userData.user;
updateUI();
}
function updateUI() {
document.title = blog.title;
document.getElementById('blog-title').textContent = blog.title;
populateFilters();
renderPosts(blog.posts);
document.getElementById('current-user').textContent = signedInUser ? ('Signed in as: ' + signedInUser) : 'Not signed in';
document.getElementById('sign-out-btn').style.display = signedInUser ? '' : 'none';
}
function populateFilters() {
const filterTags = document.getElementById('filter-tags');
const filterType = document.getElementById('filter-type');
const filterAuthor = document.getElementById('filter-author');
filterTags.innerHTML = '<option value="">Filter by Tag</option>';
filterType.innerHTML = '<option value="">Filter by Type</option>';
filterAuthor.innerHTML = '<option value="">Filter by Author</option>';
const allTags = [...new Set(blog.posts.flatMap(post => post.tags))];
allTags.forEach(tag => {
const opt = document.createElement('option');
opt.value = tag;
opt.textContent = tag;
filterTags.appendChild(opt);
});
const allTypes = [...new Set(blog.posts.map(post => post.type))];
allTypes.forEach(type => {
const opt = document.createElement('option');
opt.value = type;
opt.textContent = type;
filterType.appendChild(opt);
});
const allAuthors = [...new Set(blog.posts.map(post => post.author))];
allAuthors.forEach(author => {
const opt = document.createElement('option');
opt.value = author;
opt.textContent = author;
filterAuthor.appendChild(opt);
});
}
function renderPosts(posts) {
const postsDiv = document.getElementById('posts');
postsDiv.innerHTML = '';
posts.forEach(post => {
const postDiv = document.createElement('div');
postDiv.className = 'post';
const postTitle = document.createElement('div');
postTitle.className = 'post-title';
postTitle.textContent = post.title;
postTitle.onclick = () => showPostDetail(post);
const postDate = document.createElement('div');
postDate.className = 'post-date';
postDate.textContent = 'Date: ' + post.date;
const postTags = document.createElement('div');
postTags.className = 'post-tags';
postTags.textContent = 'Tags: ' + post.tags.join(', ');
const postType = document.createElement('div');
postType.className = 'post-type';
postType.textContent = 'Type: ' + post.type;
const postAuthor = document.createElement('div');
postAuthor.className = 'post-author';
postAuthor.textContent = 'Author: ' + post.author;
const postContent = document.createElement('div');
postContent.className = 'post-content';
postContent.textContent = post.content;
postDiv.appendChild(postTitle);
postDiv.appendChild(postDate);
postDiv.appendChild(postTags);
postDiv.appendChild(postType);
postDiv.appendChild(postAuthor);
postDiv.appendChild(postContent);
postsDiv.appendChild(postDiv);
});
}
function showPostDetail(post) {
openModal(`
<h2>${post.title}</h2>
<div><b>Date:</b> ${post.date}</div>
<div><b>Tags:</b> ${post.tags.join(', ')}</div>
<div><b>Type:</b> ${post.type}</div>
<div><b>Author:</b> ${post.author}</div>
<hr>
<div>${post.content.replace(/\n/g,'<br>')}</div>
`);
}
function openModal(html) {
document.getElementById('modal-body').innerHTML = html;
document.getElementById('modal-bg').style.display = 'flex';
}
document.getElementById('modal-close').onclick = function(){
document.getElementById('modal-bg').style.display = 'none';
};
function filterAndSearch() {
const filterTags = document.getElementById('filter-tags');
const filterType = document.getElementById('filter-type');
const filterAuthor = document.getElementById('filter-author');
const filterDateFrom = document.getElementById('filter-date-from');
const filterDateTo = document.getElementById('filter-date-to');
const selectedTags = Array.from(filterTags.selectedOptions).map(opt => opt.value).filter(Boolean);
const selectedType = filterType.value;
const selectedAuthor = filterAuthor.value;
const dateFrom = filterDateFrom.value;
const dateTo = filterDateTo.value;
const searchText = document.getElementById('search-input').value.trim().toLowerCase();
let filtered = blog.posts;
if (selectedTags.length > 0) {
filtered = filtered.filter(post => selectedTags.every(tag => post.tags.includes(tag)));
}
if (selectedType) {
filtered = filtered.filter(post => post.type === selectedType);
}
if (selectedAuthor) {
filtered = filtered.filter(post => post.author === selectedAuthor);
}
if (dateFrom) {
filtered = filtered.filter(post => post.date >= dateFrom);
}
if (dateTo) {
filtered = filtered.filter(post => post.date <= dateTo);
}
if (searchText) {
filtered = filtered.filter(post =>
post.title.toLowerCase().includes(searchText) ||
post.content.toLowerCase().includes(searchText) ||
post.tags.some(tag => tag.toLowerCase().includes(searchText)) ||
post.type.toLowerCase().includes(searchText) ||
(post.author && post.author.toLowerCase().includes(searchText))
);
}
renderPosts(filtered);
}
document.getElementById('filter-tags').addEventListener('change', filterAndSearch);
document.getElementById('filter-type').addEventListener('change', filterAndSearch);
document.getElementById('filter-author').addEventListener('change', filterAndSearch);
document.getElementById('filter-date-from').addEventListener('change', filterAndSearch);
document.getElementById('filter-date-to').addEventListener('change', filterAndSearch);
document.getElementById('search-btn').addEventListener('click', filterAndSearch);
document.getElementById('search-input').addEventListener('keydown', function(e) {
if (e.key === 'Enter') filterAndSearch();
});
document.getElementById('reset-btn').addEventListener('click', function() {
document.getElementById('filter-tags').selectedIndex = -1;
document.getElementById('filter-type').value = '';
document.getElementById('filter-author').value = '';
document.getElementById('filter-date-from').value = '';
document.getElementById('filter-date-to').value = '';
document.getElementById('search-input').value = '';
renderPosts(blog.posts);
});
document.getElementById('sign-out-btn').onclick = async function() {
await fetchWithSession('/api/currentUser', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({ user: null })
});
signedInUser = null;
updateUI();
};
window.onload = loadBlogAndUsers;
</script>
</body>
</html>