implemented loadPosts function

This commit is contained in:
Pete Gerlach
2024-06-11 14:15:03 +02:00
parent 34f95c07ac
commit b87c215525

View File

@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, {useEffect, useState} from 'react';
import PostList from './components/PostList';
import NewPostForm from './components/NewPostForm';
import Modal from './components/Modal';
@@ -6,12 +6,24 @@ import './App.scss';
import {FontAwesomeIcon} from "@fortawesome/react-fontawesome";
import {faCaretDown, faCaretUp} from "@fortawesome/free-solid-svg-icons";
import {postApi} from "./api/posts.api";
import {locationUtils} from "./utils/location";
const App = () => {
const [posts, setPosts] = useState([]);
const [selectedPost, setSelectedPost] = useState(null);
const [comment, setComment] = useState('');
useEffect(() => {
if(posts.length === 0) {
loadPosts().then(data => setPosts(data))
}
})
const loadPosts = async () => {
const location = await locationUtils.getCurrentLocation();
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]);
@@ -77,7 +89,6 @@ const App = () => {
return (
<div className="app">
{console.log(postApi.getPostsByCoords(48, 9), process.env.REACT_APP_AUTH_USERNAME, process.env.REACT_APP_AUTH_PASSWORD)}
<h1>SWA - Jodel</h1>
<NewPostForm addPost={addPost} />
<PostList