From b603eb3aa2d543582e5981cc57349fb743430b5a Mon Sep 17 00:00:00 2001 From: Diana Nanyanzi Date: Wed, 11 Dec 2024 08:58:17 +0300 Subject: [PATCH] feat: add deletion functionality - use context for Side Panel components - add delete modal to handle deletion of namespaces and keys - add context menu button on every rendered link ` --- i18n/en.pot | 56 +++++++++----- src/components/Panel.module.css | 9 +++ src/components/modals/CreateModal.tsx | 18 +++-- src/components/modals/DeleteModal.tsx | 84 +++++++++++++++++++++ src/components/panels/EditPanel.tsx | 1 + src/components/sidepanel/ContextButton.tsx | 62 +++++++++++++++ src/components/sidepanel/CreateButton.tsx | 7 +- src/components/sidepanel/PanelLink.tsx | 31 ++++---- src/components/sidepanel/PanelLinksList.tsx | 70 +++++++++++------ src/components/sidepanel/SidePanel.tsx | 51 ++++++------- src/context/SidePanelContext.tsx | 35 +++++++++ src/hooks/useCustomAlert.tsx | 2 +- src/hooks/useDeleteMutation.tsx | 31 ++++++++ 13 files changed, 359 insertions(+), 98 deletions(-) create mode 100644 src/components/modals/DeleteModal.tsx create mode 100644 src/components/sidepanel/ContextButton.tsx create mode 100644 src/context/SidePanelContext.tsx create mode 100644 src/hooks/useDeleteMutation.tsx diff --git a/i18n/en.pot b/i18n/en.pot index a46ec1f..fd0c754 100644 --- a/i18n/en.pot +++ b/i18n/en.pot @@ -5,8 +5,8 @@ msgstr "" "Content-Type: text/plain; charset=utf-8\n" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=(n != 1)\n" -"POT-Creation-Date: 2024-12-11T00:48:22.223Z\n" -"PO-Revision-Date: 2024-12-11T00:48:22.223Z\n" +"POT-Creation-Date: 2024-12-20T17:43:52.507Z\n" +"PO-Revision-Date: 2024-12-20T17:43:52.508Z\n" msgid "View keys" msgstr "View keys" @@ -23,6 +23,18 @@ msgstr "An error occurred" msgid "Error" msgstr "Error" +msgid "Add New Namespace" +msgstr "Add New Namespace" + +msgid "Add New Key" +msgstr "Add New Key" + +msgid "Add Namespace" +msgstr "Add Namespace" + +msgid "Add Key" +msgstr "Add Key" + msgid "Namespace" msgstr "Namespace" @@ -32,6 +44,18 @@ msgstr "Key" msgid "Cancel" msgstr "Cancel" +msgid "Delete Namespace" +msgstr "Delete Namespace" + +msgid "Delete Key" +msgstr "Delete Key" + +msgid "This will delete all the keys in this namespace" +msgstr "This will delete all the keys in this namespace" + +msgid "Delete" +msgstr "Delete" + msgid "Key successfully updated" msgstr "Key successfully updated" @@ -44,6 +68,15 @@ msgstr "Loading" msgid "Save changes" msgstr "Save changes" +msgid "delete" +msgstr "delete" + +msgid "New namespace" +msgstr "New namespace" + +msgid "New key" +msgstr "New key" + msgid "create" msgstr "create" @@ -59,20 +92,5 @@ msgstr "Search" msgid "Key created successfully" msgstr "Key created successfully" -msgid "Add New Namespace" -msgstr "Add New Namespace" - -msgid "Add New Key" -msgstr "Add New Key" - -msgid "Add Namespace" -msgstr "Add Namespace" - -msgid "Add Key" -msgstr "Add Key" - -msgid "New namespace" -msgstr "New namespace" - -msgid "New key" -msgstr "New key" +msgid "There was an error creating the key" +msgstr "There was an error creating the key" diff --git a/src/components/Panel.module.css b/src/components/Panel.module.css index 05d03f1..fa496b7 100644 --- a/src/components/Panel.module.css +++ b/src/components/Panel.module.css @@ -34,6 +34,7 @@ margin: 0.5em 0 0.3em 0; width: 100%; border: none; + justify-content: start; } .sidebarList ul { @@ -97,3 +98,11 @@ .navLink.active:hover { background: var(--colors-teal050); } + +/* context menu */ +.contextMenu button { + background-color: transparent; + border: none; + width: 100%; + justify-content: start; +} diff --git a/src/components/modals/CreateModal.tsx b/src/components/modals/CreateModal.tsx index acff421..dc872b5 100644 --- a/src/components/modals/CreateModal.tsx +++ b/src/components/modals/CreateModal.tsx @@ -8,6 +8,7 @@ import { InputField, } from '@dhis2/ui' import React from 'react' +import { useSidePanelContext } from '../../context/SidePanelContext' import i18n from '../../locales' import { CreateFieldValues } from '../sidepanel/SidePanel' @@ -16,9 +17,6 @@ type CreateModalProps = { values: CreateFieldValues setValues: (values) => void closeModal: () => void - title: string - type: string - buttonLabel: string } const CreateModal = ({ @@ -26,12 +24,18 @@ const CreateModal = ({ values, setValues, closeModal, - title, - type, - buttonLabel, }: CreateModalProps) => { + const { panelType: type } = useSidePanelContext() + + const title = + type === 'namespace' + ? i18n.t('Add New Namespace') + : i18n.t('Add New Key') + const buttonLabel = + type === 'namespace' ? i18n.t('Add Namespace') : i18n.t('Add Key') + return ( - + {title} {type === 'namespace' && ( diff --git a/src/components/modals/DeleteModal.tsx b/src/components/modals/DeleteModal.tsx new file mode 100644 index 0000000..5d8b7b0 --- /dev/null +++ b/src/components/modals/DeleteModal.tsx @@ -0,0 +1,84 @@ +import { + Modal, + ModalContent, + ModalActions, + ModalTitle, + Button, + ButtonStrip, +} from '@dhis2/ui' +import React from 'react' +import { useParams } from 'react-router-dom' +import { useSidePanelContext } from '../../context/SidePanelContext' +import i18n from '../../locales' + +const DeleteModal = ({ + handleDeleteAction, +}: { + handleDeleteAction: () => void +}) => { + const { + panelType: type, + totalItems, + selectedLinkItem: value, + setOpenDeleteModal, + } = useSidePanelContext() + const { namespace: currentNamespace } = useParams() + + const title = + type === 'namespace' ? i18n.t('Delete Namespace') : i18n.t('Delete Key') + + return ( + + {title} + + {type === 'namespace' && ( + <> +

+ {i18n.t( + `Are you sure you want to delete '${value}'?` + )} +

+

+ {i18n.t( + `This will delete all the keys in this namespace` + )} +

+ + )} + {type === 'keys' && ( + <> +

+ {i18n.t( + `Are you sure you want to delete '${value}' in ${currentNamespace}?` + )} +

+ {totalItems < 2 && ( +

+ {i18n.t( + `This will also delete the namespace '${currentNamespace}'` + )} +

+ )} + + )} +
+ + + + + + +
+ ) +} +export default DeleteModal diff --git a/src/components/panels/EditPanel.tsx b/src/components/panels/EditPanel.tsx index 741ee0d..4156b58 100644 --- a/src/components/panels/EditPanel.tsx +++ b/src/components/panels/EditPanel.tsx @@ -75,6 +75,7 @@ const EditPanel = () => { }) } catch (error) { // setUpdateError(error.message) + console.error(error.message) const message = i18n.t('There was an error updating the key') showError(message) } diff --git a/src/components/sidepanel/ContextButton.tsx b/src/components/sidepanel/ContextButton.tsx new file mode 100644 index 0000000..4d9603a --- /dev/null +++ b/src/components/sidepanel/ContextButton.tsx @@ -0,0 +1,62 @@ +import { Button, IconMore16, Popover, IconDelete16 } from '@dhis2/ui' +import React, { useRef } from 'react' +import { useSidePanelContext } from '../../context/SidePanelContext' +import i18n from '../../locales' +import classes from '../Panel.module.css' + +type ContextMenuButtonProps = { + handleContextMenu: () => void + openContextMenu: boolean + setOpenContextMenu: (boolean) => void +} + +const ContextMenuButton = ({ + handleContextMenu, + openContextMenu, + setOpenContextMenu, +}: ContextMenuButtonProps) => { + const ref = useRef(null) + const { setOpenDeleteModal } = useSidePanelContext() + + return ( +
+ +
+ + )} + + ) +} + +export default ContextMenuButton diff --git a/src/components/sidepanel/CreateButton.tsx b/src/components/sidepanel/CreateButton.tsx index 071bfd3..6736dbd 100644 --- a/src/components/sidepanel/CreateButton.tsx +++ b/src/components/sidepanel/CreateButton.tsx @@ -1,15 +1,18 @@ import { Button } from '@dhis2/ui' import { IconAdd16 } from '@dhis2/ui-icons' import React from 'react' +import { useSidePanelContext } from '../../context/SidePanelContext' import i18n from '../../locales' import classes from '../Panel.module.css' type CreateButtonProps = { - label: string handleClick: () => void } -const CreateButton = ({ label, handleClick }: CreateButtonProps) => { +const CreateButton = ({ handleClick }: CreateButtonProps) => { + const { panelType: type } = useSidePanelContext() + const label = + type === 'namespace' ? i18n.t('New namespace') : i18n.t('New key') return (