import { FormControl, InputLabel, Select, SelectChangeEvent, MenuItem, Box, Stack, Typography, TextField, Checkbox, FormControlLabel, FormGroup, Radio, RadioGroup, } from "@mui/material"; import { useEffect, useState } from "react"; import { RepetitionObject } from "../../features/Events/EventsTypes"; export default function RepeatEvent({ repetition, eventStart, setRepetition, isOwn = true, }: { repetition: RepetitionObject; eventStart: Date; setRepetition: Function; isOwn?: boolean; }) { const repetitionValues = ["day", "week", "month", "year"]; const days = ["MO", "TU", "WE", "TH", "FR", "SA", "SU"]; const day = new Date(eventStart); // derive endOption based on repetition const getEndOption = () => { if (repetition.occurrences && repetition.occurrences >= 0) return "after"; if (repetition.endDate) return "on"; return "never"; }; const [endOption, setEndOption] = useState(getEndOption()); // keep endOption in sync if repetition changes from parent useEffect(() => { if (!endOption) { setEndOption(getEndOption()); } }, [repetition.occurrences, repetition.endDate]); const handleDayChange = (day: string) => { const updatedDays = repetition.selectedDays?.includes(day) ? repetition.selectedDays.filter((d) => d !== day) : [...(repetition.selectedDays ?? []), day]; setRepetition({ ...repetition, selectedDays: updatedDays }); }; return ( Repetition {repetition.freq && ( {/* Interval */} Interval: setRepetition({ ...repetition, interval: Number(e.target.value), }) } size="small" sx={{ width: 80 }} /> { repetitionValues[ repetitionValues.findIndex((el) => el === repetition.freq) ] } {/* Weekly selection */} {repetition.freq === "weekly" && ( On days: {days.map((day) => ( handleDayChange(day)} /> } label={day} /> ))} )} {/* End options */} End: { const value = e.target.value; setEndOption(value); if (value === "never") { setRepetition({ ...repetition, occurrences: 0, endDate: "" }); } if (value === "after") { setRepetition({ ...repetition, occurrences: 0, endDate: "", }); } if (value === "on") { setRepetition({ ...repetition, occurrences: 0, endDate: new Date().toISOString().slice(0, 16), }); } }} > } label="Never" /> } label={ After setRepetition({ ...repetition, endDate: "", occurrences: Number(e.target.value), }) } sx={{ width: 100 }} inputProps={{ min: 1 }} disabled={endOption !== "after"} /> occurrences } /> } label={ On setRepetition({ ...repetition, occurrences: 0, endDate: e.target.value, }) } disabled={endOption !== "on"} /> } /> )} ); }