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

249 lines
8.0 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-10-22 21:15:05 +05:30
import { useGlobalInfoStore } from '../../context/globalInfo';
2024-10-23 00:13:29 +05:30
import { getSchedule, deleteSchedule } from '../../api/storage';
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-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-10-22 23:17:09 +05:30
const [schedule, setSchedule] = useState<any>(null);
2024-09-12 22:17:30 +05:30
const [settings, setSettings] = useState<ScheduleSettings>({
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-23 00:13:29 +05:30
const { recordingId } = useGlobalInfoStore();
console.log(`Recoridng ID Shculde: ${recordingId}`);
const deleteRobotSchedule = () => {
if (recordingId) {
deleteSchedule(recordingId);
setSchedule(null);
} else {
console.error('No recording id provided');
}
2024-10-22 20:39:53 +05:30
setSettings({
runEvery: 1,
runEveryUnit: 'HOURS',
startFrom: 'MONDAY',
atTimeStart: '00:00',
atTimeEnd: '01:00',
timezone: 'UTC'
});
2024-10-22 20:34:50 +05:30
};
2024-10-22 21:15:05 +05:30
const getRobotSchedule = async () => {
if (recordingId) {
const schedule = await getSchedule(recordingId);
2024-10-22 23:17:09 +05:30
console.log(`RObot found schedule: ${schedule}`)
2024-10-22 21:15:05 +05:30
if (schedule) {
2024-10-22 23:17:09 +05:30
setSchedule(schedule);
2024-10-22 21:15:05 +05:30
}
} else {
console.error('No recording id provided');
}
}
useEffect(() => {
if (isOpen) {
getRobotSchedule();
}
2024-10-22 23:17:09 +05:30
}, [isOpen, getRobotSchedule]);
2024-10-22 21:15:05 +05:30
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-10-22 20:35:10 +05:30
<>
2024-10-22 23:17:09 +05:30
{
(schedule != null) ? (
2024-10-23 00:15:07 +05:30
<>
<Typography>Robot is scheduled to run every {schedule.runEvery} {schedule.runEveryUnit} starting from {schedule.startFrom} at {schedule.atTimeStart} to {schedule.atTimeEnd} in {schedule.timezone} timezone.</Typography>
2024-10-22 23:17:09 +05:30
<Box mt={2} display="flex" justifyContent="space-between">
2024-10-23 00:13:40 +05:30
<Button
onClick={deleteRobotSchedule}
variant="outlined"
color="secondary"
>
Delete Schedule
</Button>
</Box>
2024-10-23 00:15:07 +05:30
</>
2024-10-22 23:17:09 +05:30
) : (
<>
2024-10-23 00:13:40 +05:30
<Box sx={{ display: 'flex', alignItems: 'center', width: '100%' }}>
<Typography sx={{ marginRight: '10px' }}>Run once every</Typography>
<TextField
type="number"
value={settings.runEvery}
onChange={(e) => handleChange('runEvery', parseInt(e.target.value))}
sx={textStyle}
inputProps={{ min: 1 }}
/>
<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>
</Box>
<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>
<Box mt={2} display="flex" justifyContent="flex-end">
<Button onClick={() => handleStart(settings)} variant="contained" color="primary">
Save Schedule
</Button>
<Button onClick={handleClose} color="primary" variant="outlined" style={{ marginLeft: '10px' }}>
Cancel
</Button>
</Box>
2024-10-22 23:17:09 +05:30
</>
)
}
</>
2024-09-12 22:27:32 +05:30
</Box>
2024-09-10 12:13:04 +05:30
</GenericModal>
);
2024-10-22 20:34:50 +05:30
};