diff --git a/frontend/src/pages/Posts.tsx b/frontend/src/pages/Posts.tsx index a2ea6335..8a8a074b 100644 --- a/frontend/src/pages/Posts.tsx +++ b/frontend/src/pages/Posts.tsx @@ -1,26 +1,26 @@ -import { useEffect, useState, useRef } from 'react'; -import axios from 'axios'; -import { Link } from 'react-router-dom'; -import { PostData } from '../types'; -import Loader from '../components/Loader'; +import { useEffect, useState, useRef } from "react"; +import axios from "axios"; +import { Link } from "react-router-dom"; +import { PostData } from "../types"; +import Loader from "../components/Loader"; const Posts = () => { const [posts, setPosts] = useState([]); const [loading, setLoading] = useState(true); - const [error, setError] = useState(''); + const [error, setError] = useState(""); const [showFilterDialog, setShowFilterDialog] = useState(false); - const [tagInput, setTagInput] = useState(''); + const [tagInput, setTagInput] = useState(""); const [filterTags, setFilterTags] = useState([]); const filterRef = useRef(null); useEffect(() => { const fetchPosts = async () => { try { - const response = await axios.get('/api/v1/posts'); + const response = await axios.get("/api/v1/posts"); setPosts(response.data.posts); setLoading(false); } catch (error) { - setError('Failed to fetch posts'); + setError("Failed to fetch posts"); setLoading(false); } }; @@ -30,14 +30,17 @@ const Posts = () => { useEffect(() => { const handleClickOutside = (event: MouseEvent) => { - if (filterRef.current && !filterRef.current.contains(event.target as Node)) { + if ( + filterRef.current && + !filterRef.current.contains(event.target as Node) + ) { setShowFilterDialog(false); } }; - document.addEventListener('mousedown', handleClickOutside); + document.addEventListener("mousedown", handleClickOutside); return () => { - document.removeEventListener('mousedown', handleClickOutside); + document.removeEventListener("mousedown", handleClickOutside); }; }, []); @@ -48,35 +51,41 @@ const Posts = () => { const addTag = () => { if (tagInput && !filterTags.includes(tagInput.toLowerCase())) { setFilterTags([...filterTags, tagInput.toLowerCase()]); - setTagInput(''); + setTagInput(""); } }; const removeTag = (tagToRemove: string) => { - setFilterTags(filterTags.filter(tag => tag !== tagToRemove)); + setFilterTags(filterTags.filter((tag) => tag !== tagToRemove)); }; const handleKeyDown = (event: React.KeyboardEvent) => { - if (event.key === 'Enter') { + if (event.key === "Enter") { event.preventDefault(); addTag(); } }; - const filteredPosts = posts.filter(post => - filterTags.every(tag => post.tags.map(t => t.toLowerCase()).includes(tag)) + const filteredPosts = posts.filter((post) => + filterTags.every((tag) => + post.tags.map((t) => t.toLowerCase()).includes(tag) + ) ); if (loading) { - return ; + return ; } if (error) { - return
{error}
; + return ( +
+ {error} +
+ ); } return ( -
+

Posts

{showFilterDialog && ( @@ -111,13 +131,20 @@ const Posts = () => {
{filterTags.map((tag, index) => ( -
+
{tag} @@ -129,16 +156,35 @@ const Posts = () => {
{filteredPosts.map((post) => ( -
-

{post.title}

-

{post.description.length > 100 ? `${post.description.slice(0, 100)}...` : post.description}

+
+

+ {post.title} +

+

+ {post.description.length > 100 + ? `${post.description.slice(0, 100)}...` + : post.description} +

By: {post.author.username}

{post.tags.map((tag, index) => ( - {tag} + + {tag} + ))}
- Read more + + Read more +
))}