import React, {useEffect, useState} from 'react'; import NewPostForm from './components/NewPostForm'; import './App.scss'; import {FontAwesomeIcon} from "@fortawesome/react-fontawesome"; import {faPenToSquare, faRotate, faTimes} from "@fortawesome/free-solid-svg-icons"; import {postApi} from "./api/posts.api"; import {locationUtils} from "./utils/location"; import Post from "./components/Post"; import keycloak from "./Authentification/Keycloak"; const App = () => { const [posts, setPosts] = useState([]); const [selectedPost, selectPost] = useState(null); const [comment, setComment] = useState(''); useEffect(() => { if(posts.length === 0) { reload().then(null); } }); useEffect(() => { if(selectedPost) { selectPost(posts.find(post => post.id === selectedPost.id)); } }, [posts]); const reload = async () => { await loadPosts().then(posts => { posts = recursiveSort(posts); setPosts(posts); }); } const recursiveSort = (posts) => { posts.forEach((post, index) => {posts[index].comments = recursiveSort(post.comments)}); posts.sort((a, b) => a.date < b.date ? 1 : -1); return posts; } const loadPosts = async () => { const location = await locationUtils.getCurrentLocation(); return await postApi.getPostsByCoords(location.lat, location.lon); } 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 handleCommentSubmit = async (e) => { e.preventDefault(); if (comment.trim() && selectedPost) { await addComment(selectedPost.id, comment); setComment(''); await reload(); selectPost(selectedPost); } }; const closePost = () => { selectPost(null); }; return (