Browse Source

changed module

Tomi Cvetic 7 years ago
parent
commit
f274fda96f
2 changed files with 42 additions and 5 deletions
  1. 17 5
      src/module/container.js
  2. 25 0
      src/module/module.js

+ 17 - 5
src/module/container.js

@@ -2,12 +2,12 @@
 
 // Import dependencies
 import React from 'react'
-import { createStore, applyMiddleware, combineReducers, bindActionCreators, compose } from 'redux'
-import { Provider, connect } from 'react-redux'
+// import { createStore, applyMiddleware, combineReducers, bindActionCreators, compose } from 'redux'
+// import { Provider, connect } from 'react-redux'
+import { syncHistoryWithStore, routerReducer } from 'react-router-redux'
 import createSagaMiddleware from 'redux-saga'
 import { all } from 'redux-saga/effects'
 import { browserHistory, Router, Route, IndexRoute } from 'react-router'
-import { syncHistoryWithStore, routerReducer } from 'react-router-redux'
 
 class Container {
   constructor () {
@@ -59,7 +59,17 @@ class Container {
         applyMiddleware(...this.middleware))
       : compose(applyMiddleware(...this.middleware))
 
-    if (withRedux) {
+    // https://github.com/reactjs/redux/issues/1287
+    if (withRouter) {
+      const { BrowserRouter } = require('react-router')
+      const Root = () => (
+        <BrowserRouter />
+      )
+    }
+    if (withRedux || withRouter) {
+      const { createStore, applyMiddleware, combineReducers, bindActionCreators, compose } = require('redux')
+      const { Provider, connect } = require('react-redux')
+
       /** The default state is combined from all sub-module states */
       const defaultState = { ...this.states }
       /** The root reducer is combined from all sub-module reducers */
@@ -68,6 +78,8 @@ class Container {
       const actionCreators = { ...this.actionCreators }
       const store = createStore(rootReducer, defaultState, enhancer)
       const history = syncHistoryWithStore(browserHistory, store)
+    } else if (withRedux) {
+
     }
 
     /** The root saga, calling all other sagas */
@@ -88,7 +100,7 @@ class Container {
       return propState
     }
 
-    function mapDispatchToProps (dispatch) {
+    const mapDispatchToProps = function (dispatch) {
       const boundActionCreators = {}
       Object.keys(actionCreators).forEach(key => {
         boundActionCreators[`${key}Actions`] = bindActionCreators(actionCreators[key], dispatch)

+ 25 - 0
src/module/module.js

@@ -1,3 +1,28 @@
 class Module {
+  /** e.g. Module userProfile */
+  constructor (name) {
+    this.name = name
+  }
 
+  /** e.g. Upload the avatar file */
+  addAsyncState (name, defaultState, asyncFunction) {
+    if (!this.state) {
+      this.state = {}
+    }
+    this.state[name] = defaultState
+    if (!this.actionCreators) {
+      this.actionCreators = {}
+    }
+    ['start', 'success', 'failure'].forEach(state => {
+      const actionType = `${this.name}_${name}_${state}`.toUpperCase()
+      const actionCreatorName = `${this.name}${name[0].toUpperCase() + name.substring(1)}${state[0].toUpperCase() + state.substring(1)}`
+      this.actionCreators[actionCreatorName] = args => {
+        return {
+          type: actionType,
+          ...args
+        }
+      }
+      const reducerLogic = action => {}
+    })
+  }
 }