Upload.js 14.7 KB
Newer Older
1 2 3 4
import React, { Component, createRef } from "react"
import Dropzone from 'react-dropzone'
import LinearProgress from '@material-ui/core/LinearProgress'
import Images from "../assets/Images"
5 6
import MuiAlert from '@material-ui/lab/Alert';
import { withStyles, Snackbar } from "@material-ui/core";
7 8

const dropzoneRef = createRef()
9 10
const Alert = withStyles({
})((props) => <MuiAlert elevation={6} variant="filled" {...props} />);
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

class Upload extends Component {
    constructor(props) {
        super(props)
        this.state = {
            file: '',
            nameFile: 'File name',
            typeFile: '',
            sizeFile: '0',
            inputVisible: true,
            previewVisible: false,
            labelUploadVisible: false,
            iconButtonUpload: 'fa fa-1x fa-upload',
            uploadProgress: false,
            percentage: '0',
26 27
            completed: '0',
            alert: false,
28
            alertMessage: '',
Deni Rinaldi's avatar
Deni Rinaldi committed
29
            sizeUpload: this.props.sizeUpload === undefined ? 1 : this.props.sizeUpload
30 31 32 33 34
        }
    }

    componentDidUpdate(prevs, next) {
        if (prevs.percentage > 0 && prevs.percentage <= 100) {
Deni Rinaldi's avatar
Deni Rinaldi committed
35 36
            // console.log('old percentage', prevs.percentage)
            // console.log('new percentage', this.state.percentage)
37
            if (this.state.percentage !== prevs.percentage) {
38
                this.setState({ percentage: prevs.percentage })
39 40 41 42
            }
        }
        if (prevs.result === 'success' || prevs.result === 'error') {
            if (this.state.uploadProgress === true) {
43
                this.setState({ uploadProgress: false, percentage: '100', iconButtonUpload: 'fa fa-1x fa-check' })
44 45 46 47
            }
        }
    }

Deni Rinaldi's avatar
Deni Rinaldi committed
48 49
    componentDidMount() {
        if (this.props.docName !== undefined && this.props.docName !== "") {
Deni Rinaldi's avatar
Deni Rinaldi committed
50
            // console.log('masuk');
Deni Rinaldi's avatar
Deni Rinaldi committed
51 52 53 54
            this.setState({ docName: this.props.docName, docSize: this.props.docSize, inputVisible: false, editVisible: true })
        }
    }

55 56 57 58 59
    onDrop = (acceptedFiles) => {
        const formData = new FormData()
        let length = acceptedFiles[0].name.split(".").length
        let fileType = acceptedFiles[0].name.split(".")[length - 1]
        formData.append('file', acceptedFiles[0])
Deni Rinaldi's avatar
Deni Rinaldi committed
60
        // console.log(sizeFile);
Deni Rinaldi's avatar
Deni Rinaldi committed
61 62
        if (this.props.intent === 'management') {
            if (acceptedFiles) {
63
                // if (this.props.acceptedFiles.includes(fileType)) {
64
                    if ((acceptedFiles[0].size / 1000).toFixed(0) < (this.props.sizeUpload * 1000)) {
Deni Rinaldi's avatar
Deni Rinaldi committed
65 66 67 68 69 70 71 72 73 74 75 76
                        this.setState({
                            file: acceptedFiles[0],
                            typeFile: fileType,
                            nameFile: acceptedFiles[0].name,
                            sizeFile: (acceptedFiles[0].size / 1000).toFixed(0),
                            previewVisible: true,
                            inputVisible: false,
                            uploadProgress: false,
                            percentage: '0'
                        })
                        this.props.onHandle(acceptedFiles[0])
                    } else {
77
                        this.setState({ alertMessage: `The file is too large. Allowed maximum size is ${this.props.sizeUpload} MB`, alert: true })
Deni Rinaldi's avatar
Deni Rinaldi committed
78 79
                        // alert('File Tidak Boleh Lebih Dari 1MB')
                    }
80 81 82 83
                // } else {
                //     this.setState({ alertMessage: 'File extension not allowed', alert: true })
                //     // alert('File Tidak Mendukung')
                // }
Deni Rinaldi's avatar
Deni Rinaldi committed
84 85 86 87 88 89 90 91
            }
            else {
                this.setState({
                    previewVisible: false,
                    inputVisible: true,
                    uploadProgress: false,
                    percentage: '0'
                })
92
                this.setState({ alertMessage: "File extension not allowed", alert: true })
Deni Rinaldi's avatar
Deni Rinaldi committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
                // alert("Unsupported Media Type")
            }
        } else {
            if (acceptedFiles) {
                this.setState({
                    file: acceptedFiles[0],
                    typeFile: fileType,
                    nameFile: acceptedFiles[0].name,
                    sizeFile: (acceptedFiles[0].size / 1000).toFixed(0),
                    previewVisible: true,
                    inputVisible: false,
                    uploadProgress: false,
                    percentage: '0'
                })
                this.props.onHandle(acceptedFiles[0])
            }
            else {
                this.setState({
                    previewVisible: false,
                    inputVisible: true,
                    uploadProgress: false,
                    percentage: '0'
                })
116
                this.setState({ alertMessage: "File extension not allowed", alert: true })
Deni Rinaldi's avatar
Deni Rinaldi committed
117 118 119 120 121 122 123
                // alert("Unsupported Media Type")
            }
        }
    }

