diff --git a/src/components/ChangeUrgencyHelpRequest.tsx b/src/components/ChangeUrgencyHelpRequest.tsx new file mode 100644 index 00000000..c674d36b --- /dev/null +++ b/src/components/ChangeUrgencyHelpRequest.tsx @@ -0,0 +1,99 @@ +'use client'; + +import { supabase } from '@/lib/supabase/client'; +import { ChangeEvent, MouseEvent, useState } from 'react'; +import { Spinner } from '@/components/Spinner'; +import Modal from '@/components/Modal'; +import { useModal } from '@/context/ModalProvider'; + +type ChangeStatusRequestButtonProps = { + helpRequestId: number; + onUpdate: (urgency: string) => void; + currentUrgency: string; +}; + +export default function ChangeUrgencyHelpRequest({ + helpRequestId, + onUpdate, + currentUrgency, +}: ChangeStatusRequestButtonProps) { + const { toggleModal } = useModal(); + const [isLoading, setLoading] = useState(false); + const [error, setError] = useState({}); + const [status, setStatus] = useState(currentUrgency); + + const MODAL_NAME = `Actualizar-Solicitud-${helpRequestId}`; + + const updateHelpRequest = async () => { + const { data, error } = await supabase + .from('help_requests') + .update({ urgency: status }) + .eq('id', helpRequestId) + .select(); + + return { data, error }; + }; + + async function handleUpdateSubmit(e: MouseEvent) { + e.preventDefault(); + const { data, error } = await updateHelpRequest(); + if (error) { + setError(error); + return; + } + if (data) { + onUpdate(status); + } + toggleModal(MODAL_NAME, false); + } + async function handleSubmit(e: ChangeEvent) { + e.preventDefault(); + toggleModal(MODAL_NAME, true); + } + + if (isLoading) return ; + if (error === undefined) return <>; + + return ( + <> + + + +
+

Cambiar estado

+

¿Estas seguro que deseas cambiar el estado de solicitud a {status}?

+
+ + +
+
+
+ + ); +} diff --git a/src/components/DeleteHelpRequest.tsx b/src/components/DeleteHelpRequest.tsx index ffe05287..2446bea0 100644 --- a/src/components/DeleteHelpRequest.tsx +++ b/src/components/DeleteHelpRequest.tsx @@ -15,7 +15,6 @@ export default function DeleteHelpRequest({ helpRequestId, onDelete }: DeleteReq const { toggleModal } = useModal(); const [isLoading, setLoading] = useState(false); const [error, setError] = useState({}); - const [data, setData] = useState({}); const MODAL_NAME = `Eliminar-Solicitud-${helpRequestId}`; @@ -31,8 +30,9 @@ export default function DeleteHelpRequest({ helpRequestId, onDelete }: DeleteReq setError(error); return; } - if (data) setData(data); - onDelete(); + if (data) { + onDelete(); + } toggleModal(MODAL_NAME, false); } async function handleSubmit(e: MouseEvent) { diff --git a/src/components/OfferCard.tsx b/src/components/OfferCard.tsx index 8aea9f25..4f4ab612 100644 --- a/src/components/OfferCard.tsx +++ b/src/components/OfferCard.tsx @@ -15,15 +15,7 @@ export default function OfferCard({ caso, showLink = true }: OfferCardProps) {
- +
{caso.name} #{caso.id} diff --git a/src/components/PhoneInfo.tsx b/src/components/PhoneInfo.tsx index 7d37b487..83d05ed4 100644 --- a/src/components/PhoneInfo.tsx +++ b/src/components/PhoneInfo.tsx @@ -5,8 +5,9 @@ import { HelpRequestData } from '@/types/Requests'; type PhoneInfoProps = { caseInfo: HelpRequestData; + isAdmin: boolean; }; -export default function PhoneInfo({ caseInfo }: PhoneInfoProps) { +export default function PhoneInfo({ caseInfo, isAdmin }: PhoneInfoProps) { const session = useSession(); const { @@ -26,9 +27,11 @@ export default function PhoneInfo({ caseInfo }: PhoneInfoProps) { Contacto:{' '} {session && session.user - ? !!userAssignment + ? isAdmin ? caseInfo.contact_info - : 'Dale al botón "Quiero ayudar" para ver sus datos de contacto.' + : !!userAssignment + ? caseInfo.contact_info + : 'Dale al botón "Quiero ayudar" para ver sus datos de contacto.' : 'Inicia sesion para ver este dato'} ); diff --git a/src/components/SolicitudCard.tsx b/src/components/SolicitudCard.tsx index 9d0629d0..046d72ad 100644 --- a/src/components/SolicitudCard.tsx +++ b/src/components/SolicitudCard.tsx @@ -10,6 +10,7 @@ import DeleteHelpRequest from './DeleteHelpRequest'; import { useTowns } from '@/context/TownProvider'; import { useRole } from '@/context/RoleProvider'; import { useState } from 'react'; +import ChangeUrgencyHelpRequest from './ChangeUrgencyHelpRequest'; type SolicitudCardProps = { caso: HelpRequestData; @@ -26,7 +27,7 @@ export default function SolicitudCard({ caso, showLink = true, showEdit = false const isAdmin = role === 'admin'; const [deleted, setDeleted] = useState(false); const isMyRequest = session.user?.id && session.user.id === caso.user_id; - + const [updateUrgency, setUpdateUrgency] = useState(caso.urgency); return ( !deleted && (
@@ -35,9 +36,9 @@ export default function SolicitudCard({ caso, showLink = true, showEdit = false
- Urgencia: {caso.urgency === 'alta' ? 'Alta' : caso.urgency === 'media' ? 'Media' : 'Baja'} + Urgencia: {updateUrgency === 'alta' ? 'Alta' : updateUrgency === 'media' ? 'Media' : 'Baja'} #{caso.id}
@@ -99,7 +100,7 @@ export default function SolicitudCard({ caso, showLink = true, showEdit = false {caso.contact_info && (
- +
)} {caso.help_type && ( @@ -182,6 +183,13 @@ export default function SolicitudCard({ caso, showLink = true, showEdit = false )} + {isAdmin && ( + + )} {isAdmin && setDeleted(true)} />}