import { Dropdown as MuiDropdown } from "../atoms/DropdownMui"; import { Button, MenuItem, Typography } from "@mui/material"; import React, { useRef } from "react"; import { GenericModal } from "../atoms/GenericModal"; import { WhereWhatPair } from "maxun-core"; import { SelectChangeEvent } from "@mui/material/Select/Select"; import { DisplayConditionSettings } from "./DisplayWhereConditionSettings"; import { useSocketStore } from "../../context/socket"; interface AddWhereCondModalProps { isOpen: boolean; onClose: () => void; pair: WhereWhatPair; index: number; } export const AddWhereCondModal = ({isOpen, onClose, pair, index}: AddWhereCondModalProps) => { const [whereProp, setWhereProp] = React.useState(''); const [additionalSettings, setAdditionalSettings] = React.useState(''); const [newValue, setNewValue] = React.useState(''); const [checked, setChecked] = React.useState(new Array(Object.keys(pair.where).length).fill(false)); const keyValueFormRef = useRef<{getObject: () => object}>(null); const {socket} = useSocketStore(); const handlePropSelect = (event: SelectChangeEvent) => { setWhereProp(event.target.value); switch (event.target.value) { case 'url': setNewValue(''); break; case 'selectors': setNewValue(['']); break; case 'default': return; } } const handleSubmit = () => { switch (whereProp) { case 'url': if (additionalSettings === 'string'){ pair.where.url = newValue; } else { pair.where.url = { $regex: newValue }; } break; case 'selectors': pair.where.selectors = newValue; break; case 'cookies': pair.where.cookies = keyValueFormRef.current?.getObject() as Record break; case 'before': pair.where.$before = newValue; break; case 'after': pair.where.$after = newValue; break; case 'boolean': const booleanArr = []; const deleteKeys: string[] = []; for (let i = 0; i < checked.length; i++) { if (checked[i]) { if (Object.keys(pair.where)[i]) { //@ts-ignore if (pair.where[Object.keys(pair.where)[i]]) { booleanArr.push({ //@ts-ignore [Object.keys(pair.where)[i]]: pair.where[Object.keys(pair.where)[i]]}); } deleteKeys.push(Object.keys(pair.where)[i]); } } } // @ts-ignore deleteKeys.forEach((key: string) => delete pair.where[key]); //@ts-ignore pair.where[`$${additionalSettings}`] = booleanArr; break; default: return; } onClose(); setWhereProp(''); setAdditionalSettings(''); setNewValue(''); socket?.emit('updatePair', {index: index-1, pair: pair}); } return ( { setWhereProp(''); setAdditionalSettings(''); setNewValue(''); onClose(); }} modalStyle={modalStyle}>
Add where condition:
url selectors cookies before after boolean logic
{whereProp ?
: null}
) } export const modalStyle = { top: '40%', left: '50%', transform: 'translate(-50%, -50%)', width: '30%', backgroundColor: 'background.paper', p: 4, height:'fit-content', display:'block', padding: '20px', };