DownloadReport.js 21.8 KB
Newer Older
Riri Novita's avatar
Riri Novita committed
1
import React, { Component, useRef } from 'react';
Riri Novita's avatar
Riri Novita committed
2
import { Typography, Paper, TextField, Snackbar, withStyles, Switch, FormControlLabel, InputAdornment } from '@material-ui/core';
Riri Novita's avatar
Riri Novita committed
3 4 5 6 7 8 9 10 11 12 13 14
import Images from '../../assets/Images';
import Constant from '../../library/Constant';
import api from '../../api';
import { PropagateLoader } from 'react-spinners';
import { format } from 'date-fns';
import Autocomplete from '@material-ui/lab/Autocomplete';
import { Alert } from '@material-ui/lab';
import ReactTooltip from "react-tooltip";
import SunEditor, { buttonList } from "suneditor-react";
import 'suneditor/dist/css/suneditor.min.css'; // Import Sun Editor's CSS File
import CloudDownloadIcon from '@material-ui/icons/CloudDownload';
import moment from 'moment';
Riri Novita's avatar
Riri Novita committed
15 16
import CheckCircleOutlineIcon from '@material-ui/icons/CheckCircleOutline';
import CheckCircleIcon from '@material-ui/icons/CheckCircle';
Riri Novita's avatar
Riri Novita committed
17
import SearchIcon from '@material-ui/icons/Search';
Riri Novita's avatar
Riri Novita committed
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


const IOSSwitch = withStyles((theme) => ({
    root: {
        width: 55,
        height: 31,
        padding: 0,
        margin: theme.spacing(1),
    },
    switchBase: {
        padding: 3,
        marginLeft: 1,
        '&$checked': {
            transform: 'translateX(16px)',
            color: theme.palette.common.white,
            paddingLeft: 11,
            '& + $track': {
                backgroundColor: '#52d869',
                opacity: 1,
                border: 'none',
            },
        },
        '&$focusVisible $thumb': {
            color: '#52d869',
            border: '6px solid #fff',
        },
    },
    thumb: {
        width: 25,
        height: 25,
    },
    track: {
        borderRadius: 30 / 2,
        border: `1px solid ${theme.palette.grey[400]}`,
        backgroundColor: 'red',
        opacity: 1,
        transition: theme.transitions.create(['background-color', 'border']),
    },
    checked: {},
    focusVisible: {},
}))(({ classes, ...props }) => {
    return (
        <Switch
            focusVisibleClassName={classes.focusVisible}
            disableRipple
            classes={{
                root: classes.root,
                switchBase: classes.switchBase,
                thumb: classes.thumb,
                track: classes.track,
                checked: classes.checked,
            }}
            {...props}
        />
    );
});


class DownloadReport extends Component {
    constructor(props) {
        super(props)
        this.editorRef = React.createRef();
        this.editorRefEmail = React.createRef();
        // this.handleChange = this.handleChange.bind(this)
        this.state = {
            load: false,
            loading: false,
Riri Novita's avatar
Riri Novita committed
85 86 87 88
            listDownload: [],
            oldListDownload: [],
            search: "",
            isSearch: false
Riri Novita's avatar
Riri Novita committed
89 90 91 92 93
        }
    }


    componentDidMount() {
Riri Novita's avatar
Riri Novita committed
94
        const interval = setInterval(() => {
Riri Novita's avatar
Riri Novita committed
95
            this.getListDownload()
Riri Novita's avatar
Riri Novita committed
96
        }, 500);
Riri Novita's avatar
Riri Novita committed
97
        return () => clearInterval(interval);
Riri Novita's avatar
Riri Novita committed
98 99
    }

Riri Novita's avatar
Riri Novita committed
100
    getListDownload() {
Riri Novita's avatar
Riri Novita committed
101
        api.create().getListDownload().then((response) => {
Riri Novita's avatar
Riri Novita committed
102
            // console.log(response);
Riri Novita's avatar
Riri Novita committed
103 104 105 106
            if (response.data) {
                if (response.ok) {
                    if (response.data.status === 'success') {
                        let data = response.data.data
Riri Novita's avatar
Riri Novita committed
107
                        // console.log(data);
Riri Novita's avatar
Riri Novita committed
108 109 110 111
                        let arr = [];
                        var groups = this.groupItems(data, "created_at"); // array will be grouped by 'date' property

                        for (var key in groups) {
Riri Novita's avatar
Riri Novita committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
                            if (this.state.search.length == 0) {
                                arr.push({
                                    time: key,
                                    items: groups[key],
                                });
                            } else {
                                let itmDtl = []
                                // console.log(groups);
                                groups[key].forEach((d) => {
                                    let dtS = d.download_file_report_name;
                                    let kS = this.state.search;
                                    if (dtS.toLowerCase().includes(kS.toLowerCase())) {
                                        itmDtl.push(d);
                                    }
                                })

                                let itm = {
                                    time: key,
                                    items: itmDtl
                                }

                                if (itmDtl.length > 0) {
                                    arr.push(itm)
                                }
qorri_di's avatar
qorri_di committed
136
                            }                         
Riri Novita's avatar
Riri Novita committed
137
                        }
Riri Novita's avatar
Riri Novita committed
138
                        this.setState({ listDownload: arr, oldListDownload: arr })
Riri Novita's avatar
Riri Novita committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
                    } else {
                        this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning' }, () => {
                            if (response.data.message.includes("Someone Logged In") || response.data.message.includes("Token Expired")) {
                                setTimeout(() => {
                                    localStorage.removeItem(Constant.TOKEN)
                                    window.location.reload();
                                }, 1000);
                            }
                        })
                    }
                } else {
                    this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'error' })
                }
            } else {
                this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error' })
            }
        })
    }

