import 'rxjs/add/operator/switchMap'; import { ActionsObservable, Epic } from 'redux-observable'; import { ActionTypes, RoleActions } from '../actions/role'; import { assign } from 'lodash'; import { Dezrez } from '@dezrez/typings-rezi'; import { IAction } from '@dezrez/core'; import { IState } from '../store'; import { lettingsRoleService } from '../services/lettingsRole'; import { roleService } from '../services/roleService'; import LettingsRole = Dezrez.Core.DataContracts.External.Api.Role.Query.Get.Marketing.PropertyLettingRoleDataContract; import Role = Dezrez.Core.DataContracts.External.Api.Role.Query.Get.RoleDataContract; import SetDepositCommand = Dezrez.Core.DataContracts.External.Api.Role.Command.SetDeposit.SetDepositCommandDataContract; import SetInfoCommand = Dezrez.Core.DataContracts.External.Api.Role.Command.SetInfo.SetInfoCommandDataContract; const roleEpic: Epic = action$ => action$.ofType(ActionTypes.GET) .mergeMap(action => roleService.getRole(action.payload) .map((role: Role) => RoleActions.store(role)) ); const depositEpic: Epic = (action$, store) => action$.ofType(ActionTypes.UPDATEDEPOSIT) .mergeMap((action: IAction) => { const payload: SetDepositCommand = action.payload; const tenant = store.getState().role.Tenant; const newValue = assign(tenant, { Deposit: { ...tenant.Deposit, Scheme: payload.Scheme, PriceValue: payload.PriceValue, DepositIdentification: payload.DepositIdentification, DepositStatus : payload.Status } }); return lettingsRoleService.setDeposit(tenant.LettingRoleId, newValue.Deposit) .map((role: Role) => RoleActions.store(newValue)); }); const letInfoEpic: Epic = (action$, store) => action$.ofType(ActionTypes.UPDATELETTINGSINFO) .mergeMap((action: IAction) => { const lettingsRole = store.getState().role.Letting; const newValue: LettingsRole = assign(lettingsRole, { Price: action.payload }); const payload = new SetInfoCommand(); payload.PriceType = newValue.Price.PriceType; payload.AvailableDate = newValue.AvailableDate; payload.Term = newValue.Term; payload.ServiceLevel = newValue.ServiceLevel; return lettingsRoleService.setLettingInfo(lettingsRole.Id, payload) .map((role: Role) => RoleActions.store(newValue)); }); export default [ roleEpic, letInfoEpic, depositEpic ];