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 <github@erik.michelson.eu>
This commit is contained in:
Erik Michelson 2022-08-14 23:12:56 +02:00
parent d57b2f27fd
commit 6cc11d07e3
3 changed files with 25 additions and 5 deletions

View file

@ -3,7 +3,7 @@
* *
* SPDX-License-Identifier: AGPL-3.0-only * 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 type { MediaUpload } from '../media/types'
import { GetApiRequestBuilder } from '../common/api-request-builder/get-api-request-builder' import { GetApiRequestBuilder } from '../common/api-request-builder/get-api-request-builder'
import { PostApiRequestBuilder } from '../common/api-request-builder/post-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. * @throws {Error} when the api request wasn't successful.
*/ */
export const deleteNote = async (noteIdOrAlias: string): Promise<void> => { export const deleteNote = async (noteIdOrAlias: string): Promise<void> => {
await new DeleteApiRequestBuilder('notes/' + noteIdOrAlias).sendRequest() await new DeleteApiRequestBuilder<void, NoteDeletionOptions>('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()
} }

View file

@ -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 * SPDX-License-Identifier: AGPL-3.0-only
*/ */
@ -50,3 +50,7 @@ export interface NoteGroupPermissionEntry {
groupName: string groupName: string
canEdit: boolean canEdit: boolean
} }
export interface NoteDeletionOptions {
keepMedia: boolean
}

View file

@ -15,6 +15,10 @@ import { showErrorNotification } from '../../../../redux/ui-notifications/method
import { deleteNote } from '../../../../api/notes' import { deleteNote } from '../../../../api/notes'
import { DeleteNoteModal } from './delete-note-modal' import { DeleteNoteModal } from './delete-note-modal'
import { useBooleanState } from '../../../../hooks/common/use-boolean-state' 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. * 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<PropsWithChildren<SpecificSidebarEntryProps>> = ({ hide, className }) => { export const DeleteNoteSidebarEntry: React.FC<PropsWithChildren<SpecificSidebarEntryProps>> = ({ hide, className }) => {
useTranslation() useTranslation()
const router = useRouter()
const noteId = useApplicationState((state) => state.noteDetails.id) const noteId = useApplicationState((state) => state.noteDetails.id)
const [modalVisibility, showModal, closeModal] = useBooleanState() const [modalVisibility, showModal, closeModal] = useBooleanState()
const deleteNoteAndCloseDialog = useCallback(() => { const deleteNoteAndCloseDialog = useCallback(() => {
deleteNote(noteId).catch(showErrorNotification('landing.history.error.deleteNote.text')).finally(closeModal) deleteNote(noteId)
}, [closeModal, 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 ( return (
<Fragment> <Fragment>