-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcreate_post.html
More file actions
85 lines (71 loc) · 2.8 KB
/
create_post.html
File metadata and controls
85 lines (71 loc) · 2.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
{% extends "base.html" %}
{% block title %}Create Post - Blog{% endblock %}
{% block content %}
<div class="create-post-container">
<h1>Create New Post</h1>
<div id="message" class="message" style="display: none;"></div>
<form id="createPostForm" class="post-form">
<div class="form-group">
<label for="title">Post Title</label>
<input type="text" id="title" name="title" class="form-input" required>
</div>
<div class="form-group">
<label for="content">Post Content</label>
<textarea id="content" name="content" class="form-textarea" rows="10" required></textarea>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Create Post</button>
<a href="{{ url_for('index') }}" class="btn btn-secondary">Cancel</a>
</div>
</form>
</div>
{% endblock %}
{% block scripts %}
<script>
document.getElementById('createPostForm').addEventListener('submit', async function(e) {
e.preventDefault();
const title = document.getElementById('title').value;
const content = document.getElementById('content').value;
const messageDiv = document.getElementById('message');
if (!title || !content) {
showMessage('Please fill in all fields', 'error');
return;
}
try {
const response = await fetch('/api/posts', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
title: title,
content: content
})
});
const data = await response.json();
if (data.success) {
showMessage('Post created successfully! Redirecting...', 'success');
setTimeout(() => {
window.location.href = '/';
}, 1500);
} else {
showMessage('Error: ' + data.message, 'error');
}
} catch (error) {
console.error('Error:', error);
showMessage('An error occurred. Please try again.', 'error');
}
});
function showMessage(message, type) {
const messageDiv = document.getElementById('message');
messageDiv.textContent = message;
messageDiv.className = `message ${type}`;
messageDiv.style.display = 'block';
if (type === 'success') {
setTimeout(() => {
messageDiv.style.display = 'none';
}, 3000);
}
}
</script>
{% endblock %}