1
- import React , { useEffect , useState } from 'react'
1
+ import React , { useEffect , useRef , useState } from 'react'
2
+ import queryString from 'query-string'
3
+ import { useLocation , useNavigate } from 'react-router-dom'
2
4
import { Button , Form , Input , message , Popconfirm , Space , Table , Select } from 'antd'
3
5
import type { TableColumnsType } from 'antd' ;
4
6
import { PlusOutlined } from '@ant-design/icons'
@@ -11,18 +13,34 @@ import styles from './styles.module.less'
11
13
12
14
13
15
const ModulePage = ( ) => {
16
+ const navigate = useNavigate ( ) ;
17
+ const location = useLocation ( ) ;
14
18
const [ form ] = Form . useForm ( ) ;
15
19
const [ open , setOpen ] = useState ( false )
16
20
const [ actionType , setActionType ] = useState ( 'ADD' )
17
21
const [ formData , setFormData ] = useState ( )
22
+ const { pageSize = 10 , page = 1 , total = 0 , moduleName } = queryString . parse ( location ?. search ) ;
18
23
const [ searchParams , setSearchParams ] = useState ( {
19
- pageSize : 10 ,
20
- page : 1 ,
21
- query : undefined ,
22
- total : undefined ,
23
- } )
24
-
24
+ pageSize,
25
+ page,
26
+ query : {
27
+ moduleName
28
+ } ,
29
+ total,
30
+ } ) ;
25
31
const [ dataSource , setDataSource ] = useState ( [ ] )
32
+ const searchParamsRef = useRef < any > ( ) ;
33
+
34
+ useEffect ( ( ) => {
35
+ const newParams = queryString . stringify ( {
36
+ moduleName,
37
+ ...( searchParamsRef . current ?. query || { } ) ,
38
+ page : searchParamsRef . current ?. page ,
39
+ pageSize : searchParamsRef . current ?. pageSize ,
40
+ } )
41
+ navigate ( `?${ newParams } ` )
42
+ // eslint-disable-next-line react-hooks/exhaustive-deps
43
+ } , [ searchParams , navigate ] )
26
44
27
45
async function getModuleList ( params ) {
28
46
try {
@@ -35,12 +53,14 @@ const ModulePage = () => {
35
53
} ) ;
36
54
if ( response ?. data ?. success ) {
37
55
setDataSource ( response ?. data ?. data ?. modules ) ;
38
- setSearchParams ( {
56
+ const newParams = {
39
57
query : params ?. query ,
40
58
pageSize : response ?. data ?. data ?. pageSize ,
41
59
page : response ?. data ?. data ?. currentPage ,
42
60
total : response ?. data ?. data ?. total ,
43
- } )
61
+ }
62
+ setSearchParams ( newParams )
63
+ searchParamsRef . current = newParams ;
44
64
} else {
45
65
message . error ( response ?. data ?. message )
46
66
}
@@ -50,15 +70,20 @@ const ModulePage = () => {
50
70
}
51
71
52
72
useEffect ( ( ) => {
53
- getModuleList ( { } )
73
+ getModuleList ( searchParams )
74
+ // eslint-disable-next-line react-hooks/exhaustive-deps
54
75
} , [ ] )
55
76
56
77
function handleReset ( ) {
57
78
form . resetFields ( ) ;
58
- setSearchParams ( {
79
+ const newParams = {
59
80
...searchParams ,
60
- query : undefined
61
- } )
81
+ query : {
82
+ moduleName : undefined
83
+ }
84
+ }
85
+ setSearchParams ( newParams )
86
+ searchParamsRef . current = newParams ;
62
87
getModuleList ( {
63
88
page : 1 ,
64
89
pageSize : 10 ,
@@ -67,10 +92,12 @@ const ModulePage = () => {
67
92
}
68
93
function handleSearch ( ) {
69
94
const values = form . getFieldsValue ( )
70
- setSearchParams ( {
95
+ const newParams = {
71
96
...searchParams ,
72
97
query : values
73
- } )
98
+ }
99
+ setSearchParams ( newParams )
100
+ searchParamsRef . current = newParams ;
74
101
getModuleList ( {
75
102
page : 1 ,
76
103
pageSize : 10 ,
@@ -114,15 +141,15 @@ const ModulePage = () => {
114
141
{
115
142
title : 'Registry' ,
116
143
dataIndex : 'registry' ,
117
-
144
+
118
145
render : ( _ , record ) => {
119
146
return `${ record ?. url ?. Scheme } ://${ record ?. url ?. Host } ${ record ?. url ?. Path } ` ;
120
147
}
121
148
} ,
122
149
{
123
150
title : 'Description' ,
124
151
dataIndex : 'description' ,
125
-
152
+
126
153
render : ( desc ) => {
127
154
return < DescriptionWithTooltip desc = { desc } width = { 400 } />
128
155
}
@@ -135,7 +162,7 @@ const ModulePage = () => {
135
162
render : ( _ , record ) => {
136
163
return (
137
164
< Space >
138
- { record ?. doc ?. Host && record ?. doc ?. Path ? (
165
+ { record ?. doc ?. Host ? (
139
166
< Button style = { { padding : '0px' } } type = 'link' href = { `${ record ?. doc ?. Scheme } ://${ record ?. doc ?. Host } ${ record ?. doc ?. Path } ` } target = '_blank' > doc</ Button >
140
167
) : (
141
168
< Button style = { { padding : '0px' } } type = 'link' disabled > doc</ Button >
@@ -159,6 +186,12 @@ const ModulePage = () => {
159
186
]
160
187
161
188
189
+ useEffect ( ( ) => {
190
+ form . setFieldsValue ( {
191
+ moduleName : searchParams ?. query ?. moduleName
192
+ } )
193
+ } , [ searchParams ?. query , form ] )
194
+
162
195
163
196
async function handleSubmit ( values , callback ) {
164
197
let response : any
@@ -190,7 +223,7 @@ const ModulePage = () => {
190
223
setOpen ( false )
191
224
}
192
225
193
- function handleChangePage ( page : number , pageSize : number ) {
226
+ function handleChangePage ( page : number , pageSize : any ) {
194
227
getModuleList ( {
195
228
...searchParams ,
196
229
page,
@@ -239,9 +272,9 @@ const ModulePage = () => {
239
272
scroll = { { x : 1300 } }
240
273
dataSource = { dataSource }
241
274
pagination = { {
242
- total : searchParams ?. total ,
243
- current : searchParams ?. page ,
244
- pageSize : searchParams ?. pageSize ,
275
+ total : Number ( searchParams ?. total ) ,
276
+ current : Number ( searchParams ?. page ) ,
277
+ pageSize : Number ( searchParams ?. pageSize ) ,
245
278
showTotal : ( total , range ) => (
246
279
< div style = { {
247
280
fontSize : '12px' ,
@@ -254,7 +287,7 @@ const ModulePage = () => {
254
287
value = { searchParams ?. pageSize }
255
288
size = "small"
256
289
style = { {
257
- width : 55 ,
290
+ width : 60 ,
258
291
margin : '0 4px' ,
259
292
fontSize : '12px'
260
293
} }
0 commit comments