nsgm-cli 2.0.6 → 2.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -201
- package/README.md +161 -161
- package/client/layout/index.tsx +248 -245
- package/client/redux/reducers.ts +4 -4
- package/client/redux/store.ts +50 -50
- package/client/redux/template/manage/actions.ts +190 -190
- package/client/redux/template/manage/reducers.ts +118 -118
- package/client/redux/template/manage/types.ts +24 -24
- package/client/service/template/manage.ts +96 -96
- package/client/styled/common.ts +60 -27
- package/client/styled/layout/index.ts +25 -25
- package/client/styled/template/manage.ts +51 -51
- package/client/utils/common.ts +89 -86
- package/client/utils/cookie.ts +51 -51
- package/client/utils/fetch.ts +25 -25
- package/client/utils/menu.tsx +27 -27
- package/client/utils/sso.ts +205 -0
- package/generation/README.md +19 -19
- package/generation/app.js +2 -2
- package/generation/client/redux/reducers.ts +4 -4
- package/generation/client/utils/menu.tsx +27 -27
- package/generation/gitignore +4 -4
- package/generation/mysql.config.js +12 -13
- package/generation/next.config.js +6 -6
- package/generation/package.json +21 -26
- package/generation/project.config.js +13 -12
- package/generation/server/rest.js +23 -20
- package/generation/tsconfig.json +30 -30
- package/index.js +10 -10
- package/lib/args.d.ts +6 -6
- package/lib/args.js +53 -53
- package/lib/generate.d.ts +3 -3
- package/lib/generate.js +751 -736
- package/lib/index.d.ts +2 -2
- package/lib/index.js +272 -272
- package/lib/server/db.d.ts +5 -5
- package/lib/server/db.js +110 -110
- package/lib/server/graphql.d.ts +7 -7
- package/lib/server/graphql.js +119 -119
- package/lib/server/plugins/date.d.ts +5 -5
- package/lib/server/plugins/date.js +16 -16
- package/lib/tsconfig.build.tsbuildinfo +1 -1
- package/mysql.config.js +14 -14
- package/next-env.d.ts +6 -6
- package/next.config.js +2 -2
- package/package.json +126 -126
- package/pages/_app.tsx +54 -44
- package/pages/login.tsx +79 -0
- package/pages/template/manage.tsx +278 -278
- package/project.config.js +16 -14
- package/public/slbhealthcheck.html +9 -9
- package/scripts/shutdown.sh +9 -9
- package/scripts/startup.sh +34 -34
- package/server/apis/sso.js +44 -0
- package/server/apis/template.js +17 -17
- package/server/modules/template/schema.js +33 -33
- package/server/rest.js +24 -20
- package/server/sql/template.sql +8 -8
- package/generation/.DS_Store +0 -0
package/client/layout/index.tsx
CHANGED
|
@@ -1,245 +1,248 @@
|
|
|
1
|
-
import React, { useEffect, useState } from 'react'
|
|
2
|
-
import { Layout, Menu, Breadcrumb, Image, Select } from 'antd'
|
|
3
|
-
import { Container } from '../styled/layout'
|
|
4
|
-
import { useRouter } from 'next/router'
|
|
5
|
-
import _ from 'lodash'
|
|
6
|
-
import menuConfig from '../utils/menu'
|
|
7
|
-
import
|
|
8
|
-
import
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const {
|
|
12
|
-
const {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const
|
|
16
|
-
const {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
const
|
|
87
|
-
const [
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
const
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
const
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
if (subKeyArrLen >=
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
<
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
1
|
+
import React, { useEffect, useState } from 'react'
|
|
2
|
+
import { Layout, Menu, Breadcrumb, Image, Select } from 'antd'
|
|
3
|
+
import { Container } from '../styled/layout'
|
|
4
|
+
import { useRouter } from 'next/router'
|
|
5
|
+
import _ from 'lodash'
|
|
6
|
+
import menuConfig from '../utils/menu'
|
|
7
|
+
import { logout } from '../utils/sso'
|
|
8
|
+
import getConfig from 'next/config'
|
|
9
|
+
import { ItemType, SubMenuType } from 'antd/lib/menu/hooks/useItems'
|
|
10
|
+
|
|
11
|
+
const { Option } = Select
|
|
12
|
+
const { SubMenu } = Menu
|
|
13
|
+
const { Header, Content, Sider } = Layout
|
|
14
|
+
|
|
15
|
+
const nextConfig = getConfig()
|
|
16
|
+
const { publicRuntimeConfig } = nextConfig
|
|
17
|
+
const { prefix } = publicRuntimeConfig
|
|
18
|
+
|
|
19
|
+
const getLocationKey = () => {
|
|
20
|
+
let result = {
|
|
21
|
+
topMenu: '1',
|
|
22
|
+
slideMenu: '0'
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (typeof window !== 'undefined') {
|
|
26
|
+
const locationHref = window.location.href
|
|
27
|
+
|
|
28
|
+
let locationHrefArr = locationHref.split('?')
|
|
29
|
+
if (locationHrefArr.length > 0) {
|
|
30
|
+
locationHrefArr = locationHrefArr[0].split('//')
|
|
31
|
+
|
|
32
|
+
if (locationHrefArr.length > 1) {
|
|
33
|
+
let locationStr = locationHrefArr[1]
|
|
34
|
+
const locationIndex = locationStr.indexOf('/')
|
|
35
|
+
locationStr = locationStr.substring(locationIndex)
|
|
36
|
+
|
|
37
|
+
if (prefix && locationStr.indexOf(prefix) !== -1) {
|
|
38
|
+
locationStr = locationStr.split(prefix)[1]
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// console.log('locationStr', locationStr)
|
|
42
|
+
|
|
43
|
+
_.each(menuConfig, (item, index) => {
|
|
44
|
+
const { key, url, subMenus } = item
|
|
45
|
+
|
|
46
|
+
if (subMenus) {
|
|
47
|
+
_.each(subMenus, (subItem, subIndex) => {
|
|
48
|
+
const { key: subKey, url: subUrl } = subItem
|
|
49
|
+
|
|
50
|
+
if (locationStr === subUrl.split('?')[0]) {
|
|
51
|
+
const subKeyArr = subKey.split('_')
|
|
52
|
+
const subKeyArrLen = subKeyArr.length
|
|
53
|
+
|
|
54
|
+
if (subKeyArrLen > 0) result.topMenu = subKeyArr[0]
|
|
55
|
+
|
|
56
|
+
if (subKeyArrLen > 1) result.slideMenu = subKeyArr[1]
|
|
57
|
+
|
|
58
|
+
return false
|
|
59
|
+
}
|
|
60
|
+
})
|
|
61
|
+
} else {
|
|
62
|
+
if (url && locationStr === url.split('?')[0]) {
|
|
63
|
+
result.topMenu = key
|
|
64
|
+
return false
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
})
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
// console.log('result', result)
|
|
72
|
+
return result
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const routerPush = (router: any, url: string) => {
|
|
76
|
+
// console.log('routerPush', url)
|
|
77
|
+
if (router && url) {
|
|
78
|
+
if (prefix && url.indexOf(prefix) === -1) {
|
|
79
|
+
url = prefix + url
|
|
80
|
+
}
|
|
81
|
+
router.push(url)
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const LayoutComponent = ({ user, children }) => {
|
|
86
|
+
const router = useRouter()
|
|
87
|
+
const [topMenuKey, setTopMenuKey] = useState('1')
|
|
88
|
+
const [sliderMenuKey, setSliderMenuKey] = useState('1')
|
|
89
|
+
|
|
90
|
+
// console.log('topMenuKey: ' + topMenuKey, ', sliderMenuKey: ' + sliderMenuKey, user)
|
|
91
|
+
|
|
92
|
+
useEffect(() => {
|
|
93
|
+
const { topMenu, slideMenu } = getLocationKey()
|
|
94
|
+
setTopMenuKey(topMenu)
|
|
95
|
+
setSliderMenuKey(slideMenu)
|
|
96
|
+
}, [])
|
|
97
|
+
|
|
98
|
+
const menuItems:any = []
|
|
99
|
+
const menuItemsVertical:any = []
|
|
100
|
+
|
|
101
|
+
_.each(menuConfig, (item, index) => {
|
|
102
|
+
const { key, text, url, icon, subMenus } = item
|
|
103
|
+
|
|
104
|
+
if (key) {
|
|
105
|
+
const menuObj:ItemType = {
|
|
106
|
+
label: text,
|
|
107
|
+
key,
|
|
108
|
+
onClick: () => {
|
|
109
|
+
routerPush(router, url)
|
|
110
|
+
setTopMenuKey(key)
|
|
111
|
+
|
|
112
|
+
if (subMenus) {
|
|
113
|
+
setSliderMenuKey('1')
|
|
114
|
+
} else {
|
|
115
|
+
setSliderMenuKey('0')
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
menuItems.push(menuObj)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (subMenus) {
|
|
124
|
+
const subMenusChildren:any = []
|
|
125
|
+
|
|
126
|
+
_.each(subMenus, (subItem, subIndex) => {
|
|
127
|
+
const { key: subKey, text: subText, url: subUrl } = subItem
|
|
128
|
+
|
|
129
|
+
const subMenusChildrenObj:ItemType = {
|
|
130
|
+
key: 'slider_' + subKey,
|
|
131
|
+
label: subText,
|
|
132
|
+
onClick: ()=>{
|
|
133
|
+
routerPush(router, subUrl)
|
|
134
|
+
|
|
135
|
+
const subKeyArr = subKey.split('_')
|
|
136
|
+
const subKeyArrLen = subKeyArr.length
|
|
137
|
+
|
|
138
|
+
// console.log(subKeyArr, subKeyArrLen)
|
|
139
|
+
|
|
140
|
+
if (subKeyArrLen >= 1) setTopMenuKey(subKeyArr[0])
|
|
141
|
+
if (subKeyArrLen >= 2) setSliderMenuKey(subKeyArr[1])
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
subMenusChildren.push(subMenusChildrenObj)
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
const subMenuObjVertical:SubMenuType = {
|
|
149
|
+
key: 'slider_' + key,
|
|
150
|
+
icon,
|
|
151
|
+
label: text,
|
|
152
|
+
onTitleClick: () => {
|
|
153
|
+
setTopMenuKey(key)
|
|
154
|
+
setSliderMenuKey('1')
|
|
155
|
+
},
|
|
156
|
+
children: subMenusChildren
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
menuItemsVertical.push(subMenuObjVertical)
|
|
160
|
+
} else {
|
|
161
|
+
if (key) {
|
|
162
|
+
const menuObjVertical:ItemType = {
|
|
163
|
+
label: text,
|
|
164
|
+
icon,
|
|
165
|
+
key: 'slider_' + key + '_0',
|
|
166
|
+
onClick: () => {
|
|
167
|
+
routerPush(router, url)
|
|
168
|
+
setTopMenuKey(key)
|
|
169
|
+
setSliderMenuKey('0')
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
menuItemsVertical.push(menuObjVertical)
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
return (
|
|
179
|
+
<Layout>
|
|
180
|
+
<Container>
|
|
181
|
+
<Header className="header">
|
|
182
|
+
<div className="logo">
|
|
183
|
+
<Image width={100} src={prefix + "/images/zhizuotu_1.png"} preview={false} />
|
|
184
|
+
</div>
|
|
185
|
+
<Menu theme="dark" mode="horizontal" defaultSelectedKeys={['1']} selectedKeys={[topMenuKey]} items={menuItems} />
|
|
186
|
+
<div className="user">
|
|
187
|
+
<Select value={user?.displayName} onChange={() => { logout() }}>
|
|
188
|
+
<Option value=''>{'退出'}</Option>
|
|
189
|
+
</Select>
|
|
190
|
+
</div>
|
|
191
|
+
</Header>
|
|
192
|
+
<Layout>
|
|
193
|
+
<Sider width={200} className="site-layout-background">
|
|
194
|
+
<Menu
|
|
195
|
+
mode="inline"
|
|
196
|
+
defaultSelectedKeys={['slider_1_0']}
|
|
197
|
+
defaultOpenKeys={['slider_1']}
|
|
198
|
+
selectedKeys={['slider_' + topMenuKey + '_' + sliderMenuKey]}
|
|
199
|
+
openKeys={['slider_' + topMenuKey]}
|
|
200
|
+
style={{ height: '100%', borderRight: 0 }}
|
|
201
|
+
items={menuItemsVertical}
|
|
202
|
+
/>
|
|
203
|
+
</Sider>
|
|
204
|
+
<Layout style={{ padding: '0 24px 24px' }}>
|
|
205
|
+
<Breadcrumb style={{ margin: '16px 0' }}>
|
|
206
|
+
{_.map(menuConfig, (item, index) => {
|
|
207
|
+
const { key, text, subMenus } = item
|
|
208
|
+
|
|
209
|
+
if (subMenus) {
|
|
210
|
+
// console.log('subMenus', subMenus)
|
|
211
|
+
|
|
212
|
+
let subContent:any = []
|
|
213
|
+
_.each(subMenus, (subItem, subIndex) => {
|
|
214
|
+
const { key: subKey, text: subText } = subItem
|
|
215
|
+
// console.log('subKey', subKey, key, topMenuKey, sliderMenuKey)
|
|
216
|
+
|
|
217
|
+
if (subKey === topMenuKey + '_' + sliderMenuKey) {
|
|
218
|
+
subContent.push(<Breadcrumb.Item key={'breadcrumb' + subIndex}>{text}</Breadcrumb.Item>)
|
|
219
|
+
subContent.push(<Breadcrumb.Item key={'breadcrumb' + subIndex}>{subText}</Breadcrumb.Item>)
|
|
220
|
+
return false
|
|
221
|
+
}
|
|
222
|
+
})
|
|
223
|
+
return subContent
|
|
224
|
+
} else {
|
|
225
|
+
if (key && key === topMenuKey) {
|
|
226
|
+
return <Breadcrumb.Item key={'breadcrumb' + index}>{text}</Breadcrumb.Item>
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
})}
|
|
230
|
+
</Breadcrumb>
|
|
231
|
+
<Content
|
|
232
|
+
className="site-layout-background"
|
|
233
|
+
style={{
|
|
234
|
+
padding: 24,
|
|
235
|
+
margin: 0,
|
|
236
|
+
minHeight: 280
|
|
237
|
+
}}
|
|
238
|
+
>
|
|
239
|
+
{children}
|
|
240
|
+
</Content>
|
|
241
|
+
</Layout>
|
|
242
|
+
</Layout>
|
|
243
|
+
</Container>
|
|
244
|
+
</Layout>
|
|
245
|
+
)
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export default LayoutComponent
|
package/client/redux/reducers.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { templateManageReducer } from './template/manage/reducers'
|
|
2
|
-
|
|
3
|
-
export default {
|
|
4
|
-
templateManage: templateManageReducer,
|
|
1
|
+
import { templateManageReducer } from './template/manage/reducers'
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
templateManage: templateManageReducer,
|
|
5
5
|
}
|
package/client/redux/store.ts
CHANGED
|
@@ -1,50 +1,50 @@
|
|
|
1
|
-
import { useMemo } from 'react'
|
|
2
|
-
import { combineReducers } from 'redux'
|
|
3
|
-
import { configureStore } from '@reduxjs/toolkit'
|
|
4
|
-
import thunkMiddleware from 'redux-thunk'
|
|
5
|
-
import reducers from './reducers'
|
|
6
|
-
import _ from 'lodash'
|
|
7
|
-
|
|
8
|
-
let store:any
|
|
9
|
-
|
|
10
|
-
const reducersKeysLen = _.keys(reducers).length
|
|
11
|
-
|
|
12
|
-
let combineReducer:any = function(){}
|
|
13
|
-
|
|
14
|
-
if(reducersKeysLen > 0){
|
|
15
|
-
combineReducer = combineReducers({ ...reducers })
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export type RootState = ReturnType<typeof combineReducer>
|
|
19
|
-
|
|
20
|
-
function initStore(initialState:any) {
|
|
21
|
-
return configureStore({
|
|
22
|
-
reducer: combineReducer,
|
|
23
|
-
preloadedState: initialState,
|
|
24
|
-
devTools: true,
|
|
25
|
-
middleware: [thunkMiddleware]
|
|
26
|
-
})
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export const initializeStore = (preloadedState:any) => {
|
|
30
|
-
let _store = store ?? initStore(preloadedState)
|
|
31
|
-
|
|
32
|
-
if (preloadedState && store) {
|
|
33
|
-
_store = initStore({
|
|
34
|
-
...store.getState(),
|
|
35
|
-
...preloadedState
|
|
36
|
-
})
|
|
37
|
-
store = undefined
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
if (typeof window === 'undefined') return _store
|
|
41
|
-
|
|
42
|
-
if (!store) store = _store
|
|
43
|
-
|
|
44
|
-
return _store
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export function useStore(initialState:any) {
|
|
48
|
-
const store = useMemo(() => initializeStore(initialState), [initialState])
|
|
49
|
-
return store
|
|
50
|
-
}
|
|
1
|
+
import { useMemo } from 'react'
|
|
2
|
+
import { combineReducers } from 'redux'
|
|
3
|
+
import { configureStore } from '@reduxjs/toolkit'
|
|
4
|
+
import thunkMiddleware from 'redux-thunk'
|
|
5
|
+
import reducers from './reducers'
|
|
6
|
+
import _ from 'lodash'
|
|
7
|
+
|
|
8
|
+
let store:any
|
|
9
|
+
|
|
10
|
+
const reducersKeysLen = _.keys(reducers).length
|
|
11
|
+
|
|
12
|
+
let combineReducer:any = function(){}
|
|
13
|
+
|
|
14
|
+
if(reducersKeysLen > 0){
|
|
15
|
+
combineReducer = combineReducers({ ...reducers })
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export type RootState = ReturnType<typeof combineReducer>
|
|
19
|
+
|
|
20
|
+
function initStore(initialState:any) {
|
|
21
|
+
return configureStore({
|
|
22
|
+
reducer: combineReducer,
|
|
23
|
+
preloadedState: initialState,
|
|
24
|
+
devTools: true,
|
|
25
|
+
middleware: [thunkMiddleware]
|
|
26
|
+
})
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const initializeStore = (preloadedState:any) => {
|
|
30
|
+
let _store = store ?? initStore(preloadedState)
|
|
31
|
+
|
|
32
|
+
if (preloadedState && store) {
|
|
33
|
+
_store = initStore({
|
|
34
|
+
...store.getState(),
|
|
35
|
+
...preloadedState
|
|
36
|
+
})
|
|
37
|
+
store = undefined
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (typeof window === 'undefined') return _store
|
|
41
|
+
|
|
42
|
+
if (!store) store = _store
|
|
43
|
+
|
|
44
|
+
return _store
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function useStore(initialState:any) {
|
|
48
|
+
const store = useMemo(() => initializeStore(initialState), [initialState])
|
|
49
|
+
return store
|
|
50
|
+
}
|