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; 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 ( const fetchDataWithToken = async (
endpoint: string, endpoint: string,
payload: any, payload: any,
@ -97,4 +134,4 @@ const fetchDataWithToken = async (
return DefaultResponse; return DefaultResponse;
}; };
export { fetchData, fetchDataWithToken }; export { fetchData, fetchDataWithToken, updateDataWithToken };

View File

@ -1,5 +1,5 @@
"use server"; "use server";
import { fetchDataWithToken } from "../api-fetcher"; import { fetchDataWithToken, updateDataWithToken } from "../api-fetcher";
import { import {
baseUrl, baseUrl,
FilterList, FilterList,
@ -8,6 +8,8 @@ import {
} from "../basics"; } from "../basics";
const buildListEndpoint = `${baseUrl}/building/build/list`; const buildListEndpoint = `${baseUrl}/building/build/list`;
const buildCreateEndpoint = `${baseUrl}/building/build/create`;
const buildUpdateEndpoint = `${baseUrl}/building/build/update`;
async function retrieveBuildList(payload: FilterListInterface) { async function retrieveBuildList(payload: FilterListInterface) {
const tokenResponse: any = await fetchDataWithToken( const tokenResponse: any = await fetchDataWithToken(
@ -19,10 +21,17 @@ async function retrieveBuildList(payload: FilterListInterface) {
return tokenResponse; return tokenResponse;
} }
interface BuildUpdateInterface {
uuid: string;
payload: any;
}
async function updateBuild(payload: any) { async function updateBuild(payload: any) {
const tokenResponse: any = await fetchDataWithToken( const { uu_id: extractedField, ...payloadBody } = payload;
"/building/build/update", const tokenResponse: any = await updateDataWithToken(
payload, buildUpdateEndpoint,
extractedField,
payloadBody,
"POST", "POST",
false false
); );
@ -31,7 +40,7 @@ async function updateBuild(payload: any) {
async function createBuild(payload: any) { async function createBuild(payload: any) {
const tokenResponse: any = await fetchDataWithToken( const tokenResponse: any = await fetchDataWithToken(
"/building/build/create", buildCreateEndpoint,
payload, payload,
"POST", "POST",
false false

View File

@ -1,5 +1,3 @@
import { array, boolean, number, object, string } from "zod";
interface ValidationInterface { interface ValidationInterface {
required: string[]; required: string[];
properties: Object; properties: Object;
@ -35,25 +33,24 @@ class HeadersAndValidations {
parseProcesser() { parseProcesser() {
Object.entries(this.properties).map(([key, value]) => { Object.entries(this.properties).map(([key, value]) => {
const multipleTypes: Object[] = value?.anyOf || [];
let isRequired: boolean = true; let isRequired: boolean = true;
const multipleTypes = value?.anyOf || [];
try { try {
isRequired = Object.keys(multipleTypes).includes("anyOf"); isRequired = !Object.keys(value).includes("anyOf");
} catch (error) {} } catch (error) {}
if (!isRequired) { if (!isRequired) {
multipleTypes.map((row: any) => { multipleTypes.map((row: any) => {
if (row.type !== "null") { if (row.type !== "null") {
this.validated[key] = { this.validated[key] = {
required: isRequired, required: isRequired,
fieldType: row.type, fieldType: row?.type,
}; };
} }
}); });
} else { } else {
this.validated[key] = { this.validated[key] = {
required: isRequired, required: isRequired,
fieldType: value, fieldType: value?.type,
}; };
} }
}); });

View File

@ -29,7 +29,7 @@ const Build: React.FC = () => {
updateEndpoint="/building/build/update" updateEndpoint="/building/build/update"
tableFunction={retrieveBuildList} tableFunction={retrieveBuildList}
UpdatePage={PageUpdate} UpdatePage={PageUpdate}
saveFunction={createBuild} saveFunction={updateBuild}
setFormPage={setFormPage} setFormPage={setFormPage}
returnToPage={() => setFormPage(null)} returnToPage={() => setFormPage(null)}
updatePageInfo={{ updatePageInfo={{

View File

@ -14,7 +14,7 @@ import {
import { z } from "zod"; import { z } from "zod";
import { zodResolver } from "@hookform/resolvers/zod"; import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form"; import { useForm } from "react-hook-form";
import { retrieveValidationsByEndpoint } from "../../functions/retrieveEndpointAndValidations"; import { retrieveValidationsByEndpoint } from "@/components/ContextComponents/functions/retrieveEndpointAndValidations";
interface BuildCreateProps { interface BuildCreateProps {
pageInfo: any; pageInfo: any;
@ -23,14 +23,18 @@ interface BuildCreateProps {
const BuildCreate: React.FC<BuildCreateProps> = ({ pageInfo, endpoint }) => { const BuildCreate: React.FC<BuildCreateProps> = ({ pageInfo, endpoint }) => {
const [zodValidation, setZodValidation] = React.useState(z.object({})); const [zodValidation, setZodValidation] = React.useState(z.object({}));
const [apiValidation, setApiValidation] = React.useState({}); const [apiValidation, setApiValidation] = React.useState<Record<string, any>>(
const [apiHeaders, setApiHeaders] = React.useState({}); {}
);
const [apiHeaders, setApiHeaders] = React.useState<Record<string, string>>(
{}
);
React.useEffect(() => { React.useEffect(() => {
retrieveValidationsByEndpoint(endpoint).then((validations: any) => { retrieveValidationsByEndpoint(endpoint).then((validations: any) => {
setZodValidation(validations.zodValidation as any); setZodValidation(validations.zodValidation as any);
setApiHeaders(validations.apiValidation?.headers as Object); setApiHeaders(validations.apiValidation?.headers);
setApiValidation(validations.apiValidation?.validated as Object); setApiValidation(validations.apiValidation?.validated);
}); });
}, []); }, []);

View File

@ -21,6 +21,7 @@ interface FormPageInterface {
zodValidation: any; zodValidation: any;
apiValidation: any; apiValidation: any;
apiHeaders: any; apiHeaders: any;
saveFunction: any;
} }
const FormPage: React.FC<FormPageInterface> = ({ const FormPage: React.FC<FormPageInterface> = ({
@ -28,6 +29,7 @@ const FormPage: React.FC<FormPageInterface> = ({
zodValidation, zodValidation,
apiValidation, apiValidation,
apiHeaders, apiHeaders,
saveFunction,
}) => { }) => {
const validSchemaZod = z.object({ ...zodValidation }); const validSchemaZod = z.object({ ...zodValidation });
@ -37,7 +39,9 @@ const FormPage: React.FC<FormPageInterface> = ({
...validatedData, ...validatedData,
}, },
}); });
function submitUpdate(formData: z.infer<typeof validSchemaZod>) { function submitUpdate(formData: z.infer<typeof validSchemaZod>) {
const updateUUID = validatedData?.uu_id;
let newDataForm: any = {}; let newDataForm: any = {};
Object.entries(formData).map(([key, value]) => { Object.entries(formData).map(([key, value]) => {
if (typeof value === "number" && value !== 0) { if (typeof value === "number" && value !== 0) {
@ -46,7 +50,16 @@ const FormPage: React.FC<FormPageInterface> = ({
newDataForm[key] = value; 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 ( return (
@ -109,10 +122,15 @@ const FormPage: React.FC<FormPageInterface> = ({
<FormItem> <FormItem>
<FormLabel>{apiHeaders[key]}</FormLabel> <FormLabel>{apiHeaders[key]}</FormLabel>
<FormControl> <FormControl>
<SmartDatetimeInput {/* <SmartDatetimeInput
value={field.value} value={field.value}
onValueChange={field.onChange} onValueChange={field.onChange}
placeholder="e.g. Tomorrow morning 9am" 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> </FormControl>
{String(form.formState.errors[key]?.type) === {String(form.formState.errors[key]?.type) ===

View File

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

View File

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

View File

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

View File

@ -16,6 +16,7 @@ async function retrieveValidationsByEndpoint(endpoint: string) {
}) })
.then((validator) => { .then((validator) => {
const apiValidated = validator?.validated || {}; const apiValidated = validator?.validated || {};
console.log("apiValidated", apiValidated);
if (JSON.stringify(apiValidated) !== "{}") { if (JSON.stringify(apiValidated) !== "{}") {
apiValidation = validator; apiValidation = validator;
Object.keys(apiValidated).map((key: string) => { Object.keys(apiValidated).map((key: string) => {