// Importing necessary dependencies and components import rules from "./rules"; import { useState, useEffect, useRef } from "@wordpress/element"; import { Col, Row, Input, Select, Tooltip, Modal, Button } from "antd"; import { QuestionCircleOutlined, SettingOutlined } from "@ant-design/icons"; import FloatInput from "../../Components/FloatInput"; import AjaxSelect from "./AjaxSelect"; import { __ } from "@wordpress/i18n"; // Defining the Props type for the Rule component // This includes the structure of the props passed to the component type Props = { groupIndex: number; // Index of the group this rule belongs to index: number; // Index of the rule within the group data: any; // Data associated with the rule onChange: (key: string | number, value: any) => void; // Callback for handling changes onRemove: (groupIndex: number, ruleIndex: number) => void; // Callback for removing the rule className?: string; // Optional class name for styling next: any; // Data for the next rule in the sequence }; const Rule = ({ groupIndex, index, data, onChange, onRemove, className, next, }: Props) => { // State to manage the visibility of the settings dialog const [showDialog, setShowDialog] = useState(false); // State to manage the confirmation for deletion const [confirmDelete, setConfirmDelete] = useState(false); // Reference to the error message input field const errorMessageInputRef = useRef(null); // Effect to reset the confirmDelete state when the dialog visibility changes useEffect(() => { setConfirmDelete(false); }, [showDialog]); // Function to handle closing the settings dialog const handleDialogClose = () => { setShowDialog(false); }; // Fetching the rule definition based on the data ID const rule = rules[data.id]; // If the rule is not found, return null to render nothing if (!rule) return null; // Extracting the inputs for the rule const ruleInputs = rule.inputs || []; return ( <> {/* Main container for the rule */}
{/* Header section displaying the rule title and description */}
{rule.title} {/* Tooltip for displaying the rule description */} {/* Icon to open the settings dialog */}
setShowDialog(true)} >
{/* Rendering inputs for the rule dynamically based on its configuration */} {ruleInputs.map((input, inputIndex) => { switch (input.type) { case "text": return ( onChange( input.name, e.target.value ) } /> ); case "number": return ( onChange( input.name, e.target.value ) } /> ); case "select": console.error("BALLLLO", input, data); if (input.search) { const arrayValues = input.options?.filter((o: any) => { return ( o.value === (data.data[input.name] ?? input.value) ); }) || []; const arrayValue = arrayValues.length > 0 ? arrayValues[0] : null; return ( onChange(input.name, value) } mode={ input.multiple ? input.tags ? "tags" : "multiple" : undefined } options={input.options || []} /> ); } case "ajax-select": return ( onChange(input.name, value) } mode={ input.multiple ? input.tags ? "tags" : "multiple" : undefined } /> ); } })}
{/* Modal for rule settings */} handleDialogClose()} onCancel={() => handleDialogClose()} footer={[ // Delete button with confirmation logic , // Cancel button with conditional behavior , // Save button to update settings , ]} afterOpenChange={(open) => { if (open) { errorMessageInputRef.current?.focus(); } }} > {/* Conditional rendering for error message input */}
{next && next.state === "OR" ? ( ) : ( )}
); }; export default Rule;