Skip to content
Snippets Groups Projects
Posts.js 2.04 KiB
Newer Older
  • Learn to ignore specific revisions
  • ludvighillert's avatar
    ludvighillert committed
    import React,{ useState, useEffect } from 'react'
    
    ludvighillert's avatar
    ludvighillert committed
    import styles from '../styles/feed.module.css'
    import{FaSearch } from "react-icons/fa";
    
    ludvighillert's avatar
    ludvighillert committed
    import axios from 'axios'
    import Upvote from './Upvote'
    
    import DeletePostButton from './DeletePost'
    
    ludvighillert's avatar
    ludvighillert committed
    const Posts = () => {
    
      const [posts,setPosts] = useState([])
    
    ludvighillert's avatar
    ludvighillert committed
      const [searchFilter, setSearchFilter] = useState([]);
      const [result, setResult] = useState("");
    
    ludvighillert's avatar
    ludvighillert committed
      const [loading,setLoading] = useState(false)
      const [drinks,setDrinks] = useState([])
    
      const loadPosts = () => {
    
        axios.get("http://localhost:5000/posts")
        .then((res) => {
          console.log(res.data)
          setPosts(res.data)
    
    ludvighillert's avatar
    ludvighillert committed
          setSearchFilter(res.data);
    
    ludvighillert's avatar
    ludvighillert committed
        })
      }
        useEffect(() => {
      
          loadPosts()
      
      
        },[])
    
    
    ludvighillert's avatar
    ludvighillert committed
        useEffect(() =>{
          const results = searchFilter.filter(resp =>
             resp.name.toLowerCase().includes(result)
          );
          setPosts(results)
        },[result])
    
        const handleChange = (e) => {
          setResult(e.target.value);
        }
        
    
        const handlePostDeleted = (postId) => {
          setPosts(posts.filter(post => post._id !== postId));
          
        };
    
    ludvighillert's avatar
    ludvighillert committed
      return (
    
    
        <div>
    
    ludvighillert's avatar
    ludvighillert committed
          <input className = {styles.searchbar}
         value = {result}
         type = "text"
         placeholder = "Filter your drinks by name..."
         
         onChange ={handleChange}/>
    
    
    ludvighillert's avatar
    ludvighillert committed
        <div>{posts.map((post) => {
            return(
                <div className={styles.posts_container} key={post._id}>
                <div className={styles.post_img_container}>
                <img className={styles.img} src={`http://localhost:5000/uploads/${post.photo}`}/>
                </div>
                <div>
                   <h1>{post.name}</h1>
    
    ludvighillert's avatar
    ludvighillert committed
                   <div className = {styles.recipe}>Recipe: <li> {post.recipe}</li> </div>
                   
                   <div className = {styles.description}>Description: <p>{post.description}</p> </div>
    
    ludvighillert's avatar
    ludvighillert committed
                  
                </div>
    
    ludvighillert's avatar
    ludvighillert committed
                   <Upvote/> 
    
                   <DeletePostButton postId={post._id} onDeleted={() => handlePostDeleted(post._id)}/>
    
    ludvighillert's avatar
    ludvighillert committed
              </div>
            )
          })}</div>
          </div>
    
      )
    }
    
    export default Posts