From 30e4c9fe108991ec18144ab60777044bf1e307c2 Mon Sep 17 00:00:00 2001 From: THIS ONE IS A LITTLE BIT TRICKY KRUB Date: Wed, 2 Oct 2024 19:44:58 +0700 Subject: [PATCH] Refactor Find page to implement search functionality using Supabase --- src/app/find/page.tsx | 177 ++++++++++++++++++++++++++++++------------ 1 file changed, 127 insertions(+), 50 deletions(-) diff --git a/src/app/find/page.tsx b/src/app/find/page.tsx index c449894..b19f803 100644 --- a/src/app/find/page.tsx +++ b/src/app/find/page.tsx @@ -11,63 +11,140 @@ if (!supabaseUrl || !supabaseKey) { } const supabase = createClient(supabaseUrl, supabaseKey); -export default function Find(){ +interface ProjectInvestmentDetail { + minInvestment: number; + totalInvestment: number; + targetInvestment: number; +} + +interface Project { + id: string; + projectName: string; + businessId: string; + investmentCount: number; + ProjectInvestmentDetail: ProjectInvestmentDetail[]; + tags: string[]; +} + +interface Business { + id: string; + businessName: string; + joinedDate: string; + Projects: Project[]; +} + +export default function Find() { const searchParams = useSearchParams(); const query = searchParams.get('query'); - const [result, setResult] = useState<{ businessName: string }[] | "">(""); + const [results, setResults] = useState([]); + const [loading, setLoading] = useState(true); useEffect(() => { - if (query) { - getQuery(query); - } - }, [query]); - const getQuery = async (query: string) => { - console.log('Query is: ', query); - - // search for relevant Business names first - let { data: businessData, error: businessError } = await supabase - .from('Business') - .select('id, businessName') - .ilike('businessName', `%${query}%`); - - if (businessError) { - console.error('Error fetching businesses:', businessError); - return; - } - - console.log('Business Search Results:', businessData); - - // then search for relevant Project names - let { data: projectData, error: projectError } = await supabase - .from('Projects') - .select('id, projectName') - .ilike('projectName', `%${query}%`); - - if (projectError) { - console.error('Error fetching projects:', projectError); - return; - } - - console.log('Project Search Results:', projectData); - - return { - businesses: businessData, - projects: projectData, + const fetchData = async () => { + setLoading(true); + + // fetch businesses + const { data: businesses, error: businessError } = await supabase + .from('Business') + .select('id, businessName, joinedDate') + .ilike('businessName', `%${query}%`); + + if (businessError) { + console.error('Error fetching businesses:', businessError); + setLoading(false); + return; + } + + // fetch projects for these businesses + const { data: projects, error: projectError } = await supabase + .from('Project') + .select(` + id, + projectName, + businessId, + ProjectInvestmentDetail ( + minInvestment, + totalInvestment, + targetInvestment + ) + `) + .in('businessId', businesses?.map(b => b.id) || []); + + if (projectError) { + console.error('Error fetching projects:', projectError); + setLoading(false); + return; + } + + // fetch tags for these projects + const { data: tags, error: tagError } = await supabase + .from('ItemTag') + .select('itemId, Tag (value)') + .in('itemId', projects?.map(p => p.id) || []); + + if (tagError) { + console.error('Error fetching tags:', tagError); + } + + // fetch investment counts + const { data: investmentCounts, error: investmentError } = await supabase + .from('InvestmentDeal') + .select('projectId, count', { count: 'exact', head: false }) + .in('projectId', projects?.map(p => p.id) || []); + + if (investmentError) { + console.error('Error fetching investment counts:', investmentError); + } + + // combine all data + const processedResults = businesses?.map(business => ({ + ...business, + Projects: projects + ?.filter(project => project.businessId === business.id) + .map(project => ({ + ...project, + tags: tags + ?.filter(t => t.itemId === project.id) + .map(t => t.Tag.values as unknown as string) || [], + investmentCount: investmentCounts?.find(ic => ic.projectId === project.id)?.count || 0 + })) || [] + })) || []; + + setResults(processedResults); + setLoading(false); }; - }; - + + if (query) { + fetchData(); + } + }, [query]); + return (
-
-

You searched for "{query}"

- {Array.isArray(result) ? ( - result.map((item, index) => ( -
{item.businessName}
- )) - ) : ( - result - )} -
+ {loading &&

Loading...

} + {!loading && results.length === 0 &&

No results found.

} + {!loading && results.length > 0 && ( +
    + {results.map(business => ( +
  • +

    {business.businessName}

    +

    Joined Date: {new Date(business.joinedDate).toLocaleDateString()}

    +
      + {business.Projects.map((project) => ( +
    • +

      {project.projectName}

      +

      Investment Count: {project.investmentCount}

      +

      Min Investment: ${project.ProjectInvestmentDetail[0]?.minInvestment}

      +

      Total Investment: ${project.ProjectInvestmentDetail[0]?.totalInvestment}

      +

      Target Investment: ${project.ProjectInvestmentDetail[0]?.targetInvestment}

      +

      Tags: {project.tags.join(', ')}

      +
    • + ))} +
    +
  • + ))} +
+ )}
); } \ No newline at end of file