Skip to content
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

fix: Save a new paper from the Cozy filePicker #674

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/components/ModelSteps/Scan/ScanWrapper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
removeCreatePaperDataBackup,
storeCreatePaperDataBackup
} from 'src/helpers/paperDataBackup'
import { buildFileQueryById } from 'src/queries'

import { useClient } from 'cozy-client'
import { fetchBlobFileById } from 'cozy-client/dist/models/file'
Expand Down Expand Up @@ -96,10 +97,15 @@ const ScanWrapper = ({ currentStep, onClose, onBack }) => {
}

const onChangeFilePicker = async cozyFileId => {
const buildedFileQueryById = buildFileQueryById(cozyFileId)
const { data: fileSelected } = await client.query(
buildedFileQueryById.definition,
buildedFileQueryById.options
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on ne gère pas le loading ici et si je comprends bien le fetch se fait après un click sur un bouton (quand on souhaite sélectionner un fichier), ce n'est pas gênant avec une connexion lente ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ici le fichier sera déjà dans le store puisque sélectionné au préalable. Donc pas de soucis lié au réseaux 👍

const blobFile = await fetchBlobFileById(client, cozyFileId)
const file = makeFileFromBlob(blobFile, {
name: cozyFileId,
from: 'cozy'
name: fileSelected.name,
cozyId: cozyFileId
})
await onChangeFile(file)
}
Expand Down
16 changes: 6 additions & 10 deletions src/components/ModelSteps/widgets/SubmitButton.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { createPdfAndSave } from 'src/helpers/createPdfAndSave'
import getOrCreateAppFolderWithReference from 'src/helpers/getFolderWithReference'

import { useClient } from 'cozy-client'
import { models } from 'cozy-client'
import { Qualification } from 'cozy-client/dist/models/document'
import minilog from 'cozy-minilog'
import Button from 'cozy-ui/transpiled/react/Buttons'
import useEventListener from 'cozy-ui/transpiled/react/hooks/useEventListener'
Expand All @@ -22,10 +22,6 @@ import { useI18n } from 'cozy-ui/transpiled/react/providers/I18n'

const log = minilog('SubmitButton')

const {
document: { Qualification }
} = models

const SubmitButton = ({ onSubmit, disabled, formData }) => {
const [isBusy, setIsBusy] = useState(false)
const [confirmReplaceFileModal, setConfirmReplaceFileModal] = useState(false)
Expand All @@ -35,7 +31,7 @@ const SubmitButton = ({ onSubmit, disabled, formData }) => {
const { currentDefinition, stepperDialogTitle, isEdit } = useStepperDialog()
const { showAlert } = useAlert()

const cozyFiles = formData.data.filter(d => d.file.from === 'cozy')
const filesFromCozy = formData.data.filter(d => d.file.cozyId)
const blankFiles = formData.data.filter(d => d.file.isBlank)
const wasInitiallyBlank =
formData.metadata[FILES_DOCTYPE]?.paperProps?.isBlank ?? false
Expand Down Expand Up @@ -79,15 +75,15 @@ const SubmitButton = ({ onSubmit, disabled, formData }) => {

const handleReplace = async isFileReplaced => {
if (isFileReplaced) {
for (const { file } of cozyFiles) {
await client.destroy({ _id: file.id, _type: FILES_DOCTYPE })
for (const { file } of filesFromCozy) {
await client.destroy({ _id: file.cozyId, _type: FILES_DOCTYPE })
}
}
submit()
}

const handleClick = async () => {
if (cozyFiles.length > 0) {
if (filesFromCozy.length > 0) {
return setConfirmReplaceFileModal(true)
}

Expand Down Expand Up @@ -140,7 +136,7 @@ const SubmitButton = ({ onSubmit, disabled, formData }) => {
<ConfirmReplaceFile
onClose={() => setConfirmReplaceFileModal(false)}
onReplace={handleReplace}
cozyFilesCount={cozyFiles.length}
cozyFilesCount={filesFromCozy.length}
/>
)}
</>
Expand Down
2 changes: 1 addition & 1 deletion src/components/ModelSteps/widgets/SubmitButton.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('ContactDialog', () => {
const mockFormSubmit = jest.fn()
const mockFetchCurrentUser = jest.fn(() => ({ _id: '1234' }))
const cozyFile = new File([{}], 'cozyFile')
cozyFile.from = 'cozy'
cozyFile.cozyId = '1234'
const { findByTestId, queryByTestId } = setup({
formData: mockFormData({ data: [{ file: cozyFile }] }),
mockFetchCurrentUser,
Expand Down
Loading