Skip to content

Commit

Permalink
🫥🪼 ↝ Working post filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Gizmotronn committed Dec 30, 2023
1 parent 7fb37db commit 99fee8a
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 2 deletions.
72 changes: 72 additions & 0 deletions components/Content/ClassificationFeed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div className="flex flex-col items-center gap-4 py-2" style={{ maxWidth: '100%', margin: 'auto' }}>
{posts.map((post) => (
<>
<CardForum key={post.id} {...post} />
<p>{post.planetId}</p>
</>
))}
</div>
);
};

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
Expand Down
77 changes: 77 additions & 0 deletions components/Content/Classify/AnomalyPostFormCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<>
<div className="flex gap-2 mx-5 mt-5">
{/* <div>
<img src={avatar_url} width='60px' height='60px' />
</div> */}
<textarea
value={content}
onChange={(e) => setContent(e.target.value)}
className="grow p-3 h-24 rounded-xl"
placeholder={`What do you think about this planet candidate, ${profile}?`}
/>
<div className="text-center">
<button onClick={createPost} className="text-black px-2 py-1 rounded-md">Share</button>
</div>
</div>
</>
)
}

export function PostFormCardAnomalyTagOldSchema({ onPost, planetId }) {
const supabase = useSupabaseClient();
const session = useSession();

const [content, setContent] = useState('');
const profile = session?.user?.id;
const [avatar_url, setAvatarUrl] = useState(null);
/* const [uploads, setUploads] = useState([]);
const [isUploading, setIsUploading] = useState(false);
const [userExperience, setUserExperience] = useState();
const [hasRequiredItem, setHasRequiredItem] = useState(false); */

const router = useRouter();
const anomalytId = router.query.id;

// 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
Expand Down
4 changes: 2 additions & 2 deletions pages/planets/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export default function PlanetIdPage () {
<BasePlanetData planetId={{ id: id as string }} />
<EditableBasePlanetData planetId={{ id: id as string }} />
<PostFormCardAnomalyTag planetId={id} onPost={null} />
<ClassificationFeedForIndividualPlanet planetId={id} />
<ClassificationFeedForIndividualPlanet planetId={{ id: id as string }} />
</div>
</div>
);
Expand All @@ -71,7 +71,7 @@ export default function PlanetIdPage () {
<BasePlanetData planetId={{ id: id as string }} />
<EditableBasePlanetData planetId={{ id: id as string }} />
<PostFormCardAnomalyTag planetId={id} onPost={null} />
<ClassificationFeedForIndividualPlanet planetId={id} />
<ClassificationFeedForIndividualPlanet planetId={{ id: id as string }} />
</div>
</div>
);
Expand Down

0 comments on commit 99fee8a

Please sign in to comment.