-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(mespapiers): Split Scan's components
For better maintainability
- Loading branch information
Showing
6 changed files
with
152 additions
and
100 deletions.
There are no files selected for viewing
This file contains 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
22 changes: 22 additions & 0 deletions
22
packages/cozy-mespapiers-lib/src/components/ModelSteps/ScanActionsWrapper.jsx
This file contains 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,22 @@ | ||
import PropTypes from 'prop-types' | ||
import React from 'react' | ||
|
||
import { isMobile } from 'cozy-device-helper' | ||
|
||
import ScanDesktopActions from './ScanDesktopActions' | ||
import ScanMobileActions from './ScanMobileActions' | ||
|
||
const ScanActionsWrapper = props => { | ||
if (isMobile()) { | ||
return <ScanMobileActions {...props} /> | ||
} | ||
|
||
return <ScanDesktopActions {...props} /> | ||
} | ||
|
||
ScanActionsWrapper.propTypes = { | ||
onChangeFile: PropTypes.func, | ||
openFilePickerModal: PropTypes.func, | ||
} | ||
|
||
export default ScanActionsWrapper |
This file contains 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
96 changes: 96 additions & 0 deletions
96
packages/cozy-mespapiers-lib/src/components/ModelSteps/ScanWrapper.jsx
This file contains 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,96 @@ | ||
import React, { useState, useEffect } from 'react' | ||
|
||
import { useClient, models } from 'cozy-client' | ||
import Alerter from 'cozy-ui/transpiled/react/Alerter' | ||
|
||
import AcquisitionResult from './AcquisitionResult' | ||
import Scan from './Scan' | ||
import { isFileAlreadySelected } from './helpers' | ||
import { PaperDefinitionsStepPropTypes } from '../../constants/PaperDefinitionsPropTypes' | ||
import { makeBlobWithCustomAttrs } from '../../helpers/makeBlobWithCustomAttrs' | ||
import { useFormData } from '../Hooks/useFormData' | ||
|
||
const { fetchBlobFileById } = models.file | ||
|
||
// TODO Waiting for this type of filter to be implemented on the FilePicker side | ||
// https://github.com/cozy/cozy-ui/issues/2026 | ||
const validFileType = file => { | ||
const regexValidation = /(image\/*)|(application\/pdf)/ | ||
return regexValidation.test(file.type) | ||
} | ||
|
||
const ScanWrapper = ({ currentStep }) => { | ||
const client = useClient() | ||
const { formData, setFormData } = useFormData() | ||
const { stepIndex, multipage, page } = currentStep | ||
const [currentFile, setCurrentFile] = useState(null) | ||
|
||
const onChangeFile = file => { | ||
if (file) { | ||
setCurrentFile(file) | ||
if (!isFileAlreadySelected(formData, stepIndex, file)) { | ||
setFormData(prev => ({ | ||
...prev, | ||
data: [ | ||
...prev.data, | ||
{ | ||
file: file, | ||
stepIndex, | ||
fileMetadata: { | ||
page: !multipage ? page : '', | ||
multipage | ||
} | ||
} | ||
] | ||
})) | ||
} | ||
} | ||
} | ||
|
||
const onChangeFilePicker = async cozyFileId => { | ||
const blobFile = await fetchBlobFileById(client, cozyFileId) | ||
if (validFileType(blobFile)) { | ||
const blobFileCustom = makeBlobWithCustomAttrs(blobFile, { | ||
id: cozyFileId | ||
}) | ||
onChangeFile(blobFileCustom) | ||
} else { | ||
Alerter.error('Scan.modal.validFileType', { | ||
duration: 3000 | ||
}) | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
const data = formData.data.filter(data => data.stepIndex === stepIndex) | ||
const { file } = data[data.length - 1] || {} | ||
|
||
if (file) { | ||
setCurrentFile(file) | ||
} | ||
}, [formData.data, stepIndex]) | ||
|
||
if (currentFile) { | ||
return ( | ||
<AcquisitionResult | ||
currentFile={currentFile} | ||
setCurrentFile={setCurrentFile} | ||
currentStep={currentStep} | ||
/> | ||
) | ||
} | ||
|
||
return ( | ||
<Scan | ||
currentStep={currentStep} | ||
onChangeFile={onChangeFile} | ||
onChangeFilePicker={onChangeFilePicker} | ||
/> | ||
) | ||
} | ||
|
||
ScanWrapper.propTypes = { | ||
currentStep: PaperDefinitionsStepPropTypes | ||
} | ||
|
||
export default ScanWrapper |
This file contains 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 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