import * as Constants from './constants' ; import type { Reducer } from 'redux' ; import type { Action } from '../types' ; import type { RobotControlsState , PerRobotControlsState } from './types' ; const INITIAL_STATE = { } ; const INITIAL_CONTROLS_STATE = { lightsOn : null , movementStatus : null , movementError : null , } ; const updateRobotState = ( state , robotName , update ) : RobotControlsState => { const robotState = state [ robotName ] || INITIAL_CONTROLS_STATE ; return { ... state , [ robotName ] : { ... robotState , ... update } , } ; } ; export const robotControlsReducer = ( state = INITIAL_STATE , action ) => { switch ( action . type ) { case Constants . FETCH_LIGHTS_SUCCESS : case Constants . UPDATE_LIGHTS_SUCCESS : { const { robotName , lightsOn } = action . payload ; return updateRobotState ( state , robotName , { lightsOn } ) ; } case Constants . HOME : case Constants . MOVE : { const { robotName } = action . payload ; return updateRobotState ( state , robotName , { movementStatus : action . type === Constants . HOME ? Constants . HOMING : Constants . MOVING , movementError : null , } ) ; } case Constants . HOME_SUCCESS : case Constants . MOVE_SUCCESS : case Constants . CLEAR_MOVEMENT_STATUS : { const { robotName } = action . payload ; return updateRobotState ( state , robotName , { movementStatus : null , movementError : null , } ) ; } case Constants . HOME_FAILURE : case Constants . MOVE_FAILURE : { const { robotName , error } = action . payload ; return updateRobotState ( state , robotName , { movementStatus : action . type === Constants . HOME_FAILURE ? Constants . HOME_ERROR : Constants . MOVE_ERROR , movementError : error . message , } ) ; } } return state ; } ;