import React, { useState, useEffect } from "react"; import { fetchBySub, fetchFromAll } 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 [endpoints, setEndpoints] = useState(null); // Expects to receive an array of endpoints from which to fetch the posts const [data, setData] = useState(null); // Receives data from getPosts and them maps it onto Post components const [feed, setFeed] = useState(null); // Expects to receive an array of Post components mapped with data from fetchBySub const dispatch = useDispatch(); const subs = useSelector(selectAllSubs); // Selects subreddits from redditSlice const subArray = Object.values(subs); // Places the values within an array useEffect(() => { // this useEffect loop pulls the endpoints from the selected subreddits and stores them as an array in "endpoints" const prepareData = () => { let myEndpoints = []; for (let sub of subArray) { myEndpoints.push(sub.access); } setEndpoints(myEndpoints); } if (subArray) { prepareData(); } }, [setEndpoints]); useEffect(() => { // once this is done, this loop pulls posts from each endpoint let isActive = true; const getPosts = async(arr) => { if (endpoints) { const mappedResults = arr.map(each => dispatch(fetchBySub(each))); return Promise.all([ ...mappedResults ]).then((results) => setData(results)); // data is now set to an object (returned promise) } } getPosts(endpoints); return () => { isActive = false; } }, [dispatch, setData, endpoints]); useEffect(() => { let isActive = true; const mapPosts = () => { if (data) { let allPosts = []; for (let each of data) { allPosts.push(each.payload); } let extractedPosts = []; for (let each of allPosts) { for (let indiv of each) { extractedPosts.push(indiv); } }; extractedPosts = extractedPosts.sort((x,y) => x.created_utc > y.created_utc); // sorts posts by sort time console.log(extractedPosts); let newFeed = []; newFeed = extractedPosts.map((post) => { return ( ); }) for (let post of allPosts) { newFeed.push( ); } setFeed(newFeed); } } mapPosts(); return () => { isActive = false; } }, [data, setFeed]) // useEffect(() => { // let isActive = true; // const getPosts = async() => { // let myPosts = await dispatch(fetchBySub('https://www.reddit.com/r/cats.json')); // 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 // // ); // } // setFeed(newFeed); // once populated, this array is sent to state as "feed" and rendered in this function's return method // } // }; // getPosts(); // return () => { // isActive = false; // } // }, [dispatch]) return ( <> {feed ? feed :

Loading cats for you...

} ); }