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

198 lines
7.1 KiB
TypeScript
Raw Normal View History

2024-11-17 00:31:36 +05:30
import React, { useState, useEffect } from 'react';
2024-12-21 15:42:35 +05:30
import { useTranslation } from 'react-i18next';
2024-11-17 00:31:36 +05:30
import { GenericModal } from "../atoms/GenericModal";
import { TextField, Typography, Box, Button } from "@mui/material";
import { modalStyle } from "./AddWhereCondModal";
import { useGlobalInfoStore } from '../../context/globalInfo';
2024-11-17 02:27:11 +05:30
import { getStoredRecording, updateRecording } from '../../api/storage';
2024-11-17 00:31:36 +05:30
import { WhereWhatPair } from 'maxun-core';
interface RobotMeta {
name: string;
id: string;
createdAt: string;
pairs: number;
updatedAt: string;
params: any[];
}
interface RobotWorkflow {
workflow: WhereWhatPair[];
}
2024-11-17 02:27:11 +05:30
interface RobotEditOptions {
2024-11-18 06:12:35 +05:30
name: string;
limit?: number;
2024-11-17 02:27:11 +05:30
}
2024-11-17 00:31:36 +05:30
interface ScheduleConfig {
runEvery: number;
runEveryUnit: 'MINUTES' | 'HOURS' | 'DAYS' | 'WEEKS' | 'MONTHS';
startFrom: 'SUNDAY' | 'MONDAY' | 'TUESDAY' | 'WEDNESDAY' | 'THURSDAY' | 'FRIDAY' | 'SATURDAY';
atTimeStart?: string;
atTimeEnd?: string;
timezone: string;
lastRunAt?: Date;
nextRunAt?: Date;
cronExpression?: string;
}
export interface RobotSettings {
id: string;
userId?: number;
recording_meta: RobotMeta;
recording: RobotWorkflow;
google_sheet_email?: string | null;
google_sheet_name?: string | null;
google_sheet_id?: string | null;
google_access_token?: string | null;
google_refresh_token?: string | null;
schedule?: ScheduleConfig | null;
}
interface RobotSettingsProps {
isOpen: boolean;
handleStart: (settings: RobotSettings) => void;
handleClose: () => void;
initialSettings?: RobotSettings | null;
}
export const RobotEditModal = ({ isOpen, handleStart, handleClose, initialSettings }: RobotSettingsProps) => {
2024-12-21 15:42:35 +05:30
const { t } = useTranslation();
2024-11-17 00:31:36 +05:30
const [robot, setRobot] = useState<RobotSettings | null>(null);
const { recordingId, notify } = useGlobalInfoStore();
useEffect(() => {
if (isOpen) {
getRobot();
}
}, [isOpen]);
const getRobot = async () => {
if (recordingId) {
const robot = await getStoredRecording(recordingId);
setRobot(robot);
} else {
2024-12-21 15:42:35 +05:30
notify('error', t('robot_edit.notifications.update_failed'));
2024-11-17 00:31:36 +05:30
}
}
2024-11-17 02:27:11 +05:30
const handleRobotNameChange = (newName: string) => {
setRobot((prev) =>
prev ? { ...prev, recording_meta: { ...prev.recording_meta, name: newName } } : prev
);
};
const handleLimitChange = (newLimit: number) => {
setRobot((prev) => {
if (!prev) return prev;
2024-11-18 06:12:35 +05:30
2024-11-17 02:27:11 +05:30
const updatedWorkflow = [...prev.recording.workflow];
2024-11-18 06:12:35 +05:30
2024-11-17 02:27:11 +05:30
if (
updatedWorkflow.length > 0 &&
updatedWorkflow[0]?.what &&
updatedWorkflow[0].what.length > 0 &&
updatedWorkflow[0].what[0].args &&
updatedWorkflow[0].what[0].args.length > 0 &&
updatedWorkflow[0].what[0].args[0]
) {
updatedWorkflow[0].what[0].args[0].limit = newLimit;
}
2024-11-18 06:12:35 +05:30
2024-11-17 02:27:11 +05:30
return { ...prev, recording: { ...prev.recording, workflow: updatedWorkflow } };
});
};
2024-12-21 15:42:35 +05:30
2024-11-17 02:27:11 +05:30
const handleSave = async () => {
if (!robot) return;
2024-11-18 06:12:35 +05:30
2024-11-17 02:27:11 +05:30
try {
2024-11-18 06:12:35 +05:30
const payload = {
name: robot.recording_meta.name,
limit: robot.recording.workflow[0]?.what[0]?.args?.[0]?.limit,
};
const success = await updateRecording(robot.recording_meta.id, payload);
if (success) {
2024-12-21 15:42:35 +05:30
notify('success', t('robot_edit.notifications.update_success'));
2024-11-18 06:12:35 +05:30
handleStart(robot); // Inform parent about the updated robot
2025-01-09 19:17:48 +05:30
handleClose();
2024-11-18 06:12:35 +05:30
2024-11-22 23:40:05 +05:30
setTimeout(() => {
window.location.reload();
}, 1000);
2024-11-18 06:12:35 +05:30
} else {
2024-12-21 15:42:35 +05:30
notify('error', t('robot_edit.notifications.update_failed'));
2024-11-18 06:12:35 +05:30
}
2024-11-17 02:27:11 +05:30
} catch (error) {
2024-12-21 15:42:35 +05:30
notify('error', t('robot_edit.notifications.update_error'));
2024-11-18 06:12:35 +05:30
console.error('Error updating robot:', error);
2024-11-17 02:27:11 +05:30
}
};
2024-11-17 00:31:36 +05:30
return (
<GenericModal
isOpen={isOpen}
onClose={handleClose}
modalStyle={modalStyle}
>
<>
2024-12-21 15:42:35 +05:30
<Typography variant="h5" style={{ marginBottom: '20px' }}>
{t('robot_edit.title')}
</Typography>
2024-11-17 00:31:36 +05:30
<Box style={{ display: 'flex', flexDirection: 'column' }}>
{
robot && (
<>
<TextField
2024-12-21 15:42:35 +05:30
label={t('robot_edit.change_name')}
2024-12-24 02:49:17 +05:30
key="Robot Name"
2024-11-17 02:27:11 +05:30
type='text'
2024-11-17 00:31:36 +05:30
value={robot.recording_meta.name}
2024-11-17 02:27:11 +05:30
onChange={(e) => handleRobotNameChange(e.target.value)}
2024-11-17 00:31:36 +05:30
style={{ marginBottom: '20px' }}
/>
2024-11-17 02:27:11 +05:30
{robot.recording.workflow?.[0]?.what?.[0]?.args?.[0]?.limit !== undefined && (
2024-11-17 00:31:36 +05:30
<TextField
2024-12-21 15:42:35 +05:30
label={t('robot_edit.robot_limit')}
2024-11-17 02:27:11 +05:30
type="number"
value={robot.recording.workflow[0].what[0].args[0].limit || ''}
2025-01-09 19:17:48 +05:30
onChange={(e) => {
2024-11-23 14:58:45 +05:30
const value = parseInt(e.target.value, 10);
if (value >= 1) {
handleLimitChange(value);
2025-01-09 19:17:48 +05:30
}
2024-11-23 14:58:45 +05:30
}}
inputProps={{ min: 1 }}
2024-11-17 02:27:11 +05:30
style={{ marginBottom: '20px' }}
2024-11-17 00:31:36 +05:30
/>
)}
2024-11-18 06:12:35 +05:30
<Box mt={2} display="flex" justifyContent="flex-end">
<Button variant="contained" color="primary" onClick={handleSave}>
2024-12-21 15:42:35 +05:30
{t('robot_edit.save')}
2024-11-17 00:31:36 +05:30
</Button>
2025-01-09 19:17:48 +05:30
<Button
onClick={handleClose}
color="primary"
variant="outlined"
2024-12-21 15:42:35 +05:30
style={{ marginLeft: '10px' }}
2025-01-09 19:17:48 +05:30
sx={{
2025-01-08 18:11:43 +05:30
color: '#ff00c3 !important',
borderColor: '#ff00c3 !important',
backgroundColor: 'whitesmoke !important',
}}>
2024-12-21 15:42:35 +05:30
{t('robot_edit.cancel')}
2024-11-17 00:31:36 +05:30
</Button>
</Box>
</>
)
}
</Box>
</>
</GenericModal>
);
};