    onRemove = () => {
        if (this.props.intent === "management") {
124
            this.setState({
Deni Rinaldi's avatar
Deni Rinaldi committed
125
                previewVisible: false,
Deni Rinaldi's avatar
Deni Rinaldi committed
126
                editVisible: false,
Deni Rinaldi's avatar
Deni Rinaldi committed
127
                inputVisible: true,
128 129 130
                uploadProgress: false,
                percentage: '0'
            })
Deni Rinaldi's avatar
Deni Rinaldi committed
131 132
            this.props.onDelete("delete")
        } else {
133 134
            this.setState({
                previewVisible: false,
Deni Rinaldi's avatar
Deni Rinaldi committed
135
                editVisible: false,
136 137 138 139 140 141 142 143
                inputVisible: true,
                uploadProgress: false,
                percentage: '0'
            })
        }
    }

    onUpload = () => {
144 145 146 147 148 149 150
        // this.props.onUpload()
        var strProps = this.props.acceptedFiles
        var strState = this.state.typeFile
        // console.log(strProps);
        // console.log(strState);

        if (strProps.includes(strState)) {
Deni Rinaldi's avatar
Deni Rinaldi committed
151
            if (this.props.intent === 'management') {
Deni Rinaldi's avatar
Deni Rinaldi committed
152
                if (this.state.sizeFile < this.state.sizeUpload * 1000) {
Deni Rinaldi's avatar
Deni Rinaldi committed
153 154
                    this.props.onUpload()
                } else {
Deni Rinaldi's avatar
Deni Rinaldi committed
155
                    this.setState({ alertMessage: 'The file is too large. Allowed maximum size is 1MB', alert: true })
Deni Rinaldi's avatar
Deni Rinaldi committed
156 157
                    // alert('File Tidak Boleh Lebih Dari 1MB')
                }
158
            } else {
159
                if (this.state.sizeFile < this.state.sizeUpload * 1000) {
Deni Rinaldi's avatar
Deni Rinaldi committed
160 161 162 163 164
                    this.props.onUpload()
                } else {
                    this.setState({ alertMessage: 'The file is too large. Allowed maximum size is 1MB', alert: true })
                    // alert('File Tidak Boleh Lebih Dari 1MB')
                }
165 166
            }
        } else {
167
            this.setState({ alertMessage: 'File extension not allowed', alert: true })
168
            // alert('File Tidak Mendukung')
169
        }
170
    }
171

172 173
    closeAlert() {
        this.setState({ alert: false })
174 175 176
    }

