updated event validations
This commit is contained in:
parent
64274570d6
commit
5f70bd9854
|
|
@ -0,0 +1,77 @@
|
|||
import { array, boolean, number, object, string } from "zod";
|
||||
|
||||
interface ValidationInterface {
|
||||
required: string[];
|
||||
properties: Object;
|
||||
title: string;
|
||||
type: string;
|
||||
}
|
||||
|
||||
interface HeadersAndValidationsInterface {
|
||||
headers: Object;
|
||||
validation: ValidationInterface;
|
||||
language: string;
|
||||
properties: Object;
|
||||
}
|
||||
|
||||
class HeadersAndValidations {
|
||||
headers: Object;
|
||||
validation: ValidationInterface;
|
||||
language: string;
|
||||
properties: Object;
|
||||
validated: any = {};
|
||||
|
||||
constructor({
|
||||
headers,
|
||||
validation,
|
||||
language,
|
||||
}: HeadersAndValidationsInterface) {
|
||||
this.headers = headers;
|
||||
this.validation = validation;
|
||||
this.language = language;
|
||||
this.properties = this.validation?.properties;
|
||||
this.parseProcesser();
|
||||
}
|
||||
|
||||
parseProcesser() {
|
||||
const requiredKeys = Array.from(this.validation?.required);
|
||||
Object.entries(this.properties).map(([key, value]) => {
|
||||
const isRequired: Boolean = requiredKeys.includes(key);
|
||||
const multipleTypes: Object[] = value?.anyOf;
|
||||
if (!isRequired) {
|
||||
multipleTypes.map((row: any) => {
|
||||
if (row.type !== "null") {
|
||||
this.validated[key] = {
|
||||
required: false,
|
||||
fieldType: this.parseType(row.type),
|
||||
};
|
||||
}
|
||||
});
|
||||
} else {
|
||||
this.validated[key] = {
|
||||
required: true,
|
||||
fieldType: this.parseType(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 };
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
"use server";
|
||||
import { fetchData, fetchDataWithToken } from "@/(apicalls)/api-fetcher";
|
||||
import { baseUrl, cookieObject, tokenSecret } from "@/(apicalls)/basics";
|
||||
|
||||
import { HeadersAndValidations } from "@/(apicalls)/validations/validationProcesser";
|
||||
|
||||
const headersAndValidationEndpoint = `${baseUrl}/validations/endpoint`;
|
||||
|
||||
interface EndpointInterface {
|
||||
endpoint: string;
|
||||
}
|
||||
|
||||
async function retrieveHeadersEndpoint({ endpoint }: EndpointInterface) {
|
||||
console.log("endpoint", endpoint);
|
||||
|
||||
const selectResponse: any = await fetchDataWithToken(
|
||||
headersAndValidationEndpoint,
|
||||
{
|
||||
endpoint: endpoint,
|
||||
},
|
||||
"POST",
|
||||
false
|
||||
);
|
||||
if (selectResponse.status === 200) {
|
||||
return {
|
||||
headers: selectResponse?.headers,
|
||||
};
|
||||
}
|
||||
return { headers: {} };
|
||||
}
|
||||
|
||||
async function retrieveHeadersAndValidationByEndpoint({
|
||||
endpoint,
|
||||
}: EndpointInterface) {
|
||||
console.log("endpoint", endpoint);
|
||||
const selectResponse: any = await fetchDataWithToken(
|
||||
headersAndValidationEndpoint,
|
||||
{
|
||||
endpoint: endpoint,
|
||||
},
|
||||
"POST",
|
||||
false
|
||||
);
|
||||
console.log("selectResponse", selectResponse);
|
||||
if (selectResponse.status === 200) {
|
||||
const responseParsed = new HeadersAndValidations(selectResponse);
|
||||
console.log("responseParsed", responseParsed);
|
||||
return {
|
||||
status: selectResponse.status,
|
||||
headers: responseParsed.headers,
|
||||
validated: responseParsed.validated,
|
||||
language: responseParsed.language,
|
||||
message: selectResponse.message,
|
||||
};
|
||||
}
|
||||
return selectResponse;
|
||||
}
|
||||
|
||||
export { retrieveHeadersAndValidationByEndpoint, retrieveHeadersEndpoint };
|
||||
|
|
@ -3,6 +3,7 @@
|
|||
import React from "react";
|
||||
|
||||
const BuildPage: React.FC = async () => {
|
||||
|
||||
return (
|
||||
<div>
|
||||
<h1>Build Page</h1>
|
||||
|
|
|
|||
|
|
@ -16,6 +16,9 @@ const Dashboard: React.FC = async () => {
|
|||
}
|
||||
const eventsList = await retrieveAvailableEvents();
|
||||
const availableMenu = retrieveAvailableCategories(eventsList || []);
|
||||
|
||||
|
||||
|
||||
return (
|
||||
<>
|
||||
<DashboardPage leftSideMenuContent={availableMenu} />
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ import BuildUpdatePage from "@/components/ContextComponents/Building/Build/Build
|
|||
|
||||
import { retrieveAvailableEvents } from "@/(apicalls)/cookies/token";
|
||||
import { retrieveBuildList } from "@/(apicalls)/building/build";
|
||||
import { retrieveHeadersEndpoint } from "@/(apicalls)/validations/validations";
|
||||
|
||||
const Build: React.FC = () => {
|
||||
const [renderTable, setRenderTable] = React.useState(false);
|
||||
|
|
@ -19,12 +20,20 @@ const Build: React.FC = () => {
|
|||
const [isFormEnabled, setIsFormEnabled] = React.useState(false);
|
||||
const [rowData, setRowData] = React.useState({});
|
||||
const [formPage, setFormPage] = React.useState(<IsNotAllowed />);
|
||||
const [createValidation, setCreateValidation] = React.useState({});
|
||||
const [updateValidation, setUpdateValidation] = React.useState({});
|
||||
const [deleteValidation, setDeleteValidation] = React.useState({});
|
||||
const [tableValidation, setTableValidation] = React.useState([]);
|
||||
const [tableHeaders, setTableHeaders] = React.useState({});
|
||||
|
||||
const endpointNeeds = {
|
||||
table: {
|
||||
endpoint: "/building/build/list",
|
||||
variableKey: "headers",
|
||||
variableReact: setTableHeaders,
|
||||
component: renderTable ? (
|
||||
<Table
|
||||
headers={tableHeaders || {}}
|
||||
createTable={retrieveBuildList}
|
||||
rowClickedFunction={setRowData}
|
||||
/>
|
||||
|
|
@ -35,6 +44,8 @@ const Build: React.FC = () => {
|
|||
},
|
||||
update: {
|
||||
endpoint: "/building/build/create",
|
||||
variableReact: setCreateValidation,
|
||||
variableKey: "headers",
|
||||
component: renderUpdate ? (
|
||||
<UpdateButton
|
||||
buttonLabel="Bina Güncelle"
|
||||
|
|
@ -57,6 +68,8 @@ const Build: React.FC = () => {
|
|||
},
|
||||
create: {
|
||||
endpoint: "/building/build/update/{build_uu_id}",
|
||||
variableReact: setUpdateValidation,
|
||||
variableKey: "headers",
|
||||
component: renderCreate ? (
|
||||
<CreateButton
|
||||
title="Bina Oluştur Sayfasına Hoş geldiniz"
|
||||
|
|
@ -72,6 +85,8 @@ const Build: React.FC = () => {
|
|||
},
|
||||
delete: {
|
||||
endpoint: "/building/build/delete",
|
||||
variableKey: "headers",
|
||||
variableReact: setDeleteValidation,
|
||||
component: renderDelete ? (
|
||||
<DeleteButton onClick={() => () => setIsFormEnabled(true)} />
|
||||
) : (
|
||||
|
|
@ -86,6 +101,15 @@ const Build: React.FC = () => {
|
|||
.then((data) => {
|
||||
for (const endpointNeed of Object.values(endpointNeeds)) {
|
||||
if (data?.availableEvents.includes(endpointNeed.endpoint)) {
|
||||
if (endpointNeed.variableKey === "headers") {
|
||||
retrieveHeadersEndpoint({ endpoint: endpointNeed.endpoint })
|
||||
.then((validator) => {
|
||||
if (JSON.stringify(validator?.headers) !== "{}") {
|
||||
setTableHeaders(validator?.headers);
|
||||
}
|
||||
})
|
||||
.catch((error) => {});
|
||||
}
|
||||
endpointNeed.isRender(true);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ import {
|
|||
|
||||
interface TableProps {
|
||||
createTable: any;
|
||||
headers: any;
|
||||
rowClickedFunction: React.Dispatch<React.SetStateAction<{}>>;
|
||||
}
|
||||
|
||||
|
|
@ -22,32 +23,46 @@ const formSchema = z.object({
|
|||
searchText: z.string().default(""),
|
||||
});
|
||||
|
||||
const Table: React.FC<TableProps> = ({ createTable, rowClickedFunction }) => {
|
||||
const Table: React.FC<TableProps> = ({
|
||||
createTable,
|
||||
rowClickedFunction,
|
||||
headers,
|
||||
}) => {
|
||||
const [initalData, setInitalData] = React.useState([]);
|
||||
const [tabledata, settabledata] = React.useState([]);
|
||||
const [headersList, setHeadersList] = React.useState<string[]>([]);
|
||||
const [headersList, setHeadersList] = React.useState([]);
|
||||
|
||||
const incomingHeaders = Array.from(Object.values(headers)) || [];
|
||||
const headersObject = headers || {};
|
||||
|
||||
const form = useForm<z.infer<typeof formSchema>>({
|
||||
resolver: zodResolver(formSchema),
|
||||
});
|
||||
|
||||
React.useEffect(() => {
|
||||
createTable({}).then((res: Object) => {
|
||||
if (incomingHeaders.length !== 0) {
|
||||
let headersNew: any = [];
|
||||
createTable({})
|
||||
.then((res: Object) => {
|
||||
const resData: any = res?.data || [];
|
||||
if (resData?.length > 0) {
|
||||
settabledata(resData || []);
|
||||
setInitalData(resData || []);
|
||||
setHeadersList(getHeaders(resData));
|
||||
});
|
||||
}, []);
|
||||
|
||||
function getHeaders(data: Array<any>) {
|
||||
let returnList: Array<string> = [];
|
||||
if (Array.from(data).length > 0) {
|
||||
Object.entries(data[0]).map(([key, value]) => {
|
||||
returnList.push(key);
|
||||
});
|
||||
for (const key in resData[0]) {
|
||||
if (Object.keys(headersObject).includes(key)) {
|
||||
headersNew.push(headers[key]);
|
||||
} else {
|
||||
console.log("key", key);
|
||||
}
|
||||
return returnList;
|
||||
}
|
||||
if (headersNew.length > 0) {
|
||||
setHeadersList(headersNew);
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch((err: any) => {});
|
||||
}
|
||||
}, [headers]);
|
||||
|
||||
function selectItem({ key }: { key: number }) {
|
||||
tabledata.map((item, index) => {
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import React from "react";
|
|||
interface SidebarItemProps {
|
||||
item: any;
|
||||
pageName: any;
|
||||
setPageName: any;
|
||||
setPageName: React.Dispatch<React.SetStateAction<any>>;
|
||||
leftSideMenuSetter: React.Dispatch<React.SetStateAction<Array<any>>>;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue