-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #169 from pedrolivaresanchez/feature/admin-change-…
…urgency feature: admin can change urgency
- Loading branch information
Showing
5 changed files
with
124 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<string>(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 <Spinner />; | ||
if (error === undefined) return <></>; | ||
|
||
return ( | ||
<> | ||
<select | ||
name="urgencia" | ||
id="urgencia" | ||
value={status} | ||
onChange={(e) => { | ||
setStatus(e.target.value); | ||
handleSubmit(e); | ||
}} | ||
className="w-full text-center rounded-xl px-4 py-2 font-semibold text-white sm:w-auto transition-all bg-blue-500" | ||
> | ||
<option value="alta">Alta</option> | ||
<option value="media">Media</option> | ||
<option value="baja">Baja</option> | ||
</select> | ||
|
||
<Modal id={MODAL_NAME} allowClose={false}> | ||
<div className="bg-yellow-50 p-4 rounded"> | ||
<h2 className="text-yellow-800 font-semibold mb-4">Cambiar estado</h2> | ||
<p className="text-yellow-800">¿Estas seguro que deseas cambiar el estado de solicitud a {status}?</p> | ||
<div className="mt-4 flex justify-end space-x-4"> | ||
<button | ||
onClick={() => { | ||
toggleModal(MODAL_NAME, false); | ||
setStatus(currentUrgency); | ||
}} | ||
className="flex-1 bg-gray-500 text-white py-3 px-4 rounded-lg font-semibold" | ||
> | ||
Cancelar | ||
</button> | ||
<button | ||
onClick={handleUpdateSubmit} | ||
className="flex-1 bg-red-500 text-white py-3 px-4 rounded-lg font-semibold" | ||
> | ||
Actualizar | ||
</button> | ||
</div> | ||
</div> | ||
</Modal> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters