AllDocument.js 15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
import React, { Component } from 'react'
import MUIDataTable from 'mui-datatables'
import {  withStyles } from '@material-ui/core/styles';
import { createMuiTheme, MuiThemeProvider, Snackbar } from '@material-ui/core';
import MuiAlert from '@material-ui/lab/Alert';
import Constant from '../../library/Constant';
import Images from '../../assets/Images';
import ReactTooltip from 'react-tooltip';
import PopUpDelete from "./PopUpDelete";
import api from '../../api';
import CreateManagementDoc from './CreateManagementDoc';
import EditManagementDoc from './EditManagementDoc';
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
13
import PropagateLoader from "react-spinners/PropagateLoader"
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 43 44 45 46 47 48 49 50

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 getAllDocument extends Component {
    constructor(props) {
        super(props)
        this.state = {
            dataTable: [],
            visibleCreate: false,
            visibleEdit: false,
            refresh: '',
            alert: false,
            popupDel: false,
        }
    }

    componentDidMount() {
        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
        }
51
        this.setState({ loading: true })
52
        api.create().getAllDocument(payload).then(response => {
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
53
            // console.log(response)
54 55 56
            if (response.data) {
                if (response.ok) {
                    if (response.data.status == 'success') {
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
57 58 59
                        let dataTable = []
                        response.data.data.map((item, index) => {
                            let indexId = this.props.userCompActive.findIndex((val) => val == item.company_id)
60
                            if (String(this.props.name).includes('Manual')){
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
61 62 63 64 65 66 67 68 69 70 71 72
                                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,
                                ])
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
                            } else {
                                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,
                                    ])
                                } 
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
88
                            }
89
                        })
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
90
                        // console.log(dataTable)
91
                        this.setState({ dataTable, loading: false })
92 93 94 95 96
                        let docPath = response.data.data.map((item) => {
                            return [
                                item.document_filepath
                            ]
                        })
97
                        this.setState({ docPath, loading: false })
98 99 100 101 102
                        let docId = response.data.data.map((item) => {
                            return [
                                item.document_id
                            ]
                        })
103
                        this.setState({ docId, loading: false })
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
                    } else {
                        this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning', loading: false }, () => {
                            if (response.data.message.includes("Someone Logged In")) {
                                setTimeout(() => {
                                    localStorage.removeItem(Constant.TOKEN)
                                    window.location.reload();
                                }, 1000);
                            }
                        })
                    }
                } else {
                    this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'error', loading: false })
                }
            } else {
                this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error', loading: false })
            }
        })
    }

    openPopUp = async (index, val, type) =>{
        if (type === 'download') {
            let res = await fetch(
126
                `${process.env.REACT_APP_URL_MAIN_BE}/public/document/download_document?documentName=`+this.state.docPath[val]+"&&fileType="+index[6]
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
            )
            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();
            }
        }
        if (type === 'delete') {
            this.setState({
                id: this.state.docId[val],
                rowData: index,
                popupDel: true
            })
        }
        if (type === 'edit') {
            this.setState({
                id: this.state.docId[val],
                rowData: index,
                visibleEdit: true
            })
        }
    }

    deleteDoc = (payload) => {
155
        this.setState({ popupDel: false, loading: true })
156
        api.create().deleteDocument(payload).then(response => {
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
157
            // console.log(response.data)
158 159 160 161
            if (response.data) {
                if (response.ok) {
                    if (response.data.status == 'success') {
                        this.getData()
162
                        this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'success', loading: false })
163
                    } else {
164
                        this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning', loading: false }, () => {
165 166 167 168 169 170 171 172 173
                            if (response.data.message.includes("Someone Logged In")) {
                                setTimeout(() => {
                                    localStorage.removeItem(Constant.TOKEN)
                                    window.location.reload();
                                }, 1000);
                            }
                        })
                    }
                } else {
174
                    this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'error', loading: false })
