|
| 1 | +import { EditorComponent } from '@src/editors/EditorComponent'; |
| 2 | +import { useFormikContext } from 'formik'; |
| 3 | +import React, { |
| 4 | + PropsWithChildren, useContext, useEffect, useRef, |
| 5 | +} from 'react'; |
| 6 | +import EditorContainer from '@src/editors/containers/EditorContainer'; |
| 7 | +import { PdfBlockContext, PdfState } from '@src/editors/containers/PdfEditor/contexts'; |
| 8 | +import { isEqual } from 'lodash'; |
| 9 | +import DownloadOptions from '@src/editors/containers/PdfEditor/components/sections/DownloadOptions'; |
| 10 | +import { UploadWidget, defaultUploadMessages } from '@src/editors/sharedComponents/UploadWidget'; |
| 11 | +import { Spinner } from '@openedx/paragon'; |
| 12 | +import { FormattedMessage, useIntl } from '@edx/frontend-platform/i18n'; |
| 13 | +import { messages, fileUploadMessages } from './messages'; |
| 14 | + |
| 15 | +const EditorWrapper: React.FC<PropsWithChildren> = ({ children }) => { |
| 16 | + const intl = useIntl(); |
| 17 | + const { isPending, fetchError } = useContext(PdfBlockContext); |
| 18 | + if (fetchError) { |
| 19 | + return ( |
| 20 | + <div className="text-center p-6"> |
| 21 | + <FormattedMessage {...messages.blockFailed} /> |
| 22 | + </div> |
| 23 | + ); |
| 24 | + } |
| 25 | + if (isPending) { |
| 26 | + return ( |
| 27 | + <div className="text-center p-6"> |
| 28 | + <Spinner |
| 29 | + animation="border" |
| 30 | + className="m-3" |
| 31 | + screenReaderText={intl.formatMessage(messages.blockLoading)} |
| 32 | + /> |
| 33 | + </div> |
| 34 | + ); |
| 35 | + } |
| 36 | + return <>{children}</>; /* eslint-disable-line react/jsx-no-useless-fragment */ |
| 37 | +}; |
| 38 | + |
| 39 | +const uploadMessages = { ...defaultUploadMessages, ...fileUploadMessages }; |
| 40 | + |
| 41 | +const PdfEditingModal: React.FC<EditorComponent> = (props) => { |
| 42 | + const { fields } = useContext(PdfBlockContext); |
| 43 | + const originalState = useRef({ ...fields }); |
| 44 | + const { values, setValues } = useFormikContext<PdfState>(); |
| 45 | + |
| 46 | + useEffect(() => { |
| 47 | + // Form is initialized before we get these values, so we have to set them |
| 48 | + // when they arrive. |
| 49 | + void setValues(fields); // eslint-disable-line no-void |
| 50 | + }, [fields]); |
| 51 | + |
| 52 | + const isDirty = () => isEqual(originalState, values); |
| 53 | + |
| 54 | + const getContent = () => { |
| 55 | + const settings = { ...values }; |
| 56 | + // disableAllDownload is not a setting we control, but a backend flag. Have to remove it or the |
| 57 | + // backend will reject. |
| 58 | + return Object.fromEntries(Object.entries(settings).filter(([key]) => key !== 'disableAllDownload')); |
| 59 | + }; |
| 60 | + |
| 61 | + return ( |
| 62 | + <EditorContainer {...props} isDirty={isDirty} getContent={getContent}> |
| 63 | + <EditorWrapper> |
| 64 | + <UploadWidget supportedFileFormats="application/pdf" urlFieldName="url" messages={uploadMessages} /> |
| 65 | + <DownloadOptions /> |
| 66 | + </EditorWrapper> |
| 67 | + </EditorContainer> |
| 68 | + ); |
| 69 | +}; |
| 70 | + |
| 71 | +export default PdfEditingModal; |
0 commit comments