-
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(app, api-client, react-api-client): add api-client method for pr…
- Loading branch information
Showing
9 changed files
with
241 additions
and
24 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,28 @@ | ||
import { POST, request } from '../request' | ||
|
||
import type { ProtocolAnalysisSummary } from '@opentrons/shared-data' | ||
import type { ResponsePromise } from '../request' | ||
import type { HostConfig } from '../types' | ||
import type { RunTimeParameterCreateData } from '../runs' | ||
|
||
interface CreateProtocolAnalysisData { | ||
runTimeParameterValues: RunTimeParameterCreateData | ||
forceReAnalyze: boolean | ||
} | ||
|
||
export function createProtocolAnalysis( | ||
config: HostConfig, | ||
protocolKey: string, | ||
runTimeParameterValues?: RunTimeParameterCreateData, | ||
forceReAnalyze?: boolean | ||
): ResponsePromise<ProtocolAnalysisSummary[]> { | ||
const data = { | ||
runTimeParameterValues: runTimeParameterValues ?? {}, | ||
forceReAnalyze: forceReAnalyze ?? false, | ||
} | ||
const response = request< | ||
ProtocolAnalysisSummary[], | ||
{ data: CreateProtocolAnalysisData } | ||
>(POST, `/protocols/${protocolKey}/analyses`, { data }, config) | ||
return 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
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
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
77 changes: 77 additions & 0 deletions
77
react-api-client/src/protocols/__tests__/useCreateProtocolAnalysisMutation.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,77 @@ | ||
import * as React from 'react' | ||
import { describe, it, expect, beforeEach, vi } from 'vitest' | ||
import { QueryClient, QueryClientProvider } from 'react-query' | ||
import { act, renderHook, waitFor } from '@testing-library/react' | ||
import { createProtocolAnalysis } from '@opentrons/api-client' | ||
import { useHost } from '../../api' | ||
import { useCreateProtocolAnalysisMutation } from '..' | ||
import type { HostConfig, Response } from '@opentrons/api-client' | ||
import type { ProtocolAnalysisSummary } from '@opentrons/shared-data' | ||
|
||
vi.mock('@opentrons/api-client') | ||
vi.mock('../../api/useHost') | ||
|
||
const HOST_CONFIG: HostConfig = { hostname: 'localhost' } | ||
const ANALYSIS_SUMMARY_RESPONSE = [ | ||
{ id: 'fakeAnalysis1', status: 'completed' }, | ||
{ id: 'fakeAnalysis2', status: 'pending' }, | ||
] as ProtocolAnalysisSummary[] | ||
|
||
describe('useCreateProtocolAnalysisMutation 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 when calling createProtocolAnalysis if the request fails', async () => { | ||
vi.mocked(useHost).mockReturnValue(HOST_CONFIG) | ||
vi.mocked(createProtocolAnalysis).mockRejectedValue('oh no') | ||
|
||
const { result } = renderHook( | ||
() => useCreateProtocolAnalysisMutation('fake-protocol-key'), | ||
{ | ||
wrapper, | ||
} | ||
) | ||
|
||
expect(result.current.data).toBeUndefined() | ||
result.current.createProtocolAnalysis({ | ||
protocolKey: 'fake-protocol-key', | ||
runTimeParameterValues: {}, | ||
}) | ||
await waitFor(() => { | ||
expect(result.current.data).toBeUndefined() | ||
}) | ||
}) | ||
|
||
it('should create an array of ProtocolAnalysisSummaries when calling the createProtocolAnalysis callback', async () => { | ||
vi.mocked(useHost).mockReturnValue(HOST_CONFIG) | ||
vi.mocked(createProtocolAnalysis).mockResolvedValue({ | ||
data: ANALYSIS_SUMMARY_RESPONSE, | ||
} as Response<ProtocolAnalysisSummary[]>) | ||
|
||
const { result } = renderHook( | ||
() => useCreateProtocolAnalysisMutation('fake-protocol-key'), | ||
{ | ||
wrapper, | ||
} | ||
) | ||
act(() => | ||
result.current.createProtocolAnalysis({ | ||
protocolKey: 'fake-protocol-key', | ||
runTimeParameterValues: {}, | ||
}) | ||
) | ||
|
||
await waitFor(() => { | ||
expect(result.current.data).toEqual(ANALYSIS_SUMMARY_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
86 changes: 86 additions & 0 deletions
86
react-api-client/src/protocols/useCreateProtocolAnalysisMutation.ts
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,86 @@ | ||
import { createProtocolAnalysis } from '@opentrons/api-client' | ||
import { useMutation, useQueryClient } from 'react-query' | ||
import { useHost } from '../api' | ||
import type { | ||
ErrorResponse, | ||
HostConfig, | ||
RunTimeParameterCreateData, | ||
} from '@opentrons/api-client' | ||
import type { ProtocolAnalysisSummary } from '@opentrons/shared-data' | ||
import type { AxiosError } from 'axios' | ||
import type { | ||
UseMutationResult, | ||
UseMutationOptions, | ||
UseMutateFunction, | ||
} from 'react-query' | ||
|
||
export interface CreateProtocolAnalysisVariables { | ||
protocolKey: string | ||
runTimeParameterValues?: RunTimeParameterCreateData | ||
forceReAnalyze?: boolean | ||
} | ||
export type UseCreateProtocolMutationResult = UseMutationResult< | ||
ProtocolAnalysisSummary[], | ||
AxiosError<ErrorResponse>, | ||
CreateProtocolAnalysisVariables | ||
> & { | ||
createProtocolAnalysis: UseMutateFunction< | ||
ProtocolAnalysisSummary[], | ||
AxiosError<ErrorResponse>, | ||
CreateProtocolAnalysisVariables | ||
> | ||
} | ||
|
||
export type UseCreateProtocolAnalysisMutationOptions = UseMutationOptions< | ||
ProtocolAnalysisSummary[], | ||
AxiosError<ErrorResponse>, | ||
CreateProtocolAnalysisVariables | ||
> | ||
|
||
export function useCreateProtocolAnalysisMutation( | ||
protocolId: string | null, | ||
hostOverride?: HostConfig | null, | ||
options: UseCreateProtocolAnalysisMutationOptions | undefined = {} | ||
): UseCreateProtocolMutationResult { | ||
const contextHost = useHost() | ||
const host = | ||
hostOverride != null ? { ...contextHost, ...hostOverride } : contextHost | ||
const queryClient = useQueryClient() | ||
|
||
const mutation = useMutation< | ||
ProtocolAnalysisSummary[], | ||
AxiosError<ErrorResponse>, | ||
CreateProtocolAnalysisVariables | ||
>( | ||
[host, 'protocols', protocolId, 'analyses'], | ||
({ protocolKey, runTimeParameterValues, forceReAnalyze }) => | ||
createProtocolAnalysis( | ||
host as HostConfig, | ||
protocolKey, | ||
runTimeParameterValues, | ||
forceReAnalyze | ||
) | ||
.then(response => { | ||
queryClient | ||
.invalidateQueries([host, 'protocols', protocolId, 'analyses']) | ||
.then(() => | ||
queryClient.setQueryData( | ||
[host, 'protocols', protocolId, 'analyses'], | ||
response.data | ||
) | ||
) | ||
.catch((e: Error) => { | ||
throw e | ||
}) | ||
return response.data | ||
}) | ||
.catch((e: Error) => { | ||
throw e | ||
}), | ||
options | ||
) | ||
return { | ||
...mutation, | ||
createProtocolAnalysis: mutation.mutate, | ||
} | ||
} |