From 558fe67fb87d3864448d83e7c5d35aa867501376 Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Mon, 24 Jun 2024 22:31:52 +0530 Subject: [PATCH] feat: what condition modal --- src/components/molecules/AddWhatCondModal.tsx | 133 ++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 src/components/molecules/AddWhatCondModal.tsx diff --git a/src/components/molecules/AddWhatCondModal.tsx b/src/components/molecules/AddWhatCondModal.tsx new file mode 100644 index 00000000..714c0fef --- /dev/null +++ b/src/components/molecules/AddWhatCondModal.tsx @@ -0,0 +1,133 @@ +import { WhereWhatPair } from "@wbr-project/wbr-interpret"; +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 "../atoms/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}`}/> + } +
+ )})} + +
+
+
+ ) +}