Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions Forum/app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from flask import Flask, request, jsonify
from auth_utils import token_required, roles_required
from flask_cors import CORS
import re
from functools import wraps
Expand Down Expand Up @@ -48,14 +47,9 @@ def validate_post_data(data):
forum_posts = []

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

return jsonify({"status": "success", "message": "Post created successfully"}), 201
return jsonify({
"status": "success",
"message": "Post created successfully",
"post": sanitized_data
}), 201
else:
return jsonify(forum_posts)

Expand Down
43 changes: 40 additions & 3 deletions Forum/forum.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ document.getElementById('forumForm').addEventListener('submit', function (e) {
if (!res.ok) throw new Error('Network response was not ok');
return res.json();
})
.then(() => {
loadPosts();
.then(data => {
e.target.reset();
// Show simple success feedback (optional)
if (data && data.post) {
prependPost(data.post);
} else {
loadPosts();
}
alert('Post created successfully!');
})
.catch(err => {
Expand Down Expand Up @@ -92,6 +95,40 @@ function loadPosts() {
});
}

function prependPost(post) {
const container = document.getElementById('forumPosts');
if (!container) return;

const emptyState = container.querySelector('.empty-state-card');
if (emptyState) {
emptyState.remove();
}

const el = document.createElement('div');
el.className = 'forum-post';

const title = post.title || 'Untitled Discussion';
const author = post.author || 'Anonymous';
const content = post.content || '';

el.innerHTML = `
<div class="post-meta">
<div class="author-avatar">
<i class="fas fa-user"></i>
</div>
<strong>${escapeHtml(author)}</strong>
<span>•</span>
<span>Just now</span>
</div>
<h3 class="post-title">${escapeHtml(title)}</h3>
<div class="post-content">
${escapeHtml(content)}
</div>
`;

container.prepend(el);
}

// Helper to prevent XSS
function escapeHtml(text) {
if (!text) return '';
Expand Down
Loading