made all available features functional and reloaded components after updating posts successfully
This commit is contained in:
68
src/App.js
68
src/App.js
@@ -1,25 +1,43 @@
|
|||||||
import React, {useEffect, useState} from 'react';
|
import React, {useEffect, useState} from 'react';
|
||||||
import PostList from './components/PostList';
|
import PostCommentList from './components/PostCommentList';
|
||||||
import NewPostForm from './components/NewPostForm';
|
import NewPostForm from './components/NewPostForm';
|
||||||
import './App.scss';
|
import './App.scss';
|
||||||
import {FontAwesomeIcon} from "@fortawesome/react-fontawesome";
|
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 {postApi} from "./api/posts.api";
|
||||||
import {locationUtils} from "./utils/location";
|
import {locationUtils} from "./utils/location";
|
||||||
import Post from "./components/Post";
|
import PostComment from "./components/PostComment";
|
||||||
|
|
||||||
const App = () => {
|
const App = () => {
|
||||||
const [posts, setPosts] = useState([]);
|
const [posts, setPosts] = useState([]);
|
||||||
|
const [comments, setComments] = useState([]);
|
||||||
const [selectedPost, setSelectedPost] = useState(null);
|
const [selectedPost, setSelectedPost] = useState(null);
|
||||||
const [comment, setComment] = useState('');
|
const [comment, setComment] = useState('');
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if(posts.length === 0) {
|
if(posts.length === 0) {
|
||||||
loadPosts().then(data => setPosts(data));
|
reload().then(null);
|
||||||
} else {
|
|
||||||
console.log(posts);
|
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
|
|
||||||
|
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 loadPosts = async () => {
|
||||||
const location = await locationUtils.getCurrentLocation();
|
const location = await locationUtils.getCurrentLocation();
|
||||||
@@ -28,26 +46,29 @@ const App = () => {
|
|||||||
|
|
||||||
const addPost = async (title, text) => {
|
const addPost = async (title, text) => {
|
||||||
await postApi.createNewPost(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) => {
|
const vote = (postId, reaction) => {
|
||||||
// TODO postApi.reactToPost(postId)
|
// TODO postApi.reactToPost(postId)
|
||||||
|
console.log(postId, reaction);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handlePostClick = (post) => {
|
const handlePostClick = (post) => {
|
||||||
setSelectedPost(post);
|
setSelectedPost(post);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleCommentSubmit = (e) => {
|
const handleCommentSubmit = async (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
if (comment.trim() && selectedPost) {
|
if (comment.trim() && selectedPost) {
|
||||||
addComment(selectedPost.id, comment);
|
await addComment(selectedPost.id, comment);
|
||||||
setComment('');
|
setComment('');
|
||||||
|
await reload();
|
||||||
|
handlePostClick(selectedPost);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -58,15 +79,16 @@ const App = () => {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="app">
|
<div className="app">
|
||||||
|
<h1>SWA - Jodel</h1>
|
||||||
{!selectedPost && (
|
{!selectedPost && (
|
||||||
<div>
|
<div>
|
||||||
<h1>SWA - Jodel</h1>
|
|
||||||
<NewPostForm addPost={addPost}/>
|
<NewPostForm addPost={addPost}/>
|
||||||
<PostList
|
<PostCommentList
|
||||||
posts={posts}
|
posts={posts}
|
||||||
addComment={addComment}
|
addComment={addComment}
|
||||||
vote={vote}
|
vote={vote}
|
||||||
onPostClick={handlePostClick}
|
onPostClick={handlePostClick}
|
||||||
|
type={"posts"}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
@@ -75,7 +97,7 @@ const App = () => {
|
|||||||
<div className="close-post">
|
<div className="close-post">
|
||||||
<FontAwesomeIcon className={"close-button"} onClick={closePost} icon={faTimes}/>
|
<FontAwesomeIcon className={"close-button"} onClick={closePost} icon={faTimes}/>
|
||||||
</div>
|
</div>
|
||||||
<Post post={selectedPost} vote={vote}></Post>
|
<PostComment post={selectedPost} vote={vote} type={"post"}></PostComment>
|
||||||
<form className="comment-form" onSubmit={handleCommentSubmit}>
|
<form className="comment-form" onSubmit={handleCommentSubmit}>
|
||||||
<div className="textarea-container">
|
<div className="textarea-container">
|
||||||
<textarea
|
<textarea
|
||||||
@@ -94,20 +116,8 @@ const App = () => {
|
|||||||
<button type="submit">Kommentieren</button>
|
<button type="submit">Kommentieren</button>
|
||||||
</form>
|
</form>
|
||||||
<div className="comments">
|
<div className="comments">
|
||||||
{selectedPost.comments.map((comment) => (
|
{comments && <PostCommentList posts={comments} onPostClick={null} vote={vote}
|
||||||
<div key={comment.id} className="comment">
|
type={"comments"}></PostCommentList>}
|
||||||
<p>{comment.text}</p>
|
|
||||||
<div className="votes">
|
|
||||||
<button onClick={() => vote(selectedPost.id, comment.id)}>
|
|
||||||
<FontAwesomeIcon icon={faCaretUp}/>
|
|
||||||
</button>
|
|
||||||
<span>{comment.upvotes - comment.downvotes}</span>
|
|
||||||
<button onClick={() => vote(selectedPost.id, comment.id)}>
|
|
||||||
<FontAwesomeIcon icon={faCaretDown}/>
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
))}
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|||||||
@@ -28,7 +28,6 @@ h3 {
|
|||||||
.new-post-form {
|
.new-post-form {
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
margin-bottom: 20px;
|
|
||||||
|
|
||||||
button {
|
button {
|
||||||
padding: 15px;
|
padding: 15px;
|
||||||
@@ -45,6 +44,11 @@ h3 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.post-list {
|
||||||
|
margin-top: 20px;
|
||||||
|
border-top: 1px solid lightgray;
|
||||||
|
}
|
||||||
|
|
||||||
.post {
|
.post {
|
||||||
display: flex;
|
display: flex;
|
||||||
justify-content: space-between;
|
justify-content: space-between;
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import PostComment from './PostComment';
|
|||||||
const PostCommentList = ({ posts, vote, onPostClick, type }) => {
|
const PostCommentList = ({ posts, vote, onPostClick, type }) => {
|
||||||
return (
|
return (
|
||||||
<div className="post-list">
|
<div className="post-list">
|
||||||
|
<h3>{type === "posts" ? "Posts" : "Kommentare"}</h3>
|
||||||
{type === "posts" && posts.map(post => (
|
{type === "posts" && posts.map(post => (
|
||||||
post.id === post.parent && (
|
post.id === post.parent && (
|
||||||
<div key={post.id} onClick={() => onPostClick(post)}>
|
<div key={post.id} onClick={() => onPostClick(post)}>
|
||||||
@@ -16,7 +17,6 @@ const PostCommentList = ({ posts, vote, onPostClick, type }) => {
|
|||||||
)
|
)
|
||||||
))}
|
))}
|
||||||
{type === "comments" && posts.map(comment => (
|
{type === "comments" && posts.map(comment => (
|
||||||
comment.id !== comment.parent && (
|
|
||||||
<div key={comment.id}>
|
<div key={comment.id}>
|
||||||
<PostComment
|
<PostComment
|
||||||
post={comment}
|
post={comment}
|
||||||
@@ -24,7 +24,6 @@ const PostCommentList = ({ posts, vote, onPostClick, type }) => {
|
|||||||
type={"comment"}
|
type={"comment"}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
)
|
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user