AuditTahunan.js 12.7 KB
Newer Older
Deni Rinaldi's avatar
Deni Rinaldi committed
1 2
import React, { Component } from 'react'
import MUIDataTable from 'mui-datatables'
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
3 4 5 6
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';
7 8
import Images from '../../assets/Images';
import ReactTooltip from 'react-tooltip';
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
9 10
import PopUpDelete from "./PopUpDelete";
import api from '../../api';
Deni Rinaldi's avatar
Deni Rinaldi committed
11
import CreateManagementDoc from './CreateManagementDoc';
12
import EditManagementDoc from './EditManagementDoc';
Deni Rinaldi's avatar
Deni Rinaldi committed
13 14 15 16 17 18


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

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

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

    componentDidMount() {
Deni Rinaldi's avatar
Deni Rinaldi committed
36
        // console.log(this.props.data)
Deni Rinaldi's avatar
Deni Rinaldi committed
37 38 39
        this.getData()
    }

Deni Rinaldi's avatar
Deni Rinaldi committed
40 41 42 43 44 45 46 47
    componentWillReceiveProps(props) {
        // console.log(props);
        const { refresh, id } = this.props;
        if (props.refresh !== refresh) {
            this.getData()
        }
      }

Deni Rinaldi's avatar
Deni Rinaldi committed
48 49
    getData() {
        let payload = {
Deni Rinaldi's avatar
Deni Rinaldi committed
50
            "setting_id": this.props.data.setting_id
Deni Rinaldi's avatar
Deni Rinaldi committed
51 52
        }

Deni Rinaldi's avatar
Deni Rinaldi committed
53 54
        api.create().getAllDocument(payload).then(response => {
            console.log(response)
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
            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 }, () => {
                            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 })
            }
Deni Rinaldi's avatar
Deni Rinaldi committed
100 101 102
        })
    }

Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    openPopUp = async (index, val, type) =>{
        if (type === 'download') {
            console.log(this.state.docPath[val])
            let res = await fetch(
                "https://tia.eksad.com/tia-reporting-dev/public/document/download_document?documentName="+this.state.docPath[val]+"&&fileType="+index[5]
            )
            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
119 120 121 122 123 124 125 126
        if (type === 'delete') {
            this.setState({
                id: this.state.docId[val],
                rowData: index,
                popupDel: true
            })
        }
        if (type === 'edit') {
127 128 129 130 131
            this.setState({
                id: this.state.docId[val],
                rowData: index,
                visibleEdit: true
            })
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
        }
    }

    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' }, () => {
                            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' })
                }
            } else {
                this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error' })
            }
        })
    }
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188

    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' }, () => {
                            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' })
                }
            } else {
                this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error' })
            }
        })
    }
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
189 190 191

    closeAlert() {
        this.setState({ alert: false })
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
192 193
    }

Deni Rinaldi's avatar
Deni Rinaldi committed
194
    render() {
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
        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
214
                                    onClick={() => this.openPopUp(tableMeta.rowData, val, 'download')}
215 216 217 218 219 220
                                    >
                                        <img src={Images.download} />
                                    </button>
                                </a>
                                <ReactTooltip border={true} id="download" place="bottom" type="light" effect="solid" />
                            {/* </span>} */}
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
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 251 252 253
                            {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>}
254 255 256 257
                        </div >
                    );
                }
            }
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
258
        }, "File Name", "Description", "Company Name", "Period", "Type", "File Size", "Created By", "Created Date"
Deni Rinaldi's avatar
Deni Rinaldi committed
259 260 261 262
        ]
        return (
            <div style={{ width: '100%' }}>
                <div style={{ padding: 25 }}>
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
263 264 265 266 267
                    <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
268 269 270 271 272 273 274 275 276
                    <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
277 278 279 280 281 282 283 284 285 286
                {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)}
                    />
                )}
287 288 289 290 291 292 293 294 295
                {this.state.visibleEdit && (
                    <EditManagementDoc
                        type={"edit"}
                        onClickClose={() => this.setState({ visibleEdit: false })}
                        data={this.state.rowData}
                        idoc={this.state.id}
                        updateDocument={this.updateDocument.bind(this)}
                    />
                )}
Deni Rinaldi's avatar
Deni Rinaldi committed
296 297 298 299
            </div>
        )
    }
}