diff --git a/src/(apicalls)/validations/validationProcesser.ts b/src/(apicalls)/validations/validationProcesser.ts index 3a87f30..be38f9e 100644 --- a/src/(apicalls)/validations/validationProcesser.ts +++ b/src/(apicalls)/validations/validationProcesser.ts @@ -36,41 +36,28 @@ class HeadersAndValidations { parseProcesser() { Object.entries(this.properties).map(([key, value]) => { const multipleTypes: Object[] = value?.anyOf || []; - const isRequired: boolean = Object.keys(value).includes("anyOf"); + let isRequired: boolean = true; + try { + isRequired = Object.keys(multipleTypes).includes("anyOf"); + } catch (error) {} + if (!isRequired) { multipleTypes.map((row: any) => { if (row.type !== "null") { this.validated[key] = { - required: false, + required: isRequired, fieldType: row.type, }; } }); } else { this.validated[key] = { - required: true, + required: isRequired, fieldType: value, }; } }); } - - parseType({ type }: any) { - switch (type) { - case "string": - return string; - case "number": - return number; - case "boolean": - return boolean; - case "array": - return array; - case "object": - return object; - default: - return string; - } - } } export { HeadersAndValidations }; diff --git a/src/components/ContextComponents/Building/Build/Build.tsx b/src/components/ContextComponents/Building/Build/Build.tsx index c7ecc09..b606a74 100644 --- a/src/components/ContextComponents/Building/Build/Build.tsx +++ b/src/components/ContextComponents/Building/Build/Build.tsx @@ -29,6 +29,7 @@ const Build: React.FC = () => { updateEndpoint="/building/build/update" tableFunction={retrieveBuildList} UpdatePage={PageUpdate} + saveFunction={createBuild} setFormPage={setFormPage} returnToPage={() => setFormPage(null)} updatePageInfo={{ @@ -53,6 +54,7 @@ const Build: React.FC = () => { }} endpoint="/building/build/create" returnToPage={() => setFormPage(null)} + saveFunction={createBuild} /> ) } diff --git a/src/components/ContextComponents/Commons/FormPage.tsx b/src/components/ContextComponents/Commons/FormPage.tsx new file mode 100644 index 0000000..4166ac5 --- /dev/null +++ b/src/components/ContextComponents/Commons/FormPage.tsx @@ -0,0 +1,187 @@ +"use client"; +import React from "react"; +import { + Form, + FormControl, + FormDescription, + FormField, + FormItem, + FormLabel, + FormMessage, +} from "@/components/ui/form"; + +import * as z from "zod"; +import { Input } from "@/components/ui/input"; +import { useForm } from "react-hook-form"; +import { zodResolver } from "@hookform/resolvers/zod"; + +interface FormPageInterface { + validatedData: any; + zodValidation: any; + apiValidation: any; + apiHeaders: any; +} + +const FormPage: React.FC = ({ + validatedData, + zodValidation, + apiValidation, + apiHeaders, +}) => { + const validSchemaZod = z.object({ ...zodValidation }); + + const form = useForm>({ + resolver: zodResolver(validSchemaZod), + defaultValues: { + ...validatedData, + }, + }); + + function submitUpdate(formData: FormData) { + let newFormData: any = {}; + Object.entries(Object.fromEntries(formData)).forEach(([key, value]) => { + if (apiValidation[key].fieldType === "integer") { + const newNumber = typeof value === "string" ? 0 : Number(value); + newFormData[key] = newNumber; + } else { + newFormData[key] = value; + } + }); + const validated = validSchemaZod.safeParse(newFormData); + console.log("validated", validated); + console.log("validated", validated.error); + } + + return ( + <> + { +
+
+ +
+ + + + + + +
+ {Object.keys(zodValidation).map((key: string) => { + if (apiValidation[key]?.fieldType === "string") { + return ( +
+ +
+ } + render={({ field }) => ( + + + + + + + )} + /> + + + + + + +
+
+ ); + } else if (apiValidation[key]?.fieldType === "integer") { + return ( +
+ +
+ } + render={({ field }) => ( + + + + + + + )} + /> + + + + + + +
+
+ ); + } + })} +
+ +
+ } + + ); +}; + +export default FormPage; diff --git a/src/components/ContextComponents/Commons/PageCreate.tsx b/src/components/ContextComponents/Commons/PageCreate.tsx index 1d2fec1..8a61e34 100644 --- a/src/components/ContextComponents/Commons/PageCreate.tsx +++ b/src/components/ContextComponents/Commons/PageCreate.tsx @@ -103,7 +103,10 @@ const CreatePage: React.FC = ({ {
- + {Object.keys(apiValidation).map((key: string) => { const keyValidation = apiValidation[key] || { fieldType: { type: "string" }, diff --git a/src/components/ContextComponents/Commons/PageUpdate.tsx b/src/components/ContextComponents/Commons/PageUpdate.tsx index 3ad3ab1..6c61ef4 100644 --- a/src/components/ContextComponents/Commons/PageUpdate.tsx +++ b/src/components/ContextComponents/Commons/PageUpdate.tsx @@ -1,19 +1,10 @@ -import React, { FormEvent } from "react"; -import IsNotAllowed from "./PageisNotAllowed"; +"use client"; +import React from "react"; 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"; -import { retrieveValidationsByEndpoint } from "../functions/retrieveEndpointAndValidations"; +import FormPage from "./FormPage"; + +import * as z from "zod"; +import { retrieveValidationsByEndpointWithData } from "../functions/retrieveEndpointAndValidations"; interface UpdatePageButtonProps { endpoint: string; @@ -31,7 +22,7 @@ const PageUpdate: React.FC = ({ saveFunction, }) => { const [validatedData, setValidatedData] = React.useState({}); - const [zodValidation, setZodValidation] = React.useState(z.object({})); + const [zodValidation, setZodValidation] = React.useState(null); const [apiValidation, setApiValidation] = React.useState<{ [key: string]: any; }>({}); @@ -39,48 +30,19 @@ const PageUpdate: React.FC = ({ [key: string]: any; }>({}); - React.useEffect(() => { - retrieveValidationsByEndpoint(endpoint).then((validations: any) => { - setZodValidation(validations.zodValidation as any); - setApiHeaders(validations.apiValidation?.headers as Object); - setApiValidation(validations.apiValidation?.validated as Object); - }); - }, []); - React.useEffect(() => { if (Object.keys(validatedData).length === 0) { - setValidatedData(selectedRow); - let zodValidationInternal: any = {}; - - Object.keys(selectedRow).map((key: string) => { - zodValidationInternal[key] = null; - const keyValidation = apiValidation[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(); + retrieveValidationsByEndpointWithData(endpoint, selectedRow).then( + (validations: any) => { + setValidatedData(selectedRow); + setZodValidation(validations.zodValidation); + setApiValidation(validations.apiValidation?.validated); + setApiHeaders(validations.apiValidation?.headers); } - }); - setZodValidation(zodValidationInternal); + ); } }, []); - const form = useForm>({ - resolver: zodResolver(zodValidation), - }); - - // function onSubmit(values: z.infer) { - function onSubmit() {} - return ( <>

{pageInfo?.title}

@@ -108,137 +70,15 @@ const PageUpdate: React.FC = ({ } /> -
- onSubmit()} - label="Kaydet" - bgColor="bg-emerald-700" - icon={ - - - - } - /> -
- -
- - - {Object.keys(apiValidation).map((key: string) => { - const keyValidation = apiValidation[key] || { - fieldType: { type: "string" }, - required: false, - }; - const fieldType = keyValidation.fieldType?.type || "string"; - if (fieldType === "string" && apiValidation[key]) { - return ( -
- -
- } - render={({ field }) => ( - - - - - - - )} - /> - - - - - - -
-
- ); - } else if (fieldType === "integer" && apiValidation[key]) { - return ( -
- -
- } - render={({ field }) => ( - - - - - - - )} - /> - - - - - - -
-
- ); - } - })} - -
+ {zodValidation && apiValidation && ( + + )} ); }; diff --git a/src/components/ContextComponents/Commons/Table.tsx b/src/components/ContextComponents/Commons/Table.tsx index bb5dd87..7066372 100644 --- a/src/components/ContextComponents/Commons/Table.tsx +++ b/src/components/ContextComponents/Commons/Table.tsx @@ -18,6 +18,7 @@ interface TableProps { createEndpoint: string; updateEndpoint: string; tableFunction: any; + saveFunction: any; UpdatePage: any; setFormPage: any; updatePageInfo: any; @@ -32,6 +33,7 @@ const Table: React.FC = ({ createEndpoint, updateEndpoint, tableFunction, + saveFunction, UpdatePage, setFormPage, updatePageInfo, @@ -57,12 +59,7 @@ const Table: React.FC = ({ React.useEffect(() => { retrieveHeadersByEndpoint(createEndpoint).then((validations: any) => { if (validations?.headers.length !== 0) { - tableFunction({ - page: 1, - limit: 10, - order_field: "uu_id", - order_type: "desc", - }) + tableFunction({}) .then((response: any) => { settabledata(response?.data || []); for (const key in response?.data[0]) { @@ -90,7 +87,7 @@ const Table: React.FC = ({ cleanSearch(); } else { if (searchText.length > 3) { - setQuery({ searchDropDown: searchText }); + // setQuery({ searchDropDown: searchText }); retrieveData(); } } @@ -103,7 +100,7 @@ const Table: React.FC = ({ pageInfo={updatePageInfo} selectedRow={selectedComponent} returnToPage={returnToPage} - saveFunction={() => console.log("saveFunction")} + saveFunction={saveFunction} /> ); } diff --git a/src/components/ContextComponents/functions/retrieveEndpointAndValidations.ts b/src/components/ContextComponents/functions/retrieveEndpointAndValidations.ts index 582aec2..a421818 100644 --- a/src/components/ContextComponents/functions/retrieveEndpointAndValidations.ts +++ b/src/components/ContextComponents/functions/retrieveEndpointAndValidations.ts @@ -1,5 +1,5 @@ "use client"; -import { z } from "zod"; +import * as z from "zod"; import { retrieveHeadersAndValidationByEndpoint, retrieveHeadersEndpoint, @@ -8,25 +8,101 @@ import { async function retrieveValidationsByEndpoint(endpoint: string) { let apiValidation: any = {}; let zodValidation: any = {}; + let zodSchema: any = {}; await retrieveHeadersAndValidationByEndpoint({ endpoint: endpoint, }) .then((validator) => { - if (JSON.stringify(validator?.validated) !== "{}") { + const apiValidated = validator?.validated || {}; + if (JSON.stringify(apiValidated) !== "{}") { apiValidation = validator; - Object.keys(validator?.validated).map((key: string) => { - zodValidation[key] = null; - const keyValidation = validator?.validated[key] || { - fieldType: { type: "string" }, - required: false, - }; - const fieldType: String = keyValidation.fieldType?.type || "string"; - const required = keyValidation.required || false; + Object.keys(apiValidated).map((key: string) => { + const fieldType: String = apiValidated[key].fieldType || "string"; + const required = apiValidated[key].required || false; if (fieldType === "string") { - zodValidation[key] = required ? z.string() : z.string().optional(); + zodValidation[key] = required + ? z + .string() + .min(1) + .refine((val) => val !== "" || val !== null) + : z + .string() + .min(1) + .optional() + .refine((val) => val !== "" || val !== null); } else if (fieldType === "integer") { - zodValidation[key] = required ? z.number() : z.number().optional(); + zodValidation[key] = required + ? z + .number() + .min(1) + .transform((val) => + Number(val) !== 0 || val !== null ? Number(val) : null + ) + : z + .number() + .min(1) + .optional() + .transform((val) => + Number(val) !== 0 || val !== null ? Number(val) : null + ); + } + }); + const validSchemaZod = z.object({ + ...zodValidation, + }); + return { + zodValidation: validSchemaZod, + apiValidation: apiValidation, + zodSchema: zodValidation, + }; + } + }) + .catch(() => {}); + return { + zodValidation: zodValidation, + apiValidation: apiValidation, + zodSchema: zodSchema, + }; +} + +async function retrieveValidationsByEndpointWithData( + endpoint: string, + data: any +) { + let apiValidation: any = {}; + let zodValidation: any = {}; + + await retrieveHeadersAndValidationByEndpoint({ + endpoint: endpoint, + }) + .then((validator) => { + const apiValidated = validator?.validated || {}; + if (JSON.stringify(apiValidated) !== "{}") { + apiValidation = validator; + Object.keys(apiValidated).map((key: string) => { + if (Object.keys(data).includes(key)) { + const fieldType: String = apiValidated[key].fieldType || "string"; + const required = apiValidated[key].required || false; + if (fieldType === "string") { + zodValidation[key] = required + ? z + .string() + .min(1) + .refine((val) => val !== "" || val !== null) + : z + .string() + .min(1) + .optional() + .refine((val) => val !== "" || val !== null); + } else if (fieldType === "integer") { + zodValidation[key] = required + ? z.number().transform((val) => Number(val)) + : z + .number() + .optional() + .transform((val) => Number(val)); + } } }); return { @@ -35,7 +111,9 @@ async function retrieveValidationsByEndpoint(endpoint: string) { }; } }) - .catch(() => {}); + .catch((error) => { + console.log("error", error); + }); return { zodValidation: zodValidation, apiValidation: apiValidation, @@ -62,4 +140,8 @@ async function retrieveHeadersByEndpoint(endpoint: string) { }; } -export { retrieveValidationsByEndpoint, retrieveHeadersByEndpoint }; +export { + retrieveValidationsByEndpoint, + retrieveHeadersByEndpoint, + retrieveValidationsByEndpointWithData, +}; diff --git a/src/components/ui/input.tsx b/src/components/ui/input.tsx index 69b64fb..7530608 100644 --- a/src/components/ui/input.tsx +++ b/src/components/ui/input.tsx @@ -1,6 +1,6 @@ -import * as React from "react" +import * as React from "react"; -import { cn } from "@/lib/utils" +import { cn } from "@/lib/utils"; const Input = React.forwardRef>( ({ className, type, ...props }, ref) => { @@ -8,15 +8,15 @@ const Input = React.forwardRef>( - ) + ); } -) -Input.displayName = "Input" +); +Input.displayName = "Input"; -export { Input } +export { Input };