-
Notifications
You must be signed in to change notification settings - Fork 2
feat: View PDF document preview when adding a document #689
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e875426
refactor(ScanResultCard): Split component to process image and pdf files
Merkur39 a9e383a
refactor: Move ScanResultCard components to the specific folder
Merkur39 9a8e91f
refactor(ScanResultCardActions): Split component
Merkur39 4152a18
feat: View PDF document preview when adding a document
Merkur39 bd8c628
feat(ScanResultCardPDF): Improve readability filename
Merkur39 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
55 changes: 55 additions & 0 deletions
55
src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCard.jsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import PropTypes from 'prop-types' | ||
import React, { forwardRef } from 'react' | ||
import { useFormData } from 'src/components/Contexts/FormDataProvider' | ||
import { useStepperDialog } from 'src/components/Contexts/StepperDialogProvider' | ||
import ScanResultCardImage from 'src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCardImage' | ||
import ScanResultCardPDF from 'src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCardPDF' | ||
import { | ||
getLastFormDataFile, | ||
isSameFile | ||
} from 'src/components/ModelSteps/helpers' | ||
|
||
const ScanResultCard = forwardRef((props, ref) => { | ||
const { currentFile, setCurrentFile } = props | ||
const { setFormData, formData } = useFormData() | ||
const { currentStepIndex } = useStepperDialog() | ||
|
||
const handleSelectedFile = () => { | ||
const newData = formData.data.filter( | ||
data => !isSameFile(currentFile, data.file) | ||
) | ||
setCurrentFile( | ||
getLastFormDataFile({ formData: { data: newData }, currentStepIndex }) | ||
) | ||
|
||
setFormData(prev => ({ | ||
...prev, | ||
data: newData | ||
})) | ||
} | ||
|
||
if (currentFile.type === 'application/pdf') { | ||
return ( | ||
<ScanResultCardPDF {...props} handleSelectedFile={handleSelectedFile} /> | ||
) | ||
} | ||
|
||
return ( | ||
<ScanResultCardImage | ||
{...props} | ||
handleSelectedFile={handleSelectedFile} | ||
ref={ref} | ||
/> | ||
) | ||
}) | ||
|
||
ScanResultCard.displayName = 'ScanResultCard' | ||
|
||
ScanResultCard.propTypes = { | ||
currentFile: PropTypes.object.isRequired, | ||
setCurrentFile: PropTypes.func.isRequired, | ||
setRotationImage: PropTypes.func.isRequired, | ||
rotationImage: PropTypes.number.isRequired | ||
} | ||
|
||
export default ScanResultCard |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCardImage.jsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import PropTypes from 'prop-types' | ||
import React, { useState, forwardRef } from 'react' | ||
import styles from 'src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCard.styl' | ||
import ScanResultCardImageActions from 'src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCardImageActions' | ||
import RotateImage from 'src/components/ModelSteps/widgets/RotateImage' | ||
|
||
import Box from 'cozy-ui/transpiled/react/Box' | ||
import Card from 'cozy-ui/transpiled/react/Card' | ||
|
||
const ScanResultCardImage = forwardRef((props, ref) => { | ||
const { currentFile, handleSelectedFile, setRotationImage, rotationImage } = | ||
props | ||
const [imgWrapperMinHeight, setImgWrapperMinHeight] = useState(0) | ||
const [isImageRotating, setIsImageRotating] = useState(false) | ||
|
||
const handleRotate = () => { | ||
setIsImageRotating(true) | ||
setRotationImage(prev => prev - 90) | ||
} | ||
|
||
const handleImageLoaded = () => { | ||
setIsImageRotating(false) | ||
// We don't want to recalculate the size on every rotation | ||
if (ref.current && imgWrapperMinHeight === 0) { | ||
const maxSize = Math.max( | ||
ref.current.offsetWidth, | ||
ref.current.offsetHeight | ||
) | ||
setImgWrapperMinHeight(maxSize) | ||
} | ||
} | ||
|
||
return ( | ||
<Card className="u-ta-center u-p-1 u-flex u-flex-column u-flex-justify-between"> | ||
<div className={styles['image-container']}> | ||
<RotateImage | ||
image={URL.createObjectURL(currentFile)} | ||
onLoaded={handleImageLoaded} | ||
rotation={rotationImage} | ||
a11n={{ 'aria-hidden': true }} | ||
ref={ref} | ||
/> | ||
</div> | ||
<Box display="flex" gridGap="1rem" marginTop="1rem"> | ||
<ScanResultCardImageActions | ||
onRotate={handleRotate} | ||
onCancel={handleSelectedFile} | ||
isImageRotating={isImageRotating} | ||
/> | ||
</Box> | ||
</Card> | ||
) | ||
}) | ||
ScanResultCardImage.displayName = 'ScanResultCardImage' | ||
|
||
ScanResultCardImage.propTypes = { | ||
currentFile: PropTypes.object.isRequired, | ||
handleSelectedFile: PropTypes.func.isRequired, | ||
setRotationImage: PropTypes.func.isRequired, | ||
rotationImage: PropTypes.number.isRequired | ||
} | ||
|
||
export default ScanResultCardImage |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCardPDF.jsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import PropTypes from 'prop-types' | ||
import React from 'react' | ||
import styles from 'src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCard.styl' | ||
import ScanResultCardPDFActions from 'src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCardPDFActions' | ||
import { PdfOverview } from 'src/components/PdfOverview/PdfOverview' | ||
|
||
import Box from 'cozy-ui/transpiled/react/Box' | ||
import Card from 'cozy-ui/transpiled/react/Card' | ||
import Typography from 'cozy-ui/transpiled/react/Typography' | ||
|
||
const ScanResultCardPDF = props => { | ||
const { currentFile, handleSelectedFile } = props | ||
// Replace all "_" with a space in the filename for better readability | ||
const fileName = currentFile.name.replaceAll('_', ' ') | ||
|
||
return ( | ||
<Card className="u-ta-center u-p-1 u-flex u-flex-column u-flex-justify-between"> | ||
<div className={styles['image-container']}> | ||
<PdfOverview file={currentFile} /> | ||
<Typography className="u-mt-half">{fileName}</Typography> | ||
</div> | ||
<Box display="flex" gridGap="1rem" marginTop="1rem"> | ||
<ScanResultCardPDFActions onCancel={handleSelectedFile} /> | ||
</Box> | ||
</Card> | ||
) | ||
} | ||
|
||
ScanResultCardPDF.propTypes = { | ||
currentFile: PropTypes.object.isRequired, | ||
handleSelectedFile: PropTypes.func.isRequired | ||
} | ||
|
||
export default ScanResultCardPDF |
29 changes: 29 additions & 0 deletions
29
src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCardPDFActions.jsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import PropTypes from 'prop-types' | ||
import React from 'react' | ||
|
||
import Button from 'cozy-ui/transpiled/react/Buttons' | ||
import useBreakpoints from 'cozy-ui/transpiled/react/providers/Breakpoints' | ||
import { useI18n } from 'cozy-ui/transpiled/react/providers/I18n' | ||
|
||
const ScanResultCardPDFActions = ({ onCancel }) => { | ||
const { t } = useI18n() | ||
const { isDesktop } = useBreakpoints() | ||
|
||
const device = isDesktop ? 'desktop' : 'mobile' | ||
|
||
return ( | ||
<Button | ||
data-testid="retry-button" | ||
label={t(`Acquisition.${device}.retry`)} | ||
fullWidth | ||
variant="secondary" | ||
onClick={onCancel} | ||
/> | ||
) | ||
} | ||
|
||
ScanResultCardPDFActions.propTypes = { | ||
onCancel: PropTypes.func | ||
} | ||
|
||
export default ScanResultCardPDFActions |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import React, { useEffect, useRef, useState } from 'react' | ||
import { Document, Page } from 'react-pdf' | ||
|
||
interface PdfOverviewProps { | ||
file: File | ||
} | ||
|
||
const styles = { | ||
container: { | ||
width: '100%', | ||
display: 'flex', | ||
justifyContent: 'center' | ||
} | ||
} | ||
|
||
export const PdfOverview: React.FC<PdfOverviewProps> = ({ | ||
file | ||
}: PdfOverviewProps) => { | ||
const parentRef = useRef<HTMLDivElement | null>(null) | ||
const [parentWidth, setParentWidth] = useState(0) | ||
|
||
useEffect(() => { | ||
const updateSize = (): void => { | ||
if (parentRef.current) { | ||
const width = parentRef.current.getBoundingClientRect().width | ||
setParentWidth(width) | ||
} | ||
} | ||
updateSize() | ||
}, []) | ||
|
||
return ( | ||
<div style={styles.container} ref={parentRef}> | ||
<Document file={file}> | ||
<Page pageNumber={1} width={parentWidth} /> | ||
</Document> | ||
</div> | ||
) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.