Browse Source

Clean actions/devices

master
Loëck Vézien 7 years ago
parent
commit
c40f409575
No known key found for this signature in database GPG Key ID: CBCDCE384E853AC4
  1. 49
      src/actions/devices.js
  2. 2
      src/main/app.js
  3. 19
      src/reducers/devices.js

49
src/actions/devices.js

@ -2,10 +2,6 @@
// eslint-disable import/prefer-default-export
import type { Dispatch } from 'redux'
import { getDevices, getCurrentDevice } from 'reducers/devices'
import type { Device, Devices } from 'types/common'
export type deviceChooseType = (Device | null) => { type: string, payload: Device | null }
@ -14,47 +10,20 @@ export const deviceChoose: deviceChooseType = payload => ({
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 devicesAddType = Device => { type: string, payload: Device }
export const deviceAdd: devicesAddType = payload => ({
type: 'DEVICE_ADD',
payload,
})
dispatch(deviceChooseFirst())
}
})
type devicesRemoveType = Device => (Dispatch<any>, () => { devices: Devices }) => void
export const deviceRemove: devicesRemoveType = payload => (dispatch, getState) => {
dispatch({
type devicesRemoveType = Device => { type: string, payload: Device }
export const deviceRemove: devicesRemoveType = payload => ({
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 devicesUpdateType = Devices => { type: string, payload: Devices }
export const devicesUpdate: devicesUpdateType = payload => ({
type: 'DEVICES_UPDATE',
payload,
})
dispatch(deviceChooseFirst())
}
})

2
src/main/app.js

@ -35,7 +35,7 @@ function createMainWindow() {
return window
}
// dsq
// Quit application when all windows are closed
app.on('window-all-closed', () => {
// On macOS it is common for applications to stay open

19
src/reducers/devices.js

@ -5,7 +5,7 @@ import { handleActions } from 'redux-actions'
import type { Device, Devices } from 'types/common'
type stateType = {
currentDevice: ?Device,
currentDevice: Device | null,
devices: Devices,
}
const state = {
@ -13,12 +13,21 @@ const state = {
devices: [],
}
function setCurrentDevice(state) {
return {
...state,
currentDevice: state.devices.length === 1 ? state.devices[0] : state.currentDevice,
}
}
const handlers: Object = {
DEVICES_UPDATE: (state: stateType, { payload: devices }: { payload: Devices }) => ({
DEVICES_UPDATE: (state: stateType, { payload: devices }: { payload: Devices }) =>
setCurrentDevice({
...state,
devices,
}),
DEVICE_ADD: (state: stateType, { payload: device }: { payload: Device }) => ({
DEVICE_ADD: (state: stateType, { payload: device }: { payload: Device }) =>
setCurrentDevice({
...state,
devices: [...state.devices, device].filter(
(v, i, s) => s.findIndex(t => t.path === v.path) === i,
@ -26,6 +35,10 @@ const handlers: Object = {
}),
DEVICE_REMOVE: (state: stateType, { payload: device }: { payload: Device }) => ({
...state,
currentDevice:
state.currentDevice !== null && state.currentDevice.path === device.path
? null
: state.currentDevice,
devices: state.devices.filter(d => d.path !== device.path),
}),
DEVICE_CHOOSE: (state: stateType, { payload: currentDevice }: { payload: Device }) => ({

Loading…
Cancel
Save