-
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 #86 from pedrolivaresanchez/feature/asignar-volunt…
…ario Asignar voluntario a petición de ayuda
- Loading branch information
Showing
23 changed files
with
438 additions
and
107 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -15,13 +15,15 @@ | |
"@react-google-maps/api": "^2.20.3", | ||
"@supabase/ssr": "^0.5.1", | ||
"@supabase/supabase-js": "^2.46.1", | ||
"@tanstack/react-query": "^5.59.19", | ||
"deck.gl": "^9.0.34", | ||
"leaflet": "^1.9.4", | ||
"lucide-react": "^0.454.0", | ||
"maplibre-gl": "^4.7.1", | ||
"next": "15.0.2", | ||
"react": "^18.3.1", | ||
"react-dom": "^18.3.1" | ||
"react-dom": "^18.3.1", | ||
"sonner": "^1.7.0" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^22.8.7", | ||
|
@@ -32,9 +34,9 @@ | |
"eslint-config-prettier": "^9.1.0", | ||
"postcss": "^8", | ||
"prettier": "3.3.3", | ||
"supabase": "^1.215.0", | ||
"tailwindcss": "^3.4.1", | ||
"typescript": "^5.6.3", | ||
"supabase": "^1.215.0" | ||
"typescript": "^5.6.3" | ||
}, | ||
"packageManager": "[email protected]+sha512.cce0f9de9c5a7c95bef944169cc5dfe8741abfb145078c0d508b868056848a87c81e626246cb60967cbd7fd29a6c062ef73ff840d96b3c86c40ac92cf4a813ee" | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
'use client'; | ||
|
||
import { useSession } from '@/context/SessionProvider'; | ||
import { HelpRequestAssignmentData, HelpRequestData } from '@/types/Requests'; | ||
import { helpRequestService } from '@/lib/service'; | ||
import { MouseEvent } from 'react'; | ||
import { Spinner } from '@/components/Spinner'; | ||
import Link from 'next/link'; | ||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; | ||
import { toast } from 'sonner'; | ||
|
||
type AsignarSolicitudButtonProps = { | ||
helpRequest: HelpRequestData; | ||
}; | ||
|
||
export default function AsignarSolicitudButton({ helpRequest }: AsignarSolicitudButtonProps) { | ||
const session = useSession(); | ||
|
||
const { | ||
data: assignments, | ||
isLoading, | ||
error, | ||
} = useQuery<HelpRequestAssignmentData[]>({ | ||
queryKey: ['help_request_assignments', { id: helpRequest.id }], | ||
queryFn: () => helpRequestService.getAssignments(helpRequest.id), | ||
}); | ||
|
||
const queryClient = useQueryClient(); | ||
|
||
const assignMutation = useMutation({ | ||
mutationFn: async () => { | ||
if (!session.user) return; | ||
await helpRequestService.assign({ | ||
help_request_id: helpRequest.id, | ||
user_id: session.user.id, | ||
phone_number: session.user.user_metadata.telefono!, | ||
}); | ||
}, | ||
onSuccess: () => { | ||
queryClient.invalidateQueries({ queryKey: ['help_request_assignments'] }); | ||
}, | ||
onError: (e) => { | ||
console.error('Error al asignarte a la petición de ayuda', e); | ||
toast.error('Error al asignarte :('); | ||
}, | ||
}); | ||
|
||
const unassignMutation = useMutation({ | ||
mutationFn: async () => { | ||
if (!session.user) return; | ||
if (!userAssignment) return; | ||
await helpRequestService.unassign(userAssignment.id); | ||
}, | ||
onSuccess: () => { | ||
queryClient.invalidateQueries({ queryKey: ['help_request_assignments'] }); | ||
}, | ||
onError: (e) => { | ||
console.error('Error al asignarte a la petición de ayuda', e); | ||
toast.error('Error al asignarte :('); | ||
}, | ||
}); | ||
|
||
async function handleSubmit(e: MouseEvent) { | ||
e.preventDefault(); | ||
assignMutation.mutate(); | ||
} | ||
async function handleCancel(e: MouseEvent) { | ||
e.preventDefault(); | ||
unassignMutation.mutate(); | ||
} | ||
|
||
if (isLoading) return <Spinner />; | ||
if (error || assignments === undefined) return <></>; | ||
|
||
const userAssignment = assignments.find((x) => x.user_id === session.user?.id); | ||
const userIsAssigned = !!userAssignment; | ||
|
||
if (!session || !session.user) | ||
return ( | ||
<Link href="/auth" className={`rounded-lg text-white py-2 px-4 w-full sm:w-auto text-center bg-green-500`}> | ||
Quiero ayudar | ||
</Link> | ||
); | ||
|
||
return ( | ||
<> | ||
{userIsAssigned ? ( | ||
<button | ||
onClick={handleCancel} | ||
className={`rounded-lg text-white py-2 px-4 w-full sm:w-auto text-center bg-red-500`} | ||
> | ||
Cancelar mi ayuda | ||
</button> | ||
) : ( | ||
<button | ||
onClick={handleSubmit} | ||
className={`rounded-lg text-white py-2 px-4 w-full sm:w-auto text-center bg-green-500`} | ||
> | ||
Quiero ayudar | ||
</button> | ||
)} | ||
</> | ||
); | ||
} |
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
Oops, something went wrong.