Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1. import _ from 'lodash'
  2.  
  3. import WampSrvEntity from './entity'
  4. import { createReducer, createApiCallReducers } from '../../../util/redux/reducer-utils'
  5. import { prefixActionTypes, generateApiActionTypes } from '../../../util/redux/action-utils'
  6. import { sagaMiddleware, store } from '../../redux'
  7.  
  8. class SysMgrEntity extends WampSrvEntity {
  9. static storeKeyPrefix = '::MAG_SYSMGR::'
  10. static entityType = 'sysmgr'
  11. static topic = 'sysmgr'
  12. static defaultState = {
  13. systems: {
  14. 1: {
  15. system_type: 'dreamcatcher',
  16. system_name: 'DC',
  17. ip_addr1: '172.16.232.41',
  18. ip_addr2: '',
  19. id: 1
  20. }
  21. }
  22. }
  23.  
  24. constructor(...args) {
  25. super(...args)
  26. sagaMiddleware.run(this.watchFetchAll)
  27. sagaMiddleware.run(this.watchFetch)
  28. sagaMiddleware.run(this.watchAdd)
  29. sagaMiddleware.run(this.watchSet)
  30. sagaMiddleware.run(this.watchDelete)
  31. }
  32.  
  33. init() {
  34. // this.actions.fetchAll()
  35. // this.actions.subscribe()
  36. }
  37.  
  38. actionTypes = prefixActionTypes(this.id, {
  39. ...generateApiActionTypes({
  40. SUBSCRIBE: 'SUBSCRIBE',
  41. GET_EXTERNAL_SYSTEMS: 'GET_EXTERNAL_SYSTEMS',
  42. GET_EXTERNAL_SYSTEM: 'GET_EXTERNAL_SYSTEM',
  43. ADD_EXTERNAL_SYSTEM: 'ADD_EXTERNAL_SYSTEM',
  44. SET_EXTERNAL_SYSTEM: 'SET_EXTERNAL_SYSTEM',
  45. DELETE_EXTERNAL_SYSTEM: 'DELETE_EXTERNAL_SYSTEM',
  46. }),
  47. EXTERNAL_SYSTEM_UPDATE: 'EXTERNAL_SYSTEM_UPDATE',
  48. EXTERNAL_SYSTEM_REMOVE: 'EXTERNAL_SYSTEM_REMOVE',
  49. })
  50.  
  51. actions = ({
  52. fetchAll: () => store.dispatch({type: this.actionTypes.GET_EXTERNAL_SYSTEMS}),
  53. fetch: (id) => store.dispatch({
  54. type: this.actionTypes.GET_EXTERNAL_SYSTEM,
  55. params: [id]
  56. }),
  57. add: (system_name, system_type, ip_addr1, ip_addr2) => store.dispatch({
  58. type: this.actionTypes.ADD_EXTERNAL_SYSTEM,
  59. params: [{system_name, system_type, ip_addr1, ip_addr2}]
  60. }),
  61. set: (id, system_name, system_type, ip_addr1, ip_addr2) => store.dispatch({
  62. type: this.actionTypes.SET_EXTERNAL_SYSTEM,
  63. params: [{id, system_name, system_type, ip_addr1, ip_addr2}]
  64. }),
  65. 'delete': (id) => store.dispatch({
  66. type: this.actionTypes.DELETE_EXTERNAL_SYSTEM,
  67. params: [id]
  68. }),
  69.  
  70. subscribe: () => {
  71. this.conn.subscribe(this.constructor.topic, (id, {method, params}) => {
  72. if (method === 'update.create.external.system'
  73. || method === 'update.set.external.system') {
  74. let {external_system} = params
  75. store.dispatch({
  76. external_system,
  77. type: this.actionTypes.EXTERNAL_SYSTEM_UPDATE
  78. })
  79. } else if (method === 'update.delete.external.system') {
  80. let [id] = params
  81. store.dispatch({
  82. id,
  83. type: this.actionTypes.EXTERNAL_SYSTEM_REMOVE
  84. })
  85. }
  86. })
  87. },
  88.  
  89. unsubscribe: () => {
  90. this.conn.unsubscribe(this.constructor.topic, _.noop)
  91. }
  92. })
  93.  
  94. reducers = createReducer(SysMgrEntity.defaultState, {
  95. ...createApiCallReducers(this.actionTypes.GET_EXTERNAL_SYSTEMS, [
  96. (state) => ({...state, fetching: true}),
  97. (state, action) => ({...state, systems: _.keyBy(action.result, 'id'), fetching: false}),
  98. (state, action) => ({...state, fetching: false, error: action.error})
  99. ]),
  100. ...createApiCallReducers(this.actionTypes.GET_EXTERNAL_SYSTEM, [
  101. (state) => ({...state, fetching: true}),
  102. (state, action) => ({
  103. ...state,
  104. systems: {...state.systems, ..._.keyBy(action.result, 'id')},
  105. fetching: false
  106. }),
  107. (state, action) => ({...state, fetching: false, error: action.error})
  108. ]),
  109. [this.actionTypes.EXTERNAL_SYSTEM_UPDATE]: (state, action) => ({
  110. ...state,
  111. systems: {...state.systems, [action.external_system.id]: action.external_system}
  112. }),
  113. [this.actionTypes.EXTERNAL_SYSTEM_REMOVE]: (state, action) => ({
  114. ...state,
  115. systems: _.omit(state.systems, action.id)
  116. })
  117. })
  118.  
  119.  
  120. get watchFetchAll() {
  121. return this.createApiCallWatchSaga(
  122. this.actionTypes.GET_EXTERNAL_SYSTEMS, 'get.external.systems')
  123. }
  124.  
  125. get watchFetch() {
  126. return this.createApiCallWatchSaga(
  127. this.actionTypes.GET_EXTERNAL_SYSTEM, 'get.external.system')
  128. }
  129.  
  130. get watchAdd() {
  131. return this.createApiCallWatchSaga(
  132. this.actionTypes.ADD_EXTERNAL_SYSTEM, 'add.external.system')
  133. }
  134.  
  135. get watchSet() {
  136. return this.createApiCallWatchSaga(
  137. this.actionTypes.SET_EXTERNAL_SYSTEM, 'set.external.system')
  138. }
  139.  
  140. get watchDelete() {
  141. return this.createApiCallWatchSaga(
  142. this.actionTypes.DELETE_EXTERNAL_SYSTEM, 'delete.external.system')
  143. }
  144. }
  145.  
  146.  
  147. export {
  148. SysMgrEntity,
  149. SysMgrEntity as default
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement