Upload.js 12.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 35 36 37
        }
    }

    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) {
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 48 49 50 51 52
            }
        }
    }

    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
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
        console.log(fileType);
        if (this.props.intent === 'management') {
            if (acceptedFiles) {
                if (this.props.acceptedFiles.includes(fileType)) {
                    if (this.state.sizeFile < 10000) {
                        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({ alertMessage: 'The file is too large. Allowed maximum size is 10MB', alert: true })
                        // alert('File Tidak Boleh Lebih Dari 1MB')
                    }
                } else {
74
                    this.setState({ alertMessage: 'File extension not allowed', alert: true })
Deni Rinaldi's avatar
Deni Rinaldi committed
75 76 77 78 79 80 81 82 83 84
                    // alert('File Tidak Mendukung')
                }
            }
            else {
                this.setState({
                    previewVisible: false,
                    inputVisible: true,
                    uploadProgress: false,
                    percentage: '0'
                })
85
                this.setState({ alertMessage: "File extension not allowed", alert: true })
Deni Rinaldi's avatar
Deni Rinaldi committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
                // 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'
                })
109
                this.setState({ alertMessage: "File extension not allowed", alert: true })
Deni Rinaldi's avatar
Deni Rinaldi committed
110 111 112 113 114 115 116
                // alert("Unsupported Media Type")
            }
        }
    }

    onRemove = () => {
        if (this.props.intent === "management") {
117
            this.setState({
Deni Rinaldi's avatar
Deni Rinaldi committed
118 119
                previewVisible: false,
                inputVisible: true,
120 121 122
                uploadProgress: false,
                percentage: '0'
            })
Deni Rinaldi's avatar
Deni Rinaldi committed
123 124
            this.props.onDelete("delete")
        } else {
125 126 127 128 129 130 131 132 133 134
            this.setState({
                previewVisible: false,
                inputVisible: true,
                uploadProgress: false,
                percentage: '0'
            })
        }
    }

    onUpload = () => {
135 136 137 138 139 140 141
        // 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
142 143 144 145 146 147 148
            if (this.props.intent === 'management') {
                if (this.state.sizeFile < 10000) {
                    this.props.onUpload()
                } else {
                    this.setState({ alertMessage: 'The file is too large. Allowed maximum size is 10MB', alert: true })
                    // alert('File Tidak Boleh Lebih Dari 1MB')
                }
149
            } else {
150
                if (this.state.sizeFile < this.state.sizeUpload * 1000) {
Deni Rinaldi's avatar
Deni Rinaldi committed
151 152 153 154 155
                    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')
                }
156 157
            }
        } else {
158
            this.setState({ alertMessage: 'File extension not allowed', alert: true })
159
            // alert('File Tidak Mendukung')
160
        }
161
    }
162

163 164
    closeAlert() {
        this.setState({ alert: false })
165 166 167
    }

    render() {
168
        return (
169 170
            <div>
                <Dropzone ref={dropzoneRef} onDrop={this.onDrop}>
171
                    {({ getRootProps, getInputProps }) => (
172
                        <div>
173 174 175 176 177
                            <Snackbar open={this.state.alert} autoHideDuration={6000} onClose={() => this.closeAlert()}>
                                <Alert onClose={() => this.closeAlert()} severity={"warning"}>
                                    {this.state.alertMessage}
                                </Alert>
                            </Snackbar>
178 179 180
                            <div className="upload-file">

                                {/* label */}
181 182 183 184 185
                                {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
186
                                    </div>
187 188
                                    </div>
                                    : null}
189 190

                                {/* file */}
191 192 193 194 195 196 197 198 199
                                {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
200
                                                    <img src={Images.close} />
201
                                                </button>
202
                                            </div>
203 204 205 206 207 208
                                            <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
209
                                            </div>
210 211
                                            </div>
                                            {/* this.state.uploadProgress === true
212 213 214 215 216
                                        ? (
                                            <div className="width width-100px padding-10px txt-site txt-12 txt-white txt-thin">
                                                ({this.state.percentage})%
                                            </div>
                                        ) : null  */}
217 218 219 220
                                            {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}
221

222 223 224 225 226 227 228 229
                                            {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
230 231
                                                            ? <i className={'fa fa-1x fa-spinner fa-spin'} /> : this.props.intent === 'management' ? null :
                                                                <img src={Images.upload} />}
232 233 234 235 236 237 238 239 240
                                                    </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
241 242 243 244 245
                                    ? (
                                        <div className="margin-15px">
                                            <LinearProgress variant="determinate" value={this.state.percentage} />
                                        </div>
                                    ) : null */}
246 247
                                    </div>
                                    : null}
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274

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