Skip to content

Commit

Permalink
Revert "Añadir filtro para solicitudes sin voluntarios"
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickwebsdev authored Nov 8, 2024
1 parent 00fe932 commit 0e28422
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 186 deletions.
157 changes: 63 additions & 94 deletions src/app/casos-activos/solicitudes/page.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';

import { useEffect, useState, useCallback, useMemo, Suspense } from 'react';
import { Suspense, useEffect, useState } from 'react';
import { HeartHandshake } from 'lucide-react';
import { supabase } from '@/lib/supabase/client';
import SolicitudCard from '@/components/SolicitudCard';
Expand All @@ -11,15 +11,9 @@ import { tiposAyudaOptions } from '@/helpers/constants';
import Modal from '@/components/Modal';
import { useModal } from '@/context/ModalProvider';
import { useTowns } from '@/context/TownProvider';
import { Toggle } from '@/components/Toggle';

const MODAL_NAME = 'solicitudes';

const itemsPerPage = 10;
const numPages = (count) => {
return Math.ceil(count / itemsPerPage) || 0;
};

export default function SolicitudesPage() {
return (
<Suspense>
Expand All @@ -41,46 +35,38 @@ function Solicitudes() {
const [currentCount, setCurrentCount] = useState(0);
const { toggleModal } = useModal();

const closeModal = useCallback(() => {
const closeModal = () => {
toggleModal(MODAL_NAME, false);
}, [toggleModal]);

const updateFilter = useCallback(
(filter, value) => {
const params = new URLSearchParams(searchParams.toString());
params.set(filter, value);
router.push(`?${params.toString()}`);
},
[searchParams, router],
);
};
const itemsPerPage = 10;
const numPages = (count) => {
return Math.ceil(count / itemsPerPage) || 0;
};

const updateFilter = (filter, value) => {
const params = new URLSearchParams(searchParams.toString());
params.set(filter, value);
router.push(`?${params.toString()}`);
};

const [filtroData, setFiltroData] = useState({
urgencia: searchParams.get('urgencia') || 'todas',
tipoAyuda: searchParams.get('tipoAyuda') || 'todas',
pueblo: searchParams.get('pueblo') || 'todos',
soloSinVoluntarios: searchParams.get('soloSinVoluntarios') || true,
});

const changeDataFilter = useCallback(
(type, newFilter) => {
setFiltroData((prev) => ({
...prev,
[type]: newFilter,
}));
updateFilter(type, newFilter);
},
[updateFilter, setFiltroData],
);

const changePage = useCallback(
(newPage) => {
setCurrentPage(newPage);
updateFilter('page', newPage);
},
[updateFilter],
);

const handleToggleChange = useCallback((e) => changeDataFilter('soloSinVoluntarios', e.target.checked), []);
const changeDataFilter = (type, newFilter) => {
setFiltroData((prev) => ({
...prev,
[type]: newFilter,
}));
updateFilter(type, newFilter);
};

function changePage(newPage) {
setCurrentPage(newPage);
updateFilter('page', newPage);
}

useEffect(() => {
async function fetchData() {
Expand All @@ -105,12 +91,6 @@ function Solicitudes() {
if (filtroData.urgencia !== 'todas') {
query.eq('urgency', filtroData.urgencia);
}

// Solo agregar filtro si es true
if (!!filtroData.soloSinVoluntarios) {
query.eq('asignees_count', 0);
}

query.neq('status', 'finished');
// Ejecutar la consulta con paginación
const { data, count, error } = await query
Expand All @@ -135,8 +115,6 @@ function Solicitudes() {
fetchData();
}, [filtroData, currentPage]);

const puebloSeleccionado = useMemo(() => getTownById(Number(filtroData.pueblo)), [filtroData, getTownById]);

if (loading) {
return (
<div className="flex justify-center items-center min-h-screen">
Expand All @@ -153,57 +131,48 @@ function Solicitudes() {
);
}

const puebloSeleccionado = getTownById(Number(filtroData.pueblo));

return (
<>
{/* FILTROS */}
<div className="flex flex-col sm:flex-row gap-2 items-center justify-between">
<div className="flex flex-col space-y-3 w-full">
<div className="flex flex-col sm:flex-row gap-2 w-full justify-end">
<div className="flex flex-col justify-center">
<p className="font-bold text-md">Filtros</p>
</div>
<select
value={filtroData.tipoAyuda}
onChange={(e) => changeDataFilter('tipoAyuda', e.target.value)}
className="px-4 py-2 rounded-lg w-full border border-gray-300 focus:ring-2 focus:ring-blue-500 focus:border-blue-500 bg-white text-gray-900 shadow-sm"
>
<option value="todas">Todas las necesidades</option>
{Object.entries(tiposAyudaOptions).map(([key, label]) => (
<option key={key} value={key}>
{label}
</option>
))}
</select>
<select
value={filtroData.urgencia}
onChange={(e) => changeDataFilter('urgencia', e.target.value)}
className="px-4 py-2 rounded-lg w-full border border-gray-300 focus:ring-2 focus:ring-blue-500 focus:border-blue-500 bg-white text-gray-900 shadow-sm"
>
<option value="todas">Todas las prioridades</option>
<option value="alta">Alta prioridad</option>
<option value="media">Media prioridad</option>
<option value="baja">Baja prioridad</option>
</select>
<select
value={filtroData.pueblo}
onChange={(e) => changeDataFilter('pueblo', e.target.value)}
className="px-4 py-2 rounded-lg w-full border border-gray-300 focus:ring-2 focus:ring-blue-500 focus:border-blue-500 bg-white text-gray-900 shadow-sm"
>
<option value="todos">Todos los pueblos</option>
{towns.map((item) => (
<option key={item.id} value={item.id}>
{item.name}
</option>
))}
</select>
</div>
<div className="flex flex-row flex-1 justify-end">
<Toggle
handleChange={handleToggleChange}
checked={filtroData.soloSinVoluntarios}
label="Sólo ofertas sin voluntarios"
/>
</div>
<p className="font-bold text-md">Filtros</p>
<div className="flex flex-col sm:flex-row gap-2 w-full justify-end">
<select
value={filtroData.tipoAyuda}
onChange={(e) => changeDataFilter('tipoAyuda', e.target.value)}
className="px-4 py-2 rounded-lg w-full border border-gray-300 focus:ring-2 focus:ring-blue-500 focus:border-blue-500 bg-white text-gray-900 shadow-sm"
>
<option value="todas">Todas las necesidades</option>
{Object.entries(tiposAyudaOptions).map(([key, label]) => (
<option key={key} value={key}>
{label}
</option>
))}
</select>
<select
value={filtroData.urgencia}
onChange={(e) => changeDataFilter('urgencia', e.target.value)}
className="px-4 py-2 rounded-lg w-full border border-gray-300 focus:ring-2 focus:ring-blue-500 focus:border-blue-500 bg-white text-gray-900 shadow-sm"
>
<option value="todas">Todas las prioridades</option>
<option value="alta">Alta prioridad</option>
<option value="media">Media prioridad</option>
<option value="baja">Baja prioridad</option>
</select>
<select
value={filtroData.pueblo}
onChange={(e) => changeDataFilter('pueblo', e.target.value)}
className="px-4 py-2 rounded-lg w-full border border-gray-300 focus:ring-2 focus:ring-blue-500 focus:border-blue-500 bg-white text-gray-900 shadow-sm"
>
<option value="todos">Todos los pueblos</option>
{towns.map((item) => (
<option key={item.id} value={item.id}>
{item.name}
</option>
))}
</select>
</div>
</div>
<div className="grid gap-4">
Expand Down
33 changes: 0 additions & 33 deletions src/components/Toggle.tsx

This file was deleted.

40 changes: 3 additions & 37 deletions src/lib/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,48 +65,14 @@ export const helpRequestService = {

async assign(requestData: HelpRequestAssignmentInsert) {
const { data, error } = await supabase.from('help_request_assignments').insert([requestData]).select();
if (error) throw error;

const { data: linkedRequestData, error: errorGettingLinkedData } = await supabase
.from('help_requests')
.select('*')
.eq('id', requestData.help_request_id);
if (errorGettingLinkedData) throw errorGettingLinkedData;
if (!linkedRequestData) throw new Error('No se puede encontrar esta tarea');

const { error: errorUpdatingAssigneesCount } = await supabase
.from('help_requests')
.update({ asignees_count: linkedRequestData[0].asignees_count + 1 });
if (errorUpdatingAssigneesCount) throw errorUpdatingAssigneesCount;

if (error) throw error;
return data[0];
},
async unassign(id: number) {
const { data, error: errorFindingRow } = await supabase.from('help_request_assignments').select('*').eq('id', id);
if (errorFindingRow || !data) {
throw new Error('No se puede encontrar la tarea');
}

const requestId = data[0].help_request_id;
const { error } = await supabase.from('help_request_assignments').delete().eq('id', id);

const { error: errorDeletingAssignment } = await supabase.from('help_request_assignments').delete().eq('id', id);
if (errorDeletingAssignment) throw errorDeletingAssignment;

const { data: linkedRequestData, error: errorGettingLinkedData } = await supabase
.from('help_requests')
.select('*')
.eq('id', requestId);

if (errorGettingLinkedData) throw errorGettingLinkedData;
if (!linkedRequestData) throw new Error('No se puede encontrar esta tarea');

const { asignees_count } = linkedRequestData[0];
const newNumberAssignees = asignees_count <= 0 ? 0 : asignees_count - 1;

const { error: errorUpdatingAssigneesCount } = await supabase
.from('help_requests')
.update({ asignees_count: newNumberAssignees });
if (errorUpdatingAssigneesCount) throw errorUpdatingAssigneesCount;
if (error) throw error;
},

async getByType(type: any) {
Expand Down
3 changes: 0 additions & 3 deletions src/types/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ export type Database = {
help_requests: {
Row: {
additional_info: Json | null;
asignees_count: number;
contact_info: string | null;
coordinates: unknown | null;
created_at: string | null;
Expand All @@ -193,7 +192,6 @@ export type Database = {
};
Insert: {
additional_info?: Json | null;
asignees_count?: number;
contact_info?: string | null;
coordinates?: unknown | null;
created_at?: string | null;
Expand All @@ -215,7 +213,6 @@ export type Database = {
};
Update: {
additional_info?: Json | null;
asignees_count?: number;
contact_info?: string | null;
coordinates?: unknown | null;
created_at?: string | null;
Expand Down
2 changes: 1 addition & 1 deletion supabase/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ enabled = true
# Specifies an ordered list of seed files to load during db reset.
# Supports glob patterns relative to supabase directory. For example:
# sql_paths = ['./seeds/*.sql', '../project-src/seeds/*-load-testing.sql']
sql_paths = ['./towns.sql', './help_requests.sql']
sql_paths = ['./seed.sql']

[realtime]
enabled = true
Expand Down
11 changes: 0 additions & 11 deletions supabase/help_requests.sql

This file was deleted.

This file was deleted.

This file was deleted.

File renamed without changes.

0 comments on commit 0e28422

Please sign in to comment.