OSDN Git Service

merge dashboard into electron
[bytom/bytom-electron.git] / src / features / shared / routes.js
1 import { RoutingContainer } from 'features/shared/components'
2 import { humanize } from 'utility/string'
3 import { UTXOpageSize, pageSize } from 'utility/environment'
4 import actions from 'actions'
5
6 const makeRoutes = (store, type, List, New, Show, options = {}) => {
7   const loadPage = ( state ) => {
8     if(type === 'transaction' || type === 'unspent'){
9       const query = state.location.query
10       const pageNumber = parseInt(state.location.query.page || 1)
11       const pageSizes = (type === 'unspent')? UTXOpageSize: pageSize
12       if (pageNumber == 1) {
13         store.dispatch(actions[type].fetchPage(query, pageNumber, { refresh: true, pageSize: pageSizes }))
14       } else {
15         store.dispatch(actions[type].fetchPage(query, pageNumber,  { pageSize: pageSizes }))
16       }
17     }else{
18       store.dispatch(actions[type].fetchAll())
19     }
20   }
21
22   const childRoutes = []
23
24   if (New) {
25     childRoutes.push({
26       path: 'create',
27       component: New
28     })
29   }
30
31   if (options.childRoutes) {
32     childRoutes.push(...options.childRoutes)
33   }
34
35   if (Show) {
36     childRoutes.push({
37       path: ':id',
38       component: Show
39     })
40   }
41
42   return {
43     path: options.path || type + 's',
44     component: RoutingContainer,
45     name: options.name || humanize(type + 's'),
46     name_zh: options.name_zh,
47     indexRoute: {
48       component: List,
49       onEnter: (nextState, replace) => {
50         loadPage(nextState, replace)
51       },
52       onChange: (_, nextState, replace) => { loadPage(nextState, replace) }
53     },
54     childRoutes: childRoutes
55   }
56 }
57
58 export default makeRoutes