diff --git a/src/app/[language]/projetos/[id]/page-content.tsx b/src/app/[language]/projetos/[id]/page-content.tsx index d47a99e..49522dc 100644 --- a/src/app/[language]/projetos/[id]/page-content.tsx +++ b/src/app/[language]/projetos/[id]/page-content.tsx @@ -1,42 +1,113 @@ "use client"; -import { useEffect, useState } from "react"; +import { useState } from "react"; import { useParams } from "next/navigation"; import { useQuery, useQueryClient } from "@tanstack/react-query"; -import { ArrowLeft, Lightbulb, Users } from "lucide-react"; +import { ArrowLeft, Lightbulb, Plus, Users, X } from "lucide-react"; import { useDeleteProjectInterestService, + useDeleteProjectParticipantService, + useDeleteProjectRoleService, useGetMyProjectInterestService, useGetProjectIdeaService, - useGetProjectInterestsService, + useGetProjectParticipantsService, + useGetProjectRolesService, + usePostProjectRoleService, usePutProjectInterestService, } from "@/services/api/services/projects"; import HTTP_CODES_ENUM from "@/services/api/types/http-codes"; -import { - ProjectIdea, - ProjectIdeaStatus, - ProjectRole, -} from "@/services/api/types/project"; +import { ProjectIdea, ProjectParticipant } from "@/services/api/types/project"; +import { RoleEnum } from "@/services/api/types/role"; import { Button } from "@/components/ui/button"; +import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; import Link from "@/components/link"; import { EmptyState } from "@/components/ui/empty-state"; import useAuth from "@/services/auth/use-auth"; import { useSnackbar } from "@/hooks/use-snackbar"; import { getApiError } from "@/lib/utils"; -import { PROJECT_ROLE_LABELS } from "@/lib/project-labels"; -import { FIELD_CLASS, RoleChip, UserChip } from "../project-components"; +import { projectUserName } from "@/lib/project-labels"; +import { FIELD_CLASS, UserChip } from "../project-components"; + +const MAX_STACK = 4; + +function participantLabel(participant: ProjectParticipant): string { + return participant.user?.username + ? `@${participant.user.username}` + : projectUserName(participant.user); +} + +/** Fotos sobrepostas, no estilo do Instagram; passando do limite vira "+N". */ +function AvatarStack({ participants }: { participants: ProjectParticipant[] }) { + const visible = participants.slice(0, MAX_STACK); + const extra = participants.length - visible.length; -function InterestPanel({ idea }: { idea: ProjectIdea }) { + return ( +
+ {visible.map((participant) => ( + + {participant.user?.photo?.path && ( + + )} + + {projectUserName(participant.user).charAt(0).toUpperCase()} + + + ))} + {extra > 0 && ( + + +{extra} + + )} +
+ ); +} + +function TeamCard({ idea }: { idea: ProjectIdea }) { const { user, isLoaded } = useAuth(); const { enqueueSnackbar } = useSnackbar(); const queryClient = useQueryClient(); + const fetchRoles = useGetProjectRolesService(); + const fetchParticipants = useGetProjectParticipantsService(); const getMyInterest = useGetMyProjectInterestService(); const putInterest = usePutProjectInterestService(); const deleteInterest = useDeleteProjectInterestService(); + const postRole = usePostProjectRoleService(); + const deleteRole = useDeleteProjectRoleService(); + const deleteParticipant = useDeleteProjectParticipantService(); - const [role, setRole] = useState(ProjectRole.FRONTEND); - const [message, setMessage] = useState(""); + const [newRole, setNewRole] = useState(""); const [processing, setProcessing] = useState(false); + const [confirmRoleId, setConfirmRoleId] = useState(null); + + const userId = user ? Number(user.id) : null; + const roleId = Number(user?.role?.id); + const canManage = + userId !== null && + (userId === idea.authorId || + roleId === RoleEnum.ADMIN || + roleId === RoleEnum.MODERATOR); + + const { data: roles = [], isLoading: loadingRoles } = useQuery({ + queryKey: ["project-idea-roles", idea.id], + queryFn: async () => { + const { status, data } = await fetchRoles({ ideaId: idea.id }); + return status === HTTP_CODES_ENUM.OK ? data : []; + }, + enabled: !!user, + }); + + const { data: participants = [] } = useQuery({ + queryKey: ["project-idea-participants", idea.id], + queryFn: async () => { + const { status, data } = await fetchParticipants({ ideaId: idea.id }); + return status === HTTP_CODES_ENUM.OK ? data : []; + }, + enabled: !!user, + }); const { data: mine } = useQuery({ queryKey: ["my-project-interest", idea.id], @@ -47,178 +118,242 @@ function InterestPanel({ idea }: { idea: ProjectIdea }) { enabled: !!user, }); - useEffect(() => { - if (mine?.interested && mine.role) { - setRole(mine.role); - setMessage(mine.message ?? ""); - } - }, [mine]); - const refresh = () => { + queryClient.invalidateQueries({ + queryKey: ["project-idea-roles", idea.id], + }); + queryClient.invalidateQueries({ + queryKey: ["project-idea-participants", idea.id], + }); queryClient.invalidateQueries({ queryKey: ["my-project-interest", idea.id], }); queryClient.invalidateQueries({ queryKey: ["project-idea", idea.id] }); }; - const handleSave = async () => { + const run = async ( + action: () => Promise<{ status: number; data?: unknown }>, + okStatuses: number[], + errorMessage: string, + successMessage?: string + ) => { setProcessing(true); try { - const { status, data } = await putInterest({ - ideaId: idea.id, - role, - message: message.trim() || undefined, - }); - if (status === HTTP_CODES_ENUM.OK || status === HTTP_CODES_ENUM.CREATED) { - enqueueSnackbar( - mine?.interested ? "Interesse atualizado." : "Interesse registrado!", - { variant: "success" } - ); + const { status, data } = await action(); + if (okStatuses.includes(status)) { + if (successMessage) { + enqueueSnackbar(successMessage, { variant: "success" }); + } refresh(); - } else { - enqueueSnackbar(getApiError(data, "Erro ao registrar interesse."), { - variant: "error", - }); + return true; } + enqueueSnackbar(getApiError(data, errorMessage), { variant: "error" }); + return false; } finally { setProcessing(false); } }; - const handleRemove = async () => { - setProcessing(true); - try { - const { status } = await deleteInterest({ ideaId: idea.id }); - if (status === HTTP_CODES_ENUM.NO_CONTENT) { - enqueueSnackbar("Interesse removido.", { variant: "success" }); - setMessage(""); - refresh(); - } else { - enqueueSnackbar("Erro ao remover interesse.", { variant: "error" }); - } - } finally { - setProcessing(false); + const handleToggle = (roleName: string, joined: boolean) => + joined + ? run( + () => deleteInterest({ ideaId: idea.id, role: roleName }), + [HTTP_CODES_ENUM.NO_CONTENT], + "Erro ao sair do cargo.", + "Você saiu do cargo." + ) + : run( + () => putInterest({ ideaId: idea.id, role: roleName }), + [HTTP_CODES_ENUM.OK, HTTP_CODES_ENUM.CREATED], + "Erro ao entrar no cargo.", + "Você entrou no time!" + ); + + const handleAddRole = async () => { + const name = newRole.trim(); + if (name.length < 2) return; + const ok = await run( + () => postRole({ ideaId: idea.id, name }), + [HTTP_CODES_ENUM.OK, HTTP_CODES_ENUM.CREATED], + "Erro ao adicionar cargo.", + "Cargo adicionado." + ); + if (ok) setNewRole(""); + }; + + const handleRemoveRole = (role: { id: string; name: string }) => { + if (confirmRoleId !== role.id) { + setConfirmRoleId(role.id); + return; } + setConfirmRoleId(null); + run( + () => deleteRole({ ideaId: idea.id, roleId: role.id }), + [HTTP_CODES_ENUM.NO_CONTENT], + "Erro ao remover cargo.", + "Cargo removido." + ); }; + const handleRemoveParticipant = (participant: ProjectParticipant) => + run( + () => + deleteParticipant({ + ideaId: idea.id, + userId: participant.userId, + role: participant.role, + }), + [HTTP_CODES_ENUM.NO_CONTENT], + "Erro ao remover participante." + ); + if (!isLoaded) return null; if (!user) { return ( -
- - Entre na sua conta - {" "} - para dizer em qual cargo gostaria de participar. -
+
+

+ + Time do projeto +

+

+ + Entre na sua conta + {" "} + para ver quem está no time e participar de um cargo. +

+
); } - if (Number(user.id) === idea.authorId) return null; + const joinedRoles = new Set(mine?.roles ?? []); return ( -
+
-

- {mine?.interested - ? "Você tem interesse neste projeto" - : "Quero participar"} +

+ + Time do projeto

- O autor vê a lista de interessados e escolhe quem entra no time. + Escolha um ou mais cargos para participar. Quem propôs o projeto + também pode atuar nele.

-
- - -
- -
- -