Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Add more types #151

Merged
merged 3 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NextResponse } from 'next/server';
import { NextRequest, NextResponse } from 'next/server';
import { createClient } from '@/lib/supabase/server';

export async function GET() {
Expand All @@ -16,7 +16,7 @@ export async function GET() {
return Response.json({ registeredPost });
}

export async function POST(request) {
export async function POST(request: NextRequest) {
try {
// Obtener los datos del formulario (multipart/form-data)
const formData = await request.formData();
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/app/casos-activos/ofertas/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export default function Ofertas() {
</button>
</div>
) : (
data.map((caso) => <OfferCard caso={caso} towns={towns} isHref={true} key={caso.id} />)
data.map((caso) => <OfferCard caso={caso} towns={towns} showLink={true} key={caso.id} />)
)}
</div>
<div className="flex items-center justify-center">
Expand Down
21 changes: 0 additions & 21 deletions src/app/oferta/page.js

This file was deleted.

36 changes: 15 additions & 21 deletions src/app/oferta/[id]/page.js → src/app/ofertas/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,31 @@
'use client';
import { useEffect, useState } from 'react';

import { useParams } from 'next/navigation';
import { supabase } from '@/lib/supabase/client';
import { ArrowLeft } from 'lucide-react';
import OfferCard from '@/components/OfferCard';
import { useTowns } from '@/context/TownProvider';
import { useQuery } from '@tanstack/react-query';
import { HelpRequestData } from '@/types/Requests';
import { helpRequestService } from '@/lib/service';

export default function CasoDetalle() {
const params = useParams();
const { id } = params;
const [caso, setCaso] = useState(null);
const [loading, setLoading] = useState(true);
const { towns } = useTowns();
useEffect(() => {
async function fetchCaso() {
const { data, error } = await supabase.from('help_requests').select('*').eq('id', id).single();
if (error) {
console.error('Error fetching caso:', error);
} else {
setCaso(data);
}
setLoading(false);
}
fetchCaso();
}, [id]);
if (loading) {
const {
data: request,
isLoading,
error,
} = useQuery<HelpRequestData>({
queryKey: ['help_requests', { id: id }],
queryFn: () => helpRequestService.getOne(Number(id)),
});
if (isLoading) {
return (
<div className="flex justify-center items-center min-h-screen">
<div className="animate-spin rounded-full h-12 w-12 border-b-2 border-blue-500"></div>
</div>
);
}
if (!caso) {
if (error || request === undefined) {
return (
<div className="space-y-6 mx-auto max-w-7xl px-4 sm:px-6">
<div className="flex justify-start">
Expand Down Expand Up @@ -60,7 +54,7 @@ export default function CasoDetalle() {
Volver
</button>
</div>
<OfferCard towns={towns} key={caso.id} caso={caso} />
<OfferCard caso={request} showLink={false} />
</div>
);
}
40 changes: 0 additions & 40 deletions src/app/ofertas/editar/[id]/page.js

This file was deleted.

25 changes: 25 additions & 0 deletions src/app/ofertas/editar/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import OfferHelp from '@/components/OfferHelp';
import Unauthorized from '@/components/Unauthorized';
import { createClient } from '@/lib/supabase/server';
import { helpRequestService } from '@/lib/service';

export default async function EditarSolicitud({ params }: { params: Promise<{ id: string }> }) {
const { id } = await params;
const supabase = await createClient();
const { data: session } = await supabase.auth.getUser();
if (session.user === null) {
return <Unauthorized />;
}
const numberId = Number(id);
const request = await helpRequestService.getOne(numberId);
return (
<OfferHelp
title="Editar solicitud de ayuda"
submitType="edit"
data={request}
id={numberId}
button={['Editar oferta', 'Editando oferta...']}
redirect="/ofertas"
/>
);
}
36 changes: 14 additions & 22 deletions src/app/ofertas/page.js → src/app/ofertas/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,28 @@ import { useTowns } from '@/context/TownProvider';
import { useSession } from '@/context/SessionProvider';
import OfferCard from '@/components/OfferCard';
import Link from 'next/link';
import { HelpRequestData } from '@/types/Requests';

export default function ListaSolicitudes() {
const session = useSession();

const searchParams = useSearchParams();
const router = useRouter();

const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
const [loading, setLoading] = useState<boolean>(true);
const [error, setError] = useState<string | null>(null);

const [data, setData] = useState([]);
const [currentPage, setCurrentPage] = useState(Number(searchParams.get('page')) || 1);
const [currentCount, setCurrentCount] = useState(0);
const [data, setData] = useState<HelpRequestData[]>([]);
const [currentPage, setCurrentPage] = useState<number>(Number(searchParams.get('page')) || 1);
const [currentCount, setCurrentCount] = useState<number>(0);
const { towns } = useTowns();

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

const updateFilter = (filter, value) => {
const updateFilter = (filter: 'urgencia' | 'tipoAyuda' | 'pueblo' | 'page', value: string) => {
const params = new URLSearchParams(searchParams.toString());
params.set(filter, value);
router.push(`?${params.toString()}`);
Expand All @@ -41,17 +42,17 @@ export default function ListaSolicitudes() {
pueblo: searchParams.get('pueblo') || 'todos',
});

const changeDataFilter = (type, newFilter) => {
const changeDataFilter = (type: 'urgencia' | 'tipoAyuda' | 'pueblo', newFilter: string) => {
setFiltroData((prev) => ({
...prev,
[type]: newFilter,
}));
updateFilter(type, newFilter);
};

function changePage(newPage) {
function changePage(newPage: number) {
setCurrentPage(newPage);
updateFilter('page', newPage);
updateFilter('page', newPage.toString());
}
useEffect(() => {
async function fetchData() {
Expand All @@ -64,7 +65,7 @@ export default function ListaSolicitudes() {
.from('help_requests')
.select('*', { count: 'exact' })
.eq('type', 'ofrece')
.contains('additional_info', { email: session.user.email });
.contains('additional_info', { email: session.user?.email });
// Solo agregar filtro si no es "todos"
if (filtroData.pueblo !== 'todos') {
query.eq('town_id', filtroData.pueblo);
Expand All @@ -80,7 +81,7 @@ export default function ListaSolicitudes() {
.range((currentPage - 1) * itemsPerPage, currentPage * itemsPerPage - 1)
.order('created_at', { ascending: false });

if (error) {
if (error || count === null) {
console.log('Error fetching solicitudes:', error);
setData([]);
} else {
Expand Down Expand Up @@ -174,16 +175,7 @@ export default function ListaSolicitudes() {
</Link>
</div>
) : (
data.map((caso) => (
<OfferCard
isHref={true}
key={caso.id}
caso={caso}
button={{ text: 'Editar', link: '/ofertas/editar/' }}
isEdit={true}
towns={towns}
/>
))
data.map((caso) => <OfferCard showLink={true} key={caso.id} caso={caso} />)
)}
</div>
<div className="flex items-center justify-center">
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// personas-animales-desaparecidos/page.js
// personas-animales-desaparecidos/page.tsx

import { Users, PawPrint, Car, UserSearch } from 'lucide-react';

Expand Down
Loading