-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost.js
More file actions
90 lines (78 loc) · 2.96 KB
/
Copy pathpost.js
File metadata and controls
90 lines (78 loc) · 2.96 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
// post.js
// API endpoint URL
const url = 'http://127.0.0.1:5000';
// Get form and status elements
const form = document.getElementById('postForm');
const error = document.getElementById('error');
const success = document.getElementById('success');
// Get input elements for form submission
const titleInput = document.getElementById('title');
const contentInput = document.getElementById('content');
// Get the user token from local storage
const token = localStorage.getItem('token');
// Redirect to login if token is missing
if(!token) {
window.location.href = '/login.html';
}
// Mobile menu elements
const mobileMenuButton = document.getElementById('mobile-menu-button');
const mobileMenu = document.getElementById('mobile-menu');
const menuIcon = document.getElementById('menu-icon');
const closeIcon = document.getElementById('close-icon');
// Event listener for the mobile menu button
if (mobileMenuButton && mobileMenu && menuIcon && closeIcon) {
mobileMenuButton.addEventListener('click', () => {
// Toggle the visibility of the dropdown menu
mobileMenu.classList.toggle('hidden');
// Toggle the hamburger/close icons
menuIcon.classList.toggle('hidden');
closeIcon.classList.toggle('hidden');
// Toggle WAI-ARIA expanded state for accessibility
const isExpanded = mobileMenuButton.getAttribute('aria-expanded') === 'true';
mobileMenuButton.setAttribute('aria-expanded', !isExpanded);
});
}
// Event listener for form submission
form.addEventListener('submit', async (e) => {
e.preventDefault();
error.textContent = '';
success.textContent = '';
// Get input values, correcting the previous typo
const title = titleInput.value.trim();
const content = contentInput.value.trim();
// Validate that both fields are filled
if(!title || !content) {
error.textContent = 'Please fill in both fields';
return;
}
try{
// Send POST request to create the new post
const res = await axios.post(`${url}/post`, { title, content},
{
headers: {
'Authorization' : `Bearer ${token}`,
'Content-Type' : 'application/json'
}
}
);
// Handle successful post creation
if(res.data.success) {
success.textContent = 'Post created successfully!';
form.reset();
} else {
// Handle API-reported error
error.textContent = res.data.message || 'Failed to create post.'
}
}catch(err) {
// Handle network or other request errors
console.error(err.response?.data || err.message);
error.textContent = err.response?.data?.message || 'Error creating post. Please try again.';
}
});
// Function to handle user logout
function logout() {
// Remove the user token
localStorage.removeItem('token');
// Redirect to the login page
window.location.href = '/login.html';
}