diff --git a/src/(apicalls)/api-fetcher.tsx b/src/(apicalls)/api-fetcher.tsx index 34bae7a..c942cc2 100644 --- a/src/(apicalls)/api-fetcher.tsx +++ b/src/(apicalls)/api-fetcher.tsx @@ -69,6 +69,43 @@ const fetchData = async ( return DefaultResponse; }; +const updateDataWithToken = async ( + endpoint: string, + uuid: string, + payload: any, + method: string = "POST", + cache: boolean = false +) => { + const accessToken = (await retrieveAccessToken()) || ""; + let headersObject: any = { + cache: cache ? "force-cache" : "no-cache", + method: method, + headers: { + ...defaultHeaders, + "evyos-session-key": accessToken, + }, + }; + console.log("updateDataWithToken", { + endpoint: `${endpoint}/${uuid}`, + body: JSON.stringify(payload.payload), + method, + cache, + }); + + if (method !== "GET") { + headersObject = { + ...headersObject, + body: JSON.stringify(payload.payload), + }; + } + + try { + const response = await fetch(`${endpoint}/${uuid}`, headersObject); + return await prepareResponse(response); + } catch (error) {} + return DefaultResponse; +}; + const fetchDataWithToken = async ( endpoint: string, payload: any, @@ -97,4 +134,4 @@ const fetchDataWithToken = async ( return DefaultResponse; }; -export { fetchData, fetchDataWithToken }; +export { fetchData, fetchDataWithToken, updateDataWithToken }; diff --git a/src/(apicalls)/building/build.tsx b/src/(apicalls)/building/build.tsx index 92292b5..744f3d4 100644 --- a/src/(apicalls)/building/build.tsx +++ b/src/(apicalls)/building/build.tsx @@ -1,5 +1,5 @@ "use server"; -import { fetchDataWithToken } from "../api-fetcher"; +import { fetchDataWithToken, updateDataWithToken } from "../api-fetcher"; import { baseUrl, FilterList, @@ -8,6 +8,8 @@ import { } from "../basics"; const buildListEndpoint = `${baseUrl}/building/build/list`; +const buildCreateEndpoint = `${baseUrl}/building/build/create`; +const buildUpdateEndpoint = `${baseUrl}/building/build/update`; async function retrieveBuildList(payload: FilterListInterface) { const tokenResponse: any = await fetchDataWithToken( @@ -19,10 +21,17 @@ async function retrieveBuildList(payload: FilterListInterface) { return tokenResponse; } +interface BuildUpdateInterface { + uuid: string; + payload: any; +} + async function updateBuild(payload: any) { - const tokenResponse: any = await fetchDataWithToken( - "/building/build/update", - payload, + const { uu_id: extractedField, ...payloadBody } = payload; + const tokenResponse: any = await updateDataWithToken( + buildUpdateEndpoint, + extractedField, + payloadBody, "POST", false ); @@ -31,7 +40,7 @@ async function updateBuild(payload: any) { async function createBuild(payload: any) { const tokenResponse: any = await fetchDataWithToken( - "/building/build/create", + buildCreateEndpoint, payload, "POST", false diff --git a/src/(apicalls)/validations/validationProcesser.ts b/src/(apicalls)/validations/validationProcesser.ts index be38f9e..683bddd 100644 --- a/src/(apicalls)/validations/validationProcesser.ts +++ b/src/(apicalls)/validations/validationProcesser.ts @@ -1,5 +1,3 @@ -import { array, boolean, number, object, string } from "zod"; - interface ValidationInterface { required: string[]; properties: Object; @@ -35,25 +33,24 @@ class HeadersAndValidations { parseProcesser() { Object.entries(this.properties).map(([key, value]) => { - const multipleTypes: Object[] = value?.anyOf || []; let isRequired: boolean = true; + const multipleTypes = value?.anyOf || []; try { - isRequired = Object.keys(multipleTypes).includes("anyOf"); + isRequired = !Object.keys(value).includes("anyOf"); } catch (error) {} - if (!isRequired) { multipleTypes.map((row: any) => { if (row.type !== "null") { this.validated[key] = { required: isRequired, - fieldType: row.type, + fieldType: row?.type, }; } }); } else { this.validated[key] = { required: isRequired, - fieldType: value, + fieldType: value?.type, }; } }); diff --git a/src/components/ContextComponents/Building/Build/Build.tsx b/src/components/ContextComponents/Building/Build/Build.tsx index 5f675e2..892d35f 100644 --- a/src/components/ContextComponents/Building/Build/Build.tsx +++ b/src/components/ContextComponents/Building/Build/Build.tsx @@ -29,7 +29,7 @@ const Build: React.FC = () => { updateEndpoint="/building/build/update" tableFunction={retrieveBuildList} UpdatePage={PageUpdate} - saveFunction={createBuild} + saveFunction={updateBuild} setFormPage={setFormPage} returnToPage={() => setFormPage(null)} updatePageInfo={{ diff --git a/src/components/ContextComponents/Building/Build/BuildCreate.tsx b/src/components/ContextComponents/Building/Build/BuildCreate.tsx index d8f7bf6..54eb051 100644 --- a/src/components/ContextComponents/Building/Build/BuildCreate.tsx +++ b/src/components/ContextComponents/Building/Build/BuildCreate.tsx @@ -14,7 +14,7 @@ import { import { z } from "zod"; import { zodResolver } from "@hookform/resolvers/zod"; import { useForm } from "react-hook-form"; -import { retrieveValidationsByEndpoint } from "../../functions/retrieveEndpointAndValidations"; +import { retrieveValidationsByEndpoint } from "@/components/ContextComponents/functions/retrieveEndpointAndValidations"; interface BuildCreateProps { pageInfo: any; @@ -23,14 +23,18 @@ interface BuildCreateProps { const BuildCreate: React.FC = ({ pageInfo, endpoint }) => { const [zodValidation, setZodValidation] = React.useState(z.object({})); - const [apiValidation, setApiValidation] = React.useState({}); - const [apiHeaders, setApiHeaders] = React.useState({}); + const [apiValidation, setApiValidation] = React.useState>( + {} + ); + const [apiHeaders, setApiHeaders] = React.useState>( + {} + ); React.useEffect(() => { retrieveValidationsByEndpoint(endpoint).then((validations: any) => { setZodValidation(validations.zodValidation as any); - setApiHeaders(validations.apiValidation?.headers as Object); - setApiValidation(validations.apiValidation?.validated as Object); + setApiHeaders(validations.apiValidation?.headers); + setApiValidation(validations.apiValidation?.validated); }); }, []); diff --git a/src/components/ContextComponents/Commons/FormPage.tsx b/src/components/ContextComponents/Commons/FormPage.tsx index e31a9d9..052b276 100644 --- a/src/components/ContextComponents/Commons/FormPage.tsx +++ b/src/components/ContextComponents/Commons/FormPage.tsx @@ -21,6 +21,7 @@ interface FormPageInterface { zodValidation: any; apiValidation: any; apiHeaders: any; + saveFunction: any; } const FormPage: React.FC = ({ @@ -28,6 +29,7 @@ const FormPage: React.FC = ({ zodValidation, apiValidation, apiHeaders, + saveFunction, }) => { const validSchemaZod = z.object({ ...zodValidation }); @@ -37,7 +39,9 @@ const FormPage: React.FC = ({ ...validatedData, }, }); + function submitUpdate(formData: z.infer) { + const updateUUID = validatedData?.uu_id; let newDataForm: any = {}; Object.entries(formData).map(([key, value]) => { if (typeof value === "number" && value !== 0) { @@ -46,7 +50,16 @@ const FormPage: React.FC = ({ newDataForm[key] = value; } }); - console.log(newDataForm); + saveFunction({ + uu_id: updateUUID, + payload: newDataForm, + }).then((res: any) => { + console.log(res); + if (res?.status === 200) { + } else { + alert("Güncelleme başarısız"); + } + }); } return ( @@ -109,10 +122,15 @@ const FormPage: React.FC = ({ {apiHeaders[key]} - */} + {String(form.formState.errors[key]?.type) === diff --git a/src/components/ContextComponents/Commons/FormPageValid.tsx b/src/components/ContextComponents/Commons/FormPageValid.tsx index 115ee12..f977c74 100644 --- a/src/components/ContextComponents/Commons/FormPageValid.tsx +++ b/src/components/ContextComponents/Commons/FormPageValid.tsx @@ -20,18 +20,21 @@ interface FormPageValidInterface { zodValidation: any; apiValidation: any; apiHeaders: any; + saveFunction: any; } const FormPageValid: React.FC = ({ zodValidation, apiValidation, apiHeaders, + saveFunction, }) => { const validSchemaZod = z.object({ ...zodValidation }); const form = useForm>({ resolver: zodResolver(validSchemaZod), }); + console.log("zodValidation", zodValidation); function submitUpdate(formData: z.infer) { let newDataForm: any = {}; Object.entries(formData).map(([key, value]) => { @@ -41,7 +44,7 @@ const FormPageValid: React.FC = ({ newDataForm[key] = value; } }); - console.log(newDataForm); + saveFunction(newDataForm); } return ( diff --git a/src/components/ContextComponents/Commons/PageCreate.tsx b/src/components/ContextComponents/Commons/PageCreate.tsx index 51c81b5..585feb6 100644 --- a/src/components/ContextComponents/Commons/PageCreate.tsx +++ b/src/components/ContextComponents/Commons/PageCreate.tsx @@ -46,8 +46,6 @@ const CreatePage: React.FC = ({ }); }, []); - - return (

{pageInfo.description}

@@ -81,6 +79,7 @@ const CreatePage: React.FC = ({ zodValidation={zodValidation} apiValidation={apiValidation} apiHeaders={apiHeaders} + saveFunction={saveFunction} /> )}
diff --git a/src/components/ContextComponents/Commons/PageUpdate.tsx b/src/components/ContextComponents/Commons/PageUpdate.tsx index dd34eb0..84c21f0 100644 --- a/src/components/ContextComponents/Commons/PageUpdate.tsx +++ b/src/components/ContextComponents/Commons/PageUpdate.tsx @@ -76,6 +76,7 @@ const PageUpdate: React.FC = ({ zodValidation={zodValidation} apiValidation={apiValidation} apiHeaders={apiHeaders} + saveFunction={saveFunction} /> )} diff --git a/src/components/ContextComponents/functions/retrieveEndpointAndValidations.ts b/src/components/ContextComponents/functions/retrieveEndpointAndValidations.ts index 331005a..bc35262 100644 --- a/src/components/ContextComponents/functions/retrieveEndpointAndValidations.ts +++ b/src/components/ContextComponents/functions/retrieveEndpointAndValidations.ts @@ -16,6 +16,7 @@ async function retrieveValidationsByEndpoint(endpoint: string) { }) .then((validator) => { const apiValidated = validator?.validated || {}; + console.log("apiValidated", apiValidated); if (JSON.stringify(apiValidated) !== "{}") { apiValidation = validator; Object.keys(apiValidated).map((key: string) => {