|
@@ -1,6 +1,7 @@
|
1
|
|
-import React from 'react'
|
|
1
|
+import React, { useState, useEffect } from 'react'
|
2
|
2
|
import { Button, Form, Input } from 'antd'
|
3
|
3
|
import ImageUpload from '@/components/XForm/ImageUpload'
|
|
4
|
+import { fetch, apis } from '@/utils/request'
|
4
|
5
|
|
5
|
6
|
const formItemLayout = {
|
6
|
7
|
labelCol: {
|
|
@@ -12,27 +13,82 @@ const formItemLayout = {
|
12
|
13
|
sm: { span: 19 },
|
13
|
14
|
},
|
14
|
15
|
}
|
|
16
|
+const getData = fetch(apis.activity.shareContent)
|
|
17
|
+const saveData = fetch(apis.activity.addShareContent)
|
|
18
|
+const updateData = fetch(apis.activity.updateShareContent)
|
15
|
19
|
|
16
|
20
|
const ShareForm = (props) => {
|
17
|
21
|
const { target, form } = props
|
18
|
|
- const { id: targetId, type: shareContentType } = target
|
19
|
|
- const { getFieldDecorator } = form
|
|
22
|
+ const { id: targetId, type: targetType } = target
|
|
23
|
+ const { getFieldDecorator, resetFields, setFieldsValue, validateFields } = form
|
|
24
|
+
|
|
25
|
+ const [formData, setFormData] = useState()
|
|
26
|
+ const [loading, setLoading] = useState(false)
|
|
27
|
+
|
|
28
|
+ const handleSubmit = (e) => {
|
|
29
|
+ e.preventDefault();
|
|
30
|
+ validateFields((err, values) => {
|
|
31
|
+ if (err) {
|
|
32
|
+ console.error(err)
|
|
33
|
+ return
|
|
34
|
+ }
|
|
35
|
+
|
|
36
|
+ const request = formData.shareContentId ? updateData : saveData
|
|
37
|
+ const urlData = formData.shareContentId ? { id: formData.shareContentId } : undefined
|
|
38
|
+ const data = {
|
|
39
|
+ targetId,
|
|
40
|
+ shareContentType: targetType,
|
|
41
|
+ ...(formData || {}),
|
|
42
|
+ ...values,
|
|
43
|
+ }
|
|
44
|
+
|
|
45
|
+ setLoading(true)
|
|
46
|
+ request({ urlData, data }).then((res) => {
|
|
47
|
+ setLoading(false)
|
|
48
|
+ setFormData(res)
|
|
49
|
+ message.success('操作成功')
|
|
50
|
+ }).catch((err) => {
|
|
51
|
+ setLoading(false)
|
|
52
|
+ console.error(err)
|
|
53
|
+ })
|
|
54
|
+ })
|
|
55
|
+ }
|
|
56
|
+
|
|
57
|
+ useEffect(() => {
|
|
58
|
+ if (targetId && targetType) {
|
|
59
|
+ setLoading(true)
|
|
60
|
+ getData({ params: { targetId, targetType } }).then((res) => {
|
|
61
|
+ setFormData(res ? res[0] : undefined)
|
|
62
|
+ setLoading(false)
|
|
63
|
+ }).catch((err) => {
|
|
64
|
+ console.error(err)
|
|
65
|
+ setLoading(false)
|
|
66
|
+ })
|
|
67
|
+ }
|
|
68
|
+ }, [targetId, targetType])
|
|
69
|
+
|
|
70
|
+ useEffect(() => {
|
|
71
|
+ const { shareContentImg, shareContentTitle } = formData || {}
|
|
72
|
+
|
|
73
|
+ resetFields()
|
|
74
|
+ setFieldsValue({ shareContentImg, shareContentTitle })
|
|
75
|
+ }, [formData])
|
20
|
76
|
|
21
|
77
|
return (
|
22
|
|
- <Form {...formItemLayout}>
|
|
78
|
+ <Form {...formItemLayout} onSubmit={handleSubmit}>
|
23
|
79
|
<Form.Item label="分享标题">
|
24
|
80
|
{getFieldDecorator('shareContentTitle', {
|
25
|
81
|
rules: [{required: true, message: '请填写分享标题'}]
|
26
|
|
- })(<Input />)}
|
|
82
|
+ })(<Input placeholder="建议不要超过20字" />)}
|
27
|
83
|
</Form.Item>
|
28
|
84
|
<Form.Item label="分享图" help="建议图片尺寸:750*600px,比例5:4,格式:jpg">
|
29
|
|
- {getFieldDecorator('posterImg', {
|
|
85
|
+ {getFieldDecorator('shareContentImg', {
|
30
|
86
|
rules: [{ required: true, message: '请上传分享图' }],
|
31
|
87
|
})(<ImageUpload />)}
|
32
|
88
|
</Form.Item>
|
33
|
89
|
<Form.Item label=" " colon={false} style={{marginTop: '2em'}}>
|
34
|
|
- <Button style={{marginLeft: '4em'}} type="primary" htmlType="submit">保存</Button>
|
35
|
|
- <Button style={{marginLeft: '2em'}} onClick={props.onCancel}>取消</Button>
|
|
90
|
+ <Button loading={loading} style={{marginLeft: '4em'}} type="primary" htmlType="submit">保存</Button>
|
|
91
|
+ {/* <Button style={{marginLeft: '2em'}} onClick={props.onCancel}>取消</Button> */}
|
36
|
92
|
</Form.Item>
|
37
|
93
|
</Form>
|
38
|
94
|
)
|