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 PostList from './components/PostList';
|
||||||
import NewPostForm from './components/NewPostForm';
|
import NewPostForm from './components/NewPostForm';
|
||||||
import Modal from './components/Modal';
|
import Modal from './components/Modal';
|
||||||
@@ -6,12 +6,24 @@ import './App.scss';
|
|||||||
import {FontAwesomeIcon} from "@fortawesome/react-fontawesome";
|
import {FontAwesomeIcon} from "@fortawesome/react-fontawesome";
|
||||||
import {faCaretDown, faCaretUp} from "@fortawesome/free-solid-svg-icons";
|
import {faCaretDown, faCaretUp} from "@fortawesome/free-solid-svg-icons";
|
||||||
import {postApi} from "./api/posts.api";
|
import {postApi} from "./api/posts.api";
|
||||||
|
import {locationUtils} from "./utils/location";
|
||||||
|
|
||||||
const App = () => {
|
const App = () => {
|
||||||
const [posts, setPosts] = useState([]);
|
const [posts, setPosts] = useState([]);
|
||||||
const [selectedPost, setSelectedPost] = useState(null);
|
const [selectedPost, setSelectedPost] = useState(null);
|
||||||
const [comment, setComment] = useState('');
|
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 addPost = (text) => {
|
||||||
const newPost = { id: Date.now(), text, comments: [], upvotes: 0, downvotes: 0 };
|
const newPost = { id: Date.now(), text, comments: [], upvotes: 0, downvotes: 0 };
|
||||||
setPosts([newPost, ...posts]);
|
setPosts([newPost, ...posts]);
|
||||||
@@ -77,7 +89,6 @@ const App = () => {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="app">
|
<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>
|
<h1>SWA - Jodel</h1>
|
||||||
<NewPostForm addPost={addPost} />
|
<NewPostForm addPost={addPost} />
|
||||||
<PostList
|
<PostList
|
||||||
|
|||||||
Reference in New Issue
Block a user