fix: recursive useEffect hook in dashboard page

This commit is contained in:
Sosokker 2024-11-11 11:54:29 +07:00
parent 894791e15a
commit f705fa6d4a
4 changed files with 38 additions and 36 deletions

View File

@ -1,6 +1,7 @@
"use client"; "use client";
import Image from "next/image"; import Image from "next/image";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { Overview } from "@/components/ui/overview"; import { Overview } from "@/components/ui/overview";
import { RecentFunds } from "@/components/recent-funds"; import { RecentFunds } from "@/components/recent-funds";
@ -11,13 +12,14 @@ import { getProjectByUserId } from "@/lib/data/projectQuery";
import { Loader } from "@/components/loading/loader"; import { Loader } from "@/components/loading/loader";
import { getInvestmentByProjectsIds } from "@/lib/data/investmentQuery"; import { getInvestmentByProjectsIds } from "@/lib/data/investmentQuery";
import { useQuery } from "@supabase-cache-helpers/postgrest-react-query"; import { useQuery } from "@supabase-cache-helpers/postgrest-react-query";
import { overAllGraphData, Deal, fourYearGraphData, dayOftheWeekData } from "../portfolio/[uid]/query"; import { overAllGraphData, fourYearGraphData, dayOftheWeekData } from "../portfolio/[uid]/query";
import CountUp from "react-countup"; import CountUp from "react-countup";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
export default function Dashboard() { export default function Dashboard() {
const supabase = createSupabaseClient(); const supabase = createSupabaseClient();
const userId = useSession().session?.user.id; const { session, loading: isLoadingSession } = useSession();
const userId = session?.user.id;
const [projects, setProjects] = useState< const [projects, setProjects] = useState<
{ id: number; project_name: string; business_id: { user_id: number }[]; dataroom_id: number }[] { id: number; project_name: string; business_id: { user_id: number }[]; dataroom_id: number }[]
>([]); >([]);
@ -33,8 +35,8 @@ export default function Dashboard() {
>([]); >([]);
const tabOptions = ["daily", "monthly", "yearly"]; const tabOptions = ["daily", "monthly", "yearly"];
const [activeTab, setActiveTab] = useState("daily"); const [activeTab, setActiveTab] = useState("daily");
const [isSuccess, setIsSuccess] = useState(false);
const [graphType, setGraphType] = useState("line"); const [graphType, setGraphType] = useState("line");
const [isLoadingProjects, setIsLoadingProjects] = useState(true);
const [currentProjectId, setCurrentProjectId] = useState<number>(projects[0]?.id); const [currentProjectId, setCurrentProjectId] = useState<number>(projects[0]?.id);
const investmentDetail = useQuery( const investmentDetail = useQuery(
@ -58,6 +60,24 @@ export default function Dashboard() {
} else { } else {
graphData = overAllGraphData(filteredData); graphData = overAllGraphData(filteredData);
} }
useEffect(() => {
const fetchProjects = async () => {
if (!userId) return;
const { data, error } = await getProjectByUserId(supabase, userId);
if (error) console.error("Error fetching projects");
setProjects(data || []);
setIsLoadingProjects(false);
};
fetchProjects();
}, [supabase, userId]);
useEffect(() => {
if (projects.length > 0 && !currentProjectId) {
setCurrentProjectId(projects[0].id);
}
}, [projects, currentProjectId]);
useEffect(() => { useEffect(() => {
const setTopLatestInvestment = () => { const setTopLatestInvestment = () => {
if (investmentDetail?.data) { if (investmentDetail?.data) {
@ -65,7 +85,6 @@ export default function Dashboard() {
investmentDetail.data investmentDetail.data
.slice(0, 8) .slice(0, 8)
.map((item) => { .map((item) => {
// set the project according to current project id
if (item.project_id === currentProjectId) { if (item.project_id === currentProjectId) {
return { return {
avatarUrl: item.avatar_url, avatarUrl: item.avatar_url,
@ -87,34 +106,14 @@ export default function Dashboard() {
username: string; username: string;
}[] }[]
); );
// console.table(latestInvestment);
} }
}; };
setTopLatestInvestment(); setTopLatestInvestment();
}, [supabase, investmentDetail]); }, [currentProjectId, investmentDetail?.data]);
useEffect(() => {
const fetchProjects = async () => {
if (userId) {
const { data, error } = await getProjectByUserId(supabase, userId);
if (error) {
console.error("Error while fetching projects");
}
if (data) {
// set project and current project id
setProjects(data);
setCurrentProjectId(data[0].id);
}
} else {
console.error("Error with UserId while fetching projects");
}
setIsSuccess(true);
};
fetchProjects();
}, [supabase, userId]);
return ( return (
<div className="container max-w-screen-xl"> <div className="container max-w-screen-xl">
<Loader isSuccess={isSuccess} /> <Loader isSuccess={!isLoadingSession && !isLoadingProjects} />{" "}
<div className="md:hidden"> <div className="md:hidden">
<Image <Image
src="/examples/dashboard-light.png" src="/examples/dashboard-light.png"
@ -150,7 +149,7 @@ export default function Dashboard() {
))} ))}
</TabsList> </TabsList>
{projects.map((project) => ( {projects.map((project) => (
<TabsContent value={project.project_name} className="space-y-4"> <TabsContent value={project.project_name} className="space-y-4" key={project.id}>
<div className="grid gap-4 md:grid-cols-2 lg:grid-cols-4"> <div className="grid gap-4 md:grid-cols-2 lg:grid-cols-4">
<Card> <Card>
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2"> <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">

View File

@ -1,5 +1,5 @@
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { BusinessCard } from "@/components/BusinessCard"; import { BusinessCard } from "@/components/businessCard";
import { BusinessCardProps } from "@/types/BusinessCard"; import { BusinessCardProps } from "@/types/BusinessCard";
import { Separator } from "@/components/ui/separator"; import { Separator } from "@/components/ui/separator";
import Link from "next/link"; import Link from "next/link";

View File

@ -35,6 +35,7 @@ const Gallery = ({ images }: GalleryProps) => {
/> />
</CarouselItem> </CarouselItem>
)), )),
// eslint-disable-next-line react-hooks/exhaustive-deps
[images, current] [images, current]
); );

View File

@ -12,15 +12,18 @@ export type RecentDealData = {
}; };
interface RecentFundsProps { interface RecentFundsProps {
data?: { name?: string; amount?: number; avatar?: string; date?: Date; logo_url?: string; status?: string; profile_url?: string }[]; data?: {
name?: string;
amount?: number;
avatar?: string;
date?: Date;
logo_url?: string;
status?: string;
profile_url?: string;
}[];
} }
export function RecentFunds(props: RecentFundsProps) { export function RecentFunds(props: RecentFundsProps) {
const content = (
<div>
</div>
)
return ( return (
<div className="space-y-8"> <div className="space-y-8">
{(props?.data || []).map((deal, index) => ( {(props?.data || []).map((deal, index) => (
@ -129,5 +132,4 @@ export function RecentFunds(props: RecentFundsProps) {
))} ))}
</div> </div>
); );
} }