Skip to content

Commit

Permalink
filter soloSinAsignar in server
Browse files Browse the repository at this point in the history
  • Loading branch information
j8seangel committed Nov 15, 2024
1 parent d1f377b commit 25337bb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
4 changes: 3 additions & 1 deletion src/app/casos-activos/solicitudes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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],
);
Expand Down
39 changes: 27 additions & 12 deletions src/app/casos-activos/solicitudes/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -20,12 +22,19 @@ function parseData(data: Database['public']['Tables']['help_requests']['Row'][])
});
}

const getData = async (supabase: SupabaseClient<Database>) => {
const { error, data } = await supabase
.from('help_requests')
.select('*')
const getData = async (supabase: SupabaseClient<Database>, 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);
Expand All @@ -34,11 +43,16 @@ const getData = async (supabase: SupabaseClient<Database>) => {
return parseData(data);
};

const getCount = async (supabase: SupabaseClient<Database>) => {
const { count: solicitaCount, error: solicitaError } = await supabase
.from('help_requests')
const getCount = async (supabase: SupabaseClient<Database>, 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')
Expand All @@ -58,10 +72,11 @@ const getCount = async (supabase: SupabaseClient<Database>) => {
};
};

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 (
<Suspense
Expand Down

0 comments on commit 25337bb

Please sign in to comment.