Note Loading Boundary: Replace redirect with refetch from API (#2275)

This commit is contained in:
Tilman Vatteroth 2022-08-13 14:32:19 +02:00 committed by GitHub
parent fa53bccb03
commit f2ed9d4453
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 54 additions and 28 deletions

View file

@ -5,21 +5,24 @@
*/
import { Trans, useTranslation } from 'react-i18next'
import React, { useCallback } from 'react'
import React, { useCallback, useEffect } from 'react'
import { Alert, Button } from 'react-bootstrap'
import { useSingleStringUrlParameter } from '../../../hooks/common/use-single-string-url-parameter'
import { createNoteWithPrimaryAlias } from '../../../api/notes'
import { useAsyncFn } from 'react-use'
import { ShowIf } from '../show-if/show-if'
import { Redirect } from '../redirect'
import { ForkAwesomeIcon } from '../fork-awesome/fork-awesome-icon'
import { testId } from '../../../utils/test-id'
export interface CreateNonExistingNoteHintProps {
onNoteCreated: () => void
}
/**
* Shows a button that creates an empty note with the alias from the current window URL.
* When the button was clicked it also shows the progress.
*/
export const CreateNonExistingNoteHint: React.FC = () => {
export const CreateNonExistingNoteHint: React.FC<CreateNonExistingNoteHintProps> = ({ onNoteCreated }) => {
useTranslation()
const noteIdFromUrl = useSingleStringUrlParameter('noteId', undefined)
@ -34,10 +37,21 @@ export const CreateNonExistingNoteHint: React.FC = () => {
void createNote()
}, [createNote])
useEffect(() => {
if (returnState.value !== undefined) {
onNoteCreated()
}
}, [onNoteCreated, returnState.value])
if (noteIdFromUrl === undefined) {
return null
} else if (returnState.value) {
return <Redirect to={`/n/${returnState.value.metadata.primaryAddress}`} />
return (
<Alert variant={'info'} {...testId('noteCreated')} className={'mt-5'}>
<ForkAwesomeIcon icon={'check-circle'} className={'mr-2'} />
<Trans i18nKey={'noteLoadingBoundary.createNote.success'} />
</Alert>
)
} else if (returnState.loading) {
return (
<Alert variant={'info'} {...testId('loadingMessage')} className={'mt-5'}>