feat: save robot edit changes
This commit is contained in:
@@ -3,7 +3,7 @@ import { GenericModal } from "../atoms/GenericModal";
|
|||||||
import { TextField, Typography, Box, Button } from "@mui/material";
|
import { TextField, Typography, Box, Button } from "@mui/material";
|
||||||
import { modalStyle } from "./AddWhereCondModal";
|
import { modalStyle } from "./AddWhereCondModal";
|
||||||
import { useGlobalInfoStore } from '../../context/globalInfo';
|
import { useGlobalInfoStore } from '../../context/globalInfo';
|
||||||
import { getStoredRecording } from '../../api/storage';
|
import { getStoredRecording, updateRecording } from '../../api/storage';
|
||||||
import { WhereWhatPair } from 'maxun-core';
|
import { WhereWhatPair } from 'maxun-core';
|
||||||
import { getUserById } from "../../api/auth";
|
import { getUserById } from "../../api/auth";
|
||||||
|
|
||||||
@@ -20,6 +20,11 @@ interface RobotWorkflow {
|
|||||||
workflow: WhereWhatPair[];
|
workflow: WhereWhatPair[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface RobotEditOptions {
|
||||||
|
name: string;
|
||||||
|
limit?: number;
|
||||||
|
}
|
||||||
|
|
||||||
interface ScheduleConfig {
|
interface ScheduleConfig {
|
||||||
runEvery: number;
|
runEvery: number;
|
||||||
runEveryUnit: 'MINUTES' | 'HOURS' | 'DAYS' | 'WEEKS' | 'MONTHS';
|
runEveryUnit: 'MINUTES' | 'HOURS' | 'DAYS' | 'WEEKS' | 'MONTHS';
|
||||||
@@ -55,9 +60,17 @@ interface RobotSettingsProps {
|
|||||||
|
|
||||||
export const RobotEditModal = ({ isOpen, handleStart, handleClose, initialSettings }: RobotSettingsProps) => {
|
export const RobotEditModal = ({ isOpen, handleStart, handleClose, initialSettings }: RobotSettingsProps) => {
|
||||||
const [robot, setRobot] = useState<RobotSettings | null>(null);
|
const [robot, setRobot] = useState<RobotSettings | null>(null);
|
||||||
const [userEmail, setUserEmail] = useState<string | null>(null);
|
// const [settings, setSettings] = useState<RobotEditOptions>({
|
||||||
|
// name: '',
|
||||||
|
// });
|
||||||
|
|
||||||
|
// const [userEmail, setUserEmail] = useState<string | null>(null);
|
||||||
const { recordingId, notify } = useGlobalInfoStore();
|
const { recordingId, notify } = useGlobalInfoStore();
|
||||||
|
|
||||||
|
// const handleChange = (field: keyof RobotEditOptions, value: string | number | boolean) => {
|
||||||
|
// setSettings(prev => ({ ...prev, [field]: value }));
|
||||||
|
// };
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isOpen) {
|
if (isOpen) {
|
||||||
getRobot();
|
getRobot();
|
||||||
@@ -73,22 +86,74 @@ export const RobotEditModal = ({ isOpen, handleStart, handleClose, initialSettin
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const lastPair = robot?.recording.workflow[robot?.recording.workflow.length - 1];
|
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;
|
||||||
|
|
||||||
|
const updatedWorkflow = [...prev.recording.workflow];
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
return { ...prev, recording: { ...prev.recording, workflow: updatedWorkflow } };
|
||||||
|
});
|
||||||
|
};
|
||||||
|
const handleSave = async () => {
|
||||||
|
if (!robot) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
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) {
|
||||||
|
notify('success', 'Robot updated successfully.');
|
||||||
|
handleStart(robot); // Inform parent about the updated robot
|
||||||
|
handleClose(); // Close the modal
|
||||||
|
|
||||||
|
window.location.reload();
|
||||||
|
} else {
|
||||||
|
notify('error', 'Failed to update the robot. Please try again.');
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
notify('error', 'An error occurred while updating the robot.');
|
||||||
|
console.error('Error updating robot:', error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// const lastPair = robot?.recording.workflow[robot?.recording.workflow.length - 1];
|
||||||
|
|
||||||
// Find the `goto` action in `what` and retrieve its arguments
|
// Find the `goto` action in `what` and retrieve its arguments
|
||||||
const targetUrl = lastPair?.what.find(action => action.action === "goto")?.args?.[0];
|
// const targetUrl = lastPair?.what.find(action => action.action === "goto")?.args?.[0];
|
||||||
|
|
||||||
useEffect(() => {
|
// useEffect(() => {
|
||||||
const fetchUserEmail = async () => {
|
// const fetchUserEmail = async () => {
|
||||||
if (robot && robot.userId) {
|
// if (robot && robot.userId) {
|
||||||
const userData = await getUserById(robot.userId.toString());
|
// const userData = await getUserById(robot.userId.toString());
|
||||||
if (userData && userData.user) {
|
// if (userData && userData.user) {
|
||||||
setUserEmail(userData.user.email);
|
// setUserEmail(userData.user.email);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
};
|
// };
|
||||||
fetchUserEmail();
|
// fetchUserEmail();
|
||||||
}, [robot?.userId]);
|
// }, [robot?.userId]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<GenericModal
|
<GenericModal
|
||||||
@@ -105,19 +170,24 @@ export const RobotEditModal = ({ isOpen, handleStart, handleClose, initialSettin
|
|||||||
<TextField
|
<TextField
|
||||||
label="Change Robot Name"
|
label="Change Robot Name"
|
||||||
key="Change Robot Name"
|
key="Change Robot Name"
|
||||||
|
type='text'
|
||||||
value={robot.recording_meta.name}
|
value={robot.recording_meta.name}
|
||||||
|
onChange={(e) => handleRobotNameChange(e.target.value)}
|
||||||
style={{ marginBottom: '20px' }}
|
style={{ marginBottom: '20px' }}
|
||||||
/>
|
/>
|
||||||
{robot.recording.workflow?.[0]?.what?.[0]?.args?.[0]?.limit && (
|
{robot.recording.workflow?.[0]?.what?.[0]?.args?.[0]?.limit !== undefined && (
|
||||||
<TextField
|
<TextField
|
||||||
label="Change Robot Limit"
|
label="Robot Limit"
|
||||||
key="Change Robot Limit"
|
type="number"
|
||||||
value={robot.recording.workflow[0].what[0].args[0].limit || ''}
|
value={robot.recording.workflow[0].what[0].args[0].limit || ''}
|
||||||
style={{ marginBottom: '20px' }}
|
onChange={(e) =>
|
||||||
|
handleLimitChange(parseInt(e.target.value, 10) || 0)
|
||||||
|
}
|
||||||
|
style={{ marginBottom: '20px' }}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
<Box mt={2} display="flex" justifyContent="flex-end">
|
<Box mt={2} display="flex" justifyContent="flex-end" onClick={handleSave}>
|
||||||
<Button variant="contained" color="primary">
|
<Button variant="contained" color="primary">
|
||||||
Save Changes
|
Save Changes
|
||||||
</Button>
|
</Button>
|
||||||
|
|||||||
Reference in New Issue
Block a user