175 176
                }
            } else {
177
                this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error', loading: false })
178 179 180 181 182
            }
        })
    }

    updateDocument(payload) {
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
183
        this.setState({ visibleEdit: false, loading: true })
184
        api.create().updateDocument(payload).then(response => {
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
185
            // console.log(response)
186 187 188 189
            if (response.data) {
                if (response.ok) {
                    if (response.data.status === "success") {
                        this.getData()
190
                        this.setState({ konfirmasi: false, alert: true, messageAlert: response.data.message, tipeAlert: 'success', visibleEdit: false, loading: false })
191
                    } else {
192
                        this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning', loading: false }, () => {
193 194 195 196 197 198 199 200 201
                            if (response.data.message.includes("Someone Logged In")) {
                                setTimeout(() => {
                                    localStorage.removeItem(Constant.TOKEN)
                                    window.location.reload();
                                }, 1000);
                            }
                        })
                    }
                } else {
202
                    this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'error', loading: false })
203 204
                }
            } else {
205
                this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error', loading: false })
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
            }
        })
    }

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

    render() {
        let columns = [{
            name: "Action",
            options: {
                filter: false,
                sort: false,
                customBodyRender: (val, tableMeta) => {
                    return (
                        <div style={{ display: 'flex' }}>
                                <a data-tip={'Download'} data-for="download">
                                <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" />
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
237
                            {(this.props.allsubcoEdit || this.props.btnedit) && <span>
238 239 240 241 242 243 244 245 246 247 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
                            <a data-tip={'Edit'} data-for="edit">
                                <button
                                    style={{
                                        backgroundColor: 'transparent',
                                        cursor: 'pointer',
                                        borderColor: 'transparent',
                                        marginRight: 15
                                    }}
                                    onClick={() => this.openPopUp(tableMeta.rowData, val, 'edit')}
                                    >
                                        <img src={Images.editCopy} />
                                    </button>
                                </a>
                                <ReactTooltip border={true} id="edit" place="bottom" type="light" effect="solid" />
                            </span>}
                            {this.props.btndelete && <span>
                                <a data-tip={'Delete'} data-for="delete">
                                <button
                                    style={{
                                        backgroundColor: 'transparent',
                                        cursor: 'pointer',
                                        borderColor: 'transparent',
                                    }}
                                    // onClick={() => console.log(tableMeta)}
                                    onClick={() => this.openPopUp(tableMeta.rowData, val, 'delete')}
                                    >
                                        <img src={Images.delete} />
                                    </button>
                                </a>
                                <ReactTooltip border={true} id="delete" place="bottom" type="light" effect="solid" />
                            </span>}
                        </div >
                    );
                }
            }
273 274 275 276 277 278 279
        }, "File Name", "Description",
        {
            name: "Company Name",
            options: {
                display: String(this.props.data.value).includes('Manual Book TIA') ? false : true
            }
        },
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
280
        {
281
            name: "Period Month",
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
282 283 284 285
            options: {
                display: String(this.props.data.value).includes('Manual Book TIA') ? false : true
            }
        },
286
        {
287
            name: "Period Year",
288 289 290 291 292 293
            options: {
                display: String(this.props.data.value).includes('Manual Book TIA') ? false : true
            }
        },
        "Type", "File Size", "Created By", "Created Date"
        ]
294

Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
295 296 297 298 299 300 301 302 303 304
        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>
        ); 
305

306 307 308 309 310 311 312 313 314
        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>
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
315
                    {this.state.loading && loadingComponent}
316 317
                    <MuiThemeProvider theme={getMuiTheme()}>
                        <MUIDataTable
318
                            // theme={getMuiTheme()}
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
                            data={this.state.dataTable}
                            columns={columns}
                            options={options}
                        />
                    </MuiThemeProvider>
                </div>
                )}
                {this.state.popupDel && (
                    <PopUpDelete
                        type={"delete"}
                        onClickClose={() => this.setState({ popupDel: false })}
                        data={this.state.rowData}
                        idoc={this.state.id}
                        // getList={() => this.getData.bind(this)}
                        deleteDoc={this.deleteDoc.bind(this)}
                    />
                )}
                {this.state.visibleEdit && (
                    <EditManagementDoc
                        type={"edit"}
                        onClickClose={() => this.setState({ visibleEdit: false })}
                        data={this.state.rowData}
                        idoc={this.state.id}
                        updateDocument={this.updateDocument.bind(this)}
                    />
                )}
            </div>
        )
    }
}