Skip to content

Commit d24e2ae

Browse files
ajustes de grupo e propostas
1 parent f254245 commit d24e2ae

File tree

5 files changed

+342
-96
lines changed

5 files changed

+342
-96
lines changed

codigo-fonte/cultivo/apps/web/src/routes/groups/index.tsx

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createFileRoute, Link, useNavigate } from "@tanstack/react-router";
2-
import { useEffect, useState } from "react";
2+
import { useState } from "react";
33
import { useQuery, useMutation } from "convex/react";
44
import { Button } from "@/components/ui/button";
55
import { 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>

codigo-fonte/cultivo/apps/web/src/routes/groups/join.tsx

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,28 @@ function JoinGroupPage() {
2424
const { groupId: groupIdParam } = Route.useSearch();
2525
const groupId = groupIdParam as Id<"groups">;
2626
const userId = getUserIdFromLocalStorage();
27-
27+
2828
const group = useQuery(api.group.getById, groupId ? { id: groupId } : "skip");
29+
const currentUser = useQuery(
30+
api.user.getById,
31+
userId ? { id: userId as Id<"users"> } : "skip"
32+
);
2933
const addParticipant = useMutation(api.group.addParticipant);
30-
34+
3135
const [isJoining, setIsJoining] = useState(false);
3236
const [error, setError] = useState<string | null>(null);
3337

3438
// Auto-join and redirect when group loads
3539
useEffect(() => {
36-
if (!group || !userId || isJoining) return;
40+
if (!group || !userId || !currentUser || isJoining) return;
41+
42+
// Verificar se o usuário é do tipo "Produtor Rural"
43+
if (currentUser.tipo_usuario !== "Produtor Rural") {
44+
setError(
45+
"Apenas usuários do tipo 'Produtor Rural' podem participar de grupos."
46+
);
47+
return;
48+
}
3749

3850
const isAlreadyMember = group.participants?.some(
3951
(p: any) => String(p) === String(userId)
@@ -62,14 +74,24 @@ function JoinGroupPage() {
6274
};
6375

6476
joinGroup();
65-
}, [group, userId, groupId, addParticipant, navigate, isJoining]);
77+
}, [
78+
group,
79+
userId,
80+
currentUser,
81+
groupId,
82+
addParticipant,
83+
navigate,
84+
isJoining,
85+
]);
6686

6787
// Loading state
6888
if (!groupId) {
6989
return (
7090
<div className="min-h-screen flex items-center justify-center bg-[#f8f3ed] p-4">
7191
<Card className="bg-white border-none shadow-lg p-8 max-w-md w-full text-center">
72-
<div className="text-red-600 text-lg font-semibold mb-2">Link Inválido</div>
92+
<div className="text-red-600 text-lg font-semibold mb-2">
93+
Link Inválido
94+
</div>
7395
<p className="text-[#7c6a5c] mb-4">
7496
O link de convite está incompleto ou inválido.
7597
</p>

0 commit comments

Comments
 (0)