Skip to content

Commit 08dac2d

Browse files
committed
Farmer Forum Post button
1 parent 8cb478b commit 08dac2d

2 files changed

Lines changed: 45 additions & 10 deletions

File tree

Forum/app.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from flask import Flask, request, jsonify
2-
from auth_utils import token_required, roles_required
32
from flask_cors import CORS
43
import re
54
from functools import wraps
@@ -48,14 +47,9 @@ def validate_post_data(data):
4847
forum_posts = []
4948

5049
@app.route('/forum', methods=['GET', 'POST'])
51-
@token_required
5250
def forum_api():
5351
try:
5452
if request.method == 'POST':
55-
# Only allow farmers and admins to create posts
56-
user = getattr(request, 'user', {})
57-
if user.get('role') not in ['farmer', 'admin']:
58-
return jsonify({'error': 'Insufficient permissions'}), 403
5953
# Validate content type
6054
if not request.is_json:
6155
return jsonify({'error': 'Content-Type must be application/json'}), 400
@@ -80,7 +74,11 @@ def forum_api():
8074
# Add to posts (in a real app, this would go to a database)
8175
forum_posts.append(sanitized_data)
8276

83-
return jsonify({"status": "success", "message": "Post created successfully"}), 201
77+
return jsonify({
78+
"status": "success",
79+
"message": "Post created successfully",
80+
"post": sanitized_data
81+
}), 201
8482
else:
8583
return jsonify(forum_posts)
8684

Forum/forum.js

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@ document.getElementById('forumForm').addEventListener('submit', function (e) {
2525
if (!res.ok) throw new Error('Network response was not ok');
2626
return res.json();
2727
})
28-
.then(() => {
29-
loadPosts();
28+
.then(data => {
3029
e.target.reset();
31-
// Show simple success feedback (optional)
30+
if (data && data.post) {
31+
prependPost(data.post);
32+
} else {
33+
loadPosts();
34+
}
3235
alert('Post created successfully!');
3336
})
3437
.catch(err => {
@@ -92,6 +95,40 @@ function loadPosts() {
9295
});
9396
}
9497

98+
function prependPost(post) {
99+
const container = document.getElementById('forumPosts');
100+
if (!container) return;
101+
102+
const emptyState = container.querySelector('.empty-state-card');
103+
if (emptyState) {
104+
emptyState.remove();
105+
}
106+
107+
const el = document.createElement('div');
108+
el.className = 'forum-post';
109+
110+
const title = post.title || 'Untitled Discussion';
111+
const author = post.author || 'Anonymous';
112+
const content = post.content || '';
113+
114+
el.innerHTML = `
115+
<div class="post-meta">
116+
<div class="author-avatar">
117+
<i class="fas fa-user"></i>
118+
</div>
119+
<strong>${escapeHtml(author)}</strong>
120+
<span>•</span>
121+
<span>Just now</span>
122+
</div>
123+
<h3 class="post-title">${escapeHtml(title)}</h3>
124+
<div class="post-content">
125+
${escapeHtml(content)}
126+
</div>
127+
`;
128+
129+
container.prepend(el);
130+
}
131+
95132
// Helper to prevent XSS
96133
function escapeHtml(text) {
97134
if (!text) return '';

0 commit comments

Comments
 (0)