From 5bd0e355326d1ee1a751ce40d3c7a2fd8e4327ff Mon Sep 17 00:00:00 2001 From: Pete Gerlach Date: Tue, 11 Jun 2024 18:02:18 +0200 Subject: [PATCH] made all available features functional and reloaded components after updating posts successfully --- src/App.js | 68 ++++++++++++++++++------------- src/App.scss | 6 ++- src/components/PostCommentList.js | 3 +- 3 files changed, 45 insertions(+), 32 deletions(-) diff --git a/src/App.js b/src/App.js index 297009b..1e15f55 100644 --- a/src/App.js +++ b/src/App.js @@ -1,25 +1,43 @@ import React, {useEffect, useState} from 'react'; -import PostList from './components/PostList'; +import PostCommentList from './components/PostCommentList'; import NewPostForm from './components/NewPostForm'; import './App.scss'; import {FontAwesomeIcon} from "@fortawesome/react-fontawesome"; -import {faCaretDown, faCaretUp, faTimes} from "@fortawesome/free-solid-svg-icons"; +import {faTimes} from "@fortawesome/free-solid-svg-icons"; import {postApi} from "./api/posts.api"; import {locationUtils} from "./utils/location"; -import Post from "./components/Post"; +import PostComment from "./components/PostComment"; const App = () => { const [posts, setPosts] = useState([]); + const [comments, setComments] = useState([]); const [selectedPost, setSelectedPost] = useState(null); const [comment, setComment] = useState(''); useEffect(() => { if(posts.length === 0) { - loadPosts().then(data => setPosts(data)); - } else { - console.log(posts); + reload().then(null); } - }) + }); + + useEffect(() => { + if(selectedPost) { + setSelectedPost(posts.find(post => post.id === selectedPost.id)); + } + console.log(posts); + }, [posts]); + + useEffect(() => { + if(selectedPost) { + setComments(selectedPost.comments); + } + }, [selectedPost]); + + const reload = async () => { + await loadPosts().then(data => { + setPosts(data); + }); + } const loadPosts = async () => { const location = await locationUtils.getCurrentLocation(); @@ -28,26 +46,29 @@ const App = () => { const addPost = async (title, text) => { await postApi.createNewPost(title, text); - loadPosts().then(data => setPosts(data)); + await reload(); }; - const addComment = (postId, commentText) => { - + const addComment = async (postId, commentText) => { + await postApi.createNewPost("", commentText, postId).then(await reload); }; const vote = (postId, reaction) => { // TODO postApi.reactToPost(postId) + console.log(postId, reaction); }; const handlePostClick = (post) => { setSelectedPost(post); }; - const handleCommentSubmit = (e) => { + const handleCommentSubmit = async (e) => { e.preventDefault(); if (comment.trim() && selectedPost) { - addComment(selectedPost.id, comment); + await addComment(selectedPost.id, comment); setComment(''); + await reload(); + handlePostClick(selectedPost); } }; @@ -58,15 +79,16 @@ const App = () => { return (
+

SWA - Jodel

{!selectedPost && (
-

SWA - Jodel

-
)} @@ -75,7 +97,7 @@ const App = () => {
- +