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

275 lines
9.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-29 01:09:20 +05:30
import { MenuItem, TextField, Typography, Box } from "@mui/material";
2024-09-10 12:13:04 +05:30
import { Dropdown } from "../atoms/DropdownMui";
import Button from "@mui/material/Button";
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-29 01:09:20 +05:30
dayOfMonth?: 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-23 03:52:31 +05:30
const [schedule, setSchedule] = useState<ScheduleSettings | null>(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',
dayOfMonth: '1',
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
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
const days = [
'MONDAY',
'TUESDAY',
'WEDNESDAY',
'THURSDAY',
'FRIDAY',
'SATURDAY',
'SUNDAY'
];
2024-10-23 00:13:29 +05:30
const { recordingId } = useGlobalInfoStore();
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',
dayOfMonth: '',
2024-10-22 20:39:53 +05:30
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) {
2024-10-23 03:52:31 +05:30
const scheduleData = await getSchedule(recordingId);
setSchedule(scheduleData);
2024-10-22 21:15:05 +05:30
} else {
console.error('No recording id provided');
}
}
useEffect(() => {
if (isOpen) {
2024-10-23 03:52:31 +05:30
const fetchSchedule = async () => {
await getRobotSchedule();
};
fetchSchedule();
2024-10-22 21:15:05 +05:30
}
2024-10-23 03:52:31 +05:30
}, [isOpen]);
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-29 01:09:20 +05:30
{schedule !== null ? (
<>
<Typography>Run every: {schedule.runEvery} {schedule.runEveryUnit.toLowerCase()}</Typography>
<Typography>{['MONTHS', 'WEEKS'].includes(settings.runEveryUnit) ? "Start From" : "On"} {schedule.startFrom.charAt(0).toUpperCase() + schedule.startFrom.slice(1).toLowerCase()}</Typography>
2024-10-29 01:30:47 +05:30
{schedule.runEveryUnit === 'MONTHS' && (
<Typography>On day: {schedule.dayOfMonth}{['1', '21', '31'].includes(schedule.dayOfMonth || '') ? 'st' : ['2', '22'].includes(schedule.dayOfMonth || '') ? 'nd' : ['3', '23'].includes(schedule.dayOfMonth || '') ? 'rd' : 'th'} of the month</Typography>
)}
2024-10-29 01:09:20 +05:30
<Typography>At around: {schedule.atTimeStart}, {schedule.timezone} Timezone</Typography>
<Box mt={2} display="flex" justifyContent="space-between">
<Button
onClick={deleteRobotSchedule}
variant="outlined"
color="error"
>
Delete Schedule
</Button>
</Box>
</>
) : (
<>
<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' }}>{['MONTHS', 'WEEKS'].includes(settings.runEveryUnit) ? "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>
{settings.runEveryUnit === 'MONTHS' && (
2024-10-23 00:13:40 +05:30
<Box sx={{ display: 'flex', alignItems: 'center', width: '100%' }}>
2024-10-29 01:09:20 +05:30
<Typography sx={{ marginBottom: '5px', marginRight: '25px' }}>On Day of the Month</Typography>
2024-10-23 00:13:40 +05:30
<TextField
type="number"
value={settings.dayOfMonth}
onChange={(e) => handleChange('dayOfMonth', e.target.value)}
sx={textStyle}
inputProps={{ min: 1, max: 31 }}
2024-10-23 00:13:40 +05:30
/>
</Box>
)}
2024-10-23 00:13:40 +05:30
2024-10-29 01:09:20 +05:30
{['MINUTES', 'HOURS'].includes(settings.runEveryUnit) ? (
2024-10-23 00:13:40 +05:30
<Box sx={{ display: 'flex', alignItems: 'center', width: '100%' }}>
2024-10-29 01:09:20 +05:30
<Box sx={{ marginRight: '20px' }}>
<Typography sx={{ marginBottom: '5px' }}>In Between</Typography>
2024-10-23 00:13:40 +05:30
<TextField
type="time"
value={settings.atTimeStart}
onChange={(e) => handleChange('atTimeStart', e.target.value)}
sx={textStyle}
/>
2024-10-29 01:09:20 +05:30
<TextField
type="time"
value={settings.atTimeEnd}
onChange={(e) => handleChange('atTimeEnd', e.target.value)}
sx={textStyle}
/>
2024-10-23 00:13:40 +05:30
</Box>
</Box>
2024-10-29 01:09:20 +05:30
) : (
<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}
/>
2024-10-23 00:13:40 +05:30
</Box>
2024-10-29 01:09:20 +05:30
)}
<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
};
2024-10-25 01:41:14 +05:30
const modalStyle = {
top: '50%',
left: '50%',
transform: 'translate(-50%, -50%)',
width: '40%',
backgroundColor: 'background.paper',
p: 4,
height: 'fit-content',
display: 'block',
padding: '20px',
};