mirror of
https://github.com/Sosokker/B2D-Ventures.git
synced 2025-12-20 14:34:05 +01:00
fix: data fetch error in deals and find page
This commit is contained in:
parent
6c63a30430
commit
60118ee310
@ -3,27 +3,14 @@
|
|||||||
import { useSearchParams } from "next/navigation";
|
import { useSearchParams } from "next/navigation";
|
||||||
import { createSupabaseClient } from "@/lib/supabase/clientComponentClient";
|
import { createSupabaseClient } from "@/lib/supabase/clientComponentClient";
|
||||||
import { useQuery } from "@supabase-cache-helpers/postgrest-react-query";
|
import { useQuery } from "@supabase-cache-helpers/postgrest-react-query";
|
||||||
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
|
|
||||||
import { ProjectCard } from "@/components/projectCard";
|
import { ProjectCard } from "@/components/projectCard";
|
||||||
import { Separator } from "@/components/ui/separator";
|
import { Separator } from "@/components/ui/separator";
|
||||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
|
||||||
import { getBusinesses, getInvestmentCounts, getProjects, getTags } from "@/lib/data/query";
|
import { getBusinessAndProject } from "@/lib/data/businessQuery";
|
||||||
import { Tables } from "@/types/database.types";
|
|
||||||
|
|
||||||
interface ProjectInvestmentDetail extends Tables<"ProjectInvestmentDetail"> {}
|
|
||||||
|
|
||||||
interface Project extends Tables<"Project"> {
|
|
||||||
ProjectInvestmentDetail: ProjectInvestmentDetail[];
|
|
||||||
}
|
|
||||||
|
|
||||||
interface Business extends Tables<"Business"> {
|
|
||||||
Projects: Project[];
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function Find() {
|
export default function Find() {
|
||||||
const searchParams = useSearchParams();
|
const searchParams = useSearchParams();
|
||||||
const query = searchParams.get("query");
|
const query = searchParams.get("query");
|
||||||
// const query = "neon";
|
|
||||||
|
|
||||||
let supabase = createSupabaseClient();
|
let supabase = createSupabaseClient();
|
||||||
|
|
||||||
@ -31,94 +18,62 @@ export default function Find() {
|
|||||||
data: businesses,
|
data: businesses,
|
||||||
isLoading: isLoadingBusinesses,
|
isLoading: isLoadingBusinesses,
|
||||||
error: businessError,
|
error: businessError,
|
||||||
} = useQuery(getBusinesses(supabase, query));
|
} = useQuery(getBusinessAndProject(supabase, query));
|
||||||
|
|
||||||
const businessIds = businesses?.map((b) => b.id) || [];
|
const isLoading = isLoadingBusinesses;
|
||||||
const {
|
const error = businessError;
|
||||||
data: projects,
|
|
||||||
isLoading: isLoadingProjects,
|
|
||||||
error: projectError,
|
|
||||||
} = useQuery(getProjects(supabase, businessIds), {
|
|
||||||
enabled: businessIds.length > 0,
|
|
||||||
});
|
|
||||||
|
|
||||||
const projectIds = projects?.map((p) => p.id) || [];
|
|
||||||
const {
|
|
||||||
data: tags,
|
|
||||||
isLoading: isLoadingTags,
|
|
||||||
error: tagError,
|
|
||||||
} = useQuery(getTags(supabase, projectIds), {
|
|
||||||
enabled: projectIds.length > 0,
|
|
||||||
});
|
|
||||||
|
|
||||||
const {
|
|
||||||
data: investmentCounts,
|
|
||||||
isLoading: isLoadingInvestments,
|
|
||||||
error: investmentError,
|
|
||||||
} = useQuery(getInvestmentCounts(supabase, projectIds), {
|
|
||||||
enabled: projectIds.length > 0,
|
|
||||||
});
|
|
||||||
|
|
||||||
// -----
|
|
||||||
|
|
||||||
const isLoading = isLoadingBusinesses || isLoadingProjects || isLoadingTags || isLoadingInvestments;
|
|
||||||
const error = businessError || projectError || tagError || investmentError;
|
|
||||||
|
|
||||||
const results: Business[] =
|
|
||||||
businesses?.map((business) => ({
|
|
||||||
...business,
|
|
||||||
Projects:
|
|
||||||
projects
|
|
||||||
?.filter((project) => project.businessId === business.id)
|
|
||||||
.map((project) => ({
|
|
||||||
...project,
|
|
||||||
tags: tags?.filter((tag) => tag.itemId === project.id).map((tag) => tag.Tag.value) || [],
|
|
||||||
investmentCount: investmentCounts?.find((ic) => ic.projectId === project.id)?.count || 0,
|
|
||||||
})) || [],
|
|
||||||
})) || [];
|
|
||||||
|
|
||||||
if (isLoading) return <p>Loading...</p>;
|
if (isLoading) return <p>Loading...</p>;
|
||||||
if (error) return <p>Error fetching data: {error.message}</p>;
|
if (error) return <p>Error fetching data: {error.message}</p>;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div className="container max-w-screen-xl">
|
||||||
<div className="mt-10 mx-[15%]">
|
<div className="mt-4">
|
||||||
<h1 className="text-4xl font-bold">Result</h1>
|
<h1 className="text-4xl font-bold">Result</h1>
|
||||||
|
|
||||||
<Separator className="my-4" />
|
<Separator className="my-4" />
|
||||||
|
|
||||||
{results.length === 0 && <p>No results found.</p>}
|
<Card className="w-full">
|
||||||
{results.length > 0 && (
|
<CardContent className="my-2">
|
||||||
|
{businesses!.length === 0 && <p>No results found.</p>}
|
||||||
|
{businesses!.length > 0 && (
|
||||||
<ul>
|
<ul>
|
||||||
{results.map((business) => (
|
{businesses!.map((business) => (
|
||||||
<li key={business.id}>
|
<li key={business.business_id}>
|
||||||
<Card className="w-full">
|
<Card className="w-full">
|
||||||
<CardHeader>
|
<CardHeader>
|
||||||
<CardTitle>{business.businessName}</CardTitle>
|
<CardTitle>{business.business_name}</CardTitle>
|
||||||
<CardDescription>Joined Date: {new Date(business.joinedDate).toLocaleDateString()}</CardDescription>
|
<CardDescription>
|
||||||
|
Joined Date: {new Date(business.joined_date).toLocaleDateString()}
|
||||||
|
</CardDescription>
|
||||||
</CardHeader>
|
</CardHeader>
|
||||||
<CardContent>
|
<CardContent className="grid grid-cols-4 gap-4">
|
||||||
{business.Projects.map((project) => (
|
{business?.projects && business.projects.length > 0 ? (
|
||||||
|
business.projects.map((project) => (
|
||||||
<ProjectCard
|
<ProjectCard
|
||||||
key={project.id}
|
key={project.id}
|
||||||
name={project.projectName}
|
name={project.project_name}
|
||||||
description={project.projectName}
|
description={project.project_short_description}
|
||||||
joinDate={project.projectName}
|
joinDate={project.published_time}
|
||||||
location={"Bangkok"}
|
location={business.location}
|
||||||
minInvestment={project.ProjectInvestmentDetail[0]?.minInvestment}
|
minInvestment={project.min_investment}
|
||||||
totalInvestor={project.ProjectInvestmentDetail[0]?.totalInvestment}
|
totalInvestor={project.total_investment}
|
||||||
totalRaised={project.ProjectInvestmentDetail[0]?.targetInvestment}
|
totalRaised={project.target_investment}
|
||||||
tags={[]}
|
tags={project.tags?.map((tag) => String(tag.tag_value)) || []}
|
||||||
imageUri={null}
|
imageUri={project.card_image_url}
|
||||||
/>
|
/>
|
||||||
))}
|
))
|
||||||
|
) : (
|
||||||
|
<p>No Projects</p>
|
||||||
|
)}
|
||||||
</CardContent>
|
</CardContent>
|
||||||
</Card>
|
</Card>
|
||||||
</li>
|
</li>
|
||||||
))}
|
))}
|
||||||
</ul>
|
</ul>
|
||||||
)}
|
)}
|
||||||
<ReactQueryDevtools initialIsOpen={false} />
|
</CardContent>
|
||||||
|
</Card>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
38
src/lib/data/businessQuery.ts
Normal file
38
src/lib/data/businessQuery.ts
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
import { SupabaseClient } from "@supabase/supabase-js";
|
||||||
|
|
||||||
|
export const getBusinessAndProject = (client: SupabaseClient, businessName: String | null) => {
|
||||||
|
const query = client.from("business").select(`
|
||||||
|
business_id:id,
|
||||||
|
location,
|
||||||
|
business_name,
|
||||||
|
business_type:business_type (
|
||||||
|
business_type_id:id,
|
||||||
|
value
|
||||||
|
),
|
||||||
|
joined_date,
|
||||||
|
user_id,
|
||||||
|
projects:project (
|
||||||
|
id,
|
||||||
|
project_name,
|
||||||
|
business_id,
|
||||||
|
published_time,
|
||||||
|
card_image_url,
|
||||||
|
project_short_description,
|
||||||
|
...project_investment_detail (
|
||||||
|
min_investment,
|
||||||
|
total_investment,
|
||||||
|
target_investment
|
||||||
|
),
|
||||||
|
tags:project_tag (
|
||||||
|
...tag (
|
||||||
|
tag_value:value
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
`);
|
||||||
|
|
||||||
|
if (businessName && businessName.trim() !== "") {
|
||||||
|
return query.ilike("business_name", `%${businessName}%`);
|
||||||
|
}
|
||||||
|
return query;
|
||||||
|
};
|
||||||
11
src/lib/data/investmentQuery.ts
Normal file
11
src/lib/data/investmentQuery.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import { SupabaseClient } from "@supabase/supabase-js";
|
||||||
|
|
||||||
|
export const getInvestmentCountsByProjectsIds = (client: SupabaseClient, projectIds: string[]) => {
|
||||||
|
return client
|
||||||
|
.from("investment_deal")
|
||||||
|
.select("*", {
|
||||||
|
count: "exact",
|
||||||
|
head: true,
|
||||||
|
})
|
||||||
|
.in("project_id", projectIds);
|
||||||
|
};
|
||||||
@ -1,9 +1,6 @@
|
|||||||
import { SupabaseClient } from "@supabase/supabase-js";
|
import { SupabaseClient } from "@supabase/supabase-js";
|
||||||
|
|
||||||
async function getTopProjects(
|
async function getTopProjects(client: SupabaseClient, numberOfRecords: number = 4) {
|
||||||
client: SupabaseClient,
|
|
||||||
numberOfRecords: number = 4
|
|
||||||
) {
|
|
||||||
try {
|
try {
|
||||||
const { data, error } = await client
|
const { data, error } = await client
|
||||||
.from("project")
|
.from("project")
|
||||||
@ -62,7 +59,7 @@ function getProjectDataQuery(client: SupabaseClient, projectId: number) {
|
|||||||
target_investment,
|
target_investment,
|
||||||
investment_deadline
|
investment_deadline
|
||||||
),
|
),
|
||||||
tags:item_tag!inner (
|
tags:project_tag!inner (
|
||||||
...tag!inner (
|
...tag!inner (
|
||||||
tag_name:value
|
tag_name:value
|
||||||
)
|
)
|
||||||
@ -88,7 +85,7 @@ async function getProjectData(client: SupabaseClient, projectId: number) {
|
|||||||
target_investment,
|
target_investment,
|
||||||
investment_deadline
|
investment_deadline
|
||||||
),
|
),
|
||||||
tags:item_tag!inner (
|
tags:project_tag!inner (
|
||||||
...tag!inner (
|
...tag!inner (
|
||||||
tag_name:value
|
tag_name:value
|
||||||
)
|
)
|
||||||
@ -149,7 +146,7 @@ function searchProjectsQuery(
|
|||||||
target_investment,
|
target_investment,
|
||||||
investment_deadline
|
investment_deadline
|
||||||
),
|
),
|
||||||
tags:item_tag!inner (
|
tags:project_tag!inner (
|
||||||
...tag!inner (
|
...tag!inner (
|
||||||
tag_name:value
|
tag_name:value
|
||||||
)
|
)
|
||||||
@ -186,7 +183,7 @@ function searchProjectsQuery(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (tagsFilter) {
|
if (tagsFilter) {
|
||||||
query = query.in("item_tag.tag.value", tagsFilter);
|
query = query.in("project_tag.tag.value", tagsFilter);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (projectStatus) {
|
if (projectStatus) {
|
||||||
@ -200,9 +197,25 @@ function searchProjectsQuery(
|
|||||||
return query;
|
return query;
|
||||||
}
|
}
|
||||||
|
|
||||||
export {
|
const getProjectByBusinessId = (client: SupabaseClient, businessIds: string[]) => {
|
||||||
getProjectData,
|
return client
|
||||||
getProjectDataQuery,
|
.from("project")
|
||||||
getTopProjects,
|
.select(
|
||||||
searchProjectsQuery,
|
`
|
||||||
|
id,
|
||||||
|
project_name,
|
||||||
|
business_id,
|
||||||
|
published_time,
|
||||||
|
card_image_url,
|
||||||
|
project_short_description,
|
||||||
|
...project_investment_detail (
|
||||||
|
min_investment,
|
||||||
|
total_investment,
|
||||||
|
target_investment
|
||||||
|
)
|
||||||
|
`
|
||||||
|
)
|
||||||
|
.in("business_id", businessIds);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export { getProjectData, getProjectDataQuery, getTopProjects, searchProjectsQuery, getProjectByBusinessId };
|
||||||
|
|||||||
@ -1,44 +0,0 @@
|
|||||||
import { SupabaseClient } from "@supabase/supabase-js";
|
|
||||||
|
|
||||||
function getBusinesses(client: SupabaseClient, query: string | null) {
|
|
||||||
return client.from("business").select("id, business_name, joined_date").ilike(
|
|
||||||
"business_name",
|
|
||||||
`%${query}%`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function getProjects(client: SupabaseClient, businessIds: string[]) {
|
|
||||||
return client
|
|
||||||
.from("project")
|
|
||||||
.select(
|
|
||||||
`
|
|
||||||
id,
|
|
||||||
project_name,
|
|
||||||
business_id,
|
|
||||||
published_time,
|
|
||||||
project_short_description,
|
|
||||||
project_investment_detail (
|
|
||||||
min_investment,
|
|
||||||
total_investment,
|
|
||||||
target_investment
|
|
||||||
)
|
|
||||||
`,
|
|
||||||
)
|
|
||||||
.in("business_id", businessIds);
|
|
||||||
}
|
|
||||||
|
|
||||||
function getTags(client: SupabaseClient, projectIds: string[]) {
|
|
||||||
return client.from("item_tag").select("item_id, tag (value)").in(
|
|
||||||
"item_id",
|
|
||||||
projectIds,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function getInvestmentCounts(client: SupabaseClient, projectIds: string[]) {
|
|
||||||
return client.from("investment_deal").select("*", {
|
|
||||||
count: "exact",
|
|
||||||
head: true,
|
|
||||||
}).in("project_id", projectIds);
|
|
||||||
}
|
|
||||||
|
|
||||||
export { getBusinesses, getInvestmentCounts, getProjects, getTags };
|
|
||||||
5
src/lib/data/tagQuery.ts
Normal file
5
src/lib/data/tagQuery.ts
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
import { SupabaseClient } from "@supabase/supabase-js";
|
||||||
|
|
||||||
|
export const getTagsByProjectIds = (client: SupabaseClient, projectIds: string[]) => {
|
||||||
|
return client.from("project_tag").select(`item_id, ...tag (tag_value:value)`).in("item_id", projectIds);
|
||||||
|
};
|
||||||
Loading…
Reference in New Issue
Block a user