diff --git a/src/components/ChangeUrgencyHelpRequest.tsx b/src/components/ChangeUrgencyHelpRequest.tsx new file mode 100644 index 00000000..147cfd37 --- /dev/null +++ b/src/components/ChangeUrgencyHelpRequest.tsx @@ -0,0 +1,101 @@ +'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 [data, setData] = 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) { + setData(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/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.jsx b/src/components/SolicitudCard.jsx index 576be9ec..edbc4afb 100644 --- a/src/components/SolicitudCard.jsx +++ b/src/components/SolicitudCard.jsx @@ -9,7 +9,8 @@ import PhoneInfo from '@/components/PhoneInfo'; import DeleteHelpRequest from './DeleteHelpRequest'; import { useTowns } from '@/context/TownProvider'; import { useRole } from '@/context/RoleProvider'; -import { useState } from 'react'; +import { useEffect, useState } from 'react'; +import ChangeUrgencyHelpRequest from './ChangeUrgencyHelpRequest'; // type SolicitudCardProps = { // caso: HelpRequestData; @@ -26,6 +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 +37,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 +101,7 @@ export default function SolicitudCard({ caso, showLink = true, showEdit = false {caso.contact_info && (
- +
)} {caso.help_type && ( @@ -182,6 +184,13 @@ export default function SolicitudCard({ caso, showLink = true, showEdit = false )} + {isAdmin && ( + + )} {isAdmin && setDeleted(true)} />}