Upload.js 10.1 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 28
            completed: '0',
            alert: false,
            alertMessage: ''
29 30 31 32 33 34 35 36
        }
    }

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

    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])
52 53
        console.log(acceptedFiles);
        if (acceptedFiles) {
54 55 56 57
            this.setState({
                file: acceptedFiles[0],
                typeFile: fileType,
                nameFile: acceptedFiles[0].name,
58
                sizeFile: (acceptedFiles[0].size / 1000).toFixed(0),
59 60 61 62 63 64
                previewVisible: true,
                inputVisible: false,
                uploadProgress: false,
                percentage: '0'
            })
            this.props.onHandle(acceptedFiles[0])
65 66
        }
        else {
67 68 69 70 71 72
            this.setState({
                previewVisible: false,
                inputVisible: true,
                uploadProgress: false,
                percentage: '0'
            })
73 74
            this.setState({ alertMessage: "Unsupported Media Type", alert: true })
            // alert("Unsupported Media Type")
75 76 77 78 79 80 81 82 83 84 85 86 87
        }
    }

    onRemove = () => {
        this.setState({
            previewVisible: false,
            inputVisible: true,
            uploadProgress: false,
            percentage: '0'
        })
    }

    onUpload = () => {
88 89 90 91 92 93 94
        // this.props.onUpload()
        var strProps = this.props.acceptedFiles
        var strState = this.state.typeFile
        // console.log(strProps);
        // console.log(strState);

        if (strProps.includes(strState)) {
95
            if (this.state.sizeFile < 1000) {
96 97
                this.props.onUpload()
            } else {
98 99
                this.setState({ alertMessage: 'File Tidak Boleh Lebih Dari 1MB', alert: true })
                // alert('File Tidak Boleh Lebih Dari 1MB')
100 101
            }
        } else {
102 103
            this.setState({ alertMessage: 'File Tidak Mendukung', alert: true })
            // alert('File Tidak Mendukung')
104
        }
105
    }
106

107 108
    closeAlert() {
        this.setState({ alert: false })
109 110 111
    }

    render() {
112
        return (
113 114
            <div>
                <Dropzone ref={dropzoneRef} onDrop={this.onDrop}>
115
                    {({ getRootProps, getInputProps }) => (
116
                        <div>
117 118 119 120 121
                            <Snackbar open={this.state.alert} autoHideDuration={6000} onClose={() => this.closeAlert()}>
                                <Alert onClose={() => this.closeAlert()} severity={"warning"}>
                                    {this.state.alertMessage}
                                </Alert>
                            </Snackbar>
122 123 124
                            <div className="upload-file">

                                {/* label */}
125 126 127 128 129
                                {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
130
                                    </div>
131 132
                                    </div>
                                    : null}
133 134

                                {/* file */}
135 136 137 138 139 140 141 142 143
                                {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
144
                                                    <img src={Images.close} />
145
                                                </button>
146
                                            </div>
147 148 149 150 151 152
                                            <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
153
                                            </div>
154 155
                                            </div>
                                            {/* this.state.uploadProgress === true
156 157 158 159 160
                                        ? (
                                            <div className="width width-100px padding-10px txt-site txt-12 txt-white txt-thin">
                                                ({this.state.percentage})%
                                            </div>
                                        ) : null  */}
161 162 163 164
                                            {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}
165

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
                                            {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
                                                            ? <i className={'fa fa-1x fa-spinner fa-spin'} /> :
                                                            <img src={Images.upload} />}
                                                    </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
185 186 187 188 189
                                    ? (
                                        <div className="margin-15px">
                                            <LinearProgress variant="determinate" value={this.state.percentage} />
                                        </div>
                                    ) : null */}
190 191
                                    </div>
                                    : null}
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218

                                {/* 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