implemented loadPosts function
This commit is contained in:
15
src/App.js
15
src/App.js
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user