Riri Novita's avatar
Riri Novita committed
158 159 160 161 162 163
    groupItems(array, property) {
        return array.reduce(function (groups, item) {
            var reference = moment(item.created_at).format("DD-MM-YYYY");
            var pastDate = moment(reference, "DD-MM-YYYY");
            var todayDate = moment(moment().format("DD-MM-YYYY"), "DD-MM-YYYY");
            let dDiff = todayDate.diff(pastDate, "days");
Riri Novita's avatar
Riri Novita committed
164
            var name = item.status === 0 ? "" : (dDiff === 0 ? "Today" : dDiff === 1 ? "Yesterday" : "Days Ago");
Riri Novita's avatar
Riri Novita committed
165 166 167 168 169 170 171
            var group = groups[name] || (groups[name] = []);
            group.push(item);
            return groups;
        }, {});
    }

    async handleDownload(idFile, name) {
Riri Novita's avatar
Riri Novita committed
172 173 174 175 176 177 178 179 180 181
        let url = `${process.env.REACT_APP_URL_MAIN_BE}/public/transaction/download/zip-files?downloadedFileReportId=${idFile}`
        let res = await fetch(
            `${process.env.REACT_APP_URL_MAIN_BE}/public/transaction/download/zip-files?downloadedFileReportId=${idFile}`
        )
        res = await res.blob()
        this.setState({ loading: false })
        if (res.size > 0) {
            let url = window.URL.createObjectURL(res);
            let a = document.createElement('a');
            a.href = url;
Riri Novita's avatar
Riri Novita committed
182
            a.download = `${name}.zip`;
Riri Novita's avatar
Riri Novita committed
183 184 185 186
            a.click();
        }
    }

Riri Novita's avatar
Riri Novita committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
    handleChange(e, type) {
        this.setState({ search: e.target.value }, () => {
            if (this.state.search.length == 0) {
                this.setState({ listDownload: this.state.oldListDownload })
            }
        })
    }

    // handleSearch(key) {
    //     this.setState({ isSearch: true }, () => {
    //         let newListDownload = []
    //         console.log(this.state.oldListDownload);
    //         this.state.oldListDownload.forEach((i) => {
    //             let itmDtl = []
    //             i.items.forEach((d) => {
    //                 let dtS = d.download_file_report_name;
    //                 let kS = this.state.search;
    //                 if (dtS.toLowerCase().includes(kS.toLowerCase())) {
    //                     itmDtl.push(d);
    //                 }
    //             })

    //             let itm = {
    //                 time: i.time,
    //                 items: itmDtl
    //             }
qorri_di's avatar
qorri_di committed
213
                
Riri Novita's avatar
Riri Novita committed
214 215 216 217 218 219 220 221 222
    //             if (itmDtl.length > 0) {
    //                 newListDownload.push(itm)
    //             }
    //         })
    //         console.log(newListDownload);
    //         this.setState({ listDownload: newListDownload, isSearch: false })
    //     })
    // }

