import React from "react";
import axios from "axios";

const PostContext = React.createContext();

export const PostProvider = ({ children }) => {
  const [posts, setPost] = React.useState([]);
  const [postLoading, setPostLoading] = React.useState([]);

  const fetchPost = async () => {
    setPostLoading(true);
    await axios
      .get("https://dummyjson.com/posts")
      .then((res) => {
        // setPost(res.data);
        console.log(res);
        setPost(res.data.posts);
        setPostLoading(false);
      })
      .catch((err) => {
        console.log(err);
        setPostLoading(false);
      });
  };

  return (
    <PostContext.Provider value={{ posts, fetchPost, postLoading }}>
      {children}
    </PostContext.Provider>
  );
};

export default PostContext;
