Files
parcer/src/components/molecules/ScheduleSettings.tsx

218 lines
6.6 KiB
TypeScript
Raw Normal View History

2024-10-22 19:22:31 +05:30
import React, { useState, useEffect } from 'react';
2024-09-10 12:13:04 +05:30
import { GenericModal } from "../atoms/GenericModal";
2024-10-22 19:22:31 +05:30
import { MenuItem, TextField, Typography, Box, Switch, FormControlLabel } from "@mui/material";
2024-09-10 12:13:04 +05:30
import { Dropdown } from "../atoms/DropdownMui";
import Button from "@mui/material/Button";
import { modalStyle } from "./AddWhereCondModal";
2024-09-13 15:57:48 +05:30
import { validMomentTimezones } from '../../constants/const';
2024-09-10 12:13:04 +05:30
interface ScheduleSettingsProps {
isOpen: boolean;
handleStart: (settings: ScheduleSettings) => void;
handleClose: () => void;
2024-10-22 19:22:31 +05:30
initialSettings?: ScheduleSettings | null;
2024-09-10 12:13:04 +05:30
}
export interface ScheduleSettings {
2024-10-22 19:22:31 +05:30
enabled: boolean;
2024-09-12 22:17:30 +05:30
runEvery: number;
runEveryUnit: string;
startFrom: string;
2024-10-22 19:22:31 +05:30
atTimeStart?: string;
atTimeEnd?: string;
2024-09-12 22:17:30 +05:30
timezone: string;
2024-09-10 12:13:04 +05:30
}
2024-10-22 19:22:31 +05:30
export const ScheduleSettingsModal = ({ isOpen, handleStart, handleClose, initialSettings }: ScheduleSettingsProps) => {
2024-09-12 22:17:30 +05:30
const [settings, setSettings] = useState<ScheduleSettings>({
2024-10-22 19:22:31 +05:30
enabled: true,
2024-09-12 22:17:30 +05:30
runEvery: 1,
2024-09-13 12:16:18 +05:30
runEveryUnit: 'HOURS',
startFrom: 'MONDAY',
2024-10-22 18:25:32 +05:30
atTimeStart: '00:00',
2024-10-22 19:22:31 +05:30
atTimeEnd: '01:00',
2024-09-12 22:17:30 +05:30
timezone: 'UTC'
2024-09-10 12:13:04 +05:30
});
2024-10-22 19:22:31 +05:30
// Load initial settings if provided
useEffect(() => {
if (initialSettings) {
setSettings(initialSettings);
}
}, [initialSettings]);
const handleChange = (field: keyof ScheduleSettings, value: string | number | boolean) => {
2024-09-12 22:17:30 +05:30
setSettings(prev => ({ ...prev, [field]: value }));
};
2024-09-12 23:02:49 +05:30
const textStyle = {
2024-09-12 23:01:19 +05:30
width: '150px',
2024-09-12 23:16:12 +05:30
height: '52px',
2024-09-12 23:01:19 +05:30
marginRight: '10px',
};
2024-09-12 23:02:49 +05:30
const dropDownStyle = {
2024-09-12 23:01:19 +05:30
marginTop: '2px',
width: '150px',
height: '59px',
marginRight: '10px',
};
2024-09-13 12:18:49 +05:30
const units = [
2024-10-22 18:25:32 +05:30
'MINUTES',
2024-09-13 12:18:49 +05:30
'HOURS',
'DAYS',
'WEEKS',
'MONTHS'
2024-10-22 18:25:32 +05:30
];
2024-09-13 12:18:49 +05:30
2024-09-13 12:23:18 +05:30
const days = [
'MONDAY',
'TUESDAY',
'WEDNESDAY',
'THURSDAY',
'FRIDAY',
'SATURDAY',
'SUNDAY'
2024-10-22 18:25:32 +05:30
];
2024-09-13 12:23:18 +05:30
2024-10-22 19:22:31 +05:30
const handleSubmit = () => {
// If scheduling is disabled, only send the enabled flag
if (!settings.enabled) {
handleStart({ enabled: false } as ScheduleSettings);
return;
}
handleStart(settings);
};
2024-09-10 12:13:04 +05:30
return (
<GenericModal
isOpen={isOpen}
onClose={handleClose}
modalStyle={modalStyle}
>
2024-09-12 22:27:32 +05:30
<Box sx={{
2024-09-10 12:13:04 +05:30
display: 'flex',
flexDirection: 'column',
alignItems: 'flex-start',
2024-09-12 22:27:32 +05:30
padding: '20px',
'& > *': { marginBottom: '20px' },
2024-09-10 12:13:04 +05:30
}}>
2024-09-12 23:27:26 +05:30
<Typography variant="h6" sx={{ marginBottom: '20px' }}>Schedule Settings</Typography>
2024-09-12 22:27:52 +05:30
2024-10-22 19:22:31 +05:30
<FormControlLabel
control={
<Switch
checked={settings.enabled}
onChange={(e) => handleChange('enabled', e.target.checked)}
color="primary"
/>
}
label="Enable Scheduling"
/>
{settings.enabled && (
<>
<Box sx={{ display: 'flex', alignItems: 'center', width: '100%' }}>
<Typography sx={{ marginRight: '10px' }}>Run once every</Typography>
2024-10-22 18:25:32 +05:30
<TextField
2024-10-22 19:22:31 +05:30
type="number"
value={settings.runEvery}
onChange={(e) => handleChange('runEvery', parseInt(e.target.value))}
2024-10-22 18:25:32 +05:30
sx={textStyle}
2024-10-22 19:22:31 +05:30
inputProps={{ min: 1 }}
2024-10-22 18:25:32 +05:30
/>
2024-10-22 19:22:31 +05:30
<Dropdown
label=""
id="runEveryUnit"
value={settings.runEveryUnit}
handleSelect={(e) => handleChange('runEveryUnit', e.target.value)}
sx={dropDownStyle}
>
{units.map((unit) => (
<MenuItem key={unit} value={unit}>{unit}</MenuItem>
))}
</Dropdown>
2024-10-22 18:25:32 +05:30
</Box>
2024-10-22 19:22:31 +05:30
<Box sx={{ display: 'flex', alignItems: 'center', width: '100%' }}>
<Typography sx={{ marginBottom: '5px', marginRight: '25px' }}>Start from / On</Typography>
<Dropdown
label=""
id="startFrom"
value={settings.startFrom}
handleSelect={(e) => handleChange('startFrom', e.target.value)}
sx={dropDownStyle}
>
{days.map((day) => (
<MenuItem key={day} value={day}>{day}</MenuItem>
))}
</Dropdown>
</Box>
{['MINUTES', 'HOURS'].includes(settings.runEveryUnit) ? (
<Box sx={{ display: 'flex', alignItems: 'center', width: '100%' }}>
<Box sx={{ marginRight: '20px' }}>
<Typography sx={{ marginBottom: '5px' }}>In Between</Typography>
<TextField
type="time"
value={settings.atTimeStart}
onChange={(e) => handleChange('atTimeStart', e.target.value)}
sx={textStyle}
/>
<TextField
type="time"
value={settings.atTimeEnd}
onChange={(e) => handleChange('atTimeEnd', e.target.value)}
sx={textStyle}
/>
</Box>
</Box>
) : (
<Box sx={{ display: 'flex', alignItems: 'center', width: '100%' }}>
<Typography sx={{ marginBottom: '5px', marginRight: '10px' }}>At Around</Typography>
<TextField
type="time"
value={settings.atTimeStart}
onChange={(e) => handleChange('atTimeStart', e.target.value)}
sx={textStyle}
/>
</Box>
)}
<Box sx={{ display: 'flex', alignItems: 'center', width: '100%' }}>
<Typography sx={{ marginRight: '10px' }}>Timezone</Typography>
<Dropdown
label=""
id="timezone"
value={settings.timezone}
handleSelect={(e) => handleChange('timezone', e.target.value)}
sx={dropDownStyle}
>
{validMomentTimezones.map((tz) => (
<MenuItem key={tz} value={tz}>{tz}</MenuItem>
))}
</Dropdown>
</Box>
</>
2024-10-22 18:25:32 +05:30
)}
2024-10-22 19:22:31 +05:30
{/* {settings.enabled && (
<Typography variant="body2" color="text.secondary" sx={{ mt: 2 }}>
{initialSettings?.nextRunAt && (
`Next scheduled run: ${new Date(initialSettings.nextRunAt).toLocaleString()}`
)}
</Typography>
)} */}
2024-09-12 22:17:30 +05:30
2024-09-12 23:01:19 +05:30
<Button
variant="contained"
2024-10-22 19:22:31 +05:30
onClick={handleSubmit}
color={settings.enabled ? 'primary' : 'secondary'}
2024-09-12 23:01:19 +05:30
>
2024-10-22 19:22:31 +05:30
{settings.enabled ? 'Schedule' : 'Disable Schedule'}
2024-09-12 23:01:19 +05:30
</Button>
2024-09-12 22:27:32 +05:30
</Box>
2024-09-10 12:13:04 +05:30
</GenericModal>
);
2024-10-22 19:22:31 +05:30
};