-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api-client, app): implement download CSV file (#15861)
Here, I create api-client functions and types for downloading CSV file content from the robot server's `dataFiles/{fileId}/download` endpoint. I also add a react-api-client wrapper hook and implement in the `HistoricalProtocolRunDrawer` component.
- Loading branch information
Showing
9 changed files
with
174 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { GET, request } from '../request' | ||
|
||
import type { DownloadedCsvFileResponse } from './types' | ||
import type { ResponsePromise } from '../request' | ||
import type { HostConfig } from '../types' | ||
|
||
export function getCsvFileRaw( | ||
config: HostConfig, | ||
fileId: string | ||
): ResponsePromise<DownloadedCsvFileResponse> { | ||
return request<DownloadedCsvFileResponse>( | ||
GET, | ||
`/dataFiles/${fileId}/download`, | ||
null, | ||
config | ||
) | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { getCsvFileRaw } from './getCsvFileRaw' | ||
export { uploadCsvFile } from './uploadCsvFile' | ||
|
||
export * from './types' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import * as React from 'react' | ||
import { useTranslation } from 'react-i18next' | ||
|
||
import { | ||
ALIGN_CENTER, | ||
Flex, | ||
Icon, | ||
LegacyStyledText, | ||
Link, | ||
SPACING, | ||
TYPOGRAPHY, | ||
} from '@opentrons/components' | ||
import { useCsvFileRawQuery } from '@opentrons/react-api-client' | ||
import { downloadFile } from './utils' | ||
|
||
interface DownloadCsvFileLinkProps { | ||
fileId: string | ||
fileName: string | ||
} | ||
export function DownloadCsvFileLink( | ||
props: DownloadCsvFileLinkProps | ||
): JSX.Element { | ||
const { fileId, fileName } = props | ||
const { t } = useTranslation('run_details') | ||
const { data: csvFileRaw } = useCsvFileRawQuery(fileId) | ||
|
||
return ( | ||
<Link | ||
role="button" | ||
css={ | ||
csvFileRaw == null | ||
? TYPOGRAPHY.darkLinkLabelSemiBoldDisabled | ||
: TYPOGRAPHY.linkPSemiBold | ||
} | ||
onClick={() => { | ||
if (csvFileRaw != null) { | ||
downloadFile(csvFileRaw, fileName) | ||
} | ||
}} | ||
> | ||
<Flex alignItems={ALIGN_CENTER} gridGap={SPACING.spacing4}> | ||
<LegacyStyledText as="p">{t('download')}</LegacyStyledText> | ||
<Icon name="download" size="1rem" /> | ||
</Flex> | ||
</Link> | ||
) | ||
} |
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
70 changes: 70 additions & 0 deletions
70
react-api-client/src/dataFiles/__tests__/useCsvFileRawQuery.test.tsx
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,70 @@ | ||
import * as React from 'react' | ||
import { describe, it, expect, beforeEach, vi } from 'vitest' | ||
import { QueryClient, QueryClientProvider } from 'react-query' | ||
import { renderHook, waitFor } from '@testing-library/react' | ||
import { getCsvFileRaw } from '@opentrons/api-client' | ||
import { useHost } from '../../api' | ||
import { useCsvFileRawQuery } from '..' | ||
|
||
import type { | ||
HostConfig, | ||
Response, | ||
DownloadedCsvFileResponse, | ||
} from '@opentrons/api-client' | ||
|
||
vi.mock('@opentrons/api-client') | ||
vi.mock('../../api/useHost') | ||
|
||
const HOST_CONFIG: HostConfig = { hostname: 'localhost' } | ||
const FILE_ID = 'file123' | ||
const FILE_CONTENT_RESPONSE = 'content,of,my,csv\nfile,' as DownloadedCsvFileResponse | ||
|
||
describe('useCsvFileRawQuery hook', () => { | ||
let wrapper: React.FunctionComponent<{ children: React.ReactNode }> | ||
|
||
beforeEach(() => { | ||
const queryClient = new QueryClient() | ||
const clientProvider: React.FunctionComponent<{ | ||
children: React.ReactNode | ||
}> = ({ children }) => ( | ||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider> | ||
) | ||
|
||
wrapper = clientProvider | ||
}) | ||
|
||
it('should return no data if no host', () => { | ||
vi.mocked(useHost).mockReturnValue(null) | ||
|
||
const { result } = renderHook(() => useCsvFileRawQuery(FILE_ID), { | ||
wrapper, | ||
}) | ||
|
||
expect(result.current.data).toBeUndefined() | ||
}) | ||
|
||
it('should return no data if the get file request fails', () => { | ||
vi.mocked(useHost).mockReturnValue(HOST_CONFIG) | ||
vi.mocked(getCsvFileRaw).mockRejectedValue('oh no') | ||
|
||
const { result } = renderHook(() => useCsvFileRawQuery(FILE_ID), { | ||
wrapper, | ||
}) | ||
expect(result.current.data).toBeUndefined() | ||
}) | ||
|
||
it('should return file data if successful request', async () => { | ||
vi.mocked(useHost).mockReturnValue(HOST_CONFIG) | ||
vi.mocked(getCsvFileRaw).mockResolvedValue({ | ||
data: FILE_CONTENT_RESPONSE, | ||
} as Response<DownloadedCsvFileResponse>) | ||
|
||
const { result } = renderHook(() => useCsvFileRawQuery(FILE_ID), { | ||
wrapper, | ||
}) | ||
|
||
await waitFor(() => { | ||
expect(result.current.data).toEqual(FILE_CONTENT_RESPONSE) | ||
}) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export { useCsvFileRawQuery } from './useCsvFileRawQuery' | ||
export { useUploadCsvFileMutation } from './useUploadCsvFileMutation' |
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,28 @@ | ||
import { useQuery } from 'react-query' | ||
import { getCsvFileRaw } from '@opentrons/api-client' | ||
import { useHost } from '../api' | ||
|
||
import type { UseQueryOptions, UseQueryResult } from 'react-query' | ||
import type { | ||
HostConfig, | ||
DownloadedCsvFileResponse, | ||
} from '@opentrons/api-client' | ||
|
||
export function useCsvFileRawQuery( | ||
fileId: string, | ||
options?: UseQueryOptions<DownloadedCsvFileResponse> | ||
): UseQueryResult<DownloadedCsvFileResponse> { | ||
const host = useHost() | ||
const allOptions: UseQueryOptions<DownloadedCsvFileResponse> = { | ||
...options, | ||
enabled: host !== null && fileId !== null, | ||
} | ||
|
||
const query = useQuery<DownloadedCsvFileResponse>( | ||
[host, `/dataFiles/${fileId}/download`], | ||
() => | ||
getCsvFileRaw(host as HostConfig, fileId).then(response => response.data), | ||
allOptions | ||
) | ||
return query | ||
} |