TableCafrmDocument.js 8.38 KB
Newer Older
muhammad ridwan's avatar
muhammad ridwan committed
1 2 3 4 5 6 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
import React, {Component} from 'react';
import api from "../../api";
import Constant from "../../library/Constant";
import Images from "../../assets/Images";
import ReactTooltip from "react-tooltip";
import PropagateLoader from "react-spinners/PropagateLoader";
import {createMuiTheme, MuiThemeProvider, Snackbar} from "@material-ui/core";
import MUIDataTable from "mui-datatables";
import {withStyles} from "@material-ui/core/styles";
import MuiAlert from "@material-ui/lab/Alert";

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

const Alert = withStyles({
})((props) => <MuiAlert elevation={6} variant="filled" {...props} />);


export default class TableDocument extends Component {

    constructor(props) {
        super(props)
        this.state = {
            dataTable: [],
            visibleCreate: false,
            refresh: '',
            alert: false,

        }
    }

    componentDidMount() {
        this.getData()
    }

    componentWillReceiveProps(props) {
syadziy's avatar
syadziy committed
38 39
        console.log(props);
        const {refresh} = this.props;
muhammad ridwan's avatar
muhammad ridwan committed
40 41 42 43 44 45 46 47 48 49
        if (props.refresh !== refresh) {
            this.getData()
        }
    }

    getData() {
        let payload = {
            "submenu_id": this.props.submenu_id
        }
        this.setState({ loading: true })
50 51
        api.create().getCarfmDocumentBySubmenu(payload).then(response => {
            console.log("table document carfm");
muhammad ridwan's avatar
muhammad ridwan committed
52
            console.log(response)
53
            console.log("table document carfm stop")
muhammad ridwan's avatar
muhammad ridwan committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
            if (response.data) {
                if (response.ok) {
                    if (response.data.status == 'success') {
                        let dataTable = []
                        response.data.data.map((item, index) => {
                            let indexId = this.props.userCompActive.findIndex((val) => val == item.company_id)
                                if (indexId !== -1) {
                                    dataTable.push( [
                                        index,
                                        item.document_name,
                                        item.description,
                                        item.company_name,
                                        item.document_month,
                                        item.document_periode,
                                        item.document_type,
                                        String(Number(item.document_size) / 1000 + ' KB'),
                                        item.created_by,
                                        item.created_at,
                                        item.values
                                    ])
                                }
                        })
syadziy's avatar
syadziy committed
76

muhammad ridwan's avatar
muhammad ridwan committed
77 78 79 80 81
                        let docPath = response.data.data.map((item) => {
                            return [
                                item.document_filepath
                            ]
                        })
syadziy's avatar
syadziy committed
82

muhammad ridwan's avatar
muhammad ridwan committed
83 84 85 86 87
                        let docId = response.data.data.map((item) => {
                            return [
                                item.document_id
                            ]
                        })
syadziy's avatar
syadziy committed
88 89 90

                        // console.log(dataTable)
                        this.setState({ dataTable, docPath, docId, loading: false })
muhammad ridwan's avatar
muhammad ridwan committed
91 92 93 94 95 96 97 98 99 100 101
                    } else {
                        this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning', loading: false }, () => {
                            if (response.data.message.includes("Someone Logged In") || response.data.message.includes("Token Expired")) {
                                setTimeout(() => {
                                    localStorage.removeItem(Constant.TOKEN)
                                    window.location.reload();
                                }, 1000);
                            }
                        })
                    }
                } else {
102
                    console.log("error di table document getcarfm 1");
muhammad ridwan's avatar
muhammad ridwan committed
103 104 105
                    this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'error', loading: false })
                }
            } else {
106
                console.log("error di table document getcarfm 2");
muhammad ridwan's avatar
muhammad ridwan committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
                this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error', loading: false })
            }
        })
    }

    openPopUp = async (index, val, type) =>{
        console.log(index)
        if (type === 'download') {
            let res = await fetch(
                `${process.env.REACT_APP_URL_MAIN_BE}/public/document/download_cafrm_document?documentName=`+this.state.docPath[val]+"&&fileType="+index[6]
            )
            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 = (String(index[3]).includes(":") ? String(index[3]).replace(":", " (") + ` )` : String(index[3])) +" - "+index[4]+" - "+index[5]+" - "+index[10]+"."+index[6];
                a.click();
            }
        }
    }

    closeAlert() {
        this.setState({ alert: false })
    }

    render() {
        let columns = [{
            name: "Action",
            options: {
                filter: false,
                sort: false,
                customBodyRender: (val, tableMeta) => {
                    return (
                        <div style={{ display: 'flex' }}>

144
                            {this.props.btndownload && <a data-tip={'Download'} data-for="download">
muhammad ridwan's avatar
muhammad ridwan committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
                                <button
                                    style={{
                                        backgroundColor: 'transparent',
                                        cursor: 'pointer',
                                        borderColor: 'transparent',
                                        marginRight: 15
                                    }}
                                    onClick={() => this.openPopUp(tableMeta.rowData, val, 'download')}
                                >
                                    <img src={Images.download} />
                                </button>
                            </a>}
                            <ReactTooltip border={true} id="download" place="bottom" type="light" effect="solid" />
                        </div >
                    );
                }
            }
        },
            {
                name: "File Name",
                options: {
                    display: false
                }
            },
169
            "Description", "Company Name", "Period Month", "Period Year",
muhammad ridwan's avatar
muhammad ridwan committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 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 213 214 215
            "Type", "File Size", "Created By", "Created Date",
            {
                name: "Category",
                options: {
                    display: false
                }
            },
        ]

        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={{ width: '100%' }}>
                {this.props.load && (
                    <div style={{ padding: 25 }}>
                        <Snackbar open={this.state.alert} autoHideDuration={6000} onClose={() => this.closeAlert()}>
                            <Alert onClose={() => this.closeAlert()} severity={this.state.tipeAlert}>
                                {this.state.messageAlert}
                            </Alert>
                        </Snackbar>
                        {this.state.loading && loadingComponent}
                        <MuiThemeProvider theme={getMuiTheme()}>
                            <MUIDataTable
                                // theme={getMuiTheme()}
                                data={this.state.dataTable}
                                columns={columns}
                                options={options}
                            />
                        </MuiThemeProvider>
                    </div>
                )}

            </div>
        )
    }
}