From 0d2b37be007fabb78c8b71dbacb796beac4352aa Mon Sep 17 00:00:00 2001 From: Purvesh Date: Sun, 9 Jun 2024 19:43:12 +1200 Subject: [PATCH 1/2] page table --- react-admin/src/pages/page/PageCreate.jsx | 560 +++++++++--------- react-admin/src/pages/page/PageTable.jsx | 98 --- react-admin/src/pages/page/PageTable.tsx | 99 ++++ ...{useComponentAll.js => useComponentAll.ts} | 0 .../hooks/{useGetPage.js => useGetPage.ts} | 2 +- .../{usePageTable.js => usePageTable.ts} | 0 .../{useStorePage.js => useStorePage.ts} | 0 .../{useUpdatePage.js => useUpdatePage.ts} | 2 +- react-admin/src/types/page/IPageModel.ts | 10 + 9 files changed, 391 insertions(+), 380 deletions(-) delete mode 100644 react-admin/src/pages/page/PageTable.jsx create mode 100644 react-admin/src/pages/page/PageTable.tsx rename react-admin/src/pages/page/hooks/{useComponentAll.js => useComponentAll.ts} (100%) rename react-admin/src/pages/page/hooks/{useGetPage.js => useGetPage.ts} (92%) rename react-admin/src/pages/page/hooks/{usePageTable.js => usePageTable.ts} (100%) rename react-admin/src/pages/page/hooks/{useStorePage.js => useStorePage.ts} (100%) rename react-admin/src/pages/page/hooks/{useUpdatePage.js => useUpdatePage.ts} (91%) create mode 100644 react-admin/src/types/page/IPageModel.ts diff --git a/react-admin/src/pages/page/PageCreate.jsx b/react-admin/src/pages/page/PageCreate.jsx index dad6bf48..a02534ef 100644 --- a/react-admin/src/pages/page/PageCreate.jsx +++ b/react-admin/src/pages/page/PageCreate.jsx @@ -1,320 +1,320 @@ -import { useState } from "react"; -import { Link } from "react-router-dom"; -import { PlusIcon } from "@heroicons/react/24/solid"; +import {useState} from "react"; +import {Link} from "react-router-dom"; +import {PlusIcon} from "@heroicons/react/24/solid"; import AvoredModal from "../../components/AvoredModal"; import InputField from "../../components/InputField"; import _ from "lodash"; -import { useComponentAll } from "./hooks/useComponentAll"; -import { useStorePage } from "./hooks/useStorePage"; -import { useTranslation } from "react-i18next"; +import {useComponentAll} from "./hooks/useComponentAll"; +import {useStorePage} from "./hooks/useStorePage"; +import {useTranslation} from "react-i18next"; function PageCreate() { - const [isComponentTableModalOpen, setIsComponentTableModalOpen] = - useState(false); - const [pageComponents, setPageComponents] = useState([]); - const [page, setPage] = useState({}); - const [t] = useTranslation("global"); + const [isComponentTableModalOpen, setIsComponentTableModalOpen] = + useState(false); + const [pageComponents, setPageComponents] = useState([]); + const [page, setPage] = useState({}); + const [t] = useTranslation("global"); - const component_all_api_response = useComponentAll(); - const components = _.get(component_all_api_response, "data.data", []); + const component_all_api_response = useComponentAll(); + const components = _.get(component_all_api_response, "data.data", []); - const { mutate } = useStorePage(); + const {mutate} = useStorePage(); - const getFormattedDate = (date) => { - var date_obj = new Date(date); - return `${date_obj.getFullYear()}-${ - date_obj.getMonth() + 1 - }-${date_obj.getDate()}`; - }; + const getFormattedDate = (date) => { + var date_obj = new Date(date); + return `${date_obj.getFullYear()}-${ + date_obj.getMonth() + 1 + }-${date_obj.getDate()}`; + }; - const renderComponentFieldType = (componentField) => { - switch (componentField.field_type) { - case "textarea": - return
Textarea
; - case "radio": - return ( -
- - -
- ); - case "text": - default: + const renderComponentFieldType = (componentField) => { + switch (componentField.field_type) { + case "textarea": + return
Textarea
; + case "radio": + return ( +
+ + +
+ ); + case "text": + default: + return ( +
+ + componentFieldContentOnChange(componentField.id, e.target.value) + } + /> +
+ ); + } + }; + + const renderComponentField = (componentField) => { return ( -
- - componentFieldContentOnChange(componentField.id, e.target.value) - } - /> +
+ {renderComponentFieldType(componentField)}
); - } - }; - - const renderComponentField = (componentField) => { - return ( -
- {renderComponentFieldType(componentField)} -
- ); - }; + }; - const componentSelected = (e, componentId) => { - e.preventDefault(); - const selectedComponent = components.find( - (component) => component.id === componentId - ); - - pageAddComponentSelected(selectedComponent); - setIsComponentTableModalOpen(false); + const componentSelected = (e, componentId) => { + e.preventDefault(); + const selectedComponent = components.find( + (component) => component.id === componentId + ); - setPageComponents((pageComponents) => [ - ...pageComponents, - selectedComponent, - ]); - }; + pageAddComponentSelected(selectedComponent); + setIsComponentTableModalOpen(false); - const addComponentOnClick = () => { - setIsComponentTableModalOpen(true); - }; + setPageComponents((pageComponents) => [ + ...pageComponents, + selectedComponent, + ]); + }; - const pageModelOnClose = () => { - setIsComponentTableModalOpen(false); - }; + const addComponentOnClick = () => { + setIsComponentTableModalOpen(true); + }; - const pageNameOnChange = (value) => { - setPage({ - ...page, - name: value, - }); - }; - const pageIdentifierOnChange = (value) => { - setPage({ - ...page, - identifier: value, - }); - }; - const pageAddComponentSelected = (component) => { - var componentContent = {}; - componentContent.id = component.id; - componentContent.name = component.name; - componentContent.identifier = component.identifier; - componentContent.component_fields_content = []; + const pageModelOnClose = () => { + setIsComponentTableModalOpen(false); + }; - component.fields.forEach((field) => { - var componentFieldContent = {}; + const pageNameOnChange = (value) => { + setPage({ + ...page, + name: value, + }); + }; + const pageIdentifierOnChange = (value) => { + setPage({ + ...page, + identifier: value, + }); + }; + const pageAddComponentSelected = (component) => { + var componentContent = {}; + componentContent.id = component.id; + componentContent.name = component.name; + componentContent.identifier = component.identifier; + componentContent.component_fields_content = []; - componentFieldContent.id = field.id; - componentFieldContent.name = field.name; - componentFieldContent.identifier = field.identifier; - componentFieldContent.field_type = field.field_type; - componentFieldContent.field_content = ""; + component.fields.forEach((field) => { + var componentFieldContent = {}; - componentContent.component_fields_content.push(componentFieldContent); - }); + componentFieldContent.id = field.id; + componentFieldContent.name = field.name; + componentFieldContent.identifier = field.identifier; + componentFieldContent.field_type = field.field_type; + componentFieldContent.field_content = ""; - if (_.isEmpty(_.get(page, "components_content"))) { - page["components_content"] = []; - } - page.components_content.push(componentContent); - }; + componentContent.component_fields_content.push(componentFieldContent); + }); - const componentFieldContentOnChange = (componentFieldId, value) => { - page.components_content.forEach((componentContent) => { - componentContent.component_fields_content.forEach( - // Removed variable "componentField" - (componentFieldContent) => { - if (componentFieldContent.id === componentFieldId) { - componentFieldContent.field_content = value; - } + if (_.isEmpty(_.get(page, "components_content"))) { + page["components_content"] = []; } - ); - }); - const updatedComponentContent = page.components_content; + page.components_content.push(componentContent); + }; - setPage({ - ...page, - components_content: updatedComponentContent, - }); - }; + const componentFieldContentOnChange = (componentFieldId, value) => { + page.components_content.forEach((componentContent) => { + componentContent.component_fields_content.forEach( + // Removed variable "componentField" + (componentFieldContent) => { + if (componentFieldContent.id === componentFieldId) { + componentFieldContent.field_content = value; + } + } + ); + }); + const updatedComponentContent = page.components_content; - const renderComponent = (pageComponent) => { - return ( -
-
component name: {pageComponent.name}
-
component identifier: {pageComponent.identifier}
-
- Component Fields - {pageComponent.fields.map((componentField) => { - return renderComponentField(componentField); - })} -
-
- ); - }; + setPage({ + ...page, + components_content: updatedComponentContent, + }); + }; - const handleSubmit = async (e) => { - e.preventDefault(); + const renderComponent = (pageComponent) => { + return ( +
+
component name: {pageComponent.name}
+
component identifier: {pageComponent.identifier}
+
+ Component Fields + {pageComponent.fields.map((componentField) => { + return renderComponentField(componentField); + })} +
+
+ ); + }; - mutate(page); - }; + const handleSubmit = async (e) => { + e.preventDefault(); - return ( -
-
-
-
-

- {t("pages.information")} -

- {/*

Use a permanent address where you can*/} - {/* receive mail.

*/} -
-
- pageNameOnChange(e.target.value)} - className="border p-2 rounded w-full" - /> -
-
- pageIdentifierOnChange(e.target.value)} - className="border p-2 rounded w-full" - /> -
+ mutate(page); + }; -
- {pageComponents.map((pageComponent) => { - return renderComponent(pageComponent); - })} -
+ return ( +
+
+
+
+

+ {t("pages.information")} +

+ {/*

Use a permanent address where you can*/} + {/* receive mail.

*/} + +
+ pageNameOnChange(e.target.value)} + className="border p-2 rounded w-full" + /> +
+
+ pageIdentifierOnChange(e.target.value)} + className="border p-2 rounded w-full" + /> +
-
- -
+ +
+ + + + + + + + + + + + + + + + + {components.map((component) => { + return ( + + + + + + + + + + + ); + })} + +
+ {t("common.id")} + + {t("common.name")} + + {t("common.identifier")} + + {t("common.created_at")} + + {t("common.updated_at")} + + {t("common.created_by")} + + {t("common.updated_by")} + + {t("common.action")} +
{component.id}{component.name} + {component.identifier} + + {getFormattedDate(component.created_at)} + + {getFormattedDate(component.updated_at)} + + {component.created_by} + + {component.updated_by} + + +
+
+ } + isOpen={isComponentTableModalOpen} + > - - - - - - - - - - - - - - - - {components.map((component) => { - return ( - - - - - - - - - - - ); - })} - -
- {t("common.id")} - - {t("common.name")} - - {t("common.identifier")} - - {t("common.created_at")} - - {t("common.updated_at")} - - {t("common.created_by")} - - {t("common.updated_by")} - - {t("common.action")} -
{component.id}{component.name} - {component.identifier} - - {getFormattedDate(component.created_at)} - - {getFormattedDate(component.updated_at)} - - {component.created_by} - - {component.updated_by} - +
-
-
- } - isOpen={isComponentTableModalOpen} - > - -
- - - {t("common.cancel")} - -
- -
+ + {t("common.cancel")} + +
+ +
+
+
- - - ); + ); } export default PageCreate; diff --git a/react-admin/src/pages/page/PageTable.jsx b/react-admin/src/pages/page/PageTable.jsx deleted file mode 100644 index dbeafe70..00000000 --- a/react-admin/src/pages/page/PageTable.jsx +++ /dev/null @@ -1,98 +0,0 @@ -import {Link} from "react-router-dom" -import _ from 'lodash' -import {usePageTable} from "./hooks/usePageTable" -import { useTranslation } from "react-i18next" - -function PageTable() { - const page_api_table_response = usePageTable() - const pages = _.get(page_api_table_response, 'data.data.data', []) - const [t] = useTranslation("global") - - const getFormattedDate = ((date) => { - const date_obj = new Date(date); - - return `${date_obj.getFullYear()}-${("0" + (date_obj.getMonth() + 1)).slice("-2")}-${("0" + date_obj.getDate()).slice("-2")}`; - }) - - - - return ( -
-
-
-
- {t("pages.pages")} -
- - {t("common.create")} - -
- -
- - - - - - - - - - - - - - - {pages.map((page) => { - return ( - - - - - - - - - - - ); - })} - -
- {t("common.id")} - - {t("common.name")} - - {t("common.identifier")} - - {t("common.created_at")} - - {t("common.updated_at")} - - {t("common.created_by")} - - {t("common.updated_by")} - - {t("common.action")} -
{page.id}{page.name}{page.identifier} - {getFormattedDate(page.created_at)} - - {getFormattedDate(page.updated_at)} - {page.created_by}{page.updated_by} - - {t("common.edit")} - -
-
-
-
- ); -} - -export default PageTable \ No newline at end of file diff --git a/react-admin/src/pages/page/PageTable.tsx b/react-admin/src/pages/page/PageTable.tsx new file mode 100644 index 00000000..93be0103 --- /dev/null +++ b/react-admin/src/pages/page/PageTable.tsx @@ -0,0 +1,99 @@ +import {Link} from "react-router-dom" +import _ from 'lodash' +import {usePageTable} from "./hooks/usePageTable" +import {useTranslation} from "react-i18next" +import {useRoleTable} from "../role/hooks/useRoleTable"; +import IRoleModel from "../../types/admin-user/IRoleModel"; +import {createColumnHelper, getCoreRowModel, useReactTable} from "@tanstack/react-table"; +import {getFormattedDate} from "../../lib/common"; +import IPageModel from "../../types/page/IPageModel"; +import AvoRedTable from "../../components/AvoRedTable"; + +function PageTable() { + const [t] = useTranslation("global") + const page_api_table_response = useRoleTable(); + const pages: Array = _.get(page_api_table_response, 'data.data.data', []) + + const columnHelper = createColumnHelper() + const columns = [ + columnHelper.accessor('id', { + cell: info => info.getValue(), + header: t("common.id") + }), + columnHelper.accessor('name', { + cell: info => info.getValue(), + header: t("common.name") + }), + columnHelper.accessor('identifier', { + cell: info => info.getValue(), + header: t("common.identifier") + }), + columnHelper.accessor('created_at', { + id: "created_at", + cell: info => getFormattedDate(info.getValue()), + header: t("common.created_at") + }), + columnHelper.accessor('created_by', { + cell: info => info.getValue(), + header: t("common.created_by") + }), + columnHelper.accessor('updated_at', { + cell: info => getFormattedDate(info.getValue()), + header: t("common.updated_at") + }), + columnHelper.accessor('updated_by', { + cell: info => info.getValue(), + header: t("common.updated_by") + }), + columnHelper.accessor('action', { + cell: info => { + return ( + + {t("common.edit")} + + ) + }, + header: t("common.action"), + enableHiding: false + }), + ] + + const table = useReactTable({ + data: pages, + columns, + getCoreRowModel: getCoreRowModel(), + initialState: { + columnVisibility: { + created_at: false, + created_by: false + } + } + }) + + return ( +
+
+
+
+ {t("pages.pages")} +
+ + {t("common.create")} + +
+ +
+ +
+
+
+ ); +} + +export default PageTable \ No newline at end of file diff --git a/react-admin/src/pages/page/hooks/useComponentAll.js b/react-admin/src/pages/page/hooks/useComponentAll.ts similarity index 100% rename from react-admin/src/pages/page/hooks/useComponentAll.js rename to react-admin/src/pages/page/hooks/useComponentAll.ts diff --git a/react-admin/src/pages/page/hooks/useGetPage.js b/react-admin/src/pages/page/hooks/useGetPage.ts similarity index 92% rename from react-admin/src/pages/page/hooks/useGetPage.js rename to react-admin/src/pages/page/hooks/useGetPage.ts index e45581dc..4c1ff767 100644 --- a/react-admin/src/pages/page/hooks/useGetPage.js +++ b/react-admin/src/pages/page/hooks/useGetPage.ts @@ -3,7 +3,7 @@ import { useAxios } from '../../../hooks/useAxios' import _ from 'lodash' import {useNavigate} from 'react-router-dom' -export const useGetPage = (page_id) => { +export const useGetPage = (page_id: string) => { const client = useAxios() const redirect = useNavigate() diff --git a/react-admin/src/pages/page/hooks/usePageTable.js b/react-admin/src/pages/page/hooks/usePageTable.ts similarity index 100% rename from react-admin/src/pages/page/hooks/usePageTable.js rename to react-admin/src/pages/page/hooks/usePageTable.ts diff --git a/react-admin/src/pages/page/hooks/useStorePage.js b/react-admin/src/pages/page/hooks/useStorePage.ts similarity index 100% rename from react-admin/src/pages/page/hooks/useStorePage.js rename to react-admin/src/pages/page/hooks/useStorePage.ts diff --git a/react-admin/src/pages/page/hooks/useUpdatePage.js b/react-admin/src/pages/page/hooks/useUpdatePage.ts similarity index 91% rename from react-admin/src/pages/page/hooks/useUpdatePage.js rename to react-admin/src/pages/page/hooks/useUpdatePage.ts index c04aaf6e..86bc75c6 100644 --- a/react-admin/src/pages/page/hooks/useUpdatePage.js +++ b/react-admin/src/pages/page/hooks/useUpdatePage.ts @@ -3,7 +3,7 @@ import { useAxios } from '../../../hooks/useAxios' import _ from 'lodash' import {useNavigate} from 'react-router-dom' -export const useUpdatePage = (page_id) => { +export const useUpdatePage = (page_id: string) => { const client = useAxios(); const redirect = useNavigate(); return useMutation({ diff --git a/react-admin/src/types/page/IPageModel.ts b/react-admin/src/types/page/IPageModel.ts new file mode 100644 index 00000000..dfc77c54 --- /dev/null +++ b/react-admin/src/types/page/IPageModel.ts @@ -0,0 +1,10 @@ +export default interface IPageModel { + id: string; + name: string; + identifier: string; + created_at: string; + created_by: string; + updated_at: string; + updated_by: string; + action: string; +} \ No newline at end of file From 2575505e7b9e07858fb93b1a24e8541732380d20 Mon Sep 17 00:00:00 2001 From: Purvesh Date: Sun, 9 Jun 2024 20:03:22 +1200 Subject: [PATCH 2/2] page create ts in progress --- .../src/pages/page/PageComponentTable.tsx | 86 ++++++++++++++ react-admin/src/pages/page/PageCreate.jsx | 108 +++++------------- .../pages/page/schemas/page.create.schema.ts | 10 ++ .../pages/role/schemas/role.create.schema.ts | 12 +- .../src/types/component/IComponentModel.ts | 9 ++ 5 files changed, 139 insertions(+), 86 deletions(-) create mode 100644 react-admin/src/pages/page/PageComponentTable.tsx create mode 100644 react-admin/src/pages/page/schemas/page.create.schema.ts create mode 100644 react-admin/src/types/component/IComponentModel.ts diff --git a/react-admin/src/pages/page/PageComponentTable.tsx b/react-admin/src/pages/page/PageComponentTable.tsx new file mode 100644 index 00000000..28c75df2 --- /dev/null +++ b/react-admin/src/pages/page/PageComponentTable.tsx @@ -0,0 +1,86 @@ +import {useTranslation} from "react-i18next"; +import {getFormattedDate} from "../../lib/common"; +import IComponentModel from "../../types/component/IComponentModel"; + +interface ISelectedComponentFunction { + (e: React.MouseEvent, id: string): void +} + +interface IPageComponentProps { + components: Array; + componentSelected: ISelectedComponentFunction +} + +function PageComponentTable(props: IPageComponentProps) { + const [t] = useTranslation("global"); + return ( + + + + + + + + + + + + + + + {props.components.map((component) => { + return ( + + + + + + + + + + + ); + })} + +
+ {t("common.id")} + + {t("common.name")} + + {t("common.identifier")} + + {t("common.created_at")} + + {t("common.updated_at")} + + {t("common.created_by")} + + {t("common.updated_by")} + + {t("common.action")} +
{component.id}{component.name} + {component.identifier} + + {getFormattedDate(component.created_at)} + + {getFormattedDate(component.updated_at)} + + {component.created_by} + + {component.updated_by} + + +
+ ) +} + +export default PageComponentTable \ No newline at end of file diff --git a/react-admin/src/pages/page/PageCreate.jsx b/react-admin/src/pages/page/PageCreate.jsx index a02534ef..cb289f6f 100644 --- a/react-admin/src/pages/page/PageCreate.jsx +++ b/react-admin/src/pages/page/PageCreate.jsx @@ -7,6 +7,10 @@ import _ from "lodash"; import {useComponentAll} from "./hooks/useComponentAll"; import {useStorePage} from "./hooks/useStorePage"; import {useTranslation} from "react-i18next"; +import {useForm} from "react-hook-form"; +import {joiResolver} from "@hookform/resolvers/joi"; +import {PageCreateSchema} from "./schemas/page.create.schema" +import PageComponentTable from "./PageComponentTable"; function PageCreate() { const [isComponentTableModalOpen, setIsComponentTableModalOpen] = @@ -15,6 +19,17 @@ function PageCreate() { const [page, setPage] = useState({}); const [t] = useTranslation("global"); + const { + control, + register, + handleSubmit, + formState: {errors}, + setValue, + getValues + } = useForm({ + resolver: joiResolver(PageCreateSchema, {allowUnknown: true}), + }); + const component_all_api_response = useComponentAll(); const components = _.get(component_all_api_response, "data.data", []); @@ -164,10 +179,10 @@ function PageCreate() { ); }; - const handleSubmit = async (e) => { - e.preventDefault(); - - mutate(page); + const submitHandler = async (data) => { + // e.preventDefault(); + console.log(data) + // mutate(page); }; return ( @@ -180,22 +195,20 @@ function PageCreate() { {/*

Use a permanent address where you can*/} {/* receive mail.

*/} -
+
- pageNameOnChange(e.target.value)} + register={register("name")} className="border p-2 rounded w-full" />
- pageIdentifierOnChange(e.target.value)} + register={register("identifier")} className="border p-2 rounded w-full" />
@@ -214,8 +227,8 @@ function PageCreate() { > - Add Component - + Add Component + @@ -224,72 +237,7 @@ function PageCreate() { modal_header="Select Component" modal_body={
- - - - - - - - - - - - - - - {components.map((component) => { - return ( - - - - - - - - - - - ); - })} - -
- {t("common.id")} - - {t("common.name")} - - {t("common.identifier")} - - {t("common.created_at")} - - {t("common.updated_at")} - - {t("common.created_by")} - - {t("common.updated_by")} - - {t("common.action")} -
{component.id}{component.name} - {component.identifier} - - {getFormattedDate(component.created_at)} - - {getFormattedDate(component.updated_at)} - - {component.created_by} - - {component.updated_by} - - -
+
} isOpen={isComponentTableModalOpen} diff --git a/react-admin/src/pages/page/schemas/page.create.schema.ts b/react-admin/src/pages/page/schemas/page.create.schema.ts new file mode 100644 index 00000000..7e78dc89 --- /dev/null +++ b/react-admin/src/pages/page/schemas/page.create.schema.ts @@ -0,0 +1,10 @@ +import Joi from 'joi'; + +export const PageCreateSchema = Joi.object({ + name : Joi.string().required().messages({ + 'string.empty': 'Name is required.', + }), + identifier : Joi.string().required().messages({ + 'string.empty': 'Identifier is required.', + }) +}); diff --git a/react-admin/src/pages/role/schemas/role.create.schema.ts b/react-admin/src/pages/role/schemas/role.create.schema.ts index a743cd0d..88a17b34 100644 --- a/react-admin/src/pages/role/schemas/role.create.schema.ts +++ b/react-admin/src/pages/role/schemas/role.create.schema.ts @@ -1,10 +1,10 @@ import Joi from 'joi'; export const RoleCreateSchema = Joi.object({ - // name : Joi.string().required().messages({ - // 'string.empty': 'Name is required.', - // }), - // identifier : Joi.string().required().messages({ - // 'string.empty': 'Identifier is required.', - // }) + name : Joi.string().required().messages({ + 'string.empty': 'Name is required.', + }), + identifier : Joi.string().required().messages({ + 'string.empty': 'Identifier is required.', + }) }); diff --git a/react-admin/src/types/component/IComponentModel.ts b/react-admin/src/types/component/IComponentModel.ts new file mode 100644 index 00000000..5d45c6c9 --- /dev/null +++ b/react-admin/src/types/component/IComponentModel.ts @@ -0,0 +1,9 @@ +export default interface IComponentModel { + id: string; + name: string; + identifier: string; + created_at: string; + updated_at: string; + created_by: string; + updated_by: string; +} \ No newline at end of file