Skip to content

Commit

Permalink
fix(harvest): Check for file infos before fetch
Browse files Browse the repository at this point in the history
We had a bug because `useDataCardFiles`
was called with `(undefined, undefined)`
This bug happened because `useParams` was used after `useDataCardFiles`

But it still returned some fallback values due to query cache.
So we always defaulted to some files the useQuery was able to get.

Now we will throw if the hook is called without arguments.
This case wasn't supposed to exist,
as such it should be treated as an error

It would be difficult to know what to do with empty arguments.
Are they coming later? never?

On the other hand I'm not sure at this moment if
there are scenarios where that error can happen.
We have no elegant error handler for it as of this commit.
  • Loading branch information
acezard committed Apr 6, 2023
1 parent e3b0b52 commit 32986b6
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
3 changes: 1 addition & 2 deletions packages/cozy-harvest-lib/src/datacards/ViewerModal.jsx
Expand Up @@ -8,10 +8,9 @@ import { useDataCardFiles } from './useDataCardFiles'
import { MountPointContext } from '../components/MountPointContext'

export const ViewerModal = () => {
const { accountId, folderToSaveId, fileIndex } = useParams()
const { pushHistory, replaceHistory } = useContext(MountPointContext)
const { data, fetchStatus } = useDataCardFiles(accountId, folderToSaveId)
const { accountId, folderToSaveId, fileIndex } = useParams()

const handleCloseViewer = () => replaceHistory(`/accounts/${accountId}`)
const handleFileChange = (_file, newIndex) =>
pushHistory(`/viewer/${accountId}/${folderToSaveId}/${newIndex}`)
Expand Down
3 changes: 3 additions & 0 deletions packages/cozy-harvest-lib/src/datacards/useDataCardFiles.js
Expand Up @@ -74,6 +74,9 @@ const getResponse = (folderToSaveFiles, sourceAccountFiles) => {
}

export const useDataCardFiles = (accountId, folderToSaveId) => {
if (!accountId || !folderToSaveId)
throw new Error('Missing arguments in useDataCardFiles, cannot fetch files')

const folderToSaveFiles = useFolderToSaveFiles(folderToSaveId)
const sourceAccountFiles = useSourceAccountFiles(accountId)

Expand Down

0 comments on commit 32986b6

Please sign in to comment.