import * as React from "react"; import { Controller, useFormContext } from "react-hook-form"; import { ParsedDateTimeOpton } from "utils/date-time-utils"; import { usePoll } from "@/components/poll-context"; import { ParticipantForm } from "../types"; import DateOption from "./date-option"; import TimeSlotOption from "./time-slot-option"; export interface PollOptions { options: ParsedDateTimeOpton[]; editable?: boolean; selectedParticipantId?: string; } const PollOptions: React.VoidFunctionComponent = ({ options, editable, selectedParticipantId, }) => { const { control } = useFormContext(); const { getParticipantsWhoVotedForOption, getVote, getParticipantById } = usePoll(); const selectedParticipant = selectedParticipantId ? getParticipantById(selectedParticipantId) : undefined; return (
{options.map((option) => { const participants = getParticipantsWhoVotedForOption(option.optionId); return ( { const vote = editable ? field.value.includes(option.optionId) ? "yes" : "no" : selectedParticipant ? getVote(selectedParticipant.id, option.optionId) : undefined; const handleChange = (newVote: "yes" | "no") => { if (!editable) { return; } if (newVote === "no") { field.onChange( field.value.filter( (optionId) => optionId !== option.optionId, ), ); } else { field.onChange([...field.value, option.optionId]); } }; switch (option.type) { case "timeSlot": return ( ); case "date": return ( ); } }} /> ); })}
); }; export default PollOptions;