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

309 lines
11 KiB
TypeScript
Raw Normal View History

2024-10-22 19:22:31 +05:30
import React, { useState, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
2025-01-09 19:49:20 +05:30
import { GenericModal } from "../ui/GenericModal";
2024-10-29 01:09:20 +05:30
import { MenuItem, TextField, Typography, Box } from "@mui/material";
import { Dropdown } from "../ui/DropdownMui";
2024-09-10 12:13:04 +05:30
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) => {
const { t } = useTranslation();
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'
];
const { recordingId, notify } = useGlobalInfoStore();
2024-10-23 00:13:29 +05:30
const deleteRobotSchedule = () => {
if (recordingId) {
deleteSchedule(recordingId);
setSchedule(null);
2025-01-01 22:57:23 +05:30
notify('success', t('Schedule deleted successfully'));
2024-10-23 00:13:29 +05:30
} 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
const getDayOrdinal = (day: string | undefined) => {
if (!day) return '';
const lastDigit = day.slice(-1);
const lastTwoDigits = day.slice(-2);
2025-01-09 19:18:41 +05:30
// Special cases for 11, 12, 13
if (['11', '12', '13'].includes(lastTwoDigits)) {
return t('schedule_settings.labels.on_day.th');
}
2025-01-09 19:18:41 +05:30
// Other cases
switch (lastDigit) {
case '1': return t('schedule_settings.labels.on_day.st');
case '2': return t('schedule_settings.labels.on_day.nd');
case '3': return t('schedule_settings.labels.on_day.rd');
default: return t('schedule_settings.labels.on_day.th');
}
};
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
}}>
<Typography variant="h6" sx={{ marginBottom: '20px' }}>{t('schedule_settings.title')}</Typography>
2024-10-22 20:35:10 +05:30
<>
2024-10-29 01:09:20 +05:30
{schedule !== null ? (
<>
<Typography>{t('schedule_settings.run_every')}: {schedule.runEvery} {schedule.runEveryUnit.toLowerCase()}</Typography>
<Typography>{['MONTHS', 'WEEKS'].includes(settings.runEveryUnit) ? t('schedule_settings.start_from') : t('schedule_settings.start_from')}: {schedule.startFrom.charAt(0).toUpperCase() + schedule.startFrom.slice(1).toLowerCase()}</Typography>
2024-10-29 01:30:47 +05:30
{schedule.runEveryUnit === 'MONTHS' && (
<Typography>{t('schedule_settings.on_day')}: {schedule.dayOfMonth}{getDayOrdinal(schedule.dayOfMonth)} of the month</Typography>
2024-10-29 01:30:47 +05:30
)}
<Typography>{t('schedule_settings.at_around')}: {schedule.atTimeStart}, {schedule.timezone} {t('schedule_settings.timezone')}</Typography>
2024-10-29 01:09:20 +05:30
<Box mt={2} display="flex" justifyContent="space-between">
<Button
onClick={deleteRobotSchedule}
variant="outlined"
color="error"
>
{t('schedule_settings.buttons.delete_schedule')}
2024-10-29 01:09:20 +05:30
</Button>
</Box>
</>
) : (
<>
<Box sx={{ display: 'flex', alignItems: 'center', width: '100%' }}>
<Typography sx={{ marginRight: '10px' }}>{t('schedule_settings.labels.run_once_every')}</Typography>
2024-10-29 01:09:20 +05:30
<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) => (
2025-01-20 23:55:46 +05:30
<MenuItem key={unit} value={unit}> {unit.charAt(0).toUpperCase() + unit.slice(1).toLowerCase()}</MenuItem>
2024-10-29 01:09:20 +05:30
))}
</Dropdown>
</Box>
<Box sx={{ display: 'flex', alignItems: 'center', width: '100%' }}>
<Typography sx={{ marginBottom: '5px', marginRight: '25px' }}>
{['MONTHS', 'WEEKS'].includes(settings.runEveryUnit) ? t('schedule_settings.labels.start_from_label') : t('schedule_settings.labels.start_from_label')}
</Typography>
<Dropdown
label=""
id="startFrom"
value={settings.startFrom}
handleSelect={(e) => handleChange('startFrom', e.target.value)}
sx={dropDownStyle}
>
{days.map((day) => (
2025-01-20 23:56:55 +05:30
<MenuItem key={day} value={day}>
{day.charAt(0).toUpperCase() + day.slice(1).toLowerCase()}
2025-01-20 23:56:38 +05:30
</MenuItem>
))}
</Dropdown>
</Box>
{settings.runEveryUnit === 'MONTHS' && (
2024-10-23 00:13:40 +05:30
<Box sx={{ display: 'flex', alignItems: 'center', width: '100%' }}>
<Typography sx={{ marginBottom: '5px', marginRight: '25px' }}>{t('schedule_settings.labels.on_day_of_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' }}>{t('schedule_settings.labels.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' }}>{t('schedule_settings.at_around')}</Typography>
2024-10-29 01:09:20 +05:30
<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' }}>{t('schedule_settings.timezone')}</Typography>
2024-10-29 01:09:20 +05:30
<Dropdown
label=""
id="timezone"
value={settings.timezone}
handleSelect={(e) => handleChange('timezone', e.target.value)}
sx={dropDownStyle}
>
{validMomentTimezones.map((tz) => (
2025-01-20 23:54:41 +05:30
<MenuItem key={tz} value={tz}>{tz.charAt(0).toUpperCase() + tz.slice(1).toLowerCase()}</MenuItem>
2024-10-29 01:09:20 +05:30
))}
</Dropdown>
</Box>
<Box mt={2} display="flex" justifyContent="flex-end">
<Button onClick={() => handleStart(settings)} variant="contained" color="primary">
{t('schedule_settings.buttons.save_schedule')}
2024-10-29 01:09:20 +05:30
</Button>
2025-01-09 19:18:41 +05:30
<Button
onClick={handleClose}
color="primary"
variant="outlined"
2025-01-08 18:11:23 +05:30
style={{ marginLeft: '10px' }}
2025-01-09 19:18:41 +05:30
sx={{
2025-01-08 18:11:23 +05:30
color: '#ff00c3 !important',
borderColor: '#ff00c3 !important',
backgroundColor: 'whitesmoke !important',
2025-01-08 18:11:23 +05:30
}}>
{t('schedule_settings.buttons.cancel')}
2024-10-29 01:09:20 +05:30
</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',
};