From e8754260e25f2ff9fc88c6c6c42ab424b53ea4ef Mon Sep 17 00:00:00 2001 From: AlexisG Date: Thu, 31 Oct 2024 10:26:04 +0100 Subject: [PATCH 1/5] refactor(ScanResultCard): Split component to process image and pdf files --- .../ModelSteps/ScanResult/ScanResultCard.jsx | 103 ++++++------------ .../ModelSteps/ScanResult/ScanResultCard.styl | 12 +- .../ScanResult/ScanResultCardImage.jsx | 63 +++++++++++ .../ScanResult/ScanResultCardPDF.jsx | 32 ++++++ 4 files changed, 131 insertions(+), 79 deletions(-) create mode 100644 src/components/ModelSteps/ScanResult/ScanResultCardImage.jsx create mode 100644 src/components/ModelSteps/ScanResult/ScanResultCardPDF.jsx diff --git a/src/components/ModelSteps/ScanResult/ScanResultCard.jsx b/src/components/ModelSteps/ScanResult/ScanResultCard.jsx index 6a89b795..41482283 100644 --- a/src/components/ModelSteps/ScanResult/ScanResultCard.jsx +++ b/src/components/ModelSteps/ScanResult/ScanResultCard.jsx @@ -1,89 +1,48 @@ import PropTypes from 'prop-types' -import React, { useState, forwardRef } from 'react' +import React, { forwardRef } from 'react' import { useFormData } from 'src/components/Contexts/FormDataProvider' import { useStepperDialog } from 'src/components/Contexts/StepperDialogProvider' -import styles from 'src/components/ModelSteps/ScanResult/ScanResultCard.styl' -import ScanResultCardActions from 'src/components/ModelSteps/ScanResult/ScanResultCardActions' +import ScanResultCardImage from 'src/components/ModelSteps/ScanResult/ScanResultCardImage' +import ScanResultCardPDF from 'src/components/ModelSteps/ScanResult/ScanResultCardPDF' import { getLastFormDataFile, isSameFile } from 'src/components/ModelSteps/helpers' -import RotateImage from 'src/components/ModelSteps/widgets/RotateImage' -import Box from 'cozy-ui/transpiled/react/Box' -import Card from 'cozy-ui/transpiled/react/Card' -import Icon from 'cozy-ui/transpiled/react/Icon' -import Typography from 'cozy-ui/transpiled/react/Typography' +const ScanResultCard = forwardRef((props, ref) => { + const { currentFile, setCurrentFile } = props + const { setFormData, formData } = useFormData() + const { currentStepIndex } = useStepperDialog() -const isImageType = file => file.type.match(/image\/.*/) - -const ScanResultCard = forwardRef( - ({ currentFile, setCurrentFile, setRotationImage, rotationImage }, ref) => { - const { setFormData, formData } = useFormData() - const { currentStepIndex } = useStepperDialog() - const [imgWrapperMinHeight, setImgWrapperMinHeight] = useState(0) - const [isImageRotating, setIsImageRotating] = useState(false) - - const handleSelectedFile = () => { - const newData = formData.data.filter( - data => !isSameFile(currentFile, data.file) - ) - setCurrentFile( - getLastFormDataFile({ formData: { data: newData }, currentStepIndex }) - ) - - setFormData(prev => ({ - ...prev, - data: newData - })) - } - - const handleRotate = () => { - setIsImageRotating(true) - setRotationImage(prev => prev - 90) - } + const handleSelectedFile = () => { + const newData = formData.data.filter( + data => !isSameFile(currentFile, data.file) + ) + setCurrentFile( + getLastFormDataFile({ formData: { data: newData }, currentStepIndex }) + ) - 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) - } - } + setFormData(prev => ({ + ...prev, + data: newData + })) + } + if (currentFile.type === 'application/pdf') { return ( - -
- {isImageType(currentFile) ? ( - - ) : ( - <> -
- - - -
+ ) } -) + + return ( + + ) +}) + ScanResultCard.displayName = 'ScanResultCard' ScanResultCard.propTypes = { diff --git a/src/components/ModelSteps/ScanResult/ScanResultCard.styl b/src/components/ModelSteps/ScanResult/ScanResultCard.styl index 83b0b06e..a2a319dd 100644 --- a/src/components/ModelSteps/ScanResult/ScanResultCard.styl +++ b/src/components/ModelSteps/ScanResult/ScanResultCard.styl @@ -2,18 +2,16 @@ width 100% padding-top 100% position relative - -.image-container:before - content "" - display block - margin-top -100% - -.image-container display flex flex-direction column align-items center justify-content center + &:before + content "" + display block + margin-top -100% + & > img position absolute top 0 diff --git a/src/components/ModelSteps/ScanResult/ScanResultCardImage.jsx b/src/components/ModelSteps/ScanResult/ScanResultCardImage.jsx new file mode 100644 index 00000000..74902c8b --- /dev/null +++ b/src/components/ModelSteps/ScanResult/ScanResultCardImage.jsx @@ -0,0 +1,63 @@ +import PropTypes from 'prop-types' +import React, { useState, forwardRef } from 'react' +import styles from 'src/components/ModelSteps/ScanResult/ScanResultCard.styl' +import ScanResultCardActions from 'src/components/ModelSteps/ScanResult/ScanResultCardActions' +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 ( + +
+ +
+ + + +
+ ) +}) +ScanResultCardImage.displayName = 'ScanResultCardImage' + +ScanResultCardImage.propTypes = { + currentFile: PropTypes.object.isRequired, + handleSelectedFile: PropTypes.func.isRequired, + setRotationImage: PropTypes.func.isRequired, + rotationImage: PropTypes.number.isRequired +} + +export default ScanResultCardImage diff --git a/src/components/ModelSteps/ScanResult/ScanResultCardPDF.jsx b/src/components/ModelSteps/ScanResult/ScanResultCardPDF.jsx new file mode 100644 index 00000000..86d4e282 --- /dev/null +++ b/src/components/ModelSteps/ScanResult/ScanResultCardPDF.jsx @@ -0,0 +1,32 @@ +import PropTypes from 'prop-types' +import React from 'react' +import styles from 'src/components/ModelSteps/ScanResult/ScanResultCard.styl' +import ScanResultCardActions from 'src/components/ModelSteps/ScanResult/ScanResultCardActions' + +import Box from 'cozy-ui/transpiled/react/Box' +import Card from 'cozy-ui/transpiled/react/Card' +import Icon from 'cozy-ui/transpiled/react/Icon' +import Typography from 'cozy-ui/transpiled/react/Typography' + +const ScanResultCardPDF = props => { + const { currentFile, handleSelectedFile } = props + + return ( + +
+
+ + + +
+ ) +} + +ScanResultCardPDF.propTypes = { + currentFile: PropTypes.object.isRequired, + handleSelectedFile: PropTypes.func.isRequired +} + +export default ScanResultCardPDF From a9e383a94e72269a1cc2eba429ca32884b069f30 Mon Sep 17 00:00:00 2001 From: AlexisG Date: Thu, 31 Oct 2024 10:59:23 +0100 Subject: [PATCH 2/5] refactor: Move ScanResultCard components to the specific folder --- .../ScanResult/{ => ScanResultCard}/ScanResultCard.jsx | 4 ++-- .../ScanResult/{ => ScanResultCard}/ScanResultCard.styl | 0 .../ScanResult/{ => ScanResultCard}/ScanResultCardActions.jsx | 0 .../ScanResult/{ => ScanResultCard}/ScanResultCardImage.jsx | 4 ++-- .../ScanResult/{ => ScanResultCard}/ScanResultCardPDF.jsx | 4 ++-- src/components/ModelSteps/ScanResult/ScanResultDialog.jsx | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) rename src/components/ModelSteps/ScanResult/{ => ScanResultCard}/ScanResultCard.jsx (95%) rename src/components/ModelSteps/ScanResult/{ => ScanResultCard}/ScanResultCard.styl (100%) rename src/components/ModelSteps/ScanResult/{ => ScanResultCard}/ScanResultCardActions.jsx (100%) rename src/components/ModelSteps/ScanResult/{ => ScanResultCard}/ScanResultCardImage.jsx (96%) rename src/components/ModelSteps/ScanResult/{ => ScanResultCard}/ScanResultCardPDF.jsx (93%) diff --git a/src/components/ModelSteps/ScanResult/ScanResultCard.jsx b/src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCard.jsx similarity index 95% rename from src/components/ModelSteps/ScanResult/ScanResultCard.jsx rename to src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCard.jsx index 41482283..200a3a22 100644 --- a/src/components/ModelSteps/ScanResult/ScanResultCard.jsx +++ b/src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCard.jsx @@ -2,8 +2,8 @@ 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/ScanResultCardImage' -import ScanResultCardPDF from 'src/components/ModelSteps/ScanResult/ScanResultCardPDF' +import ScanResultCardImage from 'src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCardImage' +import ScanResultCardPDF from 'src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCardPDF' import { getLastFormDataFile, isSameFile diff --git a/src/components/ModelSteps/ScanResult/ScanResultCard.styl b/src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCard.styl similarity index 100% rename from src/components/ModelSteps/ScanResult/ScanResultCard.styl rename to src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCard.styl diff --git a/src/components/ModelSteps/ScanResult/ScanResultCardActions.jsx b/src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCardActions.jsx similarity index 100% rename from src/components/ModelSteps/ScanResult/ScanResultCardActions.jsx rename to src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCardActions.jsx diff --git a/src/components/ModelSteps/ScanResult/ScanResultCardImage.jsx b/src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCardImage.jsx similarity index 96% rename from src/components/ModelSteps/ScanResult/ScanResultCardImage.jsx rename to src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCardImage.jsx index 74902c8b..848c9c6d 100644 --- a/src/components/ModelSteps/ScanResult/ScanResultCardImage.jsx +++ b/src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCardImage.jsx @@ -1,7 +1,7 @@ import PropTypes from 'prop-types' import React, { useState, forwardRef } from 'react' -import styles from 'src/components/ModelSteps/ScanResult/ScanResultCard.styl' -import ScanResultCardActions from 'src/components/ModelSteps/ScanResult/ScanResultCardActions' +import styles from 'src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCard.styl' +import ScanResultCardActions from 'src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCardActions' import RotateImage from 'src/components/ModelSteps/widgets/RotateImage' import Box from 'cozy-ui/transpiled/react/Box' diff --git a/src/components/ModelSteps/ScanResult/ScanResultCardPDF.jsx b/src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCardPDF.jsx similarity index 93% rename from src/components/ModelSteps/ScanResult/ScanResultCardPDF.jsx rename to src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCardPDF.jsx index 86d4e282..91d85737 100644 --- a/src/components/ModelSteps/ScanResult/ScanResultCardPDF.jsx +++ b/src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCardPDF.jsx @@ -1,7 +1,7 @@ import PropTypes from 'prop-types' import React from 'react' -import styles from 'src/components/ModelSteps/ScanResult/ScanResultCard.styl' -import ScanResultCardActions from 'src/components/ModelSteps/ScanResult/ScanResultCardActions' +import styles from 'src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCard.styl' +import ScanResultCardActions from 'src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCardActions' import Box from 'cozy-ui/transpiled/react/Box' import Card from 'cozy-ui/transpiled/react/Card' diff --git a/src/components/ModelSteps/ScanResult/ScanResultDialog.jsx b/src/components/ModelSteps/ScanResult/ScanResultDialog.jsx index 2397902a..1ae4f02d 100644 --- a/src/components/ModelSteps/ScanResult/ScanResultDialog.jsx +++ b/src/components/ModelSteps/ScanResult/ScanResultDialog.jsx @@ -5,7 +5,7 @@ import CompositeHeaderImage from 'src/components/CompositeHeader/CompositeHeader import { useFormData } from 'src/components/Contexts/FormDataProvider' import { useStepperDialog } from 'src/components/Contexts/StepperDialogProvider' import OcrProcessingDialog from 'src/components/ModelSteps/ScanResult/OcrProcessingDialog' -import ScanResultCard from 'src/components/ModelSteps/ScanResult/ScanResultCard' +import ScanResultCard from 'src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCard' import ScanResultTitle from 'src/components/ModelSteps/ScanResult/ScanResultTitle' import { makeFileFromBase64 } from 'src/components/ModelSteps/helpers' import StepperDialogTitle from 'src/components/StepperDialog/StepperDialogTitle' From 9a8e91fb985067bde5317a3bdfd4c9216f7bd7fd Mon Sep 17 00:00:00 2001 From: AlexisG Date: Thu, 31 Oct 2024 11:07:00 +0100 Subject: [PATCH 3/5] refactor(ScanResultCardActions): Split component --- .../ScanResultCard/ScanResultCardImage.jsx | 4 +-- ...ons.jsx => ScanResultCardImageActions.jsx} | 10 +++++-- .../ScanResultCard/ScanResultCardPDF.jsx | 4 +-- .../ScanResultCardPDFActions.jsx | 29 +++++++++++++++++++ 4 files changed, 40 insertions(+), 7 deletions(-) rename src/components/ModelSteps/ScanResult/ScanResultCard/{ScanResultCardActions.jsx => ScanResultCardImageActions.jsx} (88%) create mode 100644 src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCardPDFActions.jsx diff --git a/src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCardImage.jsx b/src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCardImage.jsx index 848c9c6d..995c9ac6 100644 --- a/src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCardImage.jsx +++ b/src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCardImage.jsx @@ -1,7 +1,7 @@ import PropTypes from 'prop-types' import React, { useState, forwardRef } from 'react' import styles from 'src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCard.styl' -import ScanResultCardActions from 'src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCardActions' +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' @@ -42,7 +42,7 @@ const ScanResultCardImage = forwardRef((props, ref) => { /> - ({ } })) -const ScanResultCardActions = ({ onRotate, onCancel, isImageRotating }) => { +const ScanResultCardImageActions = ({ + onRotate, + onCancel, + isImageRotating +}) => { const classes = useStyles() const { t } = useI18n() const { isDesktop } = useBreakpoints() @@ -49,10 +53,10 @@ const ScanResultCardActions = ({ onRotate, onCancel, isImageRotating }) => { ) } -ScanResultCardActions.propTypes = { +ScanResultCardImageActions.propTypes = { onCancel: PropTypes.func, onRotate: PropTypes.func, isImageRotating: PropTypes.bool } -export default ScanResultCardActions +export default ScanResultCardImageActions diff --git a/src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCardPDF.jsx b/src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCardPDF.jsx index 91d85737..71ab8d23 100644 --- a/src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCardPDF.jsx +++ b/src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCardPDF.jsx @@ -1,7 +1,7 @@ import PropTypes from 'prop-types' import React from 'react' import styles from 'src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCard.styl' -import ScanResultCardActions from 'src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCardActions' +import ScanResultCardPDFActions from 'src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCardPDFActions' import Box from 'cozy-ui/transpiled/react/Box' import Card from 'cozy-ui/transpiled/react/Card' @@ -18,7 +18,7 @@ const ScanResultCardPDF = props => { {currentFile.name} - + ) diff --git a/src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCardPDFActions.jsx b/src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCardPDFActions.jsx new file mode 100644 index 00000000..7d49e6fe --- /dev/null +++ b/src/components/ModelSteps/ScanResult/ScanResultCard/ScanResultCardPDFActions.jsx @@ -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 ( +