diff --git a/src/app/find/page.tsx b/src/app/find/page.tsx index c3e3672..b6611a9 100644 --- a/src/app/find/page.tsx +++ b/src/app/find/page.tsx @@ -3,27 +3,14 @@ import { useSearchParams } from "next/navigation"; import { createSupabaseClient } from "@/lib/supabase/clientComponentClient"; import { useQuery } from "@supabase-cache-helpers/postgrest-react-query"; -import { ReactQueryDevtools } from "@tanstack/react-query-devtools"; import { ProjectCard } from "@/components/projectCard"; import { Separator } from "@/components/ui/separator"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; -import { getBusinesses, getInvestmentCounts, getProjects, getTags } from "@/lib/data/query"; -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[]; -} +import { getBusinessAndProject } from "@/lib/data/businessQuery"; export default function Find() { const searchParams = useSearchParams(); const query = searchParams.get("query"); - // const query = "neon"; let supabase = createSupabaseClient(); @@ -31,94 +18,62 @@ export default function Find() { data: businesses, isLoading: isLoadingBusinesses, error: businessError, - } = useQuery(getBusinesses(supabase, query)); + } = useQuery(getBusinessAndProject(supabase, query)); - const businessIds = businesses?.map((b) => b.id) || []; - const { - 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, - })) || [], - })) || []; + const isLoading = isLoadingBusinesses; + const error = businessError; if (isLoading) return

Loading...

; if (error) return

Error fetching data: {error.message}

; return ( -
-
+
+

Result

- {results.length === 0 &&

No results found.

} - {results.length > 0 && ( -
    - {results.map((business) => ( -
  • - - - {business.businessName} - Joined Date: {new Date(business.joinedDate).toLocaleDateString()} - - - {business.Projects.map((project) => ( - - ))} - - -
  • - ))} -
- )} - + + + {businesses!.length === 0 &&

No results found.

} + {businesses!.length > 0 && ( +
    + {businesses!.map((business) => ( +
  • + + + {business.business_name} + + Joined Date: {new Date(business.joined_date).toLocaleDateString()} + + + + {business?.projects && business.projects.length > 0 ? ( + business.projects.map((project) => ( + String(tag.tag_value)) || []} + imageUri={project.card_image_url} + /> + )) + ) : ( +

    No Projects

    + )} +
    +
    +
  • + ))} +
+ )} +
+
); diff --git a/src/lib/data/businessQuery.ts b/src/lib/data/businessQuery.ts new file mode 100644 index 0000000..4938430 --- /dev/null +++ b/src/lib/data/businessQuery.ts @@ -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; +}; diff --git a/src/lib/data/investmentQuery.ts b/src/lib/data/investmentQuery.ts new file mode 100644 index 0000000..e978640 --- /dev/null +++ b/src/lib/data/investmentQuery.ts @@ -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); +}; diff --git a/src/lib/data/projectQuery.ts b/src/lib/data/projectQuery.ts index 2f131a4..568b5da 100644 --- a/src/lib/data/projectQuery.ts +++ b/src/lib/data/projectQuery.ts @@ -1,9 +1,6 @@ import { SupabaseClient } from "@supabase/supabase-js"; -async function getTopProjects( - client: SupabaseClient, - numberOfRecords: number = 4 -) { +async function getTopProjects(client: SupabaseClient, numberOfRecords: number = 4) { try { const { data, error } = await client .from("project") @@ -62,7 +59,7 @@ function getProjectDataQuery(client: SupabaseClient, projectId: number) { target_investment, investment_deadline ), - tags:item_tag!inner ( + tags:project_tag!inner ( ...tag!inner ( tag_name:value ) @@ -88,7 +85,7 @@ async function getProjectData(client: SupabaseClient, projectId: number) { target_investment, investment_deadline ), - tags:item_tag!inner ( + tags:project_tag!inner ( ...tag!inner ( tag_name:value ) @@ -149,7 +146,7 @@ function searchProjectsQuery( target_investment, investment_deadline ), - tags:item_tag!inner ( + tags:project_tag!inner ( ...tag!inner ( tag_name:value ) @@ -186,7 +183,7 @@ function searchProjectsQuery( } if (tagsFilter) { - query = query.in("item_tag.tag.value", tagsFilter); + query = query.in("project_tag.tag.value", tagsFilter); } if (projectStatus) { @@ -200,9 +197,25 @@ function searchProjectsQuery( return query; } -export { - getProjectData, - getProjectDataQuery, - getTopProjects, - searchProjectsQuery, +const getProjectByBusinessId = (client: SupabaseClient, businessIds: string[]) => { + return client + .from("project") + .select( + ` + 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 }; diff --git a/src/lib/data/query.ts b/src/lib/data/query.ts deleted file mode 100644 index 6689a34..0000000 --- a/src/lib/data/query.ts +++ /dev/null @@ -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 }; diff --git a/src/lib/data/tagQuery.ts b/src/lib/data/tagQuery.ts new file mode 100644 index 0000000..a3f92cb --- /dev/null +++ b/src/lib/data/tagQuery.ts @@ -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); +};