diff --git a/src/components/ContextComponents/Building/Build/Build.tsx b/src/components/ContextComponents/Building/Build/Build.tsx index 10d8d8c..71c6af5 100644 --- a/src/components/ContextComponents/Building/Build/Build.tsx +++ b/src/components/ContextComponents/Building/Build/Build.tsx @@ -14,6 +14,7 @@ import { retrieveHeadersEndpoint, retrieveHeadersAndValidationByEndpoint, } from "@/(apicalls)/validations/validations"; +import PageCreate from "../../Commons/PageCreate"; const Build: React.FC = () => { const [renderTable, setRenderTable] = React.useState(false); @@ -47,8 +48,9 @@ const Build: React.FC = () => { isRender: setRenderTable, }, update: { - endpoint: "/building/build/create", - variableReact: setCreateValidation, + endpoint: "/building/build/update", + isRender: setRenderUpdate, + variableReact: setUpdateValidation, variableKey: "validation", component: renderUpdate ? ( { isFormEnabled={isFormEnabled} pageToSet={ { isFormEnabledFunction={setIsFormEnabled} /> ) : ( - + ), - isRender: setRenderCreate, }, create: { - endpoint: "/building/build/update/{build_uu_id}", - variableReact: setUpdateValidation, - variableKey: "headers", + endpoint: "/building/build/create", + isRender: setRenderCreate, + variableReact: setCreateValidation, + variableKey: "validation", component: renderCreate ? ( console.log("Create button clicked")} + /> + } isFormEnabled={isFormEnabled} formPageFunction={setFormPage} isFormEnabledFunction={setIsFormEnabled} /> ) : ( - + ), - isRender: setRenderUpdate, }, delete: { endpoint: "/building/build/delete", @@ -115,26 +122,23 @@ const Build: React.FC = () => { setTableHeaders(validator?.headers); } }) - .catch((error) => {}); + .catch(() => {}); } else if (endpointNeed.variableKey === "validation") { retrieveHeadersAndValidationByEndpoint({ endpoint: endpointNeed.endpoint, }) .then((validator) => { - console.log("validator", validator); if (JSON.stringify(validator?.validated) !== "{}") { endpointNeed.variableReact(validator); } }) - .catch((error) => {}); + .catch(() => {}); } endpointNeed.isRender(true); } } }) - .catch((error) => { - console.log("error", error); - }); + .catch(() => {}); }, []); return ( diff --git a/src/components/ContextComponents/Commons/ButtonCreate.tsx b/src/components/ContextComponents/Commons/ButtonCreate.tsx index 90b4657..4e5b98b 100644 --- a/src/components/ContextComponents/Commons/ButtonCreate.tsx +++ b/src/components/ContextComponents/Commons/ButtonCreate.tsx @@ -1,34 +1,24 @@ "use client"; import React from "react"; import EventButton from "@/components/ContextComponents/Commons/ButtonEvent"; -import PageCreate from "@/components/ContextComponents/Commons/PageCreate"; interface CreateButtonProps { - title: string; - validation: any; + buttonLabel: string; + pageToSet: any; isFormEnabled: boolean; formPageFunction: React.Dispatch>; isFormEnabledFunction: React.Dispatch>; } const CreateButton: React.FC = ({ - title, - validation, + + pageToSet, buttonLabel, isFormEnabled, formPageFunction, isFormEnabledFunction, }) => { - const pageCreate = ( - console.log("Create button clicked")} - /> - ); function openFormPage({ isFormEnabled, @@ -40,7 +30,7 @@ const CreateButton: React.FC = ({ setIsFormEnabled: React.Dispatch>; }) { if (!isFormEnabled) { - setFormPage(pageCreate); + setFormPage(pageToSet); setIsFormEnabled(true); } } diff --git a/src/components/ContextComponents/Commons/PageCreate.tsx b/src/components/ContextComponents/Commons/PageCreate.tsx index b916c8a..486efcf 100644 --- a/src/components/ContextComponents/Commons/PageCreate.tsx +++ b/src/components/ContextComponents/Commons/PageCreate.tsx @@ -1,9 +1,20 @@ import React from "react"; import IsNotAllowed from "./PageisNotAllowed"; import EventButton from "./ButtonEvent"; +import { + Form, + FormControl, + FormDescription, + FormField, + FormItem, + FormLabel, + FormMessage, +} from "@/components/ui/form"; +import { z } from "zod"; +import { zodResolver } from "@hookform/resolvers/zod"; +import { useForm } from "react-hook-form"; interface CreatePageButtonProps { - title: string; validation: any; formPageFunction: React.Dispatch>; isFormEnabledFunction: React.Dispatch>; @@ -11,20 +22,63 @@ interface CreatePageButtonProps { } const PageCreate: React.FC = ({ - title, validation, formPageFunction, isFormEnabledFunction, onClickAction, }) => { + const [zodValidation, setZodValidation] = React.useState(z.object({})); + const validationObj: any = validation?.validated; + const validationHeaders = validation?.headers; + + React.useEffect(() => { + console.log("validation", validation); + console.log("validationObj", validationObj); + console.log("validationHeaders", validationHeaders); + + if (Object.keys(validationObj || {}).length === 0) { + let zodValidationInternal: any = {}; + + Object.keys(validationObj).map((key: string) => { + zodValidationInternal[key] = null; + + const keyValidation = validationObj[key] || { + fieldType: { type: "string" }, + required: false, + }; + const fieldType: String = keyValidation.fieldType?.type || "string"; + const required = keyValidation.required || false; + if (fieldType === "string") { + zodValidationInternal[key] = required + ? z.string() + : z.string().optional(); + } else if (fieldType === "integer") { + zodValidationInternal[key] = required + ? z.number() + : z.number().optional(); + } + }); + setZodValidation(zodValidationInternal); + } + }, [validation]); + + const form = useForm>({ + resolver: zodResolver(zodValidation), + }); + function closeFormPage() { formPageFunction(); isFormEnabledFunction(false); } + function onSubmit() { + console.log("onSubmit"); + } return ( <> -

{title}

+

+ Bina Oluştur Sayfasına Hoş geldiniz +

closeFormPage()} @@ -73,6 +127,118 @@ const PageCreate: React.FC = ({ />
+ { +
+ + {Object.entries(validationObj).map( + ([key, value]: [string, any]) => { + console.log("key", key); + const keyValidation = validationObj[key] || { + fieldType: { type: "string" }, + required: false, + }; + const fieldType = keyValidation.fieldType?.type || "string"; + if (fieldType === "string" && validationHeaders[key]) { + return ( + <> +
+ +
+ ( + + + + + + + )} + /> + + + + + + +
+
+ + ); + } else if (fieldType === "integer" && validationHeaders[key]) { + return ( + <> +
+ +
+ ( + + + + + + + )} + /> + + + + + + +
+
+ + ); + } + } + )} +
+ + } ); }; diff --git a/src/components/ContextComponents/Commons/PageUpdate.tsx b/src/components/ContextComponents/Commons/PageUpdate.tsx index be22597..e720ebf 100644 --- a/src/components/ContextComponents/Commons/PageUpdate.tsx +++ b/src/components/ContextComponents/Commons/PageUpdate.tsx @@ -44,7 +44,7 @@ const PageUpdate: React.FC = ({ setValidatedData(tableSelectedRow); let zodValidationInternal: any = {}; - Object.entries(tableSelectedRow).map(([key, value]: [string, any]) => { + Object.keys(tableSelectedRow).map((key: string) => { zodValidationInternal[key] = null; const keyValidation = validationObj[key] || { @@ -54,7 +54,6 @@ const PageUpdate: React.FC = ({ const fieldType: String = keyValidation.fieldType?.type || "string"; const required = keyValidation.required || false; if (fieldType === "string") { - console.log("key", key, "value", value); zodValidationInternal[key] = required ? z.string() : z.string().optional(); @@ -147,7 +146,6 @@ const PageUpdate: React.FC = ({ required: false, }; const fieldType = keyValidation.fieldType?.type || "string"; - const required = keyValidation.required || false; if (fieldType === "string" && validationHeaders[key]) { return ( <>