diff --git a/api/src/cms/schemas/content.schema.ts b/api/src/cms/schemas/content.schema.ts index bcfa7908..9c98d6f3 100644 --- a/api/src/cms/schemas/content.schema.ts +++ b/api/src/cms/schemas/content.schema.ts @@ -1,5 +1,5 @@ /* - * Copyright © 2024 Hexastack. All rights reserved. + * 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. @@ -43,7 +43,7 @@ export class ContentStub extends BaseSchema { @Prop({ type: Boolean, default: true }) status: boolean; - @Prop({ type: mongoose.Schema.Types.Mixed }) + @Prop({ type: mongoose.Schema.Types.Mixed, default: {} }) dynamicFields: Record; @Prop({ type: String }) diff --git a/frontend/public/locales/en/translation.json b/frontend/public/locales/en/translation.json index 5d2a42a6..617d3cac 100644 --- a/frontend/public/locales/en/translation.json +++ b/frontend/public/locales/en/translation.json @@ -112,7 +112,8 @@ "text_is_required": "Text is required", "invalid_file_type": "Invalid file type. Please select a file in the supported format.", "select_category": "Select a flow", - "logout_failed": "Something went wrong during logout" + "logout_failed": "Something went wrong during logout", + "duplicate_labels_not_allowed": "Duplicate labels are not allowed" }, "menu": { "terms": "Terms of Use", @@ -191,13 +192,14 @@ "entities": "Content Types", "new_content_type": "New Content Type", "edit_content_type": "Edit Content Type", - "manage_fields": "Manage Fields", "nodes": "Content", "new_node": "New Content", "edit_node": "Edit Content", "import": "Bulk Import", "media_library": "Media Library", "languages": "Languages", + "new_language": "Add Language", + "edit_language": "Edit Language", "translations": "Translations", "update_translation": "Update Translation", "broadcast": "Broadcast", diff --git a/frontend/public/locales/fr/translation.json b/frontend/public/locales/fr/translation.json index 741e1168..a81000dd 100644 --- a/frontend/public/locales/fr/translation.json +++ b/frontend/public/locales/fr/translation.json @@ -112,7 +112,8 @@ "text_is_required": "Texte requis", "invalid_file_type": "Type de fichier invalide. Veuillez choisir un fichier dans un format pris en charge.", "select_category": "Sélectionner une catégorie", - "logout_failed": "Une erreur s'est produite lors de la déconnexion" + "logout_failed": "Une erreur s'est produite lors de la déconnexion", + "duplicate_labels_not_allowed": "Les étiquettes en double ne sont pas autorisées" }, "menu": { "terms": "Conditions d'utilisation", @@ -191,13 +192,14 @@ "entities": "Types de contenu", "new_content_type": "Nouveau type de contenu", "edit_content_type": "Modifier le type de contenu", - "manage_fields": "Gérer les champs", "nodes": "Contenu", "new_node": "Nouveau contenu", "edit_node": "Modifier le contenu", "import": "Importation en masse", "media_library": "Bibliothéque Media", "languages": "Langues", + "new_language": "Nouvelle langue", + "edit_language": "Modifier la langue", "translations": "Traductions", "update_translation": "Mettre à jour la traduction", "broadcast": "Diffusion", diff --git a/frontend/src/components/content-types/ContentTypeDialog.tsx b/frontend/src/components/content-types/ContentTypeDialog.tsx index ebe33bb7..735496da 100644 --- a/frontend/src/components/content-types/ContentTypeDialog.tsx +++ b/frontend/src/components/content-types/ContentTypeDialog.tsx @@ -1,14 +1,15 @@ /* - * Copyright © 2024 Hexastack. All rights reserved. + * 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 { Dialog, DialogActions, DialogContent } from "@mui/material"; +import AddIcon from "@mui/icons-material/Add"; +import { Button, Dialog, DialogActions, DialogContent } from "@mui/material"; import { FC, useEffect } from "react"; -import { useForm } from "react-hook-form"; +import { useFieldArray, useForm } from "react-hook-form"; import DialogButtons from "@/app-components/buttons/DialogButtons"; import { DialogTitle } from "@/app-components/dialogs/DialogTitle"; @@ -21,10 +22,10 @@ import { DialogControlProps } from "@/hooks/useDialog"; import { useToast } from "@/hooks/useToast"; import { useTranslate } from "@/hooks/useTranslate"; import { EntityType } from "@/services/types"; -import { - IContentType, - IContentTypeAttributes, -} from "@/types/content-type.types"; +import { ContentFieldType, IContentType } from "@/types/content-type.types"; + +import { FieldInput } from "./components/FieldInput"; +import { FIELDS_FORM_DEFAULT_VALUES, READ_ONLY_FIELDS } from "./constants"; export type ContentTypeDialogProps = DialogControlProps; export const ContentTypeDialog: FC = ({ @@ -37,50 +38,67 @@ export const ContentTypeDialog: FC = ({ const { handleSubmit, register, + control, reset, + setValue, formState: { errors }, - } = useForm({ - defaultValues: { name: data?.name || "" }, + } = useForm>({ + defaultValues: { + name: data?.name || "", + fields: data?.fields || FIELDS_FORM_DEFAULT_VALUES, + }, }); - const CloseAndReset = () => { + const { append, fields, remove } = useFieldArray({ + name: "fields", + control, + }); + const closeAndReset = () => { closeDialog(); - reset(); + reset({ + name: "", + fields: FIELDS_FORM_DEFAULT_VALUES, + }); }; - const { mutateAsync: createContentType } = useCreate( - EntityType.CONTENT_TYPE, - { - onError: (error) => { - toast.error(error); - }, - onSuccess: () => { - closeDialog(); - toast.success(t("message.success_save")); - }, + const { mutate: createContentType } = useCreate(EntityType.CONTENT_TYPE, { + onError: (error) => { + toast.error(error.message || t("message.internal_server_error")); }, - ); - const { mutateAsync: updateContentType } = useUpdate( - EntityType.CONTENT_TYPE, - { - onError: () => { - toast.error(t("message.internal_server_error")); - }, - onSuccess: () => { - closeDialog(); - toast.success(t("message.success_save")); - }, + onSuccess: () => { + closeDialog(); + toast.success(t("message.success_save")); }, - ); - const validationRules = { - name: { - required: t("message.name_is_required"), + }); + const { mutate: updateContentType } = useUpdate(EntityType.CONTENT_TYPE, { + onError: (error) => { + toast.error(error.message || t("message.internal_server_error")); }, - }; - const onSubmitForm = async (params: IContentTypeAttributes) => { + onSuccess: () => { + closeDialog(); + toast.success(t("message.success_save")); + }, + }); + const onSubmitForm = async (params) => { + const labelCounts: Record = params.fields.reduce( + (acc, field) => { + if (!field.label.trim()) return acc; + acc[field.label] = (acc[field.label] || 0) + 1; + + return acc; + }, + {} as Record, + ); + const hasDuplicates = Object.values(labelCounts).some( + (count: number) => count > 1, + ); + + if (hasDuplicates) { + toast.error(t("message.duplicate_labels_not_allowed")); + + return; + } + if (data) { - updateContentType({ - id: data.id, - params, - }); + updateContentType({ id: data.id, params }); } else { createContentType(params); } @@ -94,16 +112,17 @@ export const ContentTypeDialog: FC = ({ if (data) { reset({ name: data.name, + fields: data.fields || FIELDS_FORM_DEFAULT_VALUES, }); } else { - reset(); + reset({ name: "", fields: FIELDS_FORM_DEFAULT_VALUES }); } }, [data, reset]); return ( - +
- + {data ? t("title.edit_content_type") : t("title.new_content_type")} @@ -112,16 +131,46 @@ export const ContentTypeDialog: FC = ({ + + {fields.map((f, index) => ( + + + + ))} + + + - +
diff --git a/frontend/src/components/content-types/EditContentTypeFieldsDialog.tsx b/frontend/src/components/content-types/EditContentTypeFieldsDialog.tsx deleted file mode 100644 index a2437e75..00000000 --- a/frontend/src/components/content-types/EditContentTypeFieldsDialog.tsx +++ /dev/null @@ -1,219 +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 AddIcon from "@mui/icons-material/Add"; -import { - Button, - CircularProgress, - Dialog, - DialogActions, - DialogContent, - Stack, -} from "@mui/material"; -import { useEffect } from "react"; -import { useFieldArray, useForm } from "react-hook-form"; - -import DialogButtons from "@/app-components/buttons/DialogButtons"; -import { - DialogTitle, - ContentContainer, - ContentItem, -} from "@/app-components/dialogs"; -import { Input } from "@/app-components/inputs/Input"; -import { useGet } from "@/hooks/crud/useGet"; -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 { ContentFieldType, IContentType } from "@/types/content-type.types"; - -import { FieldInput } from "./components/FieldInput"; -import { FIELDS_FORM_DEFAULT_VALUES, READ_ONLY_FIELDS } from "./constants"; - -export type EditContentTypeDialogFieldsProps = DialogControlProps; - -export const EditContentTypeFieldsDialog = ({ - data: contentType, - closeDialog, - open, -}: EditContentTypeDialogFieldsProps) => { - const { t } = useTranslate(); - const { isLoading, data, refetch } = useGet(contentType?.id || "", { - entity: EntityType.CONTENT_TYPE, - }); - const { toast } = useToast(); - const { - handleSubmit, - control, - reset, - setValue, - register, - formState: { errors }, - } = useForm>({ - mode: "onChange", - values: { - fields: data?.fields, - name: data?.name, - }, - defaultValues: { - fields: FIELDS_FORM_DEFAULT_VALUES, - name: data?.name, - }, - }); - const { append, fields, replace, remove } = useFieldArray< - Pick, - "fields" - >({ - name: "fields", - control, - keyName: "id", - rules: { - required: true, - }, - }); - const validationRules = { - name: { - required: t("message.name_is_required"), - }, - }; - - useEffect(() => { - register("fields"); - }, [register]); - - useEffect(() => { - if (data?.fields) { - replace(data.fields); - } - }, [data, replace]); - - useEffect(() => { - if (!open) { - reset(); - } - if (open) { - refetch(); - } - }, [open, reset]); - - useEffect(() => { - if (data) { - reset({ - name: data.name, - fields: data.fields, - }); - } else { - reset(); - } - }, [data, reset]); - - function handleClose() { - closeDialog(); - } - - const { mutateAsync: updateContentType } = useUpdate( - EntityType.CONTENT_TYPE, - { - onError: (error) => { - toast.error(`${t("message.internal_server_error")}: ${error}`); - }, - onSuccess: () => { - toast.success(t("message.success_save")); - }, - }, - ); - - return ( - - - {t("title.manage_fields")} - -
{ - if (!!contentType) - await updateContentType({ - id: contentType.id, - params: { - name, - fields, - }, - }); - handleClose(); - })} - > - - - - - - {!isLoading - ? fields.map((f, index) => ( - - - - )) - : null} - {isLoading ? ( - - - - - - ) : null} - - - - - - - - -
-
- ); -}; diff --git a/frontend/src/components/content-types/components/FieldInput.tsx b/frontend/src/components/content-types/components/FieldInput.tsx index 5d5d5b3c..74455ac4 100644 --- a/frontend/src/components/content-types/components/FieldInput.tsx +++ b/frontend/src/components/content-types/components/FieldInput.tsx @@ -1,11 +1,12 @@ /* - * Copyright © 2024 Hexastack. All rights reserved. + * 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 DeleteOutlineIcon from "@mui/icons-material/DeleteOutline"; import { MenuItem } from "@mui/material"; import { useEffect } from "react"; @@ -59,11 +60,14 @@ export const FieldInput = ({ ( + rules={{ required: t("message.label_is_required") }} + render={({ field, fieldState }) => ( )} /> diff --git a/frontend/src/components/content-types/index.tsx b/frontend/src/components/content-types/index.tsx index 84cf93c0..46ef5638 100644 --- a/frontend/src/components/content-types/index.tsx +++ b/frontend/src/components/content-types/index.tsx @@ -1,11 +1,12 @@ /* - * Copyright © 2024 Hexastack. All rights reserved. + * 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 { faAlignLeft } from "@fortawesome/free-solid-svg-icons"; import AddIcon from "@mui/icons-material/Add"; import { Button, Grid, Paper } from "@mui/material"; @@ -33,7 +34,6 @@ import { PermissionAction } from "@/types/permission.types"; import { getDateTimeFormatter } from "@/utils/date"; import { ContentTypeDialog } from "./ContentTypeDialog"; -import { EditContentTypeFieldsDialog } from "./EditContentTypeFieldsDialog"; export const ContentTypes = () => { const { t } = useTranslate(); @@ -125,7 +125,7 @@ export const ContentTypes = () => { deleteContentType(deleteDialogCtl.data); }} /> - + ; -export const LanguageDialog: FC = ({ - open, - data, - closeDialog, - ...rest -}) => { - const { t } = useTranslate(); - const { toast } = useToast(); - const { mutateAsync: createLanguage } = useCreate(EntityType.LANGUAGE, { - onError: () => { - toast.error(t("message.internal_server_error")); - }, - onSuccess() { - closeDialog(); - toast.success(t("message.success_save")); - }, - }); - const { mutateAsync: updateLanguage } = useUpdate(EntityType.LANGUAGE, { - onError: () => { - toast.error(t("message.internal_server_error")); - }, - onSuccess() { - closeDialog(); - toast.success(t("message.success_save")); - }, - }); - const { - reset, - register, - formState: { errors }, - handleSubmit, - control, - } = useForm({ - defaultValues: { - title: data?.title || "", - code: data?.code || "", - isRTL: data?.isRTL || false, - }, - }); - const validationRules = { - title: { - required: t("message.title_is_required"), - }, - code: { - required: t("message.code_is_required"), - }, - }; - const onSubmitForm = async (params: ILanguageAttributes) => { - if (data) { - updateLanguage({ id: data.id, params }); - } else { - createLanguage(params); - } - }; - - useEffect(() => { - if (open) reset(); - }, [open, reset]); - - useEffect(() => { - if (data) { - reset({ - title: data.title, - code: data.code, - isRTL: data.isRTL, - }); - } else { - reset(); - } - }, [data, reset]); - - return ( - -
- - {data ? t("title.edit_label") : t("title.new_label")} - - - - - - - - - - - ( - } - label={t("label.is_rtl")} - /> - )} - /> - - - - - - -
-
- ); -}; diff --git a/frontend/src/components/languages/LanguageForm.tsx b/frontend/src/components/languages/LanguageForm.tsx new file mode 100644 index 00000000..3f42f074 --- /dev/null +++ b/frontend/src/components/languages/LanguageForm.tsx @@ -0,0 +1,127 @@ +/* + * 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 { FormControlLabel, Switch } from "@mui/material"; +import { FC, Fragment, useEffect } from "react"; +import { Controller, useForm } from "react-hook-form"; + +import { ContentContainer, ContentItem } from "@/app-components/dialogs/"; +import { Input } from "@/app-components/inputs/Input"; +import { useCreate } from "@/hooks/crud/useCreate"; +import { useUpdate } from "@/hooks/crud/useUpdate"; +import { useToast } from "@/hooks/useToast"; +import { useTranslate } from "@/hooks/useTranslate"; +import { EntityType } from "@/services/types"; +import { ComponentFormProps } from "@/types/common/dialogs.types"; +import { ILanguage, ILanguageAttributes } from "@/types/language.types"; + +export const LanguageForm: FC> = ({ + data, + Wrapper = Fragment, + WrapperProps, + ...rest +}) => { + const { t } = useTranslate(); + const { toast } = useToast(); + const options = { + onError: () => { + rest.onError?.(); + toast.error(t("message.internal_server_error")); + }, + onSuccess() { + rest.onSuccess?.(); + toast.success(t("message.success_save")); + }, + }; + const { mutate: createLanguage } = useCreate(EntityType.LANGUAGE, options); + const { mutate: updateLanguage } = useUpdate(EntityType.LANGUAGE, options); + const { + reset, + register, + formState: { errors }, + handleSubmit, + control, + } = useForm({ + defaultValues: { + title: data?.title || "", + code: data?.code || "", + isRTL: data?.isRTL || false, + }, + }); + const validationRules = { + title: { + required: t("message.title_is_required"), + }, + code: { + required: t("message.code_is_required"), + }, + }; + const onSubmitForm = (params: ILanguageAttributes) => { + if (data) { + updateLanguage({ id: data.id, params }); + } else { + createLanguage(params); + } + }; + + useEffect(() => { + if (data) { + reset({ + title: data.title, + code: data.code, + isRTL: data.isRTL, + }); + } else { + reset(); + } + }, [data, reset]); + + return ( + +
+ + + + + + + + + ( + } + label={t("label.is_rtl")} + /> + )} + /> + + +
+
+ ); +}; diff --git a/frontend/src/components/languages/LanguageFormDialog.tsx b/frontend/src/components/languages/LanguageFormDialog.tsx new file mode 100644 index 00000000..631202ce --- /dev/null +++ b/frontend/src/components/languages/LanguageFormDialog.tsx @@ -0,0 +1,24 @@ +/* + * 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 { GenericFormDialog } from "@/app-components/dialogs"; +import { ComponentFormDialogProps } from "@/types/common/dialogs.types"; +import { ILanguage } from "@/types/language.types"; + +import { LanguageForm } from "./LanguageForm"; + +export const LanguageFormDialog = ( + props: ComponentFormDialogProps, +) => ( + + Form={LanguageForm} + addText="title.new_language" + editText="title.edit_language" + {...props} + /> +); diff --git a/frontend/src/components/languages/index.tsx b/frontend/src/components/languages/index.tsx index 2b59a49e..1d914634 100644 --- a/frontend/src/components/languages/index.tsx +++ b/frontend/src/components/languages/index.tsx @@ -12,7 +12,7 @@ import { Button, Grid, Paper, Switch } from "@mui/material"; import { GridColDef } from "@mui/x-data-grid"; import { useQueryClient } from "react-query"; -import { DeleteDialog } from "@/app-components/dialogs/DeleteDialog"; +import { ConfirmDialogBody } from "@/app-components/dialogs"; import { FilterTextfield } from "@/app-components/inputs/FilterTextfield"; import { ActionColumnLabel, @@ -24,7 +24,7 @@ import { isSameEntity } from "@/hooks/crud/helpers"; import { useDelete } from "@/hooks/crud/useDelete"; import { useFind } from "@/hooks/crud/useFind"; import { useUpdate } from "@/hooks/crud/useUpdate"; -import { getDisplayDialogs, useDialog } from "@/hooks/useDialog"; +import { useDialogs } from "@/hooks/useDialogs"; import { useHasPermission } from "@/hooks/useHasPermission"; import { useSearch } from "@/hooks/useSearch"; import { useToast } from "@/hooks/useToast"; @@ -35,14 +35,12 @@ import { ILanguage } from "@/types/language.types"; import { PermissionAction } from "@/types/permission.types"; import { getDateTimeFormatter } from "@/utils/date"; -import { LanguageDialog } from "./LanguageDialog"; +import { LanguageFormDialog } from "./LanguageFormDialog"; export const Languages = () => { const { t } = useTranslate(); const { toast } = useToast(); - const addDialogCtl = useDialog(false); - const editDialogCtl = useDialog(false); - const deleteDialogCtl = useDialog(false); + const dialogs = useDialogs(); const queryClient = useQueryClient(); const hasPermission = useHasPermission(); const { onSearch, searchPayload } = useSearch({ @@ -75,7 +73,6 @@ export const Languages = () => { return isSameEntity(qEntity, EntityType.NLP_SAMPLE); }, }); - deleteDialogCtl.closeDialog(); toast.success(t("message.item_delete_success")); }, }); @@ -94,12 +91,18 @@ export const Languages = () => { [ { label: ActionColumnLabel.Edit, - action: (row) => editDialogCtl.openDialog(row), + action: (row) => dialogs.open(LanguageFormDialog, row), requires: [PermissionAction.UPDATE], }, { label: ActionColumnLabel.Delete, - action: (row) => deleteDialogCtl.openDialog(row.id), + action: async ({ id }) => { + const isConfirmed = await dialogs.confirm(ConfirmDialogBody); + + if (isConfirmed) { + deleteLanguage(id); + } + }, requires: [PermissionAction.DELETE], isDisabled: (row) => row.isDefault, }, @@ -182,14 +185,6 @@ export const Languages = () => { return ( - - - { - if (deleteDialogCtl?.data) deleteLanguage(deleteDialogCtl.data); - }} - /> { startIcon={} variant="contained" sx={{ float: "right" }} - onClick={() => addDialogCtl.openDialog()} + onClick={() => dialogs.open(LanguageFormDialog, null)} > {t("button.add")} diff --git a/frontend/src/components/nlp/NlpEntityDialog.tsx b/frontend/src/components/nlp/NlpEntityDialog.tsx deleted file mode 100644 index f330588a..00000000 --- a/frontend/src/components/nlp/NlpEntityDialog.tsx +++ /dev/null @@ -1,163 +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, - FormControl, - FormControlLabel, - FormLabel, - Radio, - RadioGroup, - DialogContent, - DialogActions, -} 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 { - INlpEntity, - INlpEntityAttributes, - NlpLookups, -} from "@/types/nlp-entity.types"; - -export type NlpEntityDialogProps = DialogControlProps; -export const NlpEntityDialog: FC = ({ - open, - closeDialog, - data, - ...rest -}) => { - const { t } = useTranslate(); - const { toast } = useToast(); - const { mutateAsync: createNlpEntity } = useCreate(EntityType.NLP_ENTITY, { - onError: () => { - toast.error(t("message.internal_server_error")); - }, - onSuccess: () => { - closeDialog(); - toast.success(t("message.success_save")); - }, - }); - const { mutateAsync: updateNlpEntity } = useUpdate(EntityType.NLP_ENTITY, { - onError: () => { - toast.error(t("message.internal_server_error")); - }, - onSuccess: () => { - closeDialog(); - toast.success(t("message.success_save")); - }, - }); - const { - reset, - register, - formState: { errors }, - handleSubmit, - } = useForm({ - defaultValues: { - name: data?.name || "", - doc: data?.doc || "", - lookups: data?.lookups || ["keywords"], - }, - }); - const validationRules = { - name: { - required: t("message.name_is_required"), - }, - lookups: {}, - isChecked: {}, - }; - const onSubmitForm = async (params: INlpEntityAttributes) => { - if (data) { - updateNlpEntity({ id: data.id, params }); - } else { - createNlpEntity(params); - } - }; - - useEffect(() => { - if (open) reset(); - }, [open, reset]); - - useEffect(() => { - if (data) { - reset({ - name: data.name, - doc: data.doc, - }); - } else { - reset(); - } - }, [data, reset]); - - return ( - -
- - {data ? t("title.edit_nlp_entity") : t("title.new_nlp_entity")} - - - - {!data ? ( - - - {t("label.lookup_strategies")} - - {Object.values(NlpLookups).map((nlpLookup, index) => ( - } - label={nlpLookup} - /> - ))} - - - - ) : null} - - - - - - - - - - - - -
-
- ); -}; diff --git a/frontend/src/components/nlp/components/NlpEntity.tsx b/frontend/src/components/nlp/components/NlpEntity.tsx index 4ae9c969..71ef8119 100644 --- a/frontend/src/components/nlp/components/NlpEntity.tsx +++ b/frontend/src/components/nlp/components/NlpEntity.tsx @@ -1,11 +1,12 @@ /* - * Copyright © 2024 Hexastack. All rights reserved. + * 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 AddIcon from "@mui/icons-material/Add"; import DeleteIcon from "@mui/icons-material/Delete"; import { Button, Chip, Grid } from "@mui/material"; @@ -13,7 +14,7 @@ import { GridColDef, GridRowSelectionModel } from "@mui/x-data-grid"; import { useRouter } from "next/router"; import { useState } from "react"; -import { DeleteDialog } from "@/app-components/dialogs"; +import { ConfirmDialogBody } from "@/app-components/dialogs"; import { FilterTextfield } from "@/app-components/inputs/FilterTextfield"; import { ActionColumnLabel, @@ -24,7 +25,7 @@ import { DataGrid } from "@/app-components/tables/DataGrid"; import { useDelete } from "@/hooks/crud/useDelete"; import { useDeleteMany } from "@/hooks/crud/useDeleteMany"; import { useFind } from "@/hooks/crud/useFind"; -import { getDisplayDialogs, useDialog } from "@/hooks/useDialog"; +import { useDialogs } from "@/hooks/useDialogs"; import { useHasPermission } from "@/hooks/useHasPermission"; import { useSearch } from "@/hooks/useSearch"; import { useToast } from "@/hooks/useToast"; @@ -34,39 +35,32 @@ import { INlpEntity } from "@/types/nlp-entity.types"; import { PermissionAction } from "@/types/permission.types"; import { getDateTimeFormatter } from "@/utils/date"; -import { NlpEntityDialog } from "../NlpEntityDialog"; +import { NlpEntityFormDialog } from "./NlpEntityFormDialog"; const NlpEntity = () => { + const { t } = useTranslate(); + const { toast } = useToast(); + const dialogs = useDialogs(); const router = useRouter(); - const deleteEntityDialogCtl = useDialog(false); const hasPermission = useHasPermission(); - const editEntityDialogCtl = useDialog(false); - const { mutateAsync: deleteNlpEntity } = useDelete(EntityType.NLP_ENTITY, { + const { mutate: deleteNlpEntity } = useDelete(EntityType.NLP_ENTITY, { onError: () => { toast.error(t("message.internal_server_error")); }, onSuccess() { - deleteEntityDialogCtl.closeDialog(); toast.success(t("message.item_delete_success")); }, }); - const { mutateAsync: deleteNlpEntities } = useDeleteMany( - EntityType.NLP_ENTITY, - { - onError: (error) => { - toast.error(error); - }, - onSuccess: () => { - deleteEntityDialogCtl.closeDialog(); - setSelectedNlpEntities([]); - toast.success(t("message.item_delete_success")); - }, + const { mutate: deleteNlpEntities } = useDeleteMany(EntityType.NLP_ENTITY, { + onError: (error) => { + toast.error(error); }, - ); + onSuccess: () => { + setSelectedNlpEntities([]); + toast.success(t("message.item_delete_success")); + }, + }); const [selectedNlpEntities, setSelectedNlpEntities] = useState([]); - const addDialogCtl = useDialog(false); - const { t } = useTranslate(); - const { toast } = useToast(); const { onSearch, searchPayload } = useSearch({ $or: ["name", "doc"], }); @@ -100,12 +94,18 @@ const NlpEntity = () => { }, { label: ActionColumnLabel.Edit, - action: (row) => editEntityDialogCtl.openDialog(row), + action: (row) => dialogs.open(NlpEntityFormDialog, row), requires: [PermissionAction.UPDATE], }, { label: ActionColumnLabel.Delete, - action: (row) => deleteEntityDialogCtl.openDialog(row.id), + action: async ({ id }) => { + const isConfirmed = await dialogs.confirm(ConfirmDialogBody); + + if (isConfirmed) { + deleteNlpEntity(id); + } + }, requires: [PermissionAction.DELETE], }, ], @@ -177,21 +177,6 @@ const NlpEntity = () => { return ( - - - { - if (selectedNlpEntities.length > 0) { - deleteNlpEntities(selectedNlpEntities); - setSelectedNlpEntities([]); - deleteEntityDialogCtl.closeDialog(); - } else if (deleteEntityDialogCtl.data) { - deleteNlpEntity(deleteEntityDialogCtl.data); - } - }} - /> - { startIcon={} variant="contained" sx={{ float: "right" }} - onClick={() => addDialogCtl.openDialog()} + onClick={() => dialogs.open(NlpEntityFormDialog, null)} > {t("button.add")} ) : null} - {selectedNlpEntities.length > 0 && ( - - - - )} + + + diff --git a/frontend/src/components/nlp/components/NlpEntityForm.tsx b/frontend/src/components/nlp/components/NlpEntityForm.tsx new file mode 100644 index 00000000..ada67efd --- /dev/null +++ b/frontend/src/components/nlp/components/NlpEntityForm.tsx @@ -0,0 +1,141 @@ +/* + * 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 { + FormControl, + FormControlLabel, + FormLabel, + Radio, + RadioGroup, +} from "@mui/material"; +import { FC, Fragment, useEffect } from "react"; +import { useForm } from "react-hook-form"; + +import { ContentContainer, ContentItem } from "@/app-components/dialogs/"; +import { Input } from "@/app-components/inputs/Input"; +import { useCreate } from "@/hooks/crud/useCreate"; +import { useUpdate } from "@/hooks/crud/useUpdate"; +import { useToast } from "@/hooks/useToast"; +import { useTranslate } from "@/hooks/useTranslate"; +import { EntityType } from "@/services/types"; +import { ComponentFormProps } from "@/types/common/dialogs.types"; +import { + INlpEntity, + INlpEntityAttributes, + NlpLookups, +} from "@/types/nlp-entity.types"; + +export const NlpEntityVarForm: FC> = ({ + data, + Wrapper = Fragment, + WrapperProps, + ...rest +}) => { + const { t } = useTranslate(); + const { toast } = useToast(); + const options = { + onError: (error: Error) => { + rest.onError?.(); + toast.error(error.message || t("message.internal_server_error")); + }, + onSuccess: () => { + rest.onSuccess?.(); + toast.success(t("message.success_save")); + }, + }; + const { mutate: createNlpEntity } = useCreate(EntityType.NLP_ENTITY, options); + const { mutate: updateNlpEntity } = useUpdate(EntityType.NLP_ENTITY, options); + const { + reset, + register, + formState: { errors }, + handleSubmit, + } = useForm({ + defaultValues: { + name: data?.name || "", + doc: data?.doc || "", + lookups: data?.lookups || ["keywords"], + }, + }); + const validationRules = { + name: { + required: t("message.name_is_required"), + }, + lookups: {}, + isChecked: {}, + }; + const onSubmitForm = (params: INlpEntityAttributes) => { + if (data) { + updateNlpEntity({ id: data.id, params }); + } else { + createNlpEntity(params); + } + }; + + useEffect(() => { + if (data) { + reset({ + name: data.name, + doc: data.doc, + }); + } else { + reset(); + } + }, [data, reset]); + + return ( + +
+ + {!data ? ( + + + {t("label.lookup_strategies")} + + {Object.values(NlpLookups).map((nlpLookup, index) => ( + } + label={nlpLookup} + /> + ))} + + + + ) : null} + + + + + + + +
+
+ ); +}; diff --git a/frontend/src/components/nlp/components/NlpEntityFormDialog.tsx b/frontend/src/components/nlp/components/NlpEntityFormDialog.tsx new file mode 100644 index 00000000..edf37b5c --- /dev/null +++ b/frontend/src/components/nlp/components/NlpEntityFormDialog.tsx @@ -0,0 +1,24 @@ +/* + * 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 { GenericFormDialog } from "@/app-components/dialogs"; +import { ComponentFormDialogProps } from "@/types/common/dialogs.types"; +import { INlpEntity } from "@/types/nlp-entity.types"; + +import { NlpEntityVarForm } from "./NlpEntityForm"; + +export const NlpEntityFormDialog = ( + props: ComponentFormDialogProps, +) => ( + + Form={NlpEntityVarForm} + addText="title.new_nlp_entity" + editText="title.edit_nlp_entity" + {...props} + /> +);