diff --git a/src/components/molecules/ScheduleSettings.tsx b/src/components/molecules/ScheduleSettings.tsx index 04c93f82..d8bc8bb9 100644 --- a/src/components/molecules/ScheduleSettings.tsx +++ b/src/components/molecules/ScheduleSettings.tsx @@ -1,6 +1,6 @@ -import React, { useState } from 'react'; +import React, { useState, useEffect } from 'react'; import { GenericModal } from "../atoms/GenericModal"; -import { MenuItem, TextField, Typography, Box } from "@mui/material"; +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"; @@ -10,28 +10,38 @@ interface ScheduleSettingsProps { isOpen: boolean; handleStart: (settings: ScheduleSettings) => void; handleClose: () => void; + initialSettings?: ScheduleSettings | null; } export interface ScheduleSettings { + enabled: boolean; runEvery: number; runEveryUnit: string; startFrom: string; - atTimeStart?: string; // Start time for "In Between" - atTimeEnd?: string; // End time for "In Between" + atTimeStart?: string; + atTimeEnd?: string; timezone: string; } -export const ScheduleSettingsModal = ({ isOpen, handleStart, handleClose }: ScheduleSettingsProps) => { +export const ScheduleSettingsModal = ({ isOpen, handleStart, handleClose, initialSettings }: ScheduleSettingsProps) => { const [settings, setSettings] = useState({ + enabled: true, runEvery: 1, runEveryUnit: 'HOURS', startFrom: 'MONDAY', atTimeStart: '00:00', - atTimeEnd: '01:00', // Default end time + atTimeEnd: '01:00', timezone: 'UTC' }); - const handleChange = (field: keyof ScheduleSettings, value: string | number) => { + // 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 })); }; @@ -66,6 +76,15 @@ export const ScheduleSettingsModal = ({ isOpen, handleStart, handleClose }: Sche 'SUNDAY' ]; + const handleSubmit = () => { + // If scheduling is disabled, only send the enabled flag + if (!settings.enabled) { + handleStart({ enabled: false } as ScheduleSettings); + return; + } + handleStart(settings); + }; + return ( Schedule Settings - - 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} - ))} - - - - {/* Conditional rendering based on the selected runEveryUnit */} - {['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} + handleChange('enabled', e.target.checked)} + color="primary" /> - + } + label="Enable Scheduling" + /> + + {settings.enabled && ( + <> + + 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} + ))} + + + )} - - Timezone - handleChange('timezone', e.target.value)} - sx={dropDownStyle} - > - {validMomentTimezones.map((tz) => ( - {tz} - ))} - - + {/* {settings.enabled && ( + + {initialSettings?.nextRunAt && ( + `Next scheduled run: ${new Date(initialSettings.nextRunAt).toLocaleString()}` + )} + + )} */} ); -}; +}; \ No newline at end of file