TableDocumentManagement.js 15.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
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';
syadziy's avatar
syadziy committed
11 12
import CreateManagementDoc from './CreateDocumentManagement';
import EditManagementDoc from './EditDocumentManagement';
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 => {
rifkaki's avatar
rifkaki 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
                                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,
rifkaki's avatar
rifkaki committed
72
                                    item.values
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
73
                                ])
74 75 76 77 78 79 80 81 82 83 84 85 86
                            } 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,
rifkaki's avatar
rifkaki committed
87
                                        item.values
88 89
                                    ])
                                } 
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
90
                            }
91
                        })
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
92
                        // console.log(dataTable)
93
                        this.setState({ dataTable, loading: false })
94 95 96 97 98
                        let docPath = response.data.data.map((item) => {
                            return [
                                item.document_filepath
                            ]
                        })
99
                        this.setState({ docPath, loading: false })
100 101 102 103 104
                        let docId = response.data.data.map((item) => {
                            return [
                                item.document_id
                            ]
                        })
105
                        this.setState({ docId, loading: false })
106 107
                    } else {
                        this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning', loading: false }, () => {
d.arizona's avatar
d.arizona committed
108
                            if (response.data.message.includes("Someone Logged In") || response.data.message.includes("Token Expired")) {
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
                                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) =>{
rifkaki's avatar
rifkaki committed
126
        console.log(index)
127 128
        if (type === 'download') {
            let res = await fetch(
129
                `${process.env.REACT_APP_URL_MAIN_BE}/public/document/download_document?documentName=`+this.state.docPath[val]+"&&fileType="+index[6]
130 131
            )
            res = await res.blob()
rifkaki's avatar
rifkaki committed
132
            console.log(res)
133 134 135 136
            if (res.size > 0) {
                let url = window.URL.createObjectURL(res);
                let a = document.createElement('a');
                a.href = url;
rifkaki's avatar
rifkaki committed
137
                a.download = (String(index[3]).includes(":") ? String(index[3]).replace(":", " (") + ` )` : String(index[3])) +" - "+index[4]+" - "+index[5]+" - "+index[10]+"."+index[6];
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
                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) => {
158
        this.setState({ popupDel: false, loading: true })
159
        api.create().deleteDocument(payload).then(response => {
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
160
            // console.log(response.data)
161 162 163 164
            if (response.data) {
                if (response.ok) {
                    if (response.data.status == 'success') {
                        this.getData()
165
                        this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'success', loading: false })
166
                    } else {
167
                        this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning', loading: false }, () => {
d.arizona's avatar
d.arizona committed
168
                            if (response.data.message.includes("Someone Logged In") || response.data.message.includes("Token Expired")) {
169 170 171 172 173 174 175 176
                                setTimeout(() => {
                                    localStorage.removeItem(Constant.TOKEN)
                                    window.location.reload();
                                }, 1000);
                            }
                        })
                    }
                } else {
177
                    this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'error', loading: false })
178 179
                }
            } else {
180
                this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error', loading: false })
181 182 183 184 185
            }
        })
    }

    updateDocument(payload) {
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
186
        this.setState({ visibleEdit: false, loading: true })
187
        api.create().updateDocument(payload).then(response => {
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
188
            // console.log(response)
189 190 191 192
            if (response.data) {
                if (response.ok) {
                    if (response.data.status === "success") {
                        this.getData()
193
                        this.setState({ konfirmasi: false, alert: true, messageAlert: response.data.message, tipeAlert: 'success', visibleEdit: false, loading: false })
194
                    } else {
195
                        this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning', loading: false }, () => {
d.arizona's avatar
d.arizona committed
196
                            if (response.data.message.includes("Someone Logged In") || response.data.message.includes("Token Expired")) {
197 198 199 200 201 202 203 204
                                setTimeout(() => {
                                    localStorage.removeItem(Constant.TOKEN)
                                    window.location.reload();
                                }, 1000);
                            }
                        })
                    }
                } else {
205
                    this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'error', loading: false })
206 207
                }
            } else {
208
                this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error', loading: false })
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 237 238 239
            }
        })
    }

    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
240
                            {(this.props.allsubcoEdit || this.props.btnedit) && <span>
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 273 274 275
                            <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 >
                    );
                }
            }
rifkaki's avatar
rifkaki committed
276 277 278 279 280 281 282 283
        }, 
        {
            name: "File Name",
            options: {
                display: false
            }
        },
        "Description",
284 285 286 287 288 289
        {
            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
290
        {
291
            name: "Period Month",
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
292 293 294 295
            options: {
                display: String(this.props.data.value).includes('Manual Book TIA') ? false : true
            }
        },
296
        {
297
            name: "Period Year",
298 299 300 301
            options: {
                display: String(this.props.data.value).includes('Manual Book TIA') ? false : true
            }
        },
rifkaki's avatar
rifkaki committed
302 303 304 305 306 307 308
        "Type", "File Size", "Created By", "Created Date",
        {
            name: "Category",
            options: {
                display: false
            }
        },
309
        ]
310

Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
311 312 313 314 315 316 317 318 319 320
        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>
        ); 
321

322 323 324 325 326 327 328 329 330
        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
331
                    {this.state.loading && loadingComponent}
332 333
                    <MuiThemeProvider theme={getMuiTheme()}>
                        <MUIDataTable
334
                            // theme={getMuiTheme()}
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
                            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>
        )
    }
}