QReview.js 12.4 KB
Newer Older
1 2
import React, { Component } from 'react'
import MUIDataTable from 'mui-datatables'
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 api from '../../api';
import PopUpDelete from "./PopUpDelete";
11
import CreateManagementDoc from './CreateManagementDoc';
12
import EditManagementDoc from './EditManagementDoc';
13 14 15 16 17 18


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

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

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

    componentDidMount() {
36
        // console.log(this.props.data)
37 38 39
        this.getData()
    }

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

48 49
    getData() {
        let payload = {
50
            "setting_id": this.props.data.setting_id
51 52
        }

53
        api.create().getAllDocument(payload).then((response) => {
54
            console.log(response)
55 56 57
            if (response.data) {
                if (response.ok) {
                    if (response.data.status == 'success') {
58
                        let dataTable = response.data.data.map((item, index) => {
59
                            return [
60
                                index,
61 62 63 64 65
                                item.document_name,
                                item.description,
                                item.company_name,
                                item.document_periode,
                                item.document_type,
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
66
                                String(Number(item.document_size) / 1000 + ' KB'),
67
                                item.created_by,
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
68
                                item.created_at
69 70 71
                            ]
                        })
                        this.setState({ dataTable })
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
72 73 74 75 76 77
                        let docPath = response.data.data.map((item) => {
                            return [
                                item.document_filepath
                            ]
                        })
                        this.setState({ docPath })
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
78 79 80 81 82 83
                        let docId = response.data.data.map((item) => {
                            return [
                                item.document_id
                            ]
                        })
                        this.setState({ docId })
84 85 86 87 88 89 90 91 92 93 94 95 96
                    } 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 })
                }
97
            } else {
98
                this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error', loading: false })
99
            }
100 101 102
        })
    }

Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
103
    openPopUp = async (index, val, type) =>{
104
        if (type === 'download') {
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
105 106 107 108 109 110 111 112 113 114 115 116
            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();
            }
117
        }
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
118 119 120 121 122 123 124 125
        if (type === 'delete') {
            this.setState({
                id: this.state.docId[val],
                rowData: index,
                popupDel: true
            })
        }
        if (type === 'edit') {
126 127 128 129 130
            this.setState({
                id: this.state.docId[val],
                rowData: index,
                visibleEdit: true
            })
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
131
        }
132 133
    }

134 135 136 137
    closeAlert() {
        this.setState({ alert: false })
    }

Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    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' })
            }
        })
    }

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
    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' })
            }
        })
    }

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