This repository has been archived by the owner on Jul 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
ProductMedias.js
172 lines (166 loc) · 3.92 KB
/
ProductMedias.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
87
88
89
90
91
92
93
94
95
96
97
98
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
136
137
138
139
140
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
import { Button, Icon, Popconfirm, Upload } from 'antd'
import styled from 'styled-components'
import { useToggle } from 'react-use'
import noop from 'lodash/noop'
import { forwardRef, useEffect, useRef, useState } from 'react'
import gql from 'graphql-tag'
import get from 'lodash/get'
import { useMutation } from '@apollo/react-hooks'
import useViewer from '../hooks/useViewer'
import IPFSImage from './IPFSImage'
import ProductFiles from './ProductFiles.dynamic'
const StyledFiles = styled.div`
display: flex;
align-items: center;
margin-top: 10px;
`
const StyledProductFiles = styled(ProductFiles)`
img {
width: 100%;
object-fit: contain;
}
`
const StyledUpload = styled(Upload)`
width: auto;
.ant-upload-select-picture-card i {
font-size: 24px;
color: #999;
}
> .ant-upload {
width: 78px;
height: 78px;
margin: 0;
}
`
const SmallImage = styled.div`
width: 78px;
height: 78px;
margin-right: 9px;
border: 1px dashed #d9d9d9;
border-radius: 3px;
cursor: pointer;
overflow: hidden;
position: relative;
img {
width: 100%;
height: 100%;
object-fit: cover;
}
button.ant-btn-sm {
position: absolute;
bottom: 0;
left: 0;
right: 0;
width: 100%;
opacity: 0.7;
border-radius: 0;
}
`
const CREATE_MEDIA = gql`
mutation($media: IMedia!) {
createMedia(media: $media) {
id,
hash
}
}
`
export default forwardRef(({ value = [], onChange = noop, onError = noop }, ref) => {
const { viewer: user } = useViewer()
const filesRef = useRef()
const [loading, setLoading] = useToggle(false)
const [ids, setIds] = useState(value)
const [files, setFiles] = useState(value.map(x => ({ id: x })))
const [create, { loading: createLoading }] = useMutation(CREATE_MEDIA, {
onCompleted: data => {
const media = get(data, 'createMedia')
fireChange([
...ids,
media.id
])
},
onError
})
useEffect(() => {
fireChange(value)
}, [value])
const fireChange = ids => {
setIds(ids)
onChange(ids)
}
const handleRemove = file => {
fireChange(ids.filter(x => file.id !== x))
}
const handleChange = info => {
if (info.file.status === 'uploading') {
setLoading(true)
} else if (info.file.status === 'error') {
setLoading(false)
onError(info.file.response)
} else if (info.file.status === 'done') {
setLoading(false)
create({
variables: {
media: {
hash: info.file.response[0].hash
}
}
})
}
}
const uploadButton = files.length >= 6 ? null : (
<div>
<Icon type={(loading || createLoading) ? 'loading' : 'plus'} />
<div className='ant-upload-text'>点击上传</div>
</div>
)
const handleSelect = index => {
filesRef.current.slick.slickGoTo(index)
}
const renderUpload = () => {
return (
<StyledUpload
listType='picture-card'
className='avatar-uploader'
showUploadList={false}
action={process.env.UPLOAD}
onChange={handleChange}
headers={{
authorization: `Bearer ${user && user.token}`
}}
ref={ref}
>
{uploadButton}
</StyledUpload>
)
}
return (
<div>
<StyledProductFiles
ref={filesRef}
autoplay={false}
infinite={false}
variableWidth={false}
centerMode={false}
focusOnSelect={false}
slidesToScroll={1}
slidesToShow={1}
medias={ids.map(x => ({ id: x }))}
onLoad={setFiles}
/>
<StyledFiles>
{files.map((x, i) => (
<SmallImage key={x.id}>
<IPFSImage hash={x.hash} onClick={() => handleSelect(i)} />
<Popconfirm
title='确定删除这张图片吗?'
onConfirm={() => handleRemove(x)}
>
<Button type='danger' size='small' icon='delete' block />
</Popconfirm>
</SmallImage>
))}
{renderUpload()}
</StyledFiles>
</div>
)
})