    render() {
177
        return (
178 179
            <div>
                <Dropzone ref={dropzoneRef} onDrop={this.onDrop}>
180
                    {({ getRootProps, getInputProps }) => (
181
                        <div>
182 183 184 185 186
                            <Snackbar open={this.state.alert} autoHideDuration={6000} onClose={() => this.closeAlert()}>
                                <Alert onClose={() => this.closeAlert()} severity={"warning"}>
                                    {this.state.alertMessage}
                                </Alert>
                            </Snackbar>
187 188 189
                            <div className="upload-file">

                                {/* label */}
190 191 192 193 194
                                {this.state.inputVisible
                                    ? <div {...getRootProps()} className="padding-10px" style={{ cursor: 'pointer' }}>
                                        <input {...getInputProps()} />
                                        <div className="u-p-title">
                                            Drag 'n' drop some files here, or click to select files
195
                                    </div>
196 197
                                    </div>
                                    : null}
198

Deni Rinaldi's avatar
Deni Rinaldi committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
                                {/* edit */}
                                {this.state.editVisible ?
                                    <div className="u-p-file u-p-preview">
                                        <div className="display-flex-normal">
                                            <div className="width width-35px">
                                                <button
                                                    onClick={this.onRemove}
                                                    className="btn btn-small-circle btn-black"
                                                    type="button">
                                                    <img src={Images.close} />
                                                </button>
                                            </div>
                                            <div className="width width-full margin-left-10px">
                                                <div className="txt-site txt-12 txt-white txt-bold">
                                                    {this.state.docName}
                                                </div>
                                                <div className="txt-site txt-11 txt-white txt-thin">
Deni Rinaldi's avatar
Deni Rinaldi committed
216
                                                    {this.state.docSize/1000} KB
Deni Rinaldi's avatar
Deni Rinaldi committed
217 218 219 220 221
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    : null}
222
                                {/* file */}
223 224 225 226 227 228 229 230 231
                                {this.state.previewVisible
                                    ?
                                    <div className="u-p-file u-p-preview">
                                        <div className="display-flex-normal">
                                            <div className="width width-35px">
                                                <button
                                                    onClick={this.state.uploadProgress === true ? null : this.onRemove}
                                                    className="btn btn-small-circle btn-black"
                                                    type="button">
Deni Rinaldi's avatar
Deni Rinaldi committed
232
                                                    <img src={Images.close} />
233
                                                </button>
234
                                            </div>
235 236 237 238 239 240
                                            <div className="width width-full margin-left-10px">
                                                <div className="txt-site txt-12 txt-white txt-bold">
                                                    {this.state.nameFile}
                                                </div>
                                                <div className="txt-site txt-11 txt-white txt-thin">
                                                    {this.state.sizeFile} KB
241
                                            </div>
242 243
                                            </div>
                                            {/* this.state.uploadProgress === true
244 245 246 247 248
                                        ? (
                                            <div className="width width-100px padding-10px txt-site txt-12 txt-white txt-thin">
                                                ({this.state.percentage})%
                                            </div>
                                        ) : null  */}
249 250 251 252
                                            {this.props.type === 'upload' ?
                                                <div className="width width-155px padding-10px txt-site txt-11 txt-white txt-thin">
                                                    Uploading {this.state.percentage}%
                                            </div> : null}
253

254 255 256 257 258 259 260 261
                                            {this.props.disableButtonUpload !== true ? (
                                                <div className="width width-35px">
                                                    <button
                                                        className="btn btn-small-circle"
                                                        type="button"
                                                        onClick={this.state.uploadProgress === true ? null : this.onUpload}>
                                                        {/*<i className={this.state.iconButtonUpload} />*/}
                                                        {this.state.uploadProgress === true
Deni Rinaldi's avatar
Deni Rinaldi committed
262 263
                                                            ? <i className={'fa fa-1x fa-spinner fa-spin'} /> : this.props.intent === 'management' ? null :
                                                                <img src={Images.upload} />}
264 265 266 267 268 269 270 271 272
                                                    </button>
                                                </div>
                                            ) : null}
                                        </div>
                                        {this.props.type === 'upload' ?
                                            <div className="margin-top-15px">
                                                <LinearProgress variant="determinate" value={this.state.percentage} />
                                            </div> : null}
                                        {/* this.state.uploadProgress === true
273 274 275 276 277
                                    ? (
                                        <div className="margin-15px">
                                            <LinearProgress variant="determinate" value={this.state.percentage} />
                                        </div>
                                    ) : null */}
278 279
                                    </div>
                                    : null}
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306

                                {/* image */}
                                {/* <div className="u-p-image u-p-preview">
                                    <div className="display-flex-normal">
                                        <div className="width width-full display-flex-normal">
                                            <button className="btn btn-small-circle btn-black">
                                                <i className="fa fa-1x fa-times" />
                                            </button>
                                        </div>
                                        <div className="width width-full display-flex-normal content-right">
                                            <button className="btn btn-small-circle btn-black">
                                                <i className="fa fa-1x fa-upload" />
                                            </button>
                                        </div>
                                    </div>
                                </div> */}

                            </div>
                        </div>
                    )}
                </Dropzone>
            </div>
        )
    }
}

export default Upload