|
@@ -0,0 +1,140 @@
|
|
1
|
+import React, { useEffect, useRef, useState } from 'react';
|
|
2
|
+import { connect } from 'umi';
|
|
3
|
+import { PageContainer } from '@ant-design/pro-layout';
|
|
4
|
+import ProTable from '@ant-design/pro-table';
|
|
5
|
+import { PlusOutlined } from '@ant-design/icons';
|
|
6
|
+import { Button, notification } from 'antd';
|
|
7
|
+import {
|
|
8
|
+ ModalForm,
|
|
9
|
+ ProFormText,
|
|
10
|
+} from '@ant-design/pro-form';
|
|
11
|
+import request, { queryTable } from '@/utils/request';
|
|
12
|
+
|
|
13
|
+const PostTypeList = (props) => {
|
|
14
|
+ const tableRef = useRef()
|
|
15
|
+ const form = useRef();
|
|
16
|
+ const [current, setCurrent] = useState({})
|
|
17
|
+ const [visible, setVisible] = useState(false)
|
|
18
|
+
|
|
19
|
+ const handleEdit = (postType) => {
|
|
20
|
+ const data = postType || {}
|
|
21
|
+ setCurrent(data)
|
|
22
|
+ setVisible(true)
|
|
23
|
+ if (form.current) {
|
|
24
|
+ form.current.setFieldsValue(data)
|
|
25
|
+ }
|
|
26
|
+ }
|
|
27
|
+
|
|
28
|
+ const emitPublishOrNot = (postType) => {
|
|
29
|
+ // 1 变成 0, 0 变成 1
|
|
30
|
+ const status = Math.abs(postType.status - 1)
|
|
31
|
+ request(`/post-type/${postType.typeId}`, { method: 'put', data: { ...postType, status } }).then(() => {
|
|
32
|
+ notification.success({ message: '操作成功' })
|
|
33
|
+ tableRef.current.reload()
|
|
34
|
+ }).catch((e) => {
|
|
35
|
+ notification.warn({ message: e.message })
|
|
36
|
+ })
|
|
37
|
+ }
|
|
38
|
+
|
|
39
|
+ const handleSubmit = (postType) => {
|
|
40
|
+ const method = current.typeId ? 'put' : 'post'
|
|
41
|
+ const api = current.typeId ? `/post-type/${current.typeId}` : '/post-type'
|
|
42
|
+ const data = { ...current, ...postType }
|
|
43
|
+ return request(api, { method, data }).then(() => {
|
|
44
|
+ notification.success({ message: '操作成功' })
|
|
45
|
+ tableRef.current.reload()
|
|
46
|
+ setVisible(false)
|
|
47
|
+ }).catch((e) => {
|
|
48
|
+ notification.warn({ message: e.message })
|
|
49
|
+ })
|
|
50
|
+ }
|
|
51
|
+
|
|
52
|
+ const actions = [
|
|
53
|
+ <Button key="button" icon={<PlusOutlined />} type="primary" onClick={handleEdit}>
|
|
54
|
+ 新建
|
|
55
|
+ </Button>,
|
|
56
|
+ ];
|
|
57
|
+
|
|
58
|
+ const columns = [
|
|
59
|
+ {
|
|
60
|
+ title: '标题',
|
|
61
|
+ key: 'name',
|
|
62
|
+ dataIndex: 'name',
|
|
63
|
+ },
|
|
64
|
+ {
|
|
65
|
+ title: '状态',
|
|
66
|
+ key: 'status',
|
|
67
|
+ dataIndex: 'status',
|
|
68
|
+ width: 240,
|
|
69
|
+ valueType: 'select',
|
|
70
|
+ valueEnum: {
|
|
71
|
+ 0: {
|
|
72
|
+ text: '下架',
|
|
73
|
+ status: 'Processing',
|
|
74
|
+ },
|
|
75
|
+ 1: {
|
|
76
|
+ text: '上架',
|
|
77
|
+ status: 'Success',
|
|
78
|
+ },
|
|
79
|
+ },
|
|
80
|
+ },
|
|
81
|
+ {
|
|
82
|
+ title: '创建时间',
|
|
83
|
+ key: 'createDate',
|
|
84
|
+ dataIndex: 'createDate',
|
|
85
|
+ valueType: 'date',
|
|
86
|
+ width: 240,
|
|
87
|
+ hideInSearch: true,
|
|
88
|
+ },
|
|
89
|
+ {
|
|
90
|
+ title: '操作',
|
|
91
|
+ key: 'option',
|
|
92
|
+ valueType: 'option',
|
|
93
|
+ width: 160,
|
|
94
|
+ render: (_, item) => [
|
|
95
|
+ <a key="edit" onClick={() => handleEdit(item)}>
|
|
96
|
+ 编辑
|
|
97
|
+ </a>,
|
|
98
|
+ <a key="up" onClick={() => emitPublishOrNot(item)}>
|
|
99
|
+ {item.status === 1 ? '下架' : '上架'}
|
|
100
|
+ </a>,
|
|
101
|
+ ],
|
|
102
|
+ },
|
|
103
|
+ ];
|
|
104
|
+
|
|
105
|
+ useEffect(() => {
|
|
106
|
+ if (!props.typeList || !props.typeList.length) {
|
|
107
|
+ props.dispatch({
|
|
108
|
+ type: 'post/getTypeList',
|
|
109
|
+ payload: { pageSize: 999 },
|
|
110
|
+ });
|
|
111
|
+ }
|
|
112
|
+ }, [props]);
|
|
113
|
+
|
|
114
|
+ return (
|
|
115
|
+ <PageContainer>
|
|
116
|
+ <ProTable
|
|
117
|
+ actionRef={tableRef}
|
|
118
|
+ columns={columns}
|
|
119
|
+ request={queryTable('/post-type')}
|
|
120
|
+ rowKey="typeId"
|
|
121
|
+ headerTitle="科普列表"
|
|
122
|
+ search={{
|
|
123
|
+ labelWidth: '4em',
|
|
124
|
+ }}
|
|
125
|
+ toolBarRender={() => actions}
|
|
126
|
+ />
|
|
127
|
+ <ModalForm formRef={form} width={600} visible={visible} onVisibleChange={setVisible} onFinish={handleSubmit}>
|
|
128
|
+ <ProFormText
|
|
129
|
+ label="名称"
|
|
130
|
+ name="name"
|
|
131
|
+ rules={[{ required: true, message: '请填写名称' }]}
|
|
132
|
+ />
|
|
133
|
+ </ModalForm>
|
|
134
|
+ </PageContainer>
|
|
135
|
+ );
|
|
136
|
+};
|
|
137
|
+
|
|
138
|
+export default connect((s) => ({
|
|
139
|
+ typeList: s.post.typeList,
|
|
140
|
+}))(PostTypeList);
|