import { WhereWhatPair } from "maxun-core"; import { GenericModal } from "../atoms/GenericModal"; import { modalStyle } from "./AddWhereCondModal"; import { Button, MenuItem, TextField, Typography } from "@mui/material"; import React, { useRef } from "react"; import { Dropdown as MuiDropdown } from "../atoms/DropdownMui"; import { KeyValueForm } from "./KeyValueForm"; import { ClearButton } from "../ui/buttons/ClearButton"; import { useSocketStore } from "../../context/socket"; interface AddWhatCondModalProps { isOpen: boolean; onClose: () => void; pair: WhereWhatPair; index: number; } export const AddWhatCondModal = ({ isOpen, onClose, pair, index }: AddWhatCondModalProps) => { const [action, setAction] = React.useState(''); const [objectIndex, setObjectIndex] = React.useState(0); const [args, setArgs] = React.useState<({ type: string, value: (string | number | object | unknown) })[]>([]); const objectRefs = useRef<({ getObject: () => object } | unknown)[]>([]); const { socket } = useSocketStore(); const handleSubmit = () => { const argsArray: (string | number | object | unknown)[] = []; args.map((arg, index) => { switch (arg.type) { case 'string': case 'number': argsArray[index] = arg.value; break; case 'object': // @ts-ignore argsArray[index] = objectRefs.current[arg.value].getObject(); } }) setArgs([]); onClose(); pair.what.push({ // @ts-ignore action, args: argsArray, }) socket?.emit('updatePair', { index: index - 1, pair: pair }); } return ( { setArgs([]); onClose(); }} modalStyle={modalStyle}>
Add what condition:
Action: setAction(e.target.value)} value={action} label='action' />
Add new argument of type:
args: {args.map((arg, index) => { // @ts-ignore return (
{ args.splice(index, 1); setArgs([...args]); }} /> {index}: {arg.type === 'string' ? setArgs([ ...args.slice(0, index), { type: arg.type, value: e.target.value }, ...args.slice(index + 1) ])} value={args[index].value || ''} label="string" key={`arg-${arg.type}-${index}`} /> : arg.type === 'number' ? setArgs([ ...args.slice(0, index), { type: arg.type, value: Number(e.target.value) }, ...args.slice(index + 1) ])} value={args[index].value || ''} label="number" /> : //@ts-ignore objectRefs.current[arg.value] = el} key={`arg-${arg.type}-${index}`} /> }
) })}
) }