-
Notifications
You must be signed in to change notification settings - Fork 179
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
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3165e24
feat(app): add a function to get a robotName from API
koji d5dd39b
test useRobotName()
koji 78a578d
Merge branch 'release_6.0.0' into app_feat-add-get-robotname
koji 5015c92
feat(app): this PR is for robot rename function
koji bbf5c4b
fix ci check-js error
koji fb3f2fb
Update RenameRobotSlideout.tsx
koji File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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) | ||
} |
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,2 +1,3 @@ | ||
export { getRobotName } from './getRobotName' | ||
export { updateRobotName } from './updateRobotName' | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
export interface CurrentRobotName { | ||
name: string | ||
} | ||
|
||
export interface UpdatedRobotName { | ||
name: string | ||
} | ||
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,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 | ||
} |
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
69 changes: 69 additions & 0 deletions
69
react-api-client/src/server/__tests__/useRobotName.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,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) | ||
}) | ||
}) |
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 { useRobotName } from './useRobotName' | ||
export { useUpdateRobotNameMutation } from './useUpdateRobotNameMutation' |
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,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 | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 userobotName
in many components so maybe a different name would be better?