From 5ef0b504e1c3397ad65dffdcc59dfa67fb9d6ac3 Mon Sep 17 00:00:00 2001 From: Pete Gerlach Date: Tue, 11 Jun 2024 15:26:17 +0200 Subject: [PATCH] restructured code (modal => internal view) --- src/App.js | 139 ++++++++---------- src/App.scss | 286 ++++++++++++++++++++------------------ src/components/Modal.js | 21 --- src/components/Modal.scss | 34 ----- 4 files changed, 213 insertions(+), 267 deletions(-) delete mode 100644 src/components/Modal.js delete mode 100644 src/components/Modal.scss diff --git a/src/App.js b/src/App.js index cb94a72..297009b 100644 --- a/src/App.js +++ b/src/App.js @@ -1,12 +1,12 @@ import React, {useEffect, useState} from 'react'; import PostList from './components/PostList'; import NewPostForm from './components/NewPostForm'; -import Modal from './components/Modal'; import './App.scss'; import {FontAwesomeIcon} from "@fortawesome/react-fontawesome"; -import {faCaretDown, faCaretUp} from "@fortawesome/free-solid-svg-icons"; +import {faCaretDown, faCaretUp, faTimes} from "@fortawesome/free-solid-svg-icons"; import {postApi} from "./api/posts.api"; import {locationUtils} from "./utils/location"; +import Post from "./components/Post"; const App = () => { const [posts, setPosts] = useState([]); @@ -15,7 +15,9 @@ const App = () => { useEffect(() => { if(posts.length === 0) { - loadPosts().then(data => setPosts(data)) + loadPosts().then(data => setPosts(data)); + } else { + console.log(posts); } }) @@ -24,50 +26,17 @@ const App = () => { return await postApi.getPostsByCoords(location.lon, location.lat); } - const addPost = (text) => { - const newPost = { id: Date.now(), text, comments: [], upvotes: 0, downvotes: 0 }; - setPosts([newPost, ...posts]); + const addPost = async (title, text) => { + await postApi.createNewPost(title, text); + loadPosts().then(data => setPosts(data)); }; const addComment = (postId, commentText) => { - const newComment = { id: Date.now(), text: commentText, upvotes: 0, downvotes: 0 }; - setPosts(posts.map(post => - post.id === postId ? { ...post, comments: [...post.comments, newComment] } : post - )); + }; - const upvotePost = (postId) => { - setPosts(posts.map(post => - post.id === postId ? { ...post, upvotes: post.upvotes + 1 } : post - )); - }; - - const downvotePost = (postId) => { - setPosts(posts.map(post => - post.id === postId ? { ...post, downvotes: post.downvotes + 1 } : post - )); - }; - - const upvoteComment = (postId, commentId) => { - setPosts(posts.map(post => - post.id === postId ? { - ...post, - comments: post.comments.map(comment => - comment.id === commentId ? { ...comment, upvotes: comment.upvotes + 1 } : comment - ) - } : post - )); - }; - - const downvoteComment = (postId, commentId) => { - setPosts(posts.map(post => - post.id === postId ? { - ...post, - comments: post.comments.map(comment => - comment.id === commentId ? { ...comment, downvotes: comment.downvotes + 1 } : comment - ) - } : post - )); + const vote = (postId, reaction) => { + // TODO postApi.reactToPost(postId) }; const handlePostClick = (post) => { @@ -82,56 +51,66 @@ const App = () => { } }; - const closeModal = () => { + const closePost = () => { setSelectedPost(null); setComment(''); }; return (
-

SWA - Jodel

- - - - {selectedPost && ( -
-

{selectedPost.text}

-
- +

SWA - Jodel

+ + +
+ )} + {selectedPost && ( +
+
+ +
+ + +
+