pages updated
This commit is contained in:
96
oldCode/building/create/CreatePage.tsx
Normal file
96
oldCode/building/create/CreatePage.tsx
Normal file
@@ -0,0 +1,96 @@
|
||||
"use client";
|
||||
import { RetrieveInputByType } from "@/hooks/renderInputWithValidation";
|
||||
import * as z from "zod";
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { useForm } from "react-hook-form";
|
||||
|
||||
import {
|
||||
Form,
|
||||
FormControl,
|
||||
FormField,
|
||||
FormItem,
|
||||
FormLabel,
|
||||
FormMessage,
|
||||
FormDescription,
|
||||
} from "@/components/ui/form";
|
||||
import { convertApiValidationToZodValidation } from "@/lib/renderZodValidation";
|
||||
|
||||
interface CreatePageComponentInterface {
|
||||
validator: any;
|
||||
headers: any;
|
||||
}
|
||||
|
||||
const CreatePageComponent: React.FC<CreatePageComponentInterface> = ({
|
||||
validator,
|
||||
headers,
|
||||
}) => {
|
||||
const returnValidation = convertApiValidationToZodValidation(validator);
|
||||
const { validSchemaZod, zodValidation, apiValidation } = returnValidation;
|
||||
|
||||
const form = useForm<z.infer<typeof validSchemaZod>>({
|
||||
resolver: zodResolver(validSchemaZod),
|
||||
defaultValues: {},
|
||||
});
|
||||
|
||||
function submitUpdate(formData: z.infer<typeof validSchemaZod>) {
|
||||
// saveFunction({
|
||||
// uu_id: updateUUID,
|
||||
// payload: validDataParser(formData),
|
||||
// }).then((res: any) => {
|
||||
// console.log(res);
|
||||
// if (res?.status === 200) {
|
||||
// } else {
|
||||
// alert("Güncelleme başarısız");
|
||||
// }
|
||||
// });
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="container mx-auto p-4">
|
||||
<Form {...form}>
|
||||
<form action="">
|
||||
{Object.entries(validator).map(([key, value]: [string, any]) => (
|
||||
<FormField
|
||||
key={key}
|
||||
control={form.control}
|
||||
name={String(key)}
|
||||
render={({ field }) => {
|
||||
return (
|
||||
<FormItem>
|
||||
<FormLabel>
|
||||
{headers[key] || `Header not found ${key}`}
|
||||
</FormLabel>
|
||||
<FormControl>
|
||||
{RetrieveInputByType({
|
||||
type: value?.fieldType || "string",
|
||||
props: {
|
||||
className: "",
|
||||
field: field,
|
||||
placeholder: headers[key],
|
||||
required: value?.required || false,
|
||||
},
|
||||
})}
|
||||
</FormControl>
|
||||
{String(form.formState.errors[key]?.type) ===
|
||||
"invalid_type" ? (
|
||||
<span id={key} className="text-red-700">
|
||||
"Lütfen metinsel bir değer giriniz"
|
||||
</span>
|
||||
) : (
|
||||
<></>
|
||||
)}
|
||||
</FormItem>
|
||||
);
|
||||
}}
|
||||
/>
|
||||
))}
|
||||
<button type="submit" className="mt-4">
|
||||
Submit
|
||||
</button>
|
||||
</form>
|
||||
</Form>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default CreatePageComponent;
|
||||
40
oldCode/building/create/page.tsx
Normal file
40
oldCode/building/create/page.tsx
Normal file
@@ -0,0 +1,40 @@
|
||||
"use server";
|
||||
|
||||
import { retrieveAvailableEndpoint } from "@/apicalls/checkEndpoint";
|
||||
import { checkAccessTokenIsValid } from "@/apicalls/cookies/token";
|
||||
import { decryptQuery, defaultPagination } from "@/apicalls/test";
|
||||
import { retrieveHeadersAndValidationByEndpoint } from "@/apicalls/validations/validations";
|
||||
import { redirect } from "next/navigation";
|
||||
import CreatePageComponent from "./CreatePage";
|
||||
|
||||
export default async function BuildingCreatePage({
|
||||
searchParams,
|
||||
}: {
|
||||
searchParams: any;
|
||||
}) {
|
||||
if (!(await checkAccessTokenIsValid())) {
|
||||
redirect("/login/email");
|
||||
}
|
||||
|
||||
const buildKey = "building";
|
||||
const searchParamsKeys = await searchParams;
|
||||
const endpointUrl = "/building/build/create";
|
||||
|
||||
const queryEncrypt = await decryptQuery(searchParamsKeys?.q);
|
||||
const endpointAvailable = await retrieveAvailableEndpoint(endpointUrl);
|
||||
const validateAndHeaders = await retrieveHeadersAndValidationByEndpoint({
|
||||
endpoint: endpointUrl,
|
||||
});
|
||||
const validator = validateAndHeaders?.validated || {};
|
||||
const headers = validateAndHeaders?.headers || {};
|
||||
console.log("validateAndHeaders", validateAndHeaders);
|
||||
console.log("endpointAvailable", endpointAvailable);
|
||||
console.log("queryEncrypt", queryEncrypt);
|
||||
return (
|
||||
<div>
|
||||
<h1>Create Building</h1>
|
||||
<h1>{JSON.stringify(queryEncrypt)}</h1>
|
||||
<CreatePageComponent validator={validator} headers={headers} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
159
oldCode/building/page.tsx
Normal file
159
oldCode/building/page.tsx
Normal file
@@ -0,0 +1,159 @@
|
||||
"use server";
|
||||
import React from "react";
|
||||
import Link from "next/link";
|
||||
import { redirect } from "next/navigation";
|
||||
import { RefreshCcw, PlusCircle } from "lucide-react";
|
||||
import {
|
||||
decryptQuery,
|
||||
defaultPagination,
|
||||
handleFormSubmission,
|
||||
} from "@/apicalls/test";
|
||||
import { TableComponent } from "@/components/commons/Table";
|
||||
import Pagination from "@/components/commons/pagination";
|
||||
import MainBodyWithHeader from "@/components/defaultLayout/MainBodyWithHeader";
|
||||
import {
|
||||
createBuild,
|
||||
retrieveBuildList,
|
||||
updateBuild,
|
||||
} from "@/apicalls/building/build";
|
||||
import {
|
||||
checkAccessTokenIsValid,
|
||||
retrieveUserSelection,
|
||||
} from "@/apicalls/cookies/token";
|
||||
import { retrievePageInfoByComponentName } from "@/hooks/retrievePageInfoByComponentName";
|
||||
import { checkPageAvaliable } from "@/hooks/checkpageAvaliable";
|
||||
|
||||
const BuildinPage = async ({ searchParams }: { searchParams: any }) => {
|
||||
const buildKey = "building";
|
||||
const pageName = "BuildingPage";
|
||||
const searchParamsKeys = await searchParams;
|
||||
|
||||
if (!searchParamsKeys?.q) {
|
||||
const defaultURL = await defaultPagination();
|
||||
redirect(`/${buildKey}?q=${defaultURL}`);
|
||||
}
|
||||
const queryEncrypt = await decryptQuery(searchParamsKeys?.q);
|
||||
if (!(await checkAccessTokenIsValid())) {
|
||||
redirect("/login/email");
|
||||
}
|
||||
const user = await retrieveUserSelection();
|
||||
|
||||
const tableValues = {
|
||||
endpoint: "building/build/list",
|
||||
name: "table",
|
||||
url: "/building",
|
||||
function: retrieveBuildList,
|
||||
data: [],
|
||||
headers: {},
|
||||
validation: {},
|
||||
};
|
||||
const createValues = {
|
||||
endpoint: "building/build/create",
|
||||
name: "create",
|
||||
url: "/building/create",
|
||||
function: createBuild,
|
||||
data: [],
|
||||
headers: {},
|
||||
validation: {},
|
||||
};
|
||||
const updateValues = {
|
||||
endpoint: "building/build/update/{build_uu_id}",
|
||||
function: updateBuild,
|
||||
name: "update",
|
||||
url: "/building/update",
|
||||
data: [],
|
||||
headers: {},
|
||||
validation: {},
|
||||
};
|
||||
|
||||
let restrictions: any = {
|
||||
update: updateValues,
|
||||
create: createValues,
|
||||
table: tableValues,
|
||||
};
|
||||
|
||||
if (!user?.lang) {
|
||||
return (
|
||||
<MainBodyWithHeader
|
||||
children={<h1>User selection is not successfully retrieved.</h1>}
|
||||
/>
|
||||
);
|
||||
} else {
|
||||
const pageContent = retrievePageInfoByComponentName(pageName, user?.lang);
|
||||
const restrictionsChecked = await checkPageAvaliable({
|
||||
pageContent,
|
||||
restrictions,
|
||||
queryEncrypt,
|
||||
});
|
||||
|
||||
if (!restrictionsChecked || !restrictionsChecked?.table) {
|
||||
return (
|
||||
<MainBodyWithHeader
|
||||
children={<h1>This user does not have access to this page.</h1>}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
const BuildingPage = (
|
||||
<div className="p-4 overflow-hidden">
|
||||
<h1 className="text-2xl font-bold mb-4 ">Dashboard</h1>
|
||||
<form
|
||||
action={handleFormSubmission}
|
||||
className="bg-white p-4 rounded-lg shadow"
|
||||
>
|
||||
<div className="grid gap-4">
|
||||
<p>Welcome to your dashboard</p>
|
||||
{restrictionsChecked?.create && (
|
||||
<Link
|
||||
href={"/building/create"}
|
||||
className="flex items-center justify-center gap-2 px-4 py-2 bg-slate-500 text-white rounded hover:bg-slate-700"
|
||||
>
|
||||
<PlusCircle size={16} />
|
||||
Create
|
||||
</Link>
|
||||
)}
|
||||
<h1>{JSON.stringify(queryEncrypt)}</h1>
|
||||
|
||||
{restrictionsChecked && (
|
||||
<div>
|
||||
<input type="hidden" name="section" value={buildKey} readOnly />
|
||||
<TableComponent
|
||||
pageContent={pageContent}
|
||||
tableValidateAndHeaders={restrictionsChecked?.table}
|
||||
apiFunction={retrieveBuildList}
|
||||
redirectTo={"/building/update"}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
className="flex items-center justify-center gap-2 px-4 py-2 bg-slate-500 text-white rounded hover:bg-slate-700"
|
||||
>
|
||||
<RefreshCcw size={16} />
|
||||
Search
|
||||
</button>
|
||||
<Pagination
|
||||
size={parseInt(queryEncrypt?.size || "10")}
|
||||
page={parseInt(queryEncrypt?.page || "1")}
|
||||
orderBy={queryEncrypt?.orderBy || "id"}
|
||||
orderType={queryEncrypt?.orderType || "asc"}
|
||||
totalPage={3}
|
||||
/>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
);
|
||||
return (
|
||||
<>
|
||||
<MainBodyWithHeader
|
||||
children={BuildingPage}
|
||||
section={`/${buildKey}`}
|
||||
profileInfo={user}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
export default BuildinPage;
|
||||
55
oldCode/building/update/page.tsx
Normal file
55
oldCode/building/update/page.tsx
Normal file
@@ -0,0 +1,55 @@
|
||||
"use server";
|
||||
import { redirect } from "next/navigation";
|
||||
|
||||
import { updateBuild } from "@/apicalls/building/build";
|
||||
import { retrieveAvailableEndpoint } from "@/apicalls/checkEndpoint";
|
||||
import { checkAccessTokenIsValid } from "@/apicalls/cookies/token";
|
||||
import { decryptQuery, defaultPagination } from "@/apicalls/test";
|
||||
import { retrieveHeadersAndValidationByEndpoint } from "@/apicalls/validations/validations";
|
||||
|
||||
import { RetrieveInputByType } from "@/hooks/renderInputWithValidation";
|
||||
|
||||
import React from "react";
|
||||
import UpdatePageComponent from "@/components/commons/UpdatePage";
|
||||
|
||||
export default async function BuildingUpdatePage({
|
||||
searchParams,
|
||||
}: {
|
||||
searchParams: any;
|
||||
}) {
|
||||
if (!(await checkAccessTokenIsValid())) {
|
||||
redirect("/login/email");
|
||||
}
|
||||
|
||||
const buildKey = "building/update";
|
||||
const searchParamsKeys = await searchParams;
|
||||
const endpointUrl = "building/build/update/{build_uu_id}";
|
||||
if (!searchParamsKeys?.q) {
|
||||
redirect(`/${buildKey}`);
|
||||
}
|
||||
const queryEncrypt = await decryptQuery(searchParamsKeys?.q);
|
||||
const endpointAvailable = await retrieveAvailableEndpoint(endpointUrl);
|
||||
const validateAndHeaders = await retrieveHeadersAndValidationByEndpoint({
|
||||
endpoint: endpointUrl,
|
||||
});
|
||||
const validator = validateAndHeaders?.validated || {};
|
||||
const headers = validateAndHeaders?.headers || {};
|
||||
console.log("endpointAvailable", endpointAvailable);
|
||||
console.log("validator", validator);
|
||||
console.log("headers", headers);
|
||||
console.log("queryEncrypt", queryEncrypt);
|
||||
|
||||
return (
|
||||
<div>
|
||||
<h1>Update Building</h1>
|
||||
<h1>{JSON.stringify(queryEncrypt)}</h1>
|
||||
|
||||
<UpdatePageComponent
|
||||
validator={validator}
|
||||
headers={headers}
|
||||
queryEncrypt={queryEncrypt}
|
||||
commitFunction={updateBuild}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
14
oldCode/loading.tsx
Normal file
14
oldCode/loading.tsx
Normal file
@@ -0,0 +1,14 @@
|
||||
"use server";
|
||||
|
||||
export default async function Loading() {
|
||||
return (
|
||||
<>
|
||||
<div className="flex-col items-center justify-center text-center">
|
||||
<h1 className="my-8">Page is being loaded</h1>
|
||||
<div className="loading-container">
|
||||
<div className="spinner"></div>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
50
oldCode/pageTemplate.tsx
Normal file
50
oldCode/pageTemplate.tsx
Normal file
@@ -0,0 +1,50 @@
|
||||
const BuildingPage = (
|
||||
<div className="p-4 overflow-hidden">
|
||||
<h1 className="text-2xl font-bold mb-4 ">Dashboard</h1>
|
||||
<form
|
||||
action={handleFormSubmission}
|
||||
className="bg-white p-4 rounded-lg shadow"
|
||||
>
|
||||
<div className="grid gap-4">
|
||||
<p>Welcome to your dashboard</p>
|
||||
{restrictionsChecked?.create && (
|
||||
<Link
|
||||
href={"/building/create"}
|
||||
className="flex items-center justify-center gap-2 px-4 py-2 bg-slate-500 text-white rounded hover:bg-slate-700"
|
||||
>
|
||||
<PlusCircle size={16} />
|
||||
Create
|
||||
</Link>
|
||||
)}
|
||||
<h1>{JSON.stringify(queryEncrypt)}</h1>
|
||||
|
||||
{restrictionsChecked && (
|
||||
<>
|
||||
<input type="hidden" name="section" value={buildKey} readOnly />
|
||||
<TableComponent
|
||||
restrictions={restrictionsChecked}
|
||||
query={queryEncrypt?.query || {}}
|
||||
orderByValue={queryEncrypt?.orderBy || "uu_id"}
|
||||
orderTypeValue={queryEncrypt?.orderType || "asc"}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
className="flex items-center justify-center gap-2 px-4 py-2 bg-slate-500 text-white rounded hover:bg-slate-700"
|
||||
>
|
||||
<RefreshCcw size={16} />
|
||||
Search
|
||||
</button>
|
||||
<Pagination
|
||||
size={parseInt(queryEncrypt?.size || "10")}
|
||||
page={parseInt(queryEncrypt?.page || "1")}
|
||||
orderBy={queryEncrypt?.orderBy || "id"}
|
||||
orderType={queryEncrypt?.orderType || "asc"}
|
||||
totalPage={3}
|
||||
/>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
);
|
||||
210
oldCode/table.tsx
Normal file
210
oldCode/table.tsx
Normal file
@@ -0,0 +1,210 @@
|
||||
"use client";
|
||||
import React from "react";
|
||||
import {
|
||||
useReactTable,
|
||||
flexRender,
|
||||
getCoreRowModel,
|
||||
createColumnHelper,
|
||||
} from "@tanstack/react-table";
|
||||
import {
|
||||
Table,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableHead,
|
||||
TableHeader,
|
||||
TableRow,
|
||||
TableFooter,
|
||||
} from "@/components/ui/table";
|
||||
|
||||
import { getIconByName } from "@/Icons/icons";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { encryptQuery, handleUpdateSubmission } from "@/apicalls/test";
|
||||
import SingleTableHeader from "./SingleTableHeader";
|
||||
|
||||
interface TableComponentInterFace {
|
||||
restrictions: any;
|
||||
query: any;
|
||||
orderByValue: string;
|
||||
orderTypeValue: string;
|
||||
}
|
||||
const TableComponent: React.FC<TableComponentInterFace> = ({
|
||||
restrictions,
|
||||
query,
|
||||
orderByValue,
|
||||
orderTypeValue,
|
||||
}) => {
|
||||
const router = useRouter();
|
||||
const [updateRow, setUpdateRow] = React.useState<any>(null);
|
||||
const [columns, setColumns] = React.useState<any[]>([]);
|
||||
const [orderBy, setOrderBy] = React.useState<"asc" | "desc">(
|
||||
orderByValue as "asc" | "desc"
|
||||
);
|
||||
const [orderColumn, setOrderColumn] = React.useState<string>(orderTypeValue);
|
||||
|
||||
const columnHelper = createColumnHelper();
|
||||
const table = useReactTable({
|
||||
data: restrictions.table?.data?.data || [],
|
||||
columns,
|
||||
getCoreRowModel: getCoreRowModel(),
|
||||
});
|
||||
|
||||
React.useEffect(() => {
|
||||
if (restrictions?.table?.headers) {
|
||||
setColumns(createColumnsFromValidations(restrictions.table.headers));
|
||||
}
|
||||
}, [restrictions.table.headers]);
|
||||
|
||||
React.useEffect(() => {
|
||||
if (updateRow) {
|
||||
encryptQuery(updateRow).then((encryptData) => {
|
||||
router.push(`/building/update?q=${encryptData.replaceAll(" ", "+")}`);
|
||||
});
|
||||
}
|
||||
}, [updateRow]);
|
||||
|
||||
function createColumnsFromValidations(headers: any) {
|
||||
const columns = Object.entries(headers).map(([key]: [string, any]) => {
|
||||
return columnHelper.accessor(key, {
|
||||
id: key,
|
||||
footer: headers[key],
|
||||
header: () => <span>{headers[key]}</span>,
|
||||
cell: (info) => <span>{info.getValue()}</span>,
|
||||
});
|
||||
});
|
||||
if (restrictions?.update) {
|
||||
columns.push(
|
||||
columnHelper.accessor("update", {
|
||||
id: "update",
|
||||
footer: "Update",
|
||||
header: () => <span>Update</span>,
|
||||
cell: () => (
|
||||
<div className="w-8 h-8 cursor-pointer">
|
||||
{restrictions?.update.icon &&
|
||||
React.createElement(getIconByName(restrictions?.update.icon))}
|
||||
</div>
|
||||
),
|
||||
})
|
||||
);
|
||||
}
|
||||
return columns;
|
||||
}
|
||||
|
||||
function changeOrderState(headerID: string) {
|
||||
console.log("changeOrderState", headerID, orderColumn, orderBy);
|
||||
if (orderColumn === headerID) {
|
||||
setOrderBy(orderBy === "asc" ? "desc" : "asc");
|
||||
} else {
|
||||
setOrderColumn(headerID);
|
||||
setOrderBy("asc");
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="w-full min-w-full gap-4 mb-4">
|
||||
<h1>{JSON.stringify(updateRow)}</h1>
|
||||
<input
|
||||
hidden
|
||||
type="text"
|
||||
key="orderBy"
|
||||
name="orderBy"
|
||||
defaultValue={orderBy}
|
||||
readOnly
|
||||
/>
|
||||
<input
|
||||
hidden
|
||||
type="text"
|
||||
key="orderType"
|
||||
name="orderType"
|
||||
defaultValue={orderColumn}
|
||||
readOnly
|
||||
/>
|
||||
<Table className="px-8">
|
||||
<TableHeader>
|
||||
{table.getHeaderGroups().map((headerGroup) => (
|
||||
<TableRow key={headerGroup.id}>
|
||||
{headerGroup.headers.map((header) => (
|
||||
<TableHead key={header.id} className="relative">
|
||||
<SingleTableHeader
|
||||
header={header}
|
||||
orderBy={orderBy}
|
||||
orderColumn={orderColumn}
|
||||
changeOrderState={changeOrderState}
|
||||
/>
|
||||
</TableHead>
|
||||
))}
|
||||
</TableRow>
|
||||
))}
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{table.getRowModel().rows?.length ? (
|
||||
table.getRowModel().rows.map((row) => (
|
||||
<TableRow
|
||||
key={row.id}
|
||||
data-state={row.getIsSelected() && "selected"}
|
||||
>
|
||||
{row.getVisibleCells().map((cell) =>
|
||||
cell.column.id !== "update" ? (
|
||||
<TableCell key={cell.id}>
|
||||
{flexRender(
|
||||
cell.column.columnDef.cell,
|
||||
cell.getContext()
|
||||
)}
|
||||
</TableCell>
|
||||
) : (
|
||||
<TableCell
|
||||
key={cell.id}
|
||||
onClick={() => setUpdateRow(row.original)}
|
||||
>
|
||||
{flexRender(
|
||||
cell.column.columnDef.cell,
|
||||
cell.getContext()
|
||||
)}
|
||||
</TableCell>
|
||||
)
|
||||
)}
|
||||
</TableRow>
|
||||
))
|
||||
) : (
|
||||
<TableRow>
|
||||
<TableCell
|
||||
colSpan={columns.length}
|
||||
className="h-24 text-center"
|
||||
>
|
||||
No results.
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
)}
|
||||
</TableBody>
|
||||
<TableFooter>
|
||||
{table.getFooterGroups().map((footerGroup) => (
|
||||
<TableRow key={footerGroup.id}>
|
||||
{footerGroup.headers.map(
|
||||
(footer) =>
|
||||
footer.id !== "update" && (
|
||||
<TableCell key={footer.id}>
|
||||
{footer.isPlaceholder ? null : (
|
||||
<input
|
||||
key={footer.id}
|
||||
name={footer.id}
|
||||
type="text"
|
||||
className="w-full text-center border p-1 text-sm h-8"
|
||||
placeholder={`${String(
|
||||
footer.column.columnDef.footer
|
||||
)}`}
|
||||
defaultValue={query[footer.id] || ""}
|
||||
/>
|
||||
)}
|
||||
</TableCell>
|
||||
)
|
||||
)}
|
||||
</TableRow>
|
||||
))}
|
||||
</TableFooter>
|
||||
</Table>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default TableComponent;
|
||||
Reference in New Issue
Block a user