This repository has been archived by the owner on Aug 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
129 lines (115 loc) 路 3.28 KB
/
index.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
// @flow
import React, { PureComponent } from "react";
import ShowImage from "./image/ShowImage";
import EditImage from "@canner/image-upload";
import defaultMessage from '@canner/antd-locales';
import {isArray} from 'lodash';
import { Button } from "antd";
import {FormattedMessage, injectIntl} from "react-intl";
// type
import type {ObjectDefaultProps} from 'types/ObjectDefaultProps';
type Props = ObjectDefaultProps & {
uiParams: {
filename: string,
dirname: string,
limitSize: ?number
},
disabled: boolean,
imageStorage: any,
intl: Object
};
type State = {
editPopup: boolean
};
type CustomRequestArgs = {
onProgress: (event: { percent: number }) => void,
onError: (event: Error, body?: Object) => void,
onSuccess: (body: Object) => void,
data: Object,
filename: String,
file: File,
withCredentials: Boolean,
action: String,
headers: Object
};
@injectIntl
export default class Image extends PureComponent<Props, State> {
state = {
editPopup: false
};
componentWillReceiveProps(nextProps: Props) {
// if value exist, hide edit popup
if (nextProps.value.url) {
this.setState({
editPopup: false
});
}
}
showEditPopup = () => {
this.setState({
editPopup: true
});
};
closeEditPopup = () => {
this.setState({
editPopup: false
});
};
onChange = (newValue: Array<string> | string) => {
const {value} = this.props;
let url = newValue;
if (isArray(newValue)) {
url = newValue[0];
}
this.props.onChange(this.props.refId, "update", {...value, url});
};
render() {
const { value, disabled, imageStorage, uiParams: {filename, dirname, limitSize}, intl } = this.props;
const { editPopup } = this.state;
// if the image exist show it, otherwise let user upload.
if (value && value.url) {
return (
<ShowImage
onChange={this.onChange}
value={value.url}
disabled={disabled}/>
);
}
return (
<div>
<Button type="primary" onClick={this.showEditPopup} disabled={disabled}>
+ <FormattedMessage id="string.image.add" defaultMessage={defaultMessage.en["string.image.add"]}/>
</Button>
<EditImage
onChange={this.onChange}
editPopup={editPopup}
serviceConfig={{
customRequest: (obj: CustomRequestArgs) => {
const {file, onProgress, onSuccess, onError} = obj;
if (!imageStorage) {
onError(new Error(intl.formatMessage({id: 'image.error.noStorage'})));
return;
}
if (limitSize && file.size > limitSize) {
onError(new Error(intl.formatMessage({
id: 'image.error.limitSize'
}, {
limitSize
})));
return;
}
imageStorage
.upload(file, {filename: genFilename(dirname, filename || file.name)}, onProgress)
.then(({link}) => onSuccess({data: {link}}))
.catch(onError);
}}}
closeEditPopup={this.closeEditPopup}
multiple={false}
/>
</div>
);
}
}
function genFilename(dir, filename) {
return `${dir || ''}${dir ? '/' : ''}${filename}`;
}