import { Participant, Vote, VoteType } from "@prisma/client"; import clsx from "clsx"; import * as React from "react"; import CompactButton from "@/components/compact-button"; import Pencil from "@/components/icons/pencil-alt.svg"; import Trash from "@/components/icons/trash.svg"; import { usePoll } from "@/components/poll-context"; import { useSession } from "@/components/session"; import { ParticipantFormSubmitted } from "../types"; import { useDeleteParticipantModal } from "../use-delete-participant-modal"; import UserAvatar from "../user-avatar"; import VoteIcon from "../vote-icon"; import ControlledScrollArea from "./controlled-scroll-area"; import ParticipantRowForm from "./participant-row-form"; import { usePollContext } from "./poll-context"; export interface ParticipantRowProps { participant: Participant & { votes: Vote[] }; editMode?: boolean; onChangeEditMode?: (editMode: boolean) => void; onSubmit?: (data: ParticipantFormSubmitted) => Promise; } export const ParticipantRowView: React.VoidFunctionComponent<{ name: string; editable?: boolean; color?: string; votes: Array; onEdit?: () => void; onDelete?: () => void; columnWidth: number; sidebarWidth: number; isYou?: boolean; participantId: string; }> = ({ name, editable, votes, onEdit, onDelete, sidebarWidth, columnWidth, isYou, color, participantId, }) => { return (
{editable ? (
) : null}
{votes.map((vote, i) => { return (
); })}
); }; const ParticipantRow: React.VoidFunctionComponent = ({ participant, editMode, onSubmit, onChangeEditMode, }) => { const { columnWidth, sidebarWidth } = usePollContext(); const confirmDeleteParticipant = useDeleteParticipantModal(); const session = useSession(); const { poll, getVote, options } = usePoll(); const isYou = session.user && session.ownsObject(participant) ? true : false; const isUnclaimed = !participant.userId; const canEdit = !poll.closed && (poll.admin || isYou || isUnclaimed); if (editMode) { return ( { const type = getVote(participant.id, optionId); return type ? { optionId, type } : undefined; }), }} onSubmit={async ({ name, votes }) => { await onSubmit?.({ name, votes }); onChangeEditMode?.(false); }} onCancel={() => onChangeEditMode?.(false)} /> ); } return ( { return getVote(participant.id, optionId); })} participantId={participant.id} editable={canEdit} isYou={isYou} onEdit={() => { onChangeEditMode?.(true); }} onDelete={() => { confirmDeleteParticipant(participant.id); }} /> ); }; export default ParticipantRow;