@@ -4,26 +4,24 @@ import { FC, useCallback, useEffect, useState } from "react";
4
4
import React from "react" ;
5
5
import { connect } from "react-redux" ;
6
6
import { useLocation , useNavigate } from "react-router-dom" ;
7
- import { DeleteOutlined , EyeOutlined , MenuOutlined } from "@ant-design/icons" ;
7
+ import { DeleteOutlined , EyeOutlined , SwapOutlined } from "@ant-design/icons" ;
8
8
import type { DragEndEvent } from '@dnd-kit/core' ;
9
9
import { DndContext , PointerSensor , useSensor , useSensors } from '@dnd-kit/core' ;
10
10
import { restrictToVerticalAxis } from '@dnd-kit/modifiers' ;
11
11
import {
12
- arrayMove ,
13
12
SortableContext ,
14
13
useSortable ,
15
14
verticalListSortingStrategy ,
16
15
} from '@dnd-kit/sortable' ;
17
16
import { CSS } from '@dnd-kit/utilities' ;
18
- import { Button , Descriptions , Drawer , Form , Image , Input , message , Modal , Space , Table } from "antd" ;
17
+ import { Button , Descriptions , Drawer , Form , Input , InputNumber , message , Modal , Space , Table , Tooltip } from "antd" ;
19
18
import type { ColumnsType } from "antd/es/table" ;
20
19
21
- import { delColumnArticleApi , getColumnArticleListApi , getColumnByNameListApi , sortColumnArticleApi , updateColumnArticleApi } from "@/api/modules/column" ;
20
+ import { delColumnArticleApi , getColumnArticleListApi , sortColumnArticleApi , sortColumnArticleByIDApi , updateColumnArticleApi } from "@/api/modules/column" ;
22
21
import { ContentInterWrap , ContentWrap } from "@/components/common-wrap" ;
23
22
import { initPagination , IPagination , UpdateEnum } from "@/enums/common" ;
24
23
import { MapItem } from "@/typings/common" ;
25
24
import { baseDomain } from "@/utils/util" ;
26
- import { getCompleteUrl } from "@/utils/util" ;
27
25
import TableSelect from "@/views/column/article/components/tableselect/TableSelect" ;
28
26
import Search from "./search" ;
29
27
@@ -59,6 +57,18 @@ export interface IFormType {
59
57
sort : number ; // 排序
60
58
}
61
59
60
+ export interface IArticleSortFormType {
61
+ id : number ; // 主键id
62
+ articleId : number ; // 文章ID
63
+ sort : number ; // 排序
64
+ }
65
+
66
+ const defaulArticleSorttInitForm : IArticleSortFormType = {
67
+ id : - 1 ,
68
+ articleId : - 1 ,
69
+ sort : - 1
70
+ } ;
71
+
62
72
const defaultInitForm : IFormType = {
63
73
id : - 1 ,
64
74
articleId : - 1 ,
@@ -85,16 +95,22 @@ interface ColumnValue {
85
95
const ColumnArticle : FC < IProps > = props => {
86
96
87
97
const [ formRef ] = Form . useForm ( ) ;
98
+ // 调整文章书序的表单
99
+ const [ articleSortFormRef ] = Form . useForm ( ) ;
88
100
// form值(详情和新增的时候会用到)
89
101
const [ form , setForm ] = useState < IFormType > ( defaultInitForm ) ;
90
-
102
+ // form值(调整文章顺序的表单值变化时保存)
103
+ const [ articleSortForm , setArticleSortForm ] = useState < IArticleSortFormType > ( defaulArticleSorttInitForm ) ;
91
104
// 查询表单
92
105
const [ searchForm , setSearchForm ] = useState < ISearchForm > ( defaultSearchForm ) ;
93
106
94
107
// 修改添加抽屉
95
108
const [ isOpenDrawerShow , setIsOpenDrawerShow ] = useState < boolean > ( false ) ;
96
109
// 详情抽屉
97
110
const [ isDetailDrawerShow , setIsDetailDrawerShow ] = useState < boolean > ( false ) ;
111
+ // 调整顺序抽屉
112
+ const [ isSortDrawerShow , setIsSortDrawerShow ] = useState < boolean > ( false ) ;
113
+
98
114
// 文章选择下拉框是否打开
99
115
const [ isArticleSelectOpen , setIsArticleSelectOpen ] = useState < boolean > ( false ) ;
100
116
@@ -114,10 +130,12 @@ const ColumnArticle: FC<IProps> = props => {
114
130
const navigate = useNavigate ( ) ;
115
131
const { columnId : columnIdParam } = location . state || { } ;
116
132
133
+ // 拖拽相关 1
117
134
interface RowProps extends React . HTMLAttributes < HTMLTableRowElement > {
118
135
'data-row-key' : string ;
119
136
}
120
137
138
+ // 拖拽相关 2
121
139
const Row = ( props : RowProps ) => {
122
140
const { attributes, listeners, setNodeRef, transform, transition, isDragging } = useSortable ( {
123
141
id : props [ 'data-row-key' ] ,
@@ -156,6 +174,7 @@ const ColumnArticle: FC<IProps> = props => {
156
174
}
157
175
} ;
158
176
177
+ // 拖拽相关 3
159
178
const sensors = useSensors (
160
179
useSensor ( PointerSensor , {
161
180
activationConstraint : {
@@ -169,12 +188,17 @@ const ColumnArticle: FC<IProps> = props => {
169
188
setQuery ( prev => prev + 1 ) ;
170
189
} , [ ] ) ;
171
190
172
- // 值改变(新增教程文章时)
191
+ // 值改变(新增教程文章时,老的做法,将 formRef 放到了这里,不太好 )
173
192
const handleChange = ( item : MapItem ) => {
174
193
setForm ( { ...form , ...item } ) ;
175
194
formRef . setFieldsValue ( { ...item } ) ;
176
195
} ;
177
196
197
+ // 值改变(调整顺序输入框发生变化时)
198
+ const handleArticleSortChange = ( item : MapItem ) => {
199
+ setArticleSortForm ( { ...articleSortForm , ...item } ) ;
200
+ } ;
201
+
178
202
// 查询表单值改变
179
203
const handleSearchChange = ( item : MapItem ) => {
180
204
// 当 status 的值为 -1 时,重新显示
@@ -208,11 +232,8 @@ const ColumnArticle: FC<IProps> = props => {
208
232
setIsArticleSelectOpen ( false ) ;
209
233
// 关闭抽屉
210
234
setIsOpenDrawerShow ( false ) ;
211
- } ;
212
-
213
- // 关闭详情抽屉时触发
214
- const handleCloseDetailDrawer = ( ) => {
215
- setIsDetailDrawerShow ( false ) ;
235
+ // 关闭调整顺序的抽屉
236
+ setIsSortDrawerShow ( false ) ;
216
237
} ;
217
238
218
239
// 删除
@@ -256,6 +277,25 @@ const ColumnArticle: FC<IProps> = props => {
256
277
}
257
278
} ;
258
279
280
+ // 调整顺序的 submit
281
+ const handleSortByIDSubmit = async ( ) => {
282
+ const values = await articleSortFormRef . validateFields ( ) ;
283
+ const newValues = {
284
+ ...values ,
285
+ id : articleSortForm . id ,
286
+ } ;
287
+ console . log ( "提交的值:" , newValues ) ;
288
+
289
+ const { status : successStatus } = ( await sortColumnArticleByIDApi ( newValues ) ) || { } ;
290
+ const { code, msg } = successStatus || { } ;
291
+ if ( code === 0 ) {
292
+ setIsSortDrawerShow ( false ) ;
293
+ onSure ( ) ;
294
+ } else {
295
+ message . error ( msg ) ;
296
+ }
297
+ } ;
298
+
259
299
const onDragEnd = async ( { active, over } : DragEndEvent ) => {
260
300
console . log ( "active over" , active , over ) ;
261
301
if ( over != null && active . id !== over . id ) {
@@ -296,33 +336,6 @@ const ColumnArticle: FC<IProps> = props => {
296
336
getSortList ( ) ;
297
337
} , [ query , current , pageSize ] ) ;
298
338
299
- // 教程下拉框,可根据教程查询
300
- async function fetchColumnList ( key : string ) : Promise < ColumnValue [ ] > {
301
- console . log ( '根据教程名查询' , key ) ;
302
- const { status, result } = await getColumnByNameListApi ( key ) ;
303
- const { code } = status || { } ;
304
- //@ts -ignore
305
- const { items } = result || { } ;
306
- if ( code === 0 ) {
307
- const newList = items . map ( ( item : MapItem ) => ( {
308
- key : item ?. columnId ,
309
- // label 这里我想把教程封面也加上
310
- label : < div >
311
- < Image
312
- className = "cover-select"
313
- src = { getCompleteUrl ( item ?. cover ) }
314
- />
315
- < span > { item ?. column } </ span >
316
- </ div > ,
317
- value : item ?. column
318
- } ) ) ;
319
- console . log ( "教程列表" , newList ) ;
320
- return newList ;
321
- }
322
- // 没查到数据时,返回空数组
323
- return [ ] ;
324
- } ;
325
-
326
339
// 表头设置
327
340
const columns : ColumnsType < DataType > = [
328
341
{
@@ -357,37 +370,68 @@ const ColumnArticle: FC<IProps> = props => {
357
370
} ,
358
371
{
359
372
title : "操作" ,
360
- width : 210 ,
373
+ width : 150 ,
361
374
render : ( _ , item ) => {
362
375
// 删除的时候用
363
- const { id } = item ;
376
+ const { id, sort } = item ;
364
377
return (
365
378
< div className = "operation-btn" >
366
- < Button
367
- type = "primary"
368
- icon = { < EyeOutlined /> }
369
- style = { { marginRight : "10px" } }
370
- onClick = { ( ) => {
371
- setIsDetailDrawerShow ( true ) ;
372
- // 把所有的值传给 form 表单
373
- handleChange ( { ...item } ) ;
374
- } }
375
- >
376
- 详情
377
- </ Button >
378
- < Button
379
- type = "primary"
380
- danger
381
- icon = { < DeleteOutlined /> }
382
- onClick = { ( ) => handleDel ( id ) } >
383
- 删除
384
- </ Button >
379
+ < Tooltip title = "详情" >
380
+ < Button
381
+ type = "primary"
382
+ icon = { < EyeOutlined /> }
383
+ style = { { marginRight : "10px" } }
384
+ onClick = { ( ) => {
385
+ setIsDetailDrawerShow ( true ) ;
386
+ // 把所有的值传给 form 表单
387
+ handleChange ( { ...item } ) ;
388
+ } }
389
+ > </ Button >
390
+ </ Tooltip >
391
+ < Tooltip title = "调整顺序" >
392
+ < Button
393
+ type = "primary"
394
+ icon = { < SwapOutlined className = "rotated-icon" /> }
395
+ style = { { marginRight : "10px" } }
396
+ onClick = { ( ) => {
397
+ setIsSortDrawerShow ( true ) ;
398
+ // 把 id 和 sort 传给调整顺序的表单
399
+ handleArticleSortChange ( { id, sort } ) ;
400
+ articleSortFormRef . setFieldsValue ( { sort } ) ;
401
+ } }
402
+ > </ Button >
403
+ </ Tooltip >
404
+ < Tooltip title = "删除" >
405
+ < Button
406
+ type = "primary"
407
+ danger
408
+ icon = { < DeleteOutlined /> }
409
+ onClick = { ( ) => handleDel ( id ) } >
410
+ </ Button >
411
+ </ Tooltip >
385
412
</ div >
386
413
) ;
387
414
}
388
415
}
389
416
] ;
390
417
418
+ // 调整顺序的表单
419
+ const articleSortContent = (
420
+ < Form
421
+ autoComplete = "off"
422
+ form = { articleSortFormRef } >
423
+ < Form . Item
424
+ label = "设置文章顺序为"
425
+ name = "sort"
426
+ rules = { [ { required : true , message : '请输入文章顺序' } ] }
427
+ >
428
+ < InputNumber min = { 1 } size = "small"
429
+ onChange = { value => handleArticleSortChange ( { sort : value } ) }
430
+ />
431
+ </ Form . Item >
432
+ </ Form >
433
+ ) ;
434
+
391
435
// 编辑表单
392
436
const reviseDrawerContent = (
393
437
< Form
@@ -458,7 +502,7 @@ const ColumnArticle: FC<IProps> = props => {
458
502
< Drawer
459
503
title = "详情"
460
504
placement = "right"
461
- onClose = { handleCloseDetailDrawer }
505
+ onClose = { handleCloseDrawer }
462
506
open = { isDetailDrawerShow } >
463
507
< Descriptions column = { 1 } labelStyle = { { width : "100px" } } >
464
508
{ detailInfo . map ( ( { label, title } ) => (
@@ -468,6 +512,16 @@ const ColumnArticle: FC<IProps> = props => {
468
512
) ) }
469
513
</ Descriptions >
470
514
</ Drawer >
515
+ { /* 调整顺序的抽屉 */ }
516
+ < Modal
517
+ title = "调整教程顺序"
518
+ width = { 280 }
519
+ style = { { left : 200 } }
520
+ onOk = { handleSortByIDSubmit }
521
+ onCancel = { handleCloseDrawer }
522
+ open = { isSortDrawerShow } >
523
+ { articleSortContent }
524
+ </ Modal >
471
525
{ /* 把弹窗修改为抽屉 */ }
472
526
< Drawer
473
527
title = "添加"
0 commit comments