Skip to content

Commit 30fa1e2

Browse files
committed
feat: Added JSON Loader Popup
1 parent 3379da0 commit 30fa1e2

3 files changed

Lines changed: 86 additions & 5 deletions

File tree

‎src/app/page.tsx‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import { GenerateCodeDialog } from "@/components/form-builder/dialogs/generate-c
2626
import { MobileNotification } from "@/components/form-builder/ui/mobile-notification";
2727
import { useIsMobile } from "@/hooks/use-mobile";
2828
import SocialLinks from "@/components/form-builder/sidebar/socialLinks";
29+
import { OpenJsonDialog } from "@/components/form-builder/dialogs/open-json-dialog";
30+
2931
export default function FormBuilderPage() {
3032
const isMobile = useIsMobile();
3133
// Split the store selectors to only subscribe to what we need
@@ -86,7 +88,9 @@ export default function FormBuilderPage() {
8688
</h2>
8789
</div>
8890
<div className="p-2 flex-1 grid-cols-2 xl:grid-cols-3 hidden md:grid">
89-
<div className="hidden xl:block col-span-1"></div>
91+
<div className="hidden xl:block col-span-1">
92+
{process.env.NODE_ENV === "development" && <OpenJsonDialog />}
93+
</div>
9094
<div className="col-span-1 flex xl:justify-center">
9195
<div className=" text-center flex flex-row items-center justify-center gap-1 border rounded-md h-9 px-4">
9296
<div
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
"use client";
2+
3+
import { Button } from "@/components/ui/button";
4+
import {
5+
Dialog,
6+
DialogContent,
7+
DialogDescription,
8+
DialogFooter,
9+
DialogHeader,
10+
DialogTitle,
11+
DialogTrigger,
12+
} from "@/components/ui/dialog";
13+
import { Textarea } from "@/components/ui/textarea";
14+
import { useFormBuilderStore } from "@/stores/form-builder-store";
15+
import { useState } from "react";
16+
import { FormRow } from "@/types/form-builder.types";
17+
import { FormComponentModel } from "@/models/FormComponent";
18+
19+
export function OpenJsonDialog() {
20+
const [open, setOpen] = useState(false);
21+
const [jsonInput, setJsonInput] = useState("");
22+
const [error, setError] = useState<string | null>(null);
23+
const updateRows = useFormBuilderStore((state) => state.updateRows);
24+
25+
const handleSubmit = () => {
26+
try {
27+
const parsedJson = JSON.parse(jsonInput);
28+
29+
// Validate the structure
30+
if (!Array.isArray(parsedJson)) {
31+
throw new Error("JSON must be an array of rows");
32+
}
33+
34+
// Convert the parsed JSON into FormRow objects
35+
const rows: FormRow[] = parsedJson.map((row, index) => ({
36+
id: index + 1,
37+
components: row.components.map((comp: any) => new FormComponentModel(comp))
38+
}));
39+
40+
updateRows(rows);
41+
setOpen(false);
42+
setError(null);
43+
} catch (err) {
44+
setError(err instanceof Error ? err.message : "Invalid JSON format");
45+
}
46+
};
47+
48+
return (
49+
<Dialog open={open} onOpenChange={setOpen}>
50+
<DialogTrigger asChild>
51+
<Button variant="outline" size="sm">
52+
Load JSON
53+
</Button>
54+
</DialogTrigger>
55+
<DialogContent className="w-full lg:max-w-4xl">
56+
<DialogHeader>
57+
<DialogTitle>Open Form JSON</DialogTitle>
58+
<DialogDescription>
59+
Paste your form JSON code here. The JSON should be an array of rows with components.
60+
</DialogDescription>
61+
</DialogHeader>
62+
<div className="min-h-[500px] w-full overflow-y-auto flex flex-col gap-4">
63+
<Textarea
64+
value={jsonInput}
65+
onChange={(e) => setJsonInput(e.target.value)}
66+
placeholder="Paste your JSON here..."
67+
className="h-full font-mono text-sm bg-primary text-white"
68+
/>
69+
{error && (
70+
<div className="text-sm text-red-500">
71+
{error}
72+
</div>
73+
)}
74+
</div>
75+
<DialogFooter>
76+
<Button onClick={handleSubmit} variant="outline">Load JSON</Button>
77+
</DialogFooter>
78+
</DialogContent>
79+
</Dialog>
80+
);
81+
}

‎src/stores/form-builder-store.ts‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import { create } from 'zustand';
22
import { persist } from 'zustand/middleware';
33
import { FormBuilderStore, FormRow, Viewports } from '@/types/form-builder.types';
4-
import { AVAILABLE_COMPONENTS } from '@/config/available-components';
54
import { FormComponentModel } from '@/models/FormComponent';
65

76
const generateComponentId = (component: FormComponentModel, rows: FormRow[]): string => {
87
const existingComponents = rows.flatMap(row => row.components);
98
const existingTypes = existingComponents.filter(comp => comp.getField("type").startsWith(component.getField("type")));
109

11-
console.log(existingTypes);
12-
1310
let counter = existingTypes.length;
1411
let newId = `${component.getField("id")}-${counter}`;
1512

@@ -236,7 +233,6 @@ export const useFormBuilderStore = create<FormBuilderStore>()(
236233
state.rows = state.rows.map(row => ({
237234
...row,
238235
components: row.components.map(component => {
239-
const matchingComponent = AVAILABLE_COMPONENTS.find(c => c.type === component.type);
240236
return new FormComponentModel(component);
241237
})
242238
}));

0 commit comments

Comments
 (0)