import { of } from 'rxjs' ; import { ofType , combineEpics } from 'redux-observable' ; import { switchMap } from 'rxjs/operators' ; import { startDiscovery } from '../../discovery' ; import { POST } from '../../robot-api/constants' ; import { mapToRobotApiRequest } from '../../robot-api/operators' ; import * as Actions from '../actions' ; import * as Constants from '../constants' ; import type { ActionToRequestMapper , ResponseToActionMapper , } from '../../robot-api/operators' ; import type { Action , Epic } from '../../types' ; import { PostWifiConfigureAction , PostWifiConfigureSuccessAction , } from '../types' ; const mapActionToRequest = action => ( { method : POST , path : Constants . WIFI_CONFIGURE_PATH , body : action . payload . options , } ) ; const mapResponseToAction = ( response , originalAction ) => { const { host , body , ... responseMeta } = response ; const meta = { ... originalAction . meta , response : responseMeta } ; return response . ok ? Actions . postWifiConfigureSuccess ( host . name , body . ssid , meta ) : Actions . postWifiConfigureFailure ( host . name , body , meta ) ; } ; const postWifiConfigureEpic = ( action$ , state$ ) => { return action$ . pipe ( ofType < Action , PostWifiConfigureAction > ( Constants . POST_WIFI_CONFIGURE ) , mapToRobotApiRequest ( state$ , a => a . payload . robotName , mapActionToRequest , mapResponseToAction ) ) ; } ; const handleWifiConfigureSuccessEpic = action$ => { return action$ . pipe ( ofType < Action , PostWifiConfigureSuccessAction > ( Constants . POST_WIFI_CONFIGURE_SUCCESS ) , switchMap ( action => of ( Actions . fetchWifiList ( action . payload . robotName ) , startDiscovery ( ) ) ) ) ; } ; export const wifiConfigureEpic = combineEpics < Epic > ( postWifiConfigureEpic , handleWifiConfigureSuccessEpic ) ;