diff options
author | Muhamed <unknown> | 2022-11-08 07:40:02 +0530 |
---|---|---|
committer | Vijay Aravamudhan <vraravam@users.noreply.github.com> | 2022-11-08 17:25:27 +0530 |
commit | 570d26baf9e4ad87a8c0752b5edfb5c441bf9d80 (patch) | |
tree | ee0a0e6bec7d10ec438b5269537905fee80c377b /src/components/ui/ImageUpload.tsx | |
parent | refactor: remove toggle component's duplicate (diff) | |
download | ferdium-app-570d26baf9e4ad87a8c0752b5edfb5c441bf9d80.tar.gz ferdium-app-570d26baf9e4ad87a8c0752b5edfb5c441bf9d80.tar.zst ferdium-app-570d26baf9e4ad87a8c0752b5edfb5c441bf9d80.zip |
fix: slack issue caused by input TS conversion
Diffstat (limited to 'src/components/ui/ImageUpload.tsx')
-rw-r--r-- | src/components/ui/ImageUpload.tsx | 163 |
1 files changed, 0 insertions, 163 deletions
diff --git a/src/components/ui/ImageUpload.tsx b/src/components/ui/ImageUpload.tsx deleted file mode 100644 index 7732928f8..000000000 --- a/src/components/ui/ImageUpload.tsx +++ /dev/null | |||
@@ -1,163 +0,0 @@ | |||
1 | import { Component } from 'react'; | ||
2 | import { observer } from 'mobx-react'; | ||
3 | import { Field } from 'mobx-react-form'; | ||
4 | import classnames from 'classnames'; | ||
5 | import Dropzone from 'react-dropzone'; | ||
6 | import { mdiDelete, mdiFileImage } from '@mdi/js'; | ||
7 | import prettyBytes from 'pretty-bytes'; | ||
8 | import { isWindows } from '../../environment'; | ||
9 | import Icon from './icon'; | ||
10 | |||
11 | type Props = { | ||
12 | field: typeof Field; | ||
13 | className: string; | ||
14 | multiple: boolean; | ||
15 | textDelete: string; | ||
16 | textUpload: string; | ||
17 | textMaxFileSize: string; | ||
18 | textMaxFileSizeError: string; | ||
19 | maxSize?: number; | ||
20 | maxFiles?: number; | ||
21 | messages: any; | ||
22 | }; | ||
23 | |||
24 | // Should this file be converted into the coding style similar to './toggle/index.tsx'? | ||
25 | class ImageUpload extends Component<Props> { | ||
26 | static defaultProps = { | ||
27 | multiple: false, | ||
28 | maxSize: Number.POSITIVE_INFINITY, | ||
29 | maxFiles: 0, | ||
30 | }; | ||
31 | |||
32 | state = { | ||
33 | path: null, | ||
34 | errorState: false, | ||
35 | }; | ||
36 | |||
37 | errorMessage = { | ||
38 | message: '', | ||
39 | }; | ||
40 | |||
41 | onDropAccepted(acceptedFiles) { | ||
42 | const { field } = this.props; | ||
43 | this.setState({ errorState: false }); | ||
44 | |||
45 | for (const file of acceptedFiles) { | ||
46 | const imgPath = isWindows ? file.path.replace(/\\/g, '/') : file.path; | ||
47 | this.setState({ | ||
48 | path: imgPath, | ||
49 | }); | ||
50 | |||
51 | this.props.field.onDrop(file); | ||
52 | } | ||
53 | |||
54 | field.set(''); | ||
55 | } | ||
56 | |||
57 | onDropRejected(rejectedFiles): void { | ||
58 | for (const file of rejectedFiles) { | ||
59 | for (const error of file.errors) { | ||
60 | if (error.code === 'file-too-large') { | ||
61 | this.setState({ errorState: true }); | ||
62 | this.setState( | ||
63 | (this.errorMessage = { | ||
64 | message: this.props.textMaxFileSizeError, | ||
65 | }), | ||
66 | ); | ||
67 | } | ||
68 | } | ||
69 | } | ||
70 | } | ||
71 | |||
72 | render() { | ||
73 | const { | ||
74 | field, | ||
75 | className, | ||
76 | multiple, | ||
77 | textDelete, | ||
78 | textUpload, | ||
79 | textMaxFileSize, | ||
80 | maxSize, | ||
81 | maxFiles, | ||
82 | } = this.props; | ||
83 | |||
84 | const cssClasses = classnames({ | ||
85 | 'image-upload__dropzone': true, | ||
86 | [`${className}`]: className, | ||
87 | }); | ||
88 | |||
89 | const maxSizeParse: number = | ||
90 | maxSize === undefined || maxSize === Number.POSITIVE_INFINITY | ||
91 | ? 0 | ||
92 | : maxSize; | ||
93 | |||
94 | return ( | ||
95 | <div className="image-upload-wrapper"> | ||
96 | <label className="franz-form__label" htmlFor="iconUpload"> | ||
97 | {field.label} | ||
98 | </label> | ||
99 | <div className="image-upload"> | ||
100 | {(field.value && field.value !== 'delete') || this.state.path ? ( | ||
101 | <> | ||
102 | <div | ||
103 | className="image-upload__preview" | ||
104 | style={{ | ||
105 | backgroundImage: `url("${this.state.path || field.value}")`, | ||
106 | }} | ||
107 | /> | ||
108 | <div className="image-upload__action"> | ||
109 | <button | ||
110 | type="button" | ||
111 | onClick={() => { | ||
112 | if (field.value) { | ||
113 | field.set('delete'); | ||
114 | } else { | ||
115 | this.setState({ | ||
116 | path: null, | ||
117 | }); | ||
118 | } | ||
119 | }} | ||
120 | > | ||
121 | <Icon icon={mdiDelete} /> | ||
122 | <p>{textDelete}</p> | ||
123 | </button> | ||
124 | <div className="image-upload__action-background" /> | ||
125 | </div> | ||
126 | </> | ||
127 | ) : ( | ||
128 | <Dropzone | ||
129 | onDropAccepted={this.onDropAccepted.bind(this)} | ||
130 | onDropRejected={this.onDropRejected.bind(this)} | ||
131 | multiple={multiple} | ||
132 | accept="image/jpeg, image/png, image/svg+xml" | ||
133 | minSize={0} | ||
134 | maxSize={maxSize} | ||
135 | maxFiles={maxFiles} | ||
136 | > | ||
137 | {({ getRootProps, getInputProps }) => ( | ||
138 | <div {...getRootProps()} className={cssClasses}> | ||
139 | <Icon icon={mdiFileImage} /> | ||
140 | <p>{textUpload}</p> | ||
141 | <input {...getInputProps()} /> | ||
142 | </div> | ||
143 | )} | ||
144 | </Dropzone> | ||
145 | )} | ||
146 | </div> | ||
147 | {maxSizeParse !== 0 && ( | ||
148 | <span className="image-upload-wrapper__file-size"> | ||
149 | {textMaxFileSize}{' '} | ||
150 | {prettyBytes(maxSizeParse, { maximumFractionDigits: 1 })} | ||
151 | </span> | ||
152 | )} | ||
153 | {this.state.errorState && ( | ||
154 | <span className="image-upload-wrapper__file-size-error"> | ||
155 | {this.errorMessage.message} | ||
156 | </span> | ||
157 | )} | ||
158 | </div> | ||
159 | ); | ||
160 | } | ||
161 | } | ||
162 | |||
163 | export default observer(ImageUpload); | ||