Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import _ from 'lodash'
- import WampSrvEntity from './entity'
- import { createReducer, createApiCallReducers } from '../../../util/redux/reducer-utils'
- import { prefixActionTypes, generateApiActionTypes } from '../../../util/redux/action-utils'
- import { sagaMiddleware, store } from '../../redux'
- class SysMgrEntity extends WampSrvEntity {
- static storeKeyPrefix = '::MAG_SYSMGR::'
- static entityType = 'sysmgr'
- static topic = 'sysmgr'
- static defaultState = {
- systems: {
- 1: {
- system_type: 'dreamcatcher',
- system_name: 'DC',
- ip_addr1: '172.16.232.41',
- ip_addr2: '',
- id: 1
- }
- }
- }
- constructor(...args) {
- super(...args)
- sagaMiddleware.run(this.watchFetchAll)
- sagaMiddleware.run(this.watchFetch)
- sagaMiddleware.run(this.watchAdd)
- sagaMiddleware.run(this.watchSet)
- sagaMiddleware.run(this.watchDelete)
- }
- init() {
- // this.actions.fetchAll()
- // this.actions.subscribe()
- }
- actionTypes = prefixActionTypes(this.id, {
- ...generateApiActionTypes({
- SUBSCRIBE: 'SUBSCRIBE',
- GET_EXTERNAL_SYSTEMS: 'GET_EXTERNAL_SYSTEMS',
- GET_EXTERNAL_SYSTEM: 'GET_EXTERNAL_SYSTEM',
- ADD_EXTERNAL_SYSTEM: 'ADD_EXTERNAL_SYSTEM',
- SET_EXTERNAL_SYSTEM: 'SET_EXTERNAL_SYSTEM',
- DELETE_EXTERNAL_SYSTEM: 'DELETE_EXTERNAL_SYSTEM',
- }),
- EXTERNAL_SYSTEM_UPDATE: 'EXTERNAL_SYSTEM_UPDATE',
- EXTERNAL_SYSTEM_REMOVE: 'EXTERNAL_SYSTEM_REMOVE',
- })
- actions = ({
- fetchAll: () => store.dispatch({type: this.actionTypes.GET_EXTERNAL_SYSTEMS}),
- fetch: (id) => store.dispatch({
- type: this.actionTypes.GET_EXTERNAL_SYSTEM,
- params: [id]
- }),
- add: (system_name, system_type, ip_addr1, ip_addr2) => store.dispatch({
- type: this.actionTypes.ADD_EXTERNAL_SYSTEM,
- params: [{system_name, system_type, ip_addr1, ip_addr2}]
- }),
- set: (id, system_name, system_type, ip_addr1, ip_addr2) => store.dispatch({
- type: this.actionTypes.SET_EXTERNAL_SYSTEM,
- params: [{id, system_name, system_type, ip_addr1, ip_addr2}]
- }),
- 'delete': (id) => store.dispatch({
- type: this.actionTypes.DELETE_EXTERNAL_SYSTEM,
- params: [id]
- }),
- subscribe: () => {
- this.conn.subscribe(this.constructor.topic, (id, {method, params}) => {
- if (method === 'update.create.external.system'
- || method === 'update.set.external.system') {
- let {external_system} = params
- store.dispatch({
- external_system,
- type: this.actionTypes.EXTERNAL_SYSTEM_UPDATE
- })
- } else if (method === 'update.delete.external.system') {
- let [id] = params
- store.dispatch({
- id,
- type: this.actionTypes.EXTERNAL_SYSTEM_REMOVE
- })
- }
- })
- },
- unsubscribe: () => {
- this.conn.unsubscribe(this.constructor.topic, _.noop)
- }
- })
- reducers = createReducer(SysMgrEntity.defaultState, {
- ...createApiCallReducers(this.actionTypes.GET_EXTERNAL_SYSTEMS, [
- (state) => ({...state, fetching: true}),
- (state, action) => ({...state, systems: _.keyBy(action.result, 'id'), fetching: false}),
- (state, action) => ({...state, fetching: false, error: action.error})
- ]),
- ...createApiCallReducers(this.actionTypes.GET_EXTERNAL_SYSTEM, [
- (state) => ({...state, fetching: true}),
- (state, action) => ({
- ...state,
- systems: {...state.systems, ..._.keyBy(action.result, 'id')},
- fetching: false
- }),
- (state, action) => ({...state, fetching: false, error: action.error})
- ]),
- [this.actionTypes.EXTERNAL_SYSTEM_UPDATE]: (state, action) => ({
- ...state,
- systems: {...state.systems, [action.external_system.id]: action.external_system}
- }),
- [this.actionTypes.EXTERNAL_SYSTEM_REMOVE]: (state, action) => ({
- ...state,
- systems: _.omit(state.systems, action.id)
- })
- })
- get watchFetchAll() {
- return this.createApiCallWatchSaga(
- this.actionTypes.GET_EXTERNAL_SYSTEMS, 'get.external.systems')
- }
- get watchFetch() {
- return this.createApiCallWatchSaga(
- this.actionTypes.GET_EXTERNAL_SYSTEM, 'get.external.system')
- }
- get watchAdd() {
- return this.createApiCallWatchSaga(
- this.actionTypes.ADD_EXTERNAL_SYSTEM, 'add.external.system')
- }
- get watchSet() {
- return this.createApiCallWatchSaga(
- this.actionTypes.SET_EXTERNAL_SYSTEM, 'set.external.system')
- }
- get watchDelete() {
- return this.createApiCallWatchSaga(
- this.actionTypes.DELETE_EXTERNAL_SYSTEM, 'delete.external.system')
- }
- }
- export {
- SysMgrEntity,
- SysMgrEntity as default
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement