diff --git a/Forum/app.py b/Forum/app.py index 7b03ad2b..ebdcd58c 100644 --- a/Forum/app.py +++ b/Forum/app.py @@ -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 @@ -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 @@ -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) diff --git a/Forum/forum.js b/Forum/forum.js index e4f20cf8..c219c7aa 100644 --- a/Forum/forum.js +++ b/Forum/forum.js @@ -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 => { @@ -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 = ` +
+