From 45ff12f1a63ef5061626ae45f7ed5e7fd25bed2b Mon Sep 17 00:00:00 2001 From: Mehul Date: Thu, 7 Nov 2024 14:39:14 -0500 Subject: [PATCH] rebase --- .../(community)/explore-community/page.tsx | 252 +++++++++++++++++- 1 file changed, 247 insertions(+), 5 deletions(-) diff --git a/src/app/(community)/explore-community/page.tsx b/src/app/(community)/explore-community/page.tsx index 4583406..fe20604 100644 --- a/src/app/(community)/explore-community/page.tsx +++ b/src/app/(community)/explore-community/page.tsx @@ -1,7 +1,249 @@ -import React from "react"; +"use client"; +import React, { useState, useEffect, useMemo } from "react"; +import { useSession } from "@supabase/auth-helpers-react"; +import { supabase } from "../../../utils/supabase"; +import { toast } from "sonner"; +import Pagination from "../../../components/Pagination"; +import Loading from "../../../components/loading"; +import { HeartIcon } from "@heroicons/react/solid"; +import { useUserDetails } from "../../../hooks/useUserDetails"; +import Link from "next/link"; +import { motion } from "framer-motion"; +import { CalendarIcon, MapPinIcon } from "lucide-react"; -function page() { - return
page
; -} +const Page: React.FC = () => { + // Define types + interface Community { + id: string; + community_name: string; + community_description: string; + community_location: string; + community_category: string; + community_members_count: number; + community_likes: number; + community_image: string; + community_creation_date: string; + } -export default page; + // State hooks + const [loading, setLoading] = useState(true); + const [communities, setCommunities] = useState([]); + const [currentPage, setCurrentPage] = useState(1); + const [itemsPerPage, setItemsPerPage] = useState(9); + const [likedCommunities, setLikedCommunities] = useState<{ [key: string]: boolean }>({}); + const [searchTerm, setSearchTerm] = useState(""); + const [category, setCategory] = useState(""); + const [sortByLikes, setSortByLikes] = useState(""); + const session = useSession(); + const { user } = useUserDetails(); + + useEffect(() => { + async function fetchCommunities() { + const { data, error } = await supabase.from("communities").select("*"); + if (error) { + console.error("Error fetching communities:", error); + toast.error("Failed to load communities. Please try again later."); + } else { + setCommunities(data); + } + setLoading(false); + } + + fetchCommunities(); + }, []); + + const handleLikeToggle = async (communityId: string) => { + if (!user) { + toast.error("You must be logged in to like a community."); + return; + } + + const useremail = user.email; + const { data: likedData, error: checkError } = await supabase + .from("community_likes") + .select("community_id") + .eq("community_id", communityId) + .eq("useremail", useremail); + + if (checkError) { + console.error("Error checking likes:", checkError); + return; + } + + const isLiked = likedData.length > 0; + + let communityData; + const { data: fetchedCommunityData, error: fetchError } = await supabase + .from("communities") + .select("community_likes") + .eq("id", communityId) + .single(); + + if (fetchError) { + console.error("Error fetching community likes:", fetchError); + return; + } + + communityData = fetchedCommunityData; + + if (isLiked) { + const { error: unlikeError } = await supabase + .from("community_likes") + .delete() + .eq("community_id", communityId) + .eq("useremail", useremail); + + if (unlikeError) { + console.error("Error unliking community:", unlikeError); + return; + } + + const newLikesCount = communityData.community_likes - 1; + await supabase + .from("communities") + .update({ community_likes: newLikesCount }) + .eq("id", communityId); + + setLikedCommunities((prev) => ({ + ...prev, + [communityId]: false, + })); + } else { + const { error: likeError } = await supabase + .from("community_likes") + .insert({ community_id: communityId, useremail }); + + if (likeError) { + console.error("Error liking community:", likeError); + return; + } + + const newLikesCount = communityData.community_likes + 1; + await supabase + .from("communities") + .update({ community_likes: newLikesCount }) + .eq("id", communityId); + + setLikedCommunities((prev) => ({ + ...prev, + [communityId]: true, + })); + } + }; + + // Filtering and sorting communities + const filteredAndSortedCommunities = useMemo(() => { + return communities + .filter((community) => { + const matchesCategory = category ? community.community_category === category : true; + const matchesSearchTerm = + community.community_name.toLowerCase().includes(searchTerm.toLowerCase()) || + community.community_location.toLowerCase().includes(searchTerm.toLowerCase()); + return matchesCategory && matchesSearchTerm; + }) + .sort((a, b) => { + if (sortByLikes === "high") { + return b.community_likes - a.community_likes; + } else if (sortByLikes === "low") { + return a.community_likes - b.community_likes; + } + return 0; + }); + }, [communities, searchTerm, category, sortByLikes]); + + const indexOfLastItem = currentPage * itemsPerPage; + const indexOfFirstItem = indexOfLastItem - itemsPerPage; + const currentCommunities = filteredAndSortedCommunities.slice(indexOfFirstItem, indexOfLastItem); + + const totalPages = Math.ceil(filteredAndSortedCommunities.length / itemsPerPage); + + if (loading) { + return ; + } + + return ( + <> +
+
+
+
+ setSearchTerm(e.target.value)} + /> + + +
+
+ +
+ {currentCommunities.map((community) => ( + + {community.community_name} +

{community.community_name}

+

{community.community_description}

+
+
+ handleLikeToggle(community.id)} + /> + {community.community_likes} Likes +
+ + View Community + +
+
+ ))} +
+ +
+ setCurrentPage(page)} + /> +
+
+
+ + ); +}; + +export default Page;