From 6cc11d07e31b70812154d5dc083a9bf9d31a8daa Mon Sep 17 00:00:00 2001 From: Erik Michelson Date: Sun, 14 Aug 2022 23:12:56 +0200 Subject: [PATCH] fix(note-deletion): Add required keepMedia property and redirect The backend requires a property "keepMedia" that states whether uploaded media should be kept around or not. This property was missing in our API call. Additionally, after deleting a note, the user is now redirected to the history page. Signed-off-by: Erik Michelson --- src/api/notes/index.ts | 10 ++++++++-- src/api/notes/types.ts | 6 +++++- .../delete-note-sidebar-entry.tsx | 14 ++++++++++++-- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/api/notes/index.ts b/src/api/notes/index.ts index 71384168e..e434a60dc 100644 --- a/src/api/notes/index.ts +++ b/src/api/notes/index.ts @@ -3,7 +3,7 @@ * * SPDX-License-Identifier: AGPL-3.0-only */ -import type { Note } from './types' +import type { Note, NoteDeletionOptions } from './types' import type { MediaUpload } from '../media/types' import { GetApiRequestBuilder } from '../common/api-request-builder/get-api-request-builder' import { PostApiRequestBuilder } from '../common/api-request-builder/post-api-request-builder' @@ -73,5 +73,11 @@ export const createNoteWithPrimaryAlias = async (markdown: string, primaryAlias: * @throws {Error} when the api request wasn't successful. */ export const deleteNote = async (noteIdOrAlias: string): Promise => { - await new DeleteApiRequestBuilder('notes/' + noteIdOrAlias).sendRequest() + await new DeleteApiRequestBuilder('notes/' + noteIdOrAlias) + .withJsonBody({ + keepMedia: false + // TODO Ask whether the user wants to keep the media uploaded to the note. + // https://github.com/hedgedoc/react-client/issues/2288 + }) + .sendRequest() } diff --git a/src/api/notes/types.ts b/src/api/notes/types.ts index f0c9e9ef5..fd6287505 100644 --- a/src/api/notes/types.ts +++ b/src/api/notes/types.ts @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file) + * SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file) * * SPDX-License-Identifier: AGPL-3.0-only */ @@ -50,3 +50,7 @@ export interface NoteGroupPermissionEntry { groupName: string canEdit: boolean } + +export interface NoteDeletionOptions { + keepMedia: boolean +} diff --git a/src/components/editor-page/sidebar/delete-note-sidebar-entry/delete-note-sidebar-entry.tsx b/src/components/editor-page/sidebar/delete-note-sidebar-entry/delete-note-sidebar-entry.tsx index fa5eda680..9777ca9c7 100644 --- a/src/components/editor-page/sidebar/delete-note-sidebar-entry/delete-note-sidebar-entry.tsx +++ b/src/components/editor-page/sidebar/delete-note-sidebar-entry/delete-note-sidebar-entry.tsx @@ -15,6 +15,10 @@ import { showErrorNotification } from '../../../../redux/ui-notifications/method import { deleteNote } from '../../../../api/notes' import { DeleteNoteModal } from './delete-note-modal' import { useBooleanState } from '../../../../hooks/common/use-boolean-state' +import { useRouter } from 'next/router' +import { Logger } from '../../../../utils/logger' + +const logger = new Logger('note-deletion') /** * Sidebar entry that can be used to delete the current note. @@ -24,11 +28,17 @@ import { useBooleanState } from '../../../../hooks/common/use-boolean-state' */ export const DeleteNoteSidebarEntry: React.FC> = ({ hide, className }) => { useTranslation() + const router = useRouter() const noteId = useApplicationState((state) => state.noteDetails.id) const [modalVisibility, showModal, closeModal] = useBooleanState() const deleteNoteAndCloseDialog = useCallback(() => { - deleteNote(noteId).catch(showErrorNotification('landing.history.error.deleteNote.text')).finally(closeModal) - }, [closeModal, noteId]) + deleteNote(noteId) + .then(() => { + router.push('/history').catch((reason) => logger.error('Error while redirecting to /history', reason)) + }) + .catch(showErrorNotification('landing.history.error.deleteNote.text')) + .finally(closeModal) + }, [closeModal, noteId, router]) return (