import React, { useState, useEffect } from "react";
import { fetchBySub } from "./postsSlice";
import { selectAllSubs } from "../reddit/redditSlice";
import { useSelector, useDispatch } from "react-redux";
import { v4 } from "uuid";
import Post from "./Post";
export default function Feed() {
const [feed, setFeed] = useState(null); // Expects to receive an array of Post components mapped with data from fetchBySub
const [endpoints, setEndpoints] = useState(null); // Expects to receive an array of endpoints from which to fetch the posts
const dispatch = useDispatch();
const subs = useSelector(selectAllSubs); // Selects subreddits from redditSlice
const subArray = Object.values(subs); // Places the values within an array
useEffect(() => {
let isActive = true;
const getPosts = async(subreddit) => {
let myPosts = await dispatch(fetchBySub(subreddit));
myPosts = myPosts.payload; // sets myPosts to be the array of post objects fetched from the subreddit argument
if (typeof myPosts === 'object' && isActive) {
let newFeed = [];
for (let post of myPosts) { // maps the data retrieved from fetchBySub onto Post components,
newFeed.push( // then stores them in localized array