diff --git a/src/app/casos-activos/solicitudes/index.tsx b/src/app/casos-activos/solicitudes/index.tsx index 914337c..09648fd 100644 --- a/src/app/casos-activos/solicitudes/index.tsx +++ b/src/app/casos-activos/solicitudes/index.tsx @@ -47,7 +47,9 @@ export function Solicitudes({ data, count }: SolicitudesProps) { (filter: FilterType, value: string | number) => { const params = new URLSearchParams(searchParams.toString()); params.set(filter, value.toString()); - router.push(`?${params.toString()}`); + if (filter === 'soloSinAsignar') { + router.push(`?${params.toString()}`); + } }, [searchParams, router], ); diff --git a/src/app/casos-activos/solicitudes/page.tsx b/src/app/casos-activos/solicitudes/page.tsx index ecf0cea..5e5ee69 100644 --- a/src/app/casos-activos/solicitudes/page.tsx +++ b/src/app/casos-activos/solicitudes/page.tsx @@ -3,7 +3,9 @@ import { SupabaseClient } from '@supabase/supabase-js'; import { createClient } from '@/lib/supabase/server'; import { Database } from '@/types/database'; import { Solicitudes } from '.'; -import { HelpRequestData } from '@/types/Requests'; +import { helpDataSelectFields, HelpRequestData } from '@/types/Requests'; +import { PageProps } from '.next/types/app/layout'; +import { FiltersData } from './types'; export const dynamic = 'force-dynamic'; @@ -20,12 +22,19 @@ function parseData(data: Database['public']['Tables']['help_requests']['Row'][]) }); } -const getData = async (supabase: SupabaseClient) => { - const { error, data } = await supabase - .from('help_requests') - .select('*') +const getData = async (supabase: SupabaseClient, filters: FiltersData) => { + const query = supabase + .from('help_requests_with_assignment_count') + .select(helpDataSelectFields as '*', { count: 'exact' }) .eq('type', 'necesita') - .order('created_at', { ascending: false }); + .neq('status', 'finished'); + + // Solo agregar filtro si es true + if (filters.soloSinAsignar !== undefined && filters.soloSinAsignar === 'true') { + query.eq('assignments_count', 0); + } + + const { data, error } = await query.order('created_at', { ascending: false }); if (error) { throw new Error('Error fetching solicita:', error); @@ -34,11 +43,16 @@ const getData = async (supabase: SupabaseClient) => { return parseData(data); }; -const getCount = async (supabase: SupabaseClient) => { - const { count: solicitaCount, error: solicitaError } = await supabase - .from('help_requests') +const getCount = async (supabase: SupabaseClient, filters: FiltersData) => { + const query = supabase + .from('help_requests_with_assignment_count') .select('id', { count: 'exact' }) .eq('type', 'necesita'); + // Solo agregar filtro si es true + if (filters.soloSinAsignar !== undefined && filters.soloSinAsignar === 'true') { + query.eq('assignments_count', 0); + } + const { count: solicitaCount, error: solicitaError } = await query; const { count: ofreceCount, error: ofreceError } = await supabase .from('help_requests') @@ -58,10 +72,11 @@ const getCount = async (supabase: SupabaseClient) => { }; }; -export default async function SolicitudesPage() { +export default async function SolicitudesPage(props: PageProps) { + const searchParams = (await props.searchParams) as FiltersData; const supabase = await createClient(); - const data = await getData(supabase); - const count = await getCount(supabase); + const data = await getData(supabase, searchParams); + const count = await getCount(supabase, searchParams); return (