You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.6 KiB

7 years ago
// @flow
// eslint-disable import/prefer-default-export
import type { Dispatch } from 'redux'
7 years ago
import { getDevices, getCurrentDevice } from 'reducers/devices'
import type { Device, Devices } from 'types/common'
export type deviceChooseType = (Device | null) => { type: string, payload: Device | null }
export const deviceChoose: deviceChooseType = payload => ({
type: 'DEVICE_CHOOSE',
7 years ago
payload,
})
type deviceChooseFirstType = () => (Dispatch<any>, () => { devices: Devices }) => void
export const deviceChooseFirst: deviceChooseFirstType = () => (dispatch, getState) => {
const devices = getDevices(getState())
// If we detect only 1 device, we choose it
if (devices.length === 1) {
dispatch(deviceChoose(devices[0]))
}
}
type devicesAddType = Device => (Dispatch<any>) => void
export const deviceAdd: devicesAddType = payload => dispatch => {
dispatch({
type: 'DEVICE_ADD',
payload,
})
dispatch(deviceChooseFirst())
}
type devicesRemoveType = Device => (Dispatch<any>, () => { devices: Devices }) => void
export const deviceRemove: devicesRemoveType = payload => (dispatch, getState) => {
dispatch({
type: 'DEVICE_REMOVE',
payload,
})
const currentDevice = getCurrentDevice(getState())
// If we disconnect the currentDevice we reset it
if (currentDevice.path === payload.path) {
dispatch(deviceChoose(null))
}
}
type devicesUpdateType = Devices => (Dispatch<any>) => void
export const devicesUpdate: devicesUpdateType = payload => dispatch => {
dispatch({
type: 'DEVICES_UPDATE',
payload,
})
dispatch(deviceChooseFirst())
}