import { FormControl, Select, SelectChangeEvent, MenuItem, Box, Stack, Typography, TextField, Checkbox, FormControlLabel, FormGroup, Radio, RadioGroup, } from "@linagora/twake-mui"; import { useEffect, useState } from "react"; import { RepetitionObject } from "../../features/Events/EventsTypes"; import { useI18n } from "twake-i18n"; export default function RepeatEvent({ repetition, eventStart, setRepetition, isOwn = true, }: { repetition: RepetitionObject; eventStart: Date; setRepetition: Function; isOwn?: boolean; }) { const { t } = useI18n(); 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(() => { const newEndOption = getEndOption(); if (endOption !== newEndOption) { setEndOption(newEndOption); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [repetition.occurrences, repetition.endDate]); const handleDayChange = (day: string) => { const currentDays = repetition.byday || []; const updatedDays = currentDays.includes(day) ? currentDays.filter((d) => d !== day) : [...currentDays, day]; // Only set byday if there are selected days, otherwise set to null setRepetition({ ...repetition, byday: updatedDays.length > 0 ? updatedDays : null, }); }; const getDayLabel = (day: string) => { const dayMap: { [key: string]: string } = { MO: t("event.repeat.days.monday"), TU: t("event.repeat.days.tuesday"), WE: t("event.repeat.days.wednesday"), TH: t("event.repeat.days.thursday"), FR: t("event.repeat.days.friday"), SA: t("event.repeat.days.saturday"), SU: t("event.repeat.days.sunday"), }; return dayMap[day] || day; }; return ( {/* Interval */} {t("event.repeat.repeatEvery")} setRepetition({ ...repetition, interval: Number(e.target.value), }) } size="small" style={{ width: 80 }} inputProps={{ min: 1 }} /> {/* Weekly selection */} {repetition.freq === "weekly" && ( {t("event.repeat.repeatOn")} {days.map((day) => ( handleDayChange(day)} /> } label={getDayLabel(day)} /> ))} )} {/* End options */} {t("event.repeat.end.label")} { 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={ {t("event.repeat.end.never")} } /> } label={ {t("event.repeat.end.after")} setRepetition({ ...repetition, endDate: "", occurrences: Number(e.target.value), }) } style={{ width: 100 }} inputProps={{ min: 1, "data-testid": "occurrences-input" }} disabled={!isOwn || endOption !== "after"} /> {t("event.repeat.end.occurrences")} } /> } label={ {t("event.repeat.end.on")} setRepetition({ ...repetition, occurrences: 0, endDate: e.target.value, }) } disabled={endOption !== "on"} /> } /> ); }