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

[Pending] feat(app) App feat add get robotName #10871

Closed
wants to merge 6 commits into from
Closed
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
11 changes: 11 additions & 0 deletions api-client/src/server/getRobotName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { GET, request } from '../request'

import type { ResponsePromise } from '../request'
import type { HostConfig } from '../types'
import type { CurrentRobotName } from './types'

export function getRobotName(
config: HostConfig
): ResponsePromise<CurrentRobotName> {
return request<CurrentRobotName>(GET, '/server/name', null, config)
}
1 change: 1 addition & 0 deletions api-client/src/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { getRobotName } from './getRobotName'
export { updateRobotName } from './updateRobotName'
export * from './types'
4 changes: 4 additions & 0 deletions api-client/src/server/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export interface CurrentRobotName {
name: string
}

export interface UpdatedRobotName {
name: string
}
Comment on lines +1 to 7
Copy link
Contributor Author

@koji koji Jun 23, 2022

Choose a reason for hiding this comment

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

[Question to reviewers]
Should I combine these two interfaces into one like RobotName?
RobotName might be vague? In addition, we use robotName in many components so maybe a different name would be better?

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
SPACING,
COLORS,
} from '@opentrons/components'

import { useUpdateRobotNameMutation } from '@opentrons/react-api-client'
import { removeRobot } from '../../../../../redux/discovery'
import { Slideout } from '../../../../../atoms/Slideout'
Expand Down Expand Up @@ -73,6 +74,9 @@ export function RenameRobotSlideout({
// remove the previous robot name from the list
dispatch(removeRobot(previousRobotName))
// data.name != null && history.push(`/devices/${data.name}/robot-settings`)
// TODO kj: this is a temporary fix to avoid Download logs button disabled issue
// Once fix react tree rendering issue, the push direction will be switched to robot-settings
// TODO: useRobotName for redirect
// TODO 6/9/2022 kj this is a temporary fix to avoid the issue
// https://github.com/Opentrons/opentrons/issues/10709
data.name != null && history.push(`/devices`)
Expand Down
56 changes: 56 additions & 0 deletions app/src/organisms/Devices/hooks/useRobotAnalyticsData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import * as React from 'react'
import { useSelector, useDispatch } from 'react-redux'

import { useRobot } from './'
import { getAttachedPipettes } from '../../../redux/pipettes'
import { getRobotSettings, fetchSettings } from '../../../redux/robot-settings'
import { FF_PREFIX } from '../../../redux/analytics'
import {
getRobotApiVersion,
getRobotFirmwareVersion,
} from '../../../redux/discovery'

import type { State, Dispatch } from '../../../redux/types'
import type { RobotAnalyticsData } from '../../../redux/analytics/types'

/**
*
* @param {string} robotName
* @returns {RobotAnalyticsData}
* for use in trackEvent
*/
export function useRobotAnalyticsData(
robotName: string
): RobotAnalyticsData | null {
const robot = useRobot(robotName)
const pipettes = useSelector((state: State) =>
getAttachedPipettes(state, robotName)
)
const settings = useSelector((state: State) =>
getRobotSettings(state, robotName)
)
const dispatch = useDispatch<Dispatch>()

React.useEffect(() => {
dispatch(fetchSettings(robotName))
}, [dispatch, robotName])

if (robot != null) {
// @ts-expect-error RobotAnalyticsData type needs boolean values should it be boolean | string
return settings.reduce<RobotAnalyticsData>(
(result, setting) => ({
...result,
[`${FF_PREFIX}${setting.id}`]: !!(setting.value ?? false),
}),
// @ts-expect-error RobotAnalyticsData type needs boolean values should it be boolean | string
{
robotApiServerVersion: getRobotApiVersion(robot) || '',
robotSmoothieVersion: getRobotFirmwareVersion(robot) || '',
robotLeftPipette: pipettes.left?.model || '',
robotRightPipette: pipettes.right?.model || '',
}
)
}

return null
}
2 changes: 1 addition & 1 deletion react-api-client/src/runs/useAllRunsQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useHost } from '../api'
export function useAllRunsQuery(): UseQueryResult<Runs> {
const host = useHost()
const query = useQuery(
[host, 'runs', 'details'],
[host, 'runs', '`details`'],
() => getRuns(host as HostConfig).then(response => response.data),
{ enabled: host !== null }
)
Expand Down
69 changes: 69 additions & 0 deletions react-api-client/src/server/__tests__/useRobotName.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import * as React from 'react'
import { when, resetAllWhenMocks } from 'jest-when'
import { QueryClient, QueryClientProvider } from 'react-query'
import { renderHook } from '@testing-library/react-hooks'
import { getRobotName } from '@opentrons/api-client'
import { useHost } from '../../api'
import { useRobotName } from '..'

import type {
HostConfig,
Response,
CurrentRobotName,
} from '@opentrons/api-client'

jest.mock('@opentrons/api-client')
jest.mock('../../api/useHost')

const mockGetRobotName = getRobotName as jest.MockedFunction<
typeof getRobotName
>
const mockUseHost = useHost as jest.MockedFunction<typeof useHost>

const HOST_CONFIG: HostConfig = { hostname: 'localhost' }

const ROBOT_NAME = { name: 'otie' }

describe('useRobotName hook', () => {
let wrapper: React.FunctionComponent<{}>

beforeEach(() => {
const queryClient = new QueryClient()
const clientProvider: React.FunctionComponent<{}> = ({ children }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
)

wrapper = clientProvider
})
afterEach(() => {
resetAllWhenMocks()
})

it('should return no data if no host', () => {
when(mockUseHost).calledWith().mockReturnValue(null)

const { result } = renderHook(useRobotName, { wrapper })
expect(result.current.data).toBeUndefined()
})

it('should return no data if the getRobotName request fails', () => {
when(mockUseHost).calledWith().mockReturnValue(HOST_CONFIG)
when(mockGetRobotName).calledWith(HOST_CONFIG).mockRejectedValue('fail')

const { result } = renderHook(useRobotName, { wrapper })
expect(result.current.data).toBeUndefined()
})

it('should return robot name', async () => {
when(mockUseHost).calledWith().mockReturnValue(HOST_CONFIG)
when(mockGetRobotName)
.calledWith(HOST_CONFIG)
.mockResolvedValue({
data: ROBOT_NAME,
} as Response<CurrentRobotName>)

const { result, waitFor } = renderHook(useRobotName, { wrapper })
await waitFor(() => result.current.data != null)
expect(result.current.data).toEqual(ROBOT_NAME)
})
})
1 change: 1 addition & 0 deletions react-api-client/src/server/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { useRobotName } from './useRobotName'
export { useUpdateRobotNameMutation } from './useUpdateRobotNameMutation'
20 changes: 20 additions & 0 deletions react-api-client/src/server/useRobotName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {
HostConfig,
CurrentRobotName,
getRobotName,
} from '@opentrons/api-client'
import { UseQueryResult, useQuery } from 'react-query'
import { useHost } from '../api'

export function useRobotName(): UseQueryResult<CurrentRobotName> {
const host = useHost()
const query = useQuery(
[host, 'server/name'],
() => getRobotName(host as HostConfig).then(response => response.data),
{
enabled: host !== null,
}
)

return query
}