ManualBookETMS.js 4.55 KB
Newer Older
1 2 3 4
import React, { Component } from 'react'
import MUIDataTable from 'mui-datatables'
import { createMuiTheme, MuiThemeProvider } from '@material-ui/core';
import api from '../../api';
5 6
import Images from '../../assets/Images';
import ReactTooltip from 'react-tooltip';
7 8 9 10 11 12 13 14 15 16 17 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


var ct = require("../../library/CustomTable");
const getMuiTheme = () => createMuiTheme(ct.customTable());
const options = ct.customOptions();

export default class ManualBookETMS extends Component {
    constructor(props) {
        super(props)
        this.state = {
            dataTable: [],
            visibleCreate: false,
            refresh: ''
        }
    }

    componentDidMount() {
        // console.log(this.props.data)
        this.getData()
    }

    componentWillReceiveProps(props) {
        // console.log(props);
        const { refresh, id } = this.props;
        if (props.refresh !== refresh) {
            this.getData()
        }
      }

    getData() {
        let payload = {
            "setting_id": this.props.data.setting_id
        }

        api.create().getAllDocument(payload).then(response => {
            console.log(response)
43
            let dataTable = response.data.data.map((item, index) => {
44
                return [
45
                    index,
46 47 48 49
                    item.document_name,
                    item.description,
                    item.company_name,
                    item.document_type,
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
50
                    String(Number(item.document_size) / 1000 + ' KB'),
51 52 53 54 55
                    item.created_by,
                    item.created_at,
                ]
            })
            this.setState({ dataTable })
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
56 57 58 59 60 61
            let docPath = response.data.data.map((item) => {
                return [
                    item.document_filepath
                ]
            })
            this.setState({ docPath })
62 63 64
        })
    }

Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
65 66 67 68
    openPopUp = async (index, val, type) =>{
        if (type === 'download') {
            console.log(this.state.docPath[val])
            let res = await fetch(
faisalhamdi's avatar
faisalhamdi committed
69
                `${process.env.REACT_APP_URL_MAIN_BE}/public/document/download_document?documentName=`+this.state.docPath[val]+"&&fileType="+index[5]
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
70 71 72 73 74 75 76 77 78 79 80 81 82
            )
            res = await res.blob()
            // console.log(res)
            if (res.size > 0) {
                let url = window.URL.createObjectURL(res);
                let a = document.createElement('a');
                a.href = url;
                a.download = index[1];
                a.click();
            }
        }
    }

83
    render() {
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
        let columns = [{
            name: "Action",
            options: {
                filter: false,
                sort: false,
                customBodyRender: (val, tableMeta) => {
                    console.log(tableMeta)
                    return (
                        <div style={{ display: 'flex' }}>
                            {/* {this.state.btnedit && <span> */}
                                <a data-tip={'Download'} data-for="download">
                                <button
                                    style={{
                                        backgroundColor: 'transparent',
                                        cursor: 'pointer',
                                        borderColor: 'transparent',
                                        marginRight: 15
                                    }}
                                    // onClick={() => console.log(tableMeta)}
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
103
                                    onClick={() => this.openPopUp(tableMeta.rowData, val, 'download')}
104 105 106 107 108 109 110 111 112 113 114
                                    >
                                        <img src={Images.download} />
                                    </button>
                                </a>
                                <ReactTooltip border={true} id="download" place="bottom" type="light" effect="solid" />
                            {/* </span>} */}
                        </div >
                    );
                }
            }
        }, "Nama File", "Keterangan", "Perusahaan", "Tipe", "Ukuran", "Ditambahkan Oleh", "Ditambahkan Tanggal"
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
        ]
        return (
            <div style={{ width: '100%' }}>
                <div style={{ padding: 25 }}>
                    <MuiThemeProvider theme={getMuiTheme()}>
                        <MUIDataTable
                            theme={getMuiTheme()}
                            data={this.state.dataTable}
                            columns={columns}
                            options={options}
                        />
                    </MuiThemeProvider>
                </div>
            </div>
        )
    }
}