Riri Novita's avatar
Riri Novita committed
223
    render() {
Riri Novita's avatar
Riri Novita committed
224
        let { listDownload, messageAlert, pageEmpty } = this.state
Riri Novita's avatar
Riri Novita committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
        const loadingComponent = (
            <div style={{ position: 'absolute', zIndex: 110, top: 0, left: 0, width: '100%', height: '100%', display: 'flex', justifyContent: 'center', alignItems: 'center', background: 'rgba(255,255,255,0.8)' }}>
                <PropagateLoader
                    // css={override}
                    size={20}
                    color={"#274B80"}
                    loading={this.state.loading}
                />
            </div>
        );

        return (
            <div style={{ flex: 1, backgroundColor: '#f8f8f8', minHeight: this.props.height, boxShadow: 'none' }}>
                <Snackbar open={this.state.alert} autoHideDuration={6000} onClose={() => this.closeAlert()}>
                    <Alert onClose={() => this.closeAlert()} severity={this.state.tipeAlert}>
                        {messageAlert}
                    </Alert>
                </Snackbar>
                <div>
                    {this.state.loading && loadingComponent}
                    <div className={"main-color"} style={{ height: 78, display: 'flex', alignItems: 'center', paddingLeft: 20 }}>
                        <Typography style={{ fontSize: '16px', color: 'white' }}>Download Report</Typography>
                    </div>

Riri Novita's avatar
Riri Novita committed
249
                    {listDownload.length > 0 || this.state.search.length != 0 ?
Riri Novita's avatar
Riri Novita committed
250 251 252
                        <div style={{ padding: 20, width: '100%' }}>
                            <Paper style={{ paddingTop: 10, minHeight: this.props.height, paddingBottom: 50, background: '#d9d9d91c' }}>
                                <div style={{ margin: '40px 100px', background: '#fff', borderRadius: 10, minHeight: this.props.height, padding: 45 }}>
Riri Novita's avatar
Riri Novita committed
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
                                    <div style={{ display: 'flex', justifyContent: 'flex-end', marginBottom: 10 }}>
                                        <TextField
                                            variant="outlined"
                                            id="search"
                                            name="search"
                                            value={this.state.search}
                                            onChange={(e) => this.handleChange(e, '')}
                                            size="small"
                                            InputProps={{
                                                style: { borderRadius: 7 },
                                                endAdornment: <InputAdornment position="end"><SearchIcon /></InputAdornment>,
                                            }}
                                        />
                                    </div>
                                    {!this.state.isSearch && (
qorri_di's avatar
qorri_di committed
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
                                    listDownload.map((listItem) => {
                                        return (
                                            <div>
                                                <div style={{ fontSize: 18, fontWeight: 700, color: '#273B80', marginLeft: 5, marginBottom: 10 }}>{listItem.time}</div>
                                                {listItem.items.length > 0 &&
                                                    listItem.items.map((item) => {
                                                        return (
                                                            <div>
                                                                {item.status == 0 ?
                                                                    null
                                                                    :
                                                                    <div style={{ display: 'flex' }}>
                                                                        <div style={{ background: '#D9D9D9', borderRadius: 10, marginBottom: 15, marginRight: 2, borderBottomRightRadius: 0, borderTopRightRadius: 0 }}>
                                                                            <div style={{ alignItems: 'center', display: 'flex' }}>
                                                                                <img src={Images.zip} style={{ width: 80, margin: 9 }} />
                                                                            </div>
                                                                        </div>
                                                                        <div style={{ display: 'flex', justifyContent: 'space-between', width: '100%', background: '#D9D9D9', borderRadius: 10, marginBottom: 15, borderBottomLeftRadius: 0, borderTopLeftRadius: 0 }}>
                                                                            <div style={{ marginRight: 10 }}>
                                                                                <Typography style={{ fontSize: 18, color: '#273B80', fontWeight: 700, marginTop: 10, marginLeft: 10 }}>{item.download_file_report_name}</Typography>
                                                                                <Typography style={{ marginLeft: 10, marginTop: 5, fontSize: 11, marginBottom: 10 }}>Last Download by: {item.created_by}  -  {item.created_at}</Typography>
Riri Novita's avatar
Riri Novita committed
289
                                                                            </div>
qorri_di's avatar
qorri_di committed
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
                                                                            <div style={{ display: 'flex', justifyContent: 'center', marginRight: 20 }}>
                                                                                {item.status === 1 ?
                                                                                    <button
                                                                                        type="button"
                                                                                        disabled={true}
                                                                                        onClick={() => null}
                                                                                        style={{
                                                                                            backgroundColor: 'transparent',
                                                                                            borderColor: 'transparent',
                                                                                            outline: 'none',
                                                                                        }}
                                                                                    >
                                                                                        <div style={{ backgroundColor: '#939497de', width: 140, height: 40, borderRadius: 10, justifyContent: 'center', display: 'flex', alignItems: 'center' }}>
                                                                                            <Typography style={{ fontSize: '16px', color: '#fff', textAlign: 'center' }}>In Progress</Typography>
                                                                                        </div>
                                                                                    </button>
                                                                                    :
                                                                                    item.status === 2 ?
Riri Novita's avatar
Riri Novita committed
308 309
                                                                                        <button
                                                                                            type="button"
qorri_di's avatar
qorri_di committed
310 311
                                                                                            // disabled={this.state.buttonError}
                                                                                            onClick={() => this.handleDownload(item.download_file_report_id, item.download_file_report_name)}
Riri Novita's avatar
Riri Novita committed
312 313 314 315 316 317
                                                                                            style={{
                                                                                                backgroundColor: 'transparent',
                                                                                                borderColor: 'transparent',
                                                                                                outline: 'none',
                                                                                            }}
                                                                                        >
qorri_di's avatar
qorri_di committed
318 319
                                                                                            <div style={{ backgroundColor: '#354960', width: 140, height: 40, borderRadius: 10, justifyContent: 'center', display: 'flex', alignItems: 'center' }}>
                                                                                                <Typography style={{ fontSize: '16px', color: '#fff', textAlign: 'center' }}>Download</Typography>
Riri Novita's avatar
Riri Novita committed
320 321 322
                                                                                            </div>
                                                                                        </button>
                                                                                        :
qorri_di's avatar
qorri_di committed
323
                                                                                        item.status === 3 ?
Riri Novita's avatar
Riri Novita committed
324 325 326 327 328 329 330 331 332 333 334
                                                                                            <button
                                                                                                type="button"
                                                                                                // disabled={this.state.buttonError}
                                                                                                onClick={() => this.handleDownload(item.download_file_report_id, item.download_file_report_name)}
                                                                                                style={{
                                                                                                    backgroundColor: 'transparent',
                                                                                                    borderColor: 'transparent',
                                                                                                    outline: 'none',
                                                                                                }}
                                                                                            >
                                                                                                <div style={{ backgroundColor: '#354960', width: 140, height: 40, borderRadius: 10, justifyContent: 'center', display: 'flex', alignItems: 'center' }}>
qorri_di's avatar
qorri_di committed
335
                                                                                                    <Typography style={{ fontSize: '16px', color: '#fff', textAlign: 'center' }}>Download <span><CheckCircleIcon fontSize='small' style={{ marginBottom: 1, marginLeft: 7 }} /></span></Typography>
Riri Novita's avatar
Riri Novita committed
336 337 338
                                                                                                </div>
                                                                                            </button>
                                                                                            :
qorri_di's avatar
qorri_di committed
339 340
                                                                                            null
                                                                                }
Riri Novita's avatar
Riri Novita committed
341

Riri Novita's avatar
Riri Novita committed
342 343
                                                                            </div>
                                                                        </div>
qorri_di's avatar
qorri_di committed
344 345 346
                                                                    </div>
                                                                }
                                                            </div>
Riri Novita's avatar
Riri Novita committed
347

qorri_di's avatar
qorri_di committed
348 349 350
                                                        )
                                                    })}
                                            </div>
Riri Novita's avatar
Riri Novita committed
351

qorri_di's avatar
qorri_di committed
352 353
                                        )
                                    })
Riri Novita's avatar
Riri Novita committed
354 355
                                    )}
                                    <div style={{ fontSize: this.state.listDownload.length > 0 ? 12 : 18, fontWeight: 700, color: this.state.listDownload.length > 0 ? 'red' : 'black', marginLeft: 5, marginBottom: 10 }}>{this.state.listDownload.length > 0 ? 'Note : File download akan hilang dalam 3 hari setelah di generate.' : 'Data not found.'}</div>
Riri Novita's avatar
Riri Novita committed
356 357 358 359 360 361 362 363
                                </div>
                            </Paper>
                        </div>
                        :
                        <div style={{ flex: 1, height: '100vh', alignItems: 'center', justifyContent: 'center', display: 'flex' }}>
                            <div style={{ flexDirection: 'column', textAlign: 'center' }}>
                                <CloudDownloadIcon style={{ fontSize: 50, color: 'grey' }} />
                                <h3 style={{ marginTop: 5, color: 'grey' }}>
Riri Novita's avatar
Riri Novita committed
364
                                    Download Report Empty
Riri Novita's avatar
Riri Novita committed
365 366 367 368
                                </h3>
                            </div>
                        </div>
                    }
Riri Novita's avatar
Riri Novita committed
369 370 371 372 373 374 375 376 377 378 379 380

                    {/* {this.state.pageEmpty &&
                        <div style={{ flex: 1, height: '100vh', alignItems: 'center', justifyContent: 'center', display: 'flex' }}>
                            <div style={{ flexDirection: 'column', textAlign: 'center' }}>
                                <CloudDownloadIcon style={{ fontSize: 50, color: 'grey' }} />
                                <h3 style={{ marginTop: 5, color: 'grey' }}>
                                    Download Report Empty
                                </h3>
                            </div>
                        </div>
                    } */}

Riri Novita's avatar
Riri Novita committed
381 382 383 384 385 386 387 388
                </div>
            </div>
        );

    }
}

export default DownloadReport;