Skip to content

Commit

Permalink
Favorite page fetch liked posts FIX (#801)
Browse files Browse the repository at this point in the history
* useEffect changes, add filteredLikedPosts

* fix favorite page, show saved posts

* fix error of "form can not be descendant of form"

---------

Co-authored-by: Narayan soni <narayansoni854@gmail.com>
  • Loading branch information
fiskryeziu and narayan954 committed Jul 14, 2023
1 parent 37fd555 commit 590ea45
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
51 changes: 27 additions & 24 deletions src/components/Favorite.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,38 @@ function Favorite() {
let savedPostsArr = JSON.parse(localStorage.getItem("posts")) || [];

useEffect(() => {
const fetchPosts = async () => {
const posts = [];
const fetchPromises = savedPostsArr.map(async (id) => {
const posts = [];
if (savedPostsArr.length > 0) {
const fetchPosts = async () => {
try {
const docRef = doc(db, "posts", id);
const doc = await getDoc(docRef);
doc?.data() && posts.push({ id: doc.id, post: doc.data() });
} catch (e) {
enqueueSnackbar("Error while getting post", {
const fetchSavedPosts = savedPostsArr.map(async (id) => {
try {
const docRef = doc(db, "posts", id);
const docSnap = await getDoc(docRef);
if (docSnap.exists()) {
posts.push({ id: docSnap.id, post: docSnap.data() });
}
} catch (error) {
enqueueSnackbar("Error while fetching all posts", {
variant: "error",
});
setLoading(false);
}
});

await Promise.all(fetchSavedPosts); // Wait for all fetch requests to complete
setLoading(false);
setPosts(posts);
} catch (error) {
enqueueSnackbar("Error while fetching all posts", {
variant: "error",
});
setLoading(false);
}
});

try {
await Promise.all(fetchPromises);
setPosts(posts);
setLoading(false);
} catch (error) {
enqueueSnackbar("Error while fetching all posts", {
variant: "error",
});
setLoading(false);
} finally {
setLoading(false);
}
};
};

if (savedPostsArr.length === 0) {
fetchPosts();
} else {
setLoading(false);
}
}, []);
Expand Down
6 changes: 3 additions & 3 deletions src/components/Post/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function Post(prop) {
snapshot.docs.map((doc) => ({
id: doc.id,
content: doc.data(),
})),
}))
);
});
}
Expand Down Expand Up @@ -222,7 +222,7 @@ function Post(prop) {
</Flexbetween>

{user && (
<form className="post__commentBox">
<div className="post__commentBox">
<ErrorBoundary>
<PostNav
fullScreen={fullScreen}
Expand Down Expand Up @@ -298,7 +298,7 @@ function Post(prop) {
/>
</ErrorBoundary>
</DialogBox>
</form>
</div>
)}
</div>
</div>
Expand Down

0 comments on commit 590ea45

Please sign in to comment.