11import { createFileRoute , Link , useNavigate } from "@tanstack/react-router" ;
2- import { useEffect , useState } from "react" ;
2+ import { useState } from "react" ;
33import { useQuery , useMutation } from "convex/react" ;
44import { Button } from "@/components/ui/button" ;
55import { Card } from "@/components/ui/card" ;
@@ -36,7 +36,7 @@ function RouteComponent() {
3636 const removeParticipant = useMutation ( api . group . removeParticipant ) ;
3737
3838 const [ editing , setEditing ] = useState ( false ) ;
39- const [ editGroup , setEditGroup ] = useState < any | null > ( null ) ;
39+ const [ editGroup , setEditGroup ] = useState < any > ( null ) ;
4040
4141 // Check if user is already in a group
4242 const isInGroup = userGroup !== undefined && userGroup !== null ;
@@ -46,6 +46,58 @@ function RouteComponent() {
4646 await remove ( { id } ) ;
4747 }
4848
49+ async function handleRemoveParticipant ( groupId : any , participantId : any ) {
50+ try {
51+ console . log ( "=== Iniciando remoção de participante ===" ) ;
52+ console . log ( "Group ID:" , groupId ) ;
53+ console . log ( "Participant ID:" , participantId ) ;
54+ console . log ( "Estado atual editGroup:" , editGroup ) ;
55+
56+ await removeParticipant ( {
57+ groupId,
58+ userId : participantId ,
59+ } ) ;
60+
61+ console . log ( "✅ Participante removido no backend" ) ;
62+
63+ // Atualizar o estado local removendo o participante
64+ setEditGroup ( ( prev : any ) => {
65+ console . log ( "Estado anterior:" , prev ) ;
66+ if ( ! prev ) {
67+ console . log ( "⚠️ Estado anterior é null" ) ;
68+ return prev ;
69+ }
70+
71+ const newParticipants = prev . participants . filter (
72+ ( pid : any ) => String ( pid ) !== String ( participantId )
73+ ) ;
74+ const newParticipantsFull = prev . participantsFull . filter (
75+ ( participant : any ) =>
76+ String ( participant . _id ) !== String ( participantId )
77+ ) ;
78+
79+ console . log ( "Participantes antes:" , prev . participants . length ) ;
80+ console . log ( "Participantes depois:" , newParticipants . length ) ;
81+ console . log ( "ParticipantsFull antes:" , prev . participantsFull . length ) ;
82+ console . log ( "ParticipantsFull depois:" , newParticipantsFull . length ) ;
83+
84+ const newState = {
85+ ...prev ,
86+ participants : newParticipants ,
87+ participantsFull : newParticipantsFull ,
88+ } ;
89+
90+ console . log ( "Novo estado:" , newState ) ;
91+ return newState ;
92+ } ) ;
93+
94+ console . log ( "=== Remoção concluída ===" ) ;
95+ } catch ( error ) {
96+ console . error ( "❌ Erro ao remover participante:" , error ) ;
97+ alert ( `Erro ao remover participante: ${ error } ` ) ;
98+ }
99+ }
100+
49101 return (
50102 < main className = "min-h-screen bg-[#f8f3ed] py-8 px-4 pt-16" >
51103 < div className = "max-w-4xl mx-auto space-y-6" >
@@ -363,19 +415,12 @@ function RouteComponent() {
363415 </ span >
364416 ) : (
365417 < Button
418+ type = "button"
366419 size = "sm"
367420 variant = "destructive"
368- onClick = { async ( ) => {
369- await removeParticipant ( {
370- groupId : editGroup . _id ,
371- userId : p . _id ,
372- } ) ;
373- const refreshed =
374- groups ?. find (
375- ( gg : any ) => gg . _id === editGroup . _id
376- ) ?? null ;
377- setEditGroup ( refreshed ) ;
378- } }
421+ onClick = { ( ) =>
422+ handleRemoveParticipant ( editGroup . _id , p . _id )
423+ }
379424 >
380425 Remover
381426 </ Button >
0 commit comments