ManualBookTia.js 12.7 KB
Newer Older
Deni Rinaldi's avatar
Deni Rinaldi committed
1
import React, { Component } from 'react'
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
2 3 4 5
import ReactTooltip from 'react-tooltip';
import { createMuiTheme, MuiThemeProvider, Snackbar } from '@material-ui/core';
import {  withStyles } from '@material-ui/core/styles';
import MuiAlert from '@material-ui/lab/Alert';
Deni Rinaldi's avatar
Deni Rinaldi committed
6
import MUIDataTable from 'mui-datatables'
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
7
import Constant from '../../library/Constant';
8
import Images from '../../assets/Images';
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
9 10 11
import EditManagementDoc from "./EditManagementDoc";
import PopUpDelete from "./PopUpDelete";
import api from '../../api';
Deni Rinaldi's avatar
Deni Rinaldi committed
12 13 14 15 16

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

Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
17 18 19
const Alert = withStyles({
})((props) => <MuiAlert elevation={6} variant="filled" {...props} />);

Deni Rinaldi's avatar
Deni Rinaldi committed
20 21 22 23
export default class ManualBookTia extends Component {
    constructor(props) {
        super(props)
        this.state = {
24 25
            dataTable: [],
            visibleCreate: false,
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
26
            visibleEdit: false,
27 28
            refresh: '',
            alert: false,
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
29
            popupDel: false,
Deni Rinaldi's avatar
Deni Rinaldi committed
30 31 32
        }
    }

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
    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)
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
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
            if (response.data) {
                if (response.ok) {
                    if (response.data.status == 'success') {
                        let dataTable = response.data.data.map((item, index) => {
                            return [
                                index,
                                item.document_name,
                                item.description,
                                item.company_name,
                                item.document_periode,
                                item.document_type,
                                String(Number(item.document_size) / 1000 + ' KB'),
                                item.created_by,
                                item.created_at,
                            ]
                        })
                        this.setState({ dataTable })
                        let docPath = response.data.data.map((item) => {
                            return [
                                item.document_filepath
                            ]
                        })
                        this.setState({ docPath })
                        let docId = response.data.data.map((item) => {
                            return [
                                item.document_id
                            ]
                        })
                        this.setState({ docId })
                    } else {
                        this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning', loading: false }, () => {
d.arizona's avatar
d.arizona committed
84
                            if (response.data.message.includes("Someone Logged In") || response.data.message.includes("Token Expired")) {
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
85 86 87 88 89 90 91 92 93 94 95 96 97
                                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 })
            }
98 99 100
        })
    }

Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
101 102 103 104
    closeAlert() {
        this.setState({ alert: false })
    }

Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
105 106 107
    openPopUp = async (index, val, type) =>{
        if (type === 'download') {
            let res = await fetch(
faisalhamdi's avatar
faisalhamdi committed
108
                `${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
109 110 111 112 113 114 115 116 117 118 119
            )
            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();
            }
        }
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
        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) => {
        this.setState({ popupDel: false })
        api.create().deleteDocument(payload).then(response => {
            console.log(response.data)
            if (response.data) {
                if (response.ok) {
                    if (response.data.status == 'success') {
                        this.getData()
                        this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'success' })
                    } else {
                        this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning' }, () => {
d.arizona's avatar
d.arizona committed
147
                            if (response.data.message.includes("Someone Logged In") || response.data.message.includes("Token Expired")) {
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161
                                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' })
            }
        })
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
162 163
    }

164 165 166 167 168 169 170 171 172 173
    updateDocument(payload) {
        api.create().updateDocument(payload).then(response => {
            console.log(response)
            if (response.data) {
                if (response.ok) {
                    if (response.data.status === "success") {
                        this.getData()
                        this.setState({ konfirmasi: false, alert: true, messageAlert: response.data.message, tipeAlert: 'success', visibleEdit: false })
                    } else {
                        this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning' }, () => {
d.arizona's avatar
d.arizona committed
174
                            if (response.data.message.includes("Someone Logged In") || response.data.message.includes("Token Expired")) {
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
                                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' })
            }
        })
    }

Deni Rinaldi's avatar
Deni Rinaldi committed
191
    render() {
192 193 194 195 196 197
        let columns = [{
            name: "Action",
            options: {
                filter: false,
                sort: false,
                customBodyRender: (val, tableMeta) => {
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
198
                    // console.log(tableMeta)
199 200 201 202 203 204 205 206 207 208 209 210
                    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
211
                                    onClick={() => this.openPopUp(tableMeta.rowData, val, 'download')}
212
                                    >
Deni Rinaldi's avatar
Deni Rinaldi committed
213
                                        <img src={Images.download} style={{ width: 33}} />
214 215 216 217
                                    </button>
                                </a>
                                <ReactTooltip border={true} id="download" place="bottom" type="light" effect="solid" />
                            {/* </span>} */}
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
                            {this.props.btnedit && <span>
                                <a data-tip={'Edit'} data-for="edit">
                                <button
                                    style={{
                                        backgroundColor: 'transparent',
                                        cursor: 'pointer',
                                        borderColor: 'transparent',
                                        marginRight: 15
                                    }}
                                    // onClick={() => console.log(tableMeta)}
                                    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>}
251 252 253 254
                        </div >
                    );
                }
            }
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
255
        }, "File Name", "Description", "Company Name", "Type", "File Size", "Created By", "Created Date"
Deni Rinaldi's avatar
Deni Rinaldi committed
256 257 258 259
        ]
        return (
            <div style={{ width: '100%' }}>
                <div style={{ padding: 25 }}>
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
260 261 262 263 264
                    <Snackbar open={this.state.alert} autoHideDuration={6000} onClose={() => this.closeAlert()}>
                        <Alert onClose={() => this.closeAlert()} severity={this.state.tipeAlert}>
                            {this.state.messageAlert}
                        </Alert>
                    </Snackbar>
Deni Rinaldi's avatar
Deni Rinaldi committed
265 266 267 268 269 270 271 272 273
                    <MuiThemeProvider theme={getMuiTheme()}>
                        <MUIDataTable
                            theme={getMuiTheme()}
                            data={this.state.dataTable}
                            columns={columns}
                            options={options}
                        />
                    </MuiThemeProvider>
                </div>
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
                {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}
290
                        updateDocument={this.updateDocument.bind(this)}
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
291 292
                    />
                )}
Deni Rinaldi's avatar
Deni Rinaldi committed
293 294 295 296
            </div>
        )
    }
}