From 5f228b1bf218776d4c286906ee5e02325a3fed05 Mon Sep 17 00:00:00 2001 From: Philip Molares Date: Mon, 14 Feb 2022 00:44:17 +0100 Subject: [PATCH] refactor: Deduplicate delete modal (#1734) --- .../delete-note-modal.tsx | 34 +++++++++-- .../entry-menu/delete-note-item.tsx | 4 -- .../dropdown-item-with-deletion-modal.tsx | 58 +++++++++++-------- .../history-card/history-card.tsx | 2 +- 4 files changed, 62 insertions(+), 36 deletions(-) diff --git a/src/components/editor-page/sidebar/delete-note-sidebar-entry/delete-note-modal.tsx b/src/components/editor-page/sidebar/delete-note-sidebar-entry/delete-note-modal.tsx index 06c78ff6f..bc44e80d9 100644 --- a/src/components/editor-page/sidebar/delete-note-sidebar-entry/delete-note-modal.tsx +++ b/src/components/editor-page/sidebar/delete-note-sidebar-entry/delete-note-modal.tsx @@ -11,36 +11,58 @@ import { DeletionModal } from '../../../common/modals/deletion-modal' import { useApplicationState } from '../../../../hooks/common/use-application-state' import type { ModalVisibilityProps } from '../../../common/modals/common-modal' +export interface DeleteHistoryNoteModalProps { + modalTitleI18nKey?: string + modalQuestionI18nKey?: string + modalWarningI18nKey?: string + modalButtonI18nKey?: string +} + export interface DeleteNoteModalProps extends ModalVisibilityProps { + optionalNoteTitle?: string onConfirm: () => void } /** * A modal that asks the user if they really want to delete the current note. * + * @param optionalNoteTitle optional note title * @param show Defines if the modal should be shown * @param onHide A callback that fires if the modal should be hidden without confirmation * @param onConfirm A callback that fires if the user confirmed the request + * @param modalTitleI18nKey optional i18nKey for the title + * @param modalQuestionI18nKey optional i18nKey for the question + * @param modalWarningI18nKey optional i18nKey for the warning + * @param modalButtonI18nKey optional i18nKey for the button */ -export const DeleteNoteModal: React.FC = ({ show, onHide, onConfirm }) => { +export const DeleteNoteModal: React.FC = ({ + optionalNoteTitle, + show, + onHide, + onConfirm, + modalTitleI18nKey, + modalQuestionI18nKey, + modalWarningI18nKey, + modalButtonI18nKey +}) => { const noteTitle = useApplicationState((state) => state.noteDetails.noteTitle) return ( + title={modalTitleI18nKey ?? 'editor.modal.deleteNote.title'}>
- +
    -
  •  {noteTitle}
  • +
  • {optionalNoteTitle ?? noteTitle}
- +
) diff --git a/src/components/history-page/entry-menu/delete-note-item.tsx b/src/components/history-page/entry-menu/delete-note-item.tsx index d0e16f8f3..7420d0dc0 100644 --- a/src/components/history-page/entry-menu/delete-note-item.tsx +++ b/src/components/history-page/entry-menu/delete-note-item.tsx @@ -17,11 +17,7 @@ export const DeleteNoteItem: React.FC = ({ noteTitle, onCon ) diff --git a/src/components/history-page/entry-menu/dropdown-item-with-deletion-modal.tsx b/src/components/history-page/entry-menu/dropdown-item-with-deletion-modal.tsx index bc4aed0e6..60b153198 100644 --- a/src/components/history-page/entry-menu/dropdown-item-with-deletion-modal.tsx +++ b/src/components/history-page/entry-menu/dropdown-item-with-deletion-modal.tsx @@ -4,26 +4,38 @@ * SPDX-License-Identifier: AGPL-3.0-only */ -import React, { Fragment, useState } from 'react' +import React, { Fragment, useCallback, useState } from 'react' import { Dropdown } from 'react-bootstrap' import { Trans, useTranslation } from 'react-i18next' import { ForkAwesomeIcon } from '../../common/fork-awesome/fork-awesome-icon' import type { IconName } from '../../common/fork-awesome/types' -import { DeletionModal } from '../../common/modals/deletion-modal' +import type { DeleteHistoryNoteModalProps } from '../../editor-page/sidebar/delete-note-sidebar-entry/delete-note-modal' +import { DeleteNoteModal } from '../../editor-page/sidebar/delete-note-sidebar-entry/delete-note-modal' export interface DropdownItemWithDeletionModalProps { onConfirm: () => void itemI18nKey: string - modalButtonI18nKey: string modalIcon: IconName - modalTitleI18nKey: string - modalQuestionI18nKey: string - modalWarningI18nKey: string noteTitle: string className?: string } -export const DropdownItemWithDeletionModal: React.FC = ({ +/** + * Renders a dropdown item and the corresponding deletion modal + * + * @param onConfirm A callback that fires if the user confirmed the request + * @param noteTitle The note title to be displayed + * @param modalTitleI18nKey The i18nKey for title to be shown in the modal + * @param modalButtonI18nKey The i18nKey for button to be shown in the modal + * @param itemI18nKey The i18nKey for the dropdown item + * @param modalIcon The icon for the dropdown item + * @param modalQuestionI18nKey The i18nKey for question to be shown in the modal + * @param modalWarningI18nKey The i18nKey for warning to be shown in the modal + * @param className Additional classes given to the dropdown item + */ +export const DropdownItemWithDeletionModal: React.FC< + DropdownItemWithDeletionModalProps & DeleteHistoryNoteModalProps +> = ({ onConfirm, noteTitle, modalTitleI18nKey, @@ -36,6 +48,11 @@ export const DropdownItemWithDeletionModal: React.FC { useTranslation() const [showDialog, setShowDialog] = useState(false) + const handleConfirm = useCallback(() => { + setShowDialog(false) + onConfirm() + }, [onConfirm]) + const onHide = useCallback(() => setShowDialog(false), []) return ( @@ -43,25 +60,16 @@ export const DropdownItemWithDeletionModal: React.FC - { - setShowDialog(false) - onConfirm() - }} - deletionButtonI18nKey={modalButtonI18nKey} + setShowDialog(false)} - title={modalTitleI18nKey}> -
- -
-
    -
  • {noteTitle}
  • -
-
- -
-
+ onHide={onHide} + modalTitleI18nKey={modalTitleI18nKey} + modalButtonI18nKey={modalButtonI18nKey} + modalQuestionI18nKey={modalQuestionI18nKey} + modalWarningI18nKey={modalWarningI18nKey} + />
) } diff --git a/src/components/history-page/history-card/history-card.tsx b/src/components/history-page/history-card/history-card.tsx index cd3920e9f..8ff1a9980 100644 --- a/src/components/history-page/history-card/history-card.tsx +++ b/src/components/history-page/history-card/history-card.tsx @@ -72,7 +72,7 @@ export const HistoryCard: React.FC = (