diff --git a/frontend/src/app-components/buttons/FormButtons.tsx b/frontend/src/app-components/buttons/FormButtons.tsx new file mode 100644 index 00000000..181c9c51 --- /dev/null +++ b/frontend/src/app-components/buttons/FormButtons.tsx @@ -0,0 +1,42 @@ +/* + * Copyright © 2024 Hexastack. All rights reserved. + * + * Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms: + * 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission. + * 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file). + */ + +import CheckIcon from "@mui/icons-material/Check"; +import CloseIcon from "@mui/icons-material/Close"; +import { Button } from "@mui/material"; + +import { useTranslate } from "@/hooks/useTranslate"; +import { FormButtonsProps } from "@/types/common/dialogs.types"; + +export const FormButtons = ({ + onCancel, + onConfirm, +}: FormButtonsProps) => { + const { t } = useTranslate(); + + return ( + <> + + + + ); +}; diff --git a/frontend/src/app-components/dialogs/FormDialog.tsx b/frontend/src/app-components/dialogs/FormDialog.tsx index 262c83cf..62341ce2 100644 --- a/frontend/src/app-components/dialogs/FormDialog.tsx +++ b/frontend/src/app-components/dialogs/FormDialog.tsx @@ -6,34 +6,30 @@ * 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file). */ -import { - Dialog, - DialogActions, - DialogContent, - DialogProps, -} from "@mui/material"; -import { FC, ReactNode } from "react"; +import { Dialog, DialogActions, DialogContent } from "@mui/material"; -import { DialogTitle } from "@/app-components/dialogs/DialogTitle"; +import { DialogTitle } from "@/app-components/dialogs"; +import { FormDialogProps } from "@/types/common/dialogs.types"; -export interface FormDialogProps extends DialogProps { - title: string; - children: ReactNode; -} +import { FormButtons } from "../buttons/FormButtons"; -export const FormDialog: FC = ({ +export const FormDialog = ({ title, children, - open, - onClose, + onConfirm, ...rest -}) => { +}: FormDialogProps) => { return ( - - {title} + + rest.onClose?.({}, "backdropClick")}> + {title} + {children} - {/* */} + + onCancel={() => rest.onClose?.({}, "backdropClick")} + onConfirm={onConfirm} + /> ); diff --git a/frontend/src/app-components/dialogs/confirm/ConfirmDialog.tsx b/frontend/src/app-components/dialogs/confirm/ConfirmDialog.tsx new file mode 100644 index 00000000..512adeb6 --- /dev/null +++ b/frontend/src/app-components/dialogs/confirm/ConfirmDialog.tsx @@ -0,0 +1,56 @@ +/* + * Copyright © 2025 Hexastack. All rights reserved. + * + * Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms: + * 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission. + * 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file). + */ + +import { Button, Dialog, DialogActions, DialogContent } from "@mui/material"; +import { FC, ReactNode } from "react"; + +import { ConfirmOptions, DialogProps } from "@/types/common/dialogs.types"; + +import { DialogTitle } from "../DialogTitle"; + +import { useDialogLoadingButton } from "./hooks/useDialogLoadingButton"; + +export interface ConfirmDialogPayload extends ConfirmOptions { + msg: ReactNode; +} + +export interface ConfirmDialogProps + extends DialogProps {} + +export const ConfirmDialog: FC = ({ payload, ...rest }) => { + const cancelButtonProps = useDialogLoadingButton(() => rest.onClose(false)); + const okButtonProps = useDialogLoadingButton(() => rest.onClose(true)); + + return ( + rest.onClose(false)} + > + rest.onClose(false)}> + {payload.title} + + {payload.msg} + + + + + + ); +}; diff --git a/frontend/src/app-components/dialogs/confirm/ConfirmDialogBody.tsx b/frontend/src/app-components/dialogs/confirm/ConfirmDialogBody.tsx new file mode 100644 index 00000000..f54f954a --- /dev/null +++ b/frontend/src/app-components/dialogs/confirm/ConfirmDialogBody.tsx @@ -0,0 +1,27 @@ +/* + * Copyright © 2025 Hexastack. All rights reserved. + * + * Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms: + * 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission. + * 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file). + */ + +import ErrorIcon from "@mui/icons-material/Error"; +import { Grid, Typography } from "@mui/material"; + +import { useTranslate } from "@/hooks/useTranslate"; + +export const ConfirmDialogBody = () => { + const { t } = useTranslate(); + + return ( + + + + + + {t("message.item_delete_confirm")} + + + ); +}; diff --git a/frontend/src/app-components/dialogs/confirm/hooks/useDialogLoadingButton.ts b/frontend/src/app-components/dialogs/confirm/hooks/useDialogLoadingButton.ts new file mode 100644 index 00000000..915f9d9c --- /dev/null +++ b/frontend/src/app-components/dialogs/confirm/hooks/useDialogLoadingButton.ts @@ -0,0 +1,26 @@ +/* + * Copyright © 2025 Hexastack. All rights reserved. + * + * Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms: + * 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission. + * 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file). + */ + +import { useState } from "react"; + +export const useDialogLoadingButton = (onClose: () => Promise) => { + const [loading, setLoading] = useState(false); + const handleClick = async () => { + try { + setLoading(true); + await onClose(); + } finally { + setLoading(false); + } + }; + + return { + onClick: handleClick, + loading, + }; +}; diff --git a/frontend/src/app-components/dialogs/index.ts b/frontend/src/app-components/dialogs/index.ts index 6948629d..f56c6c22 100644 --- a/frontend/src/app-components/dialogs/index.ts +++ b/frontend/src/app-components/dialogs/index.ts @@ -6,7 +6,10 @@ * 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file). */ +export * from "./confirm/ConfirmDialog"; +export * from "./confirm/ConfirmDialogBody"; export * from "./DeleteDialog"; export * from "./DialogTitle"; +export * from "./FormDialog"; export * from "./layouts/ContentContainer"; export * from "./layouts/ContentItem"; diff --git a/frontend/src/components/categories/CategoryDialog.tsx b/frontend/src/components/categories/CategoryDialog.tsx deleted file mode 100644 index 8a2b69ec..00000000 --- a/frontend/src/components/categories/CategoryDialog.tsx +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Copyright © 2024 Hexastack. All rights reserved. - * - * Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms: - * 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission. - * 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file). - */ - -import { Dialog, DialogActions, DialogContent } from "@mui/material"; -import { FC, useEffect } from "react"; -import { useForm } from "react-hook-form"; - -import DialogButtons from "@/app-components/buttons/DialogButtons"; -import { DialogTitle } from "@/app-components/dialogs/DialogTitle"; -import { ContentContainer } from "@/app-components/dialogs/layouts/ContentContainer"; -import { ContentItem } from "@/app-components/dialogs/layouts/ContentItem"; -import { Input } from "@/app-components/inputs/Input"; -import { useCreate } from "@/hooks/crud/useCreate"; -import { useUpdate } from "@/hooks/crud/useUpdate"; -import { DialogControlProps } from "@/hooks/useDialog"; -import { useToast } from "@/hooks/useToast"; -import { useTranslate } from "@/hooks/useTranslate"; -import { EntityType } from "@/services/types"; -import { ICategory, ICategoryAttributes } from "@/types/category.types"; - -export type CategoryDialogProps = DialogControlProps; - -export const CategoryDialog: FC = ({ - open, - data, - closeDialog, - ...rest -}) => { - const { t } = useTranslate(); - const { toast } = useToast(); - const { mutateAsync: createCategory } = useCreate(EntityType.CATEGORY, { - onError: (error) => { - toast.error(error); - }, - onSuccess: () => { - closeDialog(); - toast.success(t("message.success_save")); - }, - }); - const { mutateAsync: updateCategory } = useUpdate(EntityType.CATEGORY, { - onError: () => { - toast.error(t("message.internal_server_error")); - }, - onSuccess: () => { - closeDialog(); - toast.success(t("message.success_save")); - }, - }); - const { - reset, - register, - formState: { errors }, - handleSubmit, - } = useForm({ - defaultValues: { label: data?.label || "" }, - }); - const validationRules = { - label: { - required: t("message.label_is_required"), - }, - }; - const onSubmitForm = async (params: ICategoryAttributes) => { - if (data) { - updateCategory({ id: data.id, params }); - } else { - createCategory(params); - } - }; - - useEffect(() => { - if (open) reset(); - }, [open, reset]); - - useEffect(() => { - if (data) { - reset({ - label: data.label, - }); - } else { - reset(); - } - }, [data, reset]); - - return ( - -
- - {data ? t("title.edit_category") : t("title.new_category")} - - - - - - - - - - - -
-
- ); -}; diff --git a/frontend/src/components/categories/CategoryForm.tsx b/frontend/src/components/categories/CategoryForm.tsx index 59fb1ed2..9294a214 100644 --- a/frontend/src/components/categories/CategoryForm.tsx +++ b/frontend/src/components/categories/CategoryForm.tsx @@ -6,7 +6,12 @@ * 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file). */ -import { FC, useEffect } from "react"; +import { + BaseSyntheticEvent, + forwardRef, + useEffect, + useImperativeHandle, +} from "react"; import { useForm } from "react-hook-form"; import { ContentContainer } from "@/app-components/dialogs/layouts/ContentContainer"; @@ -18,30 +23,36 @@ import { useToast } from "@/hooks/useToast"; import { useTranslate } from "@/hooks/useTranslate"; import { EntityType } from "@/services/types"; import { ICategory, ICategoryAttributes } from "@/types/category.types"; +import { + ComponentFormProps, + HTMLFormElementExtension, + HTMLFormElementExtra, +} from "@/types/common/dialogs.types"; -export type CategoryFormProps = { - data: ICategory | null; -}; - -export const CategoryForm: FC = ({ data }) => { +export const CategoryForm = forwardRef< + HTMLFormElement, + ComponentFormProps +>(({ data, ...rest }, ref) => { const { t } = useTranslate(); const { toast } = useToast(); - const { mutateAsync: createCategory } = useCreate(EntityType.CATEGORY, { - onError: (error) => { - toast.error(error); + const options = { + onError: (error: Error) => { + rest.onError?.(); + toast.error(error || t("message.internal_server_error")); }, onSuccess: () => { + rest.onSuccess?.(); toast.success(t("message.success_save")); }, - }); - const { mutateAsync: updateCategory } = useUpdate(EntityType.CATEGORY, { - onError: () => { - toast.error(t("message.internal_server_error")); - }, - onSuccess: () => { - toast.success(t("message.success_save")); - }, - }); + }; + const { mutateAsync: createCategory } = useCreate( + EntityType.CATEGORY, + options, + ); + const { mutateAsync: updateCategory } = useUpdate( + EntityType.CATEGORY, + options, + ); const { reset, register, @@ -57,11 +68,25 @@ export const CategoryForm: FC = ({ data }) => { }; const onSubmitForm = async (params: ICategoryAttributes) => { if (data) { - updateCategory({ id: data.id, params }); + return await updateCategory({ id: data.id, params }); } else { - createCategory(params); + return await createCategory(params); } }; + const submitAsync = async (e: BaseSyntheticEvent) => { + return await new Promise((resolve) => { + handleSubmit((params) => { + resolve(onSubmitForm(params)); + })(e); + }); + }; + + useImperativeHandle< + HTMLFormElementExtension, + HTMLFormElementExtra + >(ref, () => ({ + submitAsync, + })); useEffect(() => { if (data) { @@ -74,7 +99,7 @@ export const CategoryForm: FC = ({ data }) => { }, [data, reset]); return ( -
+ = ({ data }) => {
); -}; +}); + +CategoryForm.displayName = "CategoryForm"; diff --git a/frontend/src/components/categories/CategoryFormDialog.tsx b/frontend/src/components/categories/CategoryFormDialog.tsx index f95f0c35..e174a222 100644 --- a/frontend/src/components/categories/CategoryFormDialog.tsx +++ b/frontend/src/components/categories/CategoryFormDialog.tsx @@ -6,32 +6,42 @@ * 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file). */ -import { FC } from "react"; +import { useRef } from "react"; -import { FormDialog } from "@/app-components/dialogs/FormDialog"; -import { DialogProps } from "@/contexts/dialogs.context"; +import { FormDialog } from "@/app-components/dialogs"; import { useTranslate } from "@/hooks/useTranslate"; import { ICategory } from "@/types/category.types"; +import { + ComponentFormDialogProps, + HTMLFormElementExtra, +} from "@/types/common/dialogs.types"; import { CategoryForm } from "./CategoryForm"; -export type CategoryFormProps = DialogProps; - -export const CategoryFormDialog: FC = ({ - onClose, +export const CategoryFormDialog = ({ payload, ...rest -}) => { +}: ComponentFormDialogProps) => { const { t } = useTranslate(); + const formRef = useRef<(HTMLFormElement & HTMLFormElementExtra) | null>( + null, + ); return ( - title={payload ? t("title.edit_category") : t("title.new_category")} + onConfirm={async (e) => { + return await formRef.current?.submitAsync(e); + }} {...rest} - // @TODO: fix typing - onClose={async () => await onClose(false)} > - + { + rest.onClose(true); + }} + /> ); }; diff --git a/frontend/src/components/categories/index.tsx b/frontend/src/components/categories/index.tsx index bdb8bff1..6c974e08 100644 --- a/frontend/src/components/categories/index.tsx +++ b/frontend/src/components/categories/index.tsx @@ -13,6 +13,7 @@ import { Button, Grid, Paper } from "@mui/material"; import { GridColDef, GridRowSelectionModel } from "@mui/x-data-grid"; import { useState } from "react"; +import { ConfirmDialogBody } from "@/app-components/dialogs"; import { FilterTextfield } from "@/app-components/inputs/FilterTextfield"; import { ActionColumnLabel, @@ -78,12 +79,24 @@ export const Categories = () => { [ { label: ActionColumnLabel.Edit, - action: (row) => dialogs.open(CategoryFormDialog, row), + action: async (row) => { + await dialogs.open(CategoryFormDialog, row); + }, requires: [PermissionAction.UPDATE], }, { label: ActionColumnLabel.Delete, - action: (row) => deleteDialogCtl.openDialog(row.id), + action: async ({ id }) => { + const isConfirmed = await dialogs.confirm(, { + title: t("title.warning"), + okText: t("label.yes"), + cancelText: t("label.no"), + }); + + if (isConfirmed) { + await deleteCategory(id); + } + }, requires: [PermissionAction.DELETE], }, ], @@ -130,22 +143,6 @@ export const Categories = () => { return ( - {/* - - { - if (selectedCategories.length > 0) { - deleteCategories(selectedCategories), setSelectedCategories([]); - deleteDialogCtl.closeDialog(); - } else if (deleteDialogCtl?.data) { - { - deleteCategory(deleteDialogCtl.data); - deleteDialogCtl.closeDialog(); - } - } - }} - /> */} { startIcon={} variant="contained" color="error" - onClick={() => deleteDialogCtl.openDialog(undefined)} + onClick={async () => { + const isConfirmed = await dialogs.confirm( + , + { + title: t("title.warning"), + okText: t("label.yes"), + cancelText: t("label.no"), + }, + ); + + if (isConfirmed) { + await deleteCategories(selectedCategories); + } + }} > {t("button.delete")}