update build updated

This commit is contained in:
berkay 2024-12-20 22:04:40 +03:00
parent 6720c69e6f
commit 1514fab6f0
10 changed files with 93 additions and 24 deletions

View File

@ -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 };

View File

@ -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

View File

@ -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,
};
}
});

View File

@ -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={{

View File

@ -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<BuildCreateProps> = ({ pageInfo, endpoint }) => {
const [zodValidation, setZodValidation] = React.useState(z.object({}));
const [apiValidation, setApiValidation] = React.useState({});
const [apiHeaders, setApiHeaders] = React.useState({});
const [apiValidation, setApiValidation] = React.useState<Record<string, any>>(
{}
);
const [apiHeaders, setApiHeaders] = React.useState<Record<string, string>>(
{}
);
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);
});
}, []);

View File

@ -21,6 +21,7 @@ interface FormPageInterface {
zodValidation: any;
apiValidation: any;
apiHeaders: any;
saveFunction: any;
}
const FormPage: React.FC<FormPageInterface> = ({
@ -28,6 +29,7 @@ const FormPage: React.FC<FormPageInterface> = ({
zodValidation,
apiValidation,
apiHeaders,
saveFunction,
}) => {
const validSchemaZod = z.object({ ...zodValidation });
@ -37,7 +39,9 @@ const FormPage: React.FC<FormPageInterface> = ({
...validatedData,
},
});
function submitUpdate(formData: z.infer<typeof validSchemaZod>) {
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<FormPageInterface> = ({
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<FormPageInterface> = ({
<FormItem>
<FormLabel>{apiHeaders[key]}</FormLabel>
<FormControl>
<SmartDatetimeInput
{/* <SmartDatetimeInput
value={field.value}
onValueChange={field.onChange}
placeholder="e.g. Tomorrow morning 9am"
/> */}
<Input
className="w-full rounded-lg border border-stroke bg-transparent py-4 pl-6 pr-10 text-black outline-none focus:border-primary focus-visible:shadow-none dark:border-form-strokedark dark:bg-form-input dark:text-white dark:focus:border-primary"
{...field}
value={field.value || ""}
/>
</FormControl>
{String(form.formState.errors[key]?.type) ===

View File

@ -20,18 +20,21 @@ interface FormPageValidInterface {
zodValidation: any;
apiValidation: any;
apiHeaders: any;
saveFunction: any;
}
const FormPageValid: React.FC<FormPageValidInterface> = ({
zodValidation,
apiValidation,
apiHeaders,
saveFunction,
}) => {
const validSchemaZod = z.object({ ...zodValidation });
const form = useForm<z.infer<typeof validSchemaZod>>({
resolver: zodResolver(validSchemaZod),
});
console.log("zodValidation", zodValidation);
function submitUpdate(formData: z.infer<typeof validSchemaZod>) {
let newDataForm: any = {};
Object.entries(formData).map(([key, value]) => {
@ -41,7 +44,7 @@ const FormPageValid: React.FC<FormPageValidInterface> = ({
newDataForm[key] = value;
}
});
console.log(newDataForm);
saveFunction(newDataForm);
}
return (

View File

@ -46,8 +46,6 @@ const CreatePage: React.FC<CreatePageProps> = ({
});
}, []);
return (
<div>
<h1 className="text-center text-3xl my-7">{pageInfo.description}</h1>
@ -81,6 +79,7 @@ const CreatePage: React.FC<CreatePageProps> = ({
zodValidation={zodValidation}
apiValidation={apiValidation}
apiHeaders={apiHeaders}
saveFunction={saveFunction}
/>
)}
</div>

View File

@ -76,6 +76,7 @@ const PageUpdate: React.FC<UpdatePageButtonProps> = ({
zodValidation={zodValidation}
apiValidation={apiValidation}
apiHeaders={apiHeaders}
saveFunction={saveFunction}
/>
)}
</>

View File

@ -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) => {