import React, {useEffect, useState} from 'react'; import PostCommentList from './components/PostCommentList'; import NewPostForm from './components/NewPostForm'; import './App.scss'; import {FontAwesomeIcon} from "@fortawesome/react-fontawesome"; import {faTimes} from "@fortawesome/free-solid-svg-icons"; import {postApi} from "./api/posts.api"; import {locationUtils} from "./utils/location"; import PostComment from "./components/PostComment"; import keycloak from "./Authentification/Keycloak"; const App = () => { const [posts, setPosts] = useState([]); const [comments, setComments] = useState([]); const [selectedPost, setSelectedPost] = useState(null); const [comment, setComment] = useState(''); useEffect(() => { if(posts.length === 0) { 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(); return await postApi.getPostsByCoords(location.lon, location.lat); } const addPost = async (title, text) => { await postApi.createNewPost(title, text); await reload(); }; 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 = async (e) => { e.preventDefault(); if (comment.trim() && selectedPost) { await addComment(selectedPost.id, comment); setComment(''); await reload(); handlePostClick(selectedPost); } }; const closePost = () => { setSelectedPost(null); setComment(''); }; return (

SWA - Jodel

{!selectedPost && (
)} {selectedPost && (