From 99fee8a44ae1270c5465dabe21a5b96996462927 Mon Sep 17 00:00:00 2001 From: Liam Arbuckle Date: Mon, 6 Nov 2023 00:44:34 +1100 Subject: [PATCH] =?UTF-8?q?=F0=9F=AB=A5=F0=9F=AA=BC=20=E2=86=9D=20Working?= =?UTF-8?q?=20post=20filter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/Content/ClassificationFeed.tsx | 72 +++++++++++++++++ .../Content/Classify/AnomalyPostFormCard.tsx | 77 +++++++++++++++++++ pages/planets/[id].tsx | 4 +- 3 files changed, 151 insertions(+), 2 deletions(-) diff --git a/components/Content/ClassificationFeed.tsx b/components/Content/ClassificationFeed.tsx index 7e4e918f..c38a9e20 100644 --- a/components/Content/ClassificationFeed.tsx +++ b/components/Content/ClassificationFeed.tsx @@ -80,6 +80,78 @@ export function ClassificationFeedForIndividualPlanet(planetId) { } }, []); + async function fetchPosts() { + try { + const postsResponse = await supabase + .from("classifications") + .select("*") + .eq('anomaly', planetId.planetId.id) + .order('created_at', { ascending: false }); + + if (postsResponse.error || !postsResponse.data) { + console.error("Error fetching posts:", postsResponse.error); + return; + } + + const postIds = postsResponse.data.map((post) => post.id); + + const commentsResponse = await supabase + .from("comments") + .select("id, content, created_at, profiles(id, avatar_url, username), post_id") + .in("post_id", postIds) + .order("created_at", { ascending: true }); + + const commentsByPostId = commentsResponse.data.reduce((acc, comment) => { + const postId = comment.post_id; + if (!acc[postId]) { + acc[postId] = []; + } + acc[postId].push(comment); + return acc; + }, {}); + + const postsWithComments = postsResponse.data.map((post) => ({ + ...post, + comments: commentsByPostId[post.id] || [], + })); + + setPosts(postsWithComments); + // console.log(posts); + } catch (error) { + console.error("Error fetching posts:", error.message); + } + } + + return ( +
+ {posts.map((post) => ( + <> + +

{post.planetId}

+ + ))} +
+ ); +}; + +export function ClassificationFeedForIndividualPlanetDuplicates(planetId) { + const supabase: SupabaseClient = useSupabaseClient(); + const session = useSession(); + + const [posts, setPosts] = useState([]); + // const [profile, setProfile] = useState(null); + const [planetPosts, setPlanetPosts] = useState([]); + + useEffect(() => { + fetchPosts(); + }, []); + + useEffect(() => { + if (planetPosts.length > 0) { + console.log("Comments: ", planetPosts.flatMap((post) => post.comments)); + } + }, []); + async function fetchPosts() { try { const postsResponse = await supabase diff --git a/components/Content/Classify/AnomalyPostFormCard.tsx b/components/Content/Classify/AnomalyPostFormCard.tsx index bf4fa1d6..3882bd33 100644 --- a/components/Content/Classify/AnomalyPostFormCard.tsx +++ b/components/Content/Classify/AnomalyPostFormCard.tsx @@ -19,6 +19,83 @@ export default function PostFormCardAnomalyTag({ onPost, planetId }) { // Check if user has items to make post -> not required functionality yet + // Create the publication + async function createPost() { + // Will add an identifier to determine the number of posts mentioning the planet, as that will affect the classification rating + + supabase + .from("classifications") + .insert({ + author: profile, + content, + // media: uploads + // planets2: planetId, + anomaly: planetId, // Set this to multiple anomaly types/foreign key options + }).then(response => { + if (!response.error) { + alert(`Post ${content} created`); + setContent(''); + // setUploads([]); + if ( onPost ) { + onPost(); + } + } + }); + + // .then (update user experience/currency) + } + + /* Get user avatar & other data + useEffect(() => { + supabase + .from('profiles') + .select(`avatatar_url`) + .eq("id", profile) + .then((result) => { + setAvatarUrl(result?.data[0]?.avatatar_url); + }); + }, [session]); */ + + // Function to add media to the publication + + // Frontend output + return ( + <> +
+ {/*
+ +
*/} +