diff --git a/src/components/SchemaEditor/AddConditionalRuleButton.tsx b/src/components/SchemaEditor/AddConditionalRuleButton.tsx new file mode 100644 index 0000000..2565edf --- /dev/null +++ b/src/components/SchemaEditor/AddConditionalRuleButton.tsx @@ -0,0 +1,264 @@ +import { Badge } from "@/components/ui/badge"; +import { Button } from "@/components/ui/button"; +import { + Dialog, + DialogContent, + DialogDescription, + DialogFooter, + DialogHeader, + DialogTitle, +} from "@/components/ui/dialog"; +import { Textarea } from "@/components/ui/textarea"; // Import Textarea +import { + Tooltip, + TooltipContent, + TooltipProvider, + TooltipTrigger, +} from "@/components/ui/tooltip"; +import type { JSONSchema } from "@/types/jsonSchema"; // Assuming JSONSchema type is available +import { CirclePlus, Info } from "lucide-react"; +import type React from "react"; +import { useState } from "react"; + +// Define the structure for a conditional rule +export interface ConditionalRule { + if: JSONSchema; + then: JSONSchema; + else?: JSONSchema; +} + +interface AddConditionalRuleButtonProps { + onAddConditionalRule: (rule: ConditionalRule) => void; + variant?: "primary" | "secondary"; +} + +const AddConditionalRuleButton: React.FC = ({ + onAddConditionalRule, + variant = "primary", +}) => { + const [dialogOpen, setDialogOpen] = useState(false); + const [ifSchemaStr, setIfSchemaStr] = useState(""); + const [thenSchemaStr, setThenSchemaStr] = useState(""); + const [elseSchemaStr, setElseSchemaStr] = useState(""); + const [error, setError] = useState(null); + + const handleSubmit = (e: React.FormEvent) => { + e.preventDefault(); + setError(null); + + let ifSchema: JSONSchema; + let thenSchema: JSONSchema; + let elseSchema: JSONSchema | undefined; + + if (!ifSchemaStr.trim() || !thenSchemaStr.trim()) { + setError( + "The 'If' and 'Then' schema parts are required and cannot be empty.", + ); + return; + } + + try { + ifSchema = JSON.parse(ifSchemaStr); + } catch (err) { + setError( + "Invalid JSON in 'If' schema. Please provide a valid JSON object.", + ); + return; + } + + try { + thenSchema = JSON.parse(thenSchemaStr); + } catch (err) { + setError( + "Invalid JSON in 'Then' schema. Please provide a valid JSON object.", + ); + return; + } + + if (elseSchemaStr.trim()) { + try { + elseSchema = JSON.parse(elseSchemaStr); + } catch (err) { + setError( + "Invalid JSON in 'Else' schema. Please provide a valid JSON object or leave it empty.", + ); + return; + } + } + + onAddConditionalRule({ + if: ifSchema, + then: thenSchema, + ...(elseSchema && { else: elseSchema }), + }); + + // Reset state + setIfSchemaStr(""); + setThenSchemaStr(""); + setElseSchemaStr(""); + setDialogOpen(false); + }; + + return ( + <> + + + + + + + Add Conditional Rule (If/Then/Else) + + Schema Builder + + + + Define a conditional subschema. If the data validates against the + 'If' schema, then it must also validate against the 'Then' schema. + Optionally, if it does not validate against 'If', it must validate + against the 'Else' schema. + + + +
+
+
+ + + + + + + +

+ A schema that the data must validate against for the + 'Then' schema to apply. (Required) +
+ Example:{" "} + + {'{ "properties": { "country": { "const": "US" } } }'} + +

+
+
+
+
+