import React, { useState, useEffect } from 'react'; import { GenericModal } from "../atoms/GenericModal"; import { MenuItem, TextField, Typography, Box, Switch, FormControlLabel } from "@mui/material"; import { Dropdown } from "../atoms/DropdownMui"; import Button from "@mui/material/Button"; import { modalStyle } from "./AddWhereCondModal"; import { validMomentTimezones } from '../../constants/const'; import { useGlobalInfoStore } from '../../context/globalInfo'; import { getSchedule, deleteSchedule } from '../../api/storage'; interface ScheduleSettingsProps { isOpen: boolean; handleStart: (settings: ScheduleSettings) => void; handleClose: () => void; initialSettings?: ScheduleSettings | null; } export interface ScheduleSettings { runEvery: number; runEveryUnit: string; startFrom: string; atTimeStart?: string; atTimeEnd?: string; timezone: string; } export const ScheduleSettingsModal = ({ isOpen, handleStart, handleClose, initialSettings }: ScheduleSettingsProps) => { const [schedule, setSchedule] = useState(null); const [settings, setSettings] = useState({ runEvery: 1, runEveryUnit: 'HOURS', startFrom: 'MONDAY', atTimeStart: '00:00', atTimeEnd: '01:00', timezone: 'UTC' }); // Load initial settings if provided useEffect(() => { if (initialSettings) { setSettings(initialSettings); } }, [initialSettings]); const handleChange = (field: keyof ScheduleSettings, value: string | number | boolean) => { setSettings(prev => ({ ...prev, [field]: value })); }; const textStyle = { width: '150px', height: '52px', marginRight: '10px', }; const dropDownStyle = { marginTop: '2px', width: '150px', height: '59px', marginRight: '10px', }; const units = [ 'MINUTES', 'HOURS', 'DAYS', 'WEEKS', 'MONTHS' ]; const days = [ 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY' ]; const { recordingId } = useGlobalInfoStore(); console.log(`Recoridng ID Shculde: ${recordingId}`); const deleteRobotSchedule = () => { if (recordingId) { deleteSchedule(recordingId); setSchedule(null); } else { console.error('No recording id provided'); } setSettings({ runEvery: 1, runEveryUnit: 'HOURS', startFrom: 'MONDAY', atTimeStart: '00:00', atTimeEnd: '01:00', timezone: 'UTC' }); }; const getRobotSchedule = async () => { if (recordingId) { const scheduleData = await getSchedule(recordingId); console.log(`Robot found schedule: ${JSON.stringify(scheduleData, null, 2)}`); setSchedule(scheduleData); } else { console.error('No recording id provided'); } } useEffect(() => { if (isOpen) { const fetchSchedule = async () => { await getRobotSchedule(); }; fetchSchedule(); } }, [isOpen]); return ( *': { marginBottom: '20px' }, }}> Schedule Settings <> { (schedule !== null) ? ( <> Robot is scheduled to run every {schedule.runEvery} {schedule.runEveryUnit} starting from {schedule.startFrom} at {schedule.atTimeStart} to {schedule.atTimeEnd} in {schedule.timezone} timezone. ) : ( <> Run once every handleChange('runEvery', parseInt(e.target.value))} sx={textStyle} inputProps={{ min: 1 }} /> handleChange('runEveryUnit', e.target.value)} sx={dropDownStyle} > {units.map((unit) => ( {unit} ))} Start from / On handleChange('startFrom', e.target.value)} sx={dropDownStyle} > {days.map((day) => ( {day} ))} {['MINUTES', 'HOURS'].includes(settings.runEveryUnit) ? ( In Between handleChange('atTimeStart', e.target.value)} sx={textStyle} /> handleChange('atTimeEnd', e.target.value)} sx={textStyle} /> ) : ( At Around handleChange('atTimeStart', e.target.value)} sx={textStyle} /> )} Timezone handleChange('timezone', e.target.value)} sx={dropDownStyle} > {validMomentTimezones.map((tz) => ( {tz} ))} ) } ); };