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

refactor(app): Consolidate /pipettes calls into common API #2262

Merged
merged 2 commits into from
Sep 14, 2018
Merged
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
10 changes: 7 additions & 3 deletions app/src/http-api-client/__tests__/motors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ describe('/motors/**', () => {
robot = {name: NAME, ip: '1.2.3.4', port: '1234'}
state = {
api: {
pipettes: {},
motors: {},
api: {
[NAME]: {
pipettes: {},
motors: {},
},
},
},
}

Expand Down Expand Up @@ -55,7 +59,7 @@ describe('/motors/**', () => {
test('skips GET /pipettes call if axes are in state', () => {
const expected = {axes: ['z', 'b']}

state.api.pipettes = {[NAME]: {response: mockPipettesResponse}}
state.api.api[NAME].pipettes = {response: mockPipettesResponse}
client.__setMockResponse(response)

// use mock.calls to verify call order
Expand Down
103 changes: 17 additions & 86 deletions app/src/http-api-client/__tests__/pipettes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import configureMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'

import client from '../client'
import {reducer, fetchPipettes, makeGetRobotPipettes} from '..'
import {fetchPipettes, makeGetRobotPipettes} from '..'

jest.mock('../client')

Expand All @@ -27,7 +27,7 @@ describe('pipettes', () => {
right: {model: 'p10_multi', mount_axis: 'a', plunger_axis: 'c'},
}

state = {api: {pipettes: {}}}
state = {api: {api: {}}}
store = mockStore(state)
})

Expand All @@ -48,110 +48,41 @@ describe('pipettes', () => {
.toHaveBeenCalledWith(robot, 'GET', 'pipettes?refresh=true'))
})

test('fetchPipettes dispatches PIPETTES_REQUEST + SUCCESS', () => {
test('dispatches api:REQUEST and api:SUCCESS', () => {
const path = 'pipettes'
const request = null
const response = {pipettes}
const expectedActions = [
{type: 'api:PIPETTES_REQUEST', payload: {robot}},
{type: 'api:PIPETTES_SUCCESS', payload: {robot, pipettes}},
{type: 'api:REQUEST', payload: {robot, request, path}},
{type: 'api:SUCCESS', payload: {robot, response, path}},
]

client.__setMockResponse(pipettes)

return store.dispatch(fetchPipettes(robot))
.then(() => expect(store.getActions()).toEqual(expectedActions))
})

test('fetchPipettes dispatches PIPETTES_REQUEST + FAILURE', () => {
const error = new Error('AH')
const expectedActions = [
{type: 'api:PIPETTES_REQUEST', payload: {robot}},
{type: 'api:PIPETTES_FAILURE', payload: {robot, error}},
]

client.__setMockError(error)
client.__setMockResponse(response)

return store.dispatch(fetchPipettes(robot))
.then(() => expect(store.getActions()).toEqual(expectedActions))
})
})

describe('reducer', () => {
describe('selectors', () => {
beforeEach(() => {
state = state.api
})

test('handles PIPETTE_REQUEST with new robot', () => {
const action = {type: 'api:PIPETTES_REQUEST', payload: {robot}}

expect(reducer(state, action).pipettes).toEqual({
[NAME]: {inProgress: true, error: null},
})
})

test('handles PIPETTE_REQUEST with existing robot', () => {
state.pipettes[NAME] = {
inProgress: false,
error: new Error('AH'),
response: pipettes,
state.api.api[NAME] = {
pipettes: {inProgress: true},
}

const action = {type: 'api:PIPETTES_REQUEST', payload: {robot}}

expect(reducer(state, action).pipettes).toEqual({
[NAME]: {inProgress: true, error: null, response: pipettes},
})
})

test('handles PIPETTE_SUCCESS', () => {
state.pipettes[NAME] = {
inProgress: true,
error: null,
response: null,
}

const action = {type: 'api:PIPETTES_SUCCESS', payload: {robot, pipettes}}

expect(reducer(state, action).pipettes).toEqual({
[NAME]: {inProgress: false, error: null, response: pipettes},
})
})

test('handles PIPETTES_FAILURE', () => {
state.pipettes[NAME] = {
inProgress: true,
error: null,
response: pipettes,
}

const error = new Error('AH')
const action = {type: 'api:PIPETTES_FAILURE', payload: {robot, error}}

expect(reducer(state, action).pipettes).toEqual({
[NAME]: {inProgress: false, response: pipettes, error},
})
})
})

describe('selectors', () => {
test('makeGetRobotPipettes with exiting robot', () => {
test('makeGetRobotPipettes with existing robot', () => {
const getRobotPipettes = makeGetRobotPipettes()
const robotPipettes = {
inProgress: false,
error: null,
response: pipettes,
}

state.api.pipettes[NAME] = robotPipettes
expect(getRobotPipettes(state, robot)).toEqual(robotPipettes)
expect(getRobotPipettes(state, robot))
.toEqual(state.api.api[NAME].pipettes)
expect(getRobotPipettes(state, {name: 'foo'})).toEqual({inProgress: false})
})

test('makeGetRobotPipettes with non-existent robot', () => {
const getRobotPipettes = makeGetRobotPipettes()

expect(getRobotPipettes(state, robot)).toEqual({
inProgress: false,
error: null,
response: null,
})
expect(getRobotPipettes(state, 'foo')).toEqual({inProgress: false})
})
})
})
16 changes: 3 additions & 13 deletions app/src/http-api-client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import {combineReducers} from 'redux'
import apiReducer from './reducer'
import {calibrationReducer, type CalibrationAction} from './calibration'
import type {HealthAction} from './health'
import type {PipettesAction} from './pipettes'
import type {ModulesAction} from './modules'
import {motorsReducer, type MotorsAction} from './motors'
import {pipettesReducer, type PipettesAction} from './pipettes'
import type {ResetAction} from './reset'
import {robotReducer, type RobotAction} from './robot'
import {serverReducer, type ServerAction} from './server'
Expand All @@ -16,7 +16,6 @@ import {wifiReducer, type WifiAction} from './wifi'
export const reducer = combineReducers({
calibration: calibrationReducer,
motors: motorsReducer,
pipettes: pipettesReducer,
robot: robotReducer,
server: serverReducer,
wifi: wifiReducer,
Expand All @@ -42,12 +41,6 @@ export type {
DeckCalPoint,
} from './calibration'

export type {
Pipette,
PipettesResponse,
RobotPipettes,
} from './pipettes'

export type {
RobotMove,
RobotHome,
Expand Down Expand Up @@ -98,15 +91,12 @@ export * from './reset'

export * from './settings'

export * from './pipettes'

export {
disengagePipetteMotors,
} from './motors'

export {
fetchPipettes,
makeGetRobotPipettes,
} from './pipettes'

export {
home,
clearHomeResponse,
Expand Down
2 changes: 1 addition & 1 deletion app/src/http-api-client/motors.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function disengagePipetteMotors (
...mounts: Array<Mount>
): ThunkPromiseAction {
return (dispatch, getState) => {
const pipettesState = getState().api.pipettes[robot.name]
const pipettesState = getState().api.api[robot.name].pipettes

dispatch(motorsRequest(robot, DISENGAGE, {mounts}))

Expand Down
98 changes: 20 additions & 78 deletions app/src/http-api-client/pipettes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
// pipette state from api
import {createSelector, type Selector} from 'reselect'

import type {State, Action, ThunkPromiseAction} from '../types'
import type {State, ThunkPromiseAction} from '../types'
import type {BaseRobot, RobotService} from '../robot'

import {apiRequest, apiSuccess, apiFailure} from './actions'
import type {ApiCall} from './types'
import type {ApiAction} from './actions'
import {getRobotApiState} from './reducer'

import type {MotorAxis} from './motors'
import client, {type ApiRequestError} from './client'

Expand All @@ -21,104 +25,42 @@ export type PipettesResponse = {
left: Pipette,
}

export type PipettesRequestAction = {|
type: 'api:PIPETTES_REQUEST',
payload: {|
robot: RobotService,
|},
|}

export type PipettesSuccessAction = {|
type: 'api:PIPETTES_SUCCESS',
payload: {|
robot: RobotService,
pipettes: PipettesResponse,
|},
|}
type FetchPipettesCall = ApiCall<null, PipettesResponse>

export type PipettesFailureAction = {|
type: 'api:PIPETTES_FAILURE',
payload: {|
robot: RobotService,
error: ApiRequestError,
|},
|}

export type PipettesAction =
| PipettesRequestAction
| PipettesSuccessAction
| PipettesFailureAction
export type PipettesAction = ApiAction<'pipette', null, PipettesResponse>

export type RobotPipettes = ApiCall<void, PipettesResponse>

type PipettesState = {
[robotName: string]: ?RobotPipettes,
export type PipettesState = {
pipettes?: FetchPipettesCall,
}

const PIPETTES: 'pipettes' = 'pipettes'

export function fetchPipettes (
robot: RobotService,
refresh: boolean = false
): ThunkPromiseAction {
let path = 'pipettes'
let path = PIPETTES
if (refresh) path += '?refresh=true'

return (dispatch) => {
dispatch({type: 'api:PIPETTES_REQUEST', payload: {robot}})
dispatch(apiRequest(robot, path, null))

return client(robot, 'GET', path)
.then((pipettes) => (
{type: 'api:PIPETTES_SUCCESS', payload: {robot, pipettes}}
)).catch((error) => (
{type: 'api:PIPETTES_FAILURE', payload: {robot, error}}
)).then((action) => dispatch(action))
}
}

export function pipettesReducer (
state: ?PipettesState,
action: Action
): PipettesState {
if (state == null) return {}

let name
let pipettes
let error

switch (action.type) {
case 'api:PIPETTES_REQUEST':
({robot: {name}} = action.payload)
return {
...state,
[name]: {...state[name], error: null, inProgress: true},
}

case 'api:PIPETTES_SUCCESS':
({pipettes, robot: {name}} = action.payload)
return {
...state,
[name]: {error: null, response: pipettes, inProgress: false},
}

case 'api:PIPETTES_FAILURE':
({error, robot: {name}} = action.payload)
return {
...state,
[name]: {...state[name], error, inProgress: false},
}
.then(
(resp: PipettesResponse) => apiSuccess(robot, PIPETTES, resp),
(err: ApiRequestError) => apiFailure(robot, PIPETTES, err)
)
.then(dispatch)
}

return state
}

export const makeGetRobotPipettes = () => {
const selector: Selector<State, BaseRobot, RobotPipettes> = createSelector(
selectRobotPipettesState,
(state) => state || {inProgress: false, error: null, response: null}
getRobotApiState,
(state) => state[PIPETTES] || {inProgress: false}
)

return selector
}

function selectRobotPipettesState (state: State, props: BaseRobot) {
return state.api.pipettes[props.name]
}
3 changes: 1 addition & 2 deletions app/src/http-api-client/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ type SettingsRequest = ?{id: Id, value: Value}

type SettingsResponse = {settings: Array<Setting>}

export type SettingsAction =
ApiAction<'settings', SettingsRequest, SettingsResponse>
export type SettingsAction = ApiAction<'settings', SettingsRequest, SettingsResponse>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry about this, really a whitespace issue that snuck in. LMK


export type RobotSettingsCall = ApiCall<SettingsRequest, SettingsResponse>

Expand Down
13 changes: 7 additions & 6 deletions app/src/support.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,16 @@ export const supportMiddleware: Middleware = (store) => (next) => (action) => {
const robot = state.robot.connection.connectRequest.name

update = {[ROBOT_NAME]: robot}
} else if (action.type === 'api:PIPETTES_SUCCESS') {
} else if (action.type === 'api:SUCCESS') {
const state = store.getState()

if (state.robot.connection.connectedTo === action.payload.robot.name) {
const {left, right} = action.payload.pipettes
if (action.payload.path === 'pipettes') {
const {left, right} = action.payload.response

update = {
[PIPETTE_MODEL_LEFT]: left.model,
[PIPETTE_MODEL_RIGHT]: right.model,
update = {
[PIPETTE_MODEL_LEFT]: left.model,
[PIPETTE_MODEL_RIGHT]: right.model,
}
}
}
}
Expand Down