ApprovalMatrix.js 50.4 KB
Newer Older
1 2
import React, { Component } from 'react';
import { Container, Row, Col } from "react-bootstrap";
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
3 4
import { makeStyles, createMuiTheme, MuiThemeProvider } from '@material-ui/core/styles';
import { TextField, InputBase, Snackbar, withStyles, Tooltip } from "@material-ui/core";
5
import { ExcelRenderer } from 'react-excel-renderer';
6 7
import Images from '../../assets/Images';
import MUIDataTable from "mui-datatables";
8
import ReactTooltip from 'react-tooltip';
Deni Rinaldi's avatar
Deni Rinaldi committed
9
import MuiAlert from '@material-ui/lab/Alert';
10
import UploadFile from "../../library/Upload";
11 12
import CreateApprovalMatrix from "./CreateApprovalMatrix";
import EditApprovalMatrix from "./EditApprovalMatrix";
13
import VisualisasiAM from "./VisualisasiAM";
14
import PopUpDeleteAM from "./PopUpDeleteAM";
15
import PopUpFailedSave from "../../library/PopUpFailedSave";
16
import api from "../../api";
a.bairuha's avatar
a.bairuha committed
17
import Constant from '../../library/Constant';
d.arizona's avatar
d.arizona committed
18 19
import { css } from "@emotion/core";
import PropagateLoader from "react-spinners/PropagateLoader"
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
20 21 22 23 24 25 26 27 28 29

const LightTooltip = withStyles((theme) => ({
    tooltip: {
        backgroundColor: theme.palette.common.white,
        color: 'rgba(0, 0, 0, 0.87)',
        boxShadow: theme.shadows[1],
        fontSize: 11,
    },
}))(Tooltip);

30 31 32
var ct = require("../../library/CustomTable");
const getMuiTheme = () => createMuiTheme(ct.customTable());
const options = ct.customOptions();
33
const options2 = ct.customOptions2();
34

Deni Rinaldi's avatar
Deni Rinaldi committed
35 36 37
const Alert = withStyles({
})((props) => <MuiAlert elevation={6} variant="filled" {...props} />);

38 39 40 41
export default class ApprovalMatrix extends Component {
    constructor(props) {
        super(props)
        this.state = {
42
            visibleAM: true,
43
            visibleCreate: false,
44 45
            visibleEdit: false,
            visibleVisual: false,
46 47 48
            dataTable: [],
            listData: [],
            data: [],
49
            search: "",
50 51
            cols: null,
            rows: null,
52
            dataLoaded: false,
53
            popupError: false,
54 55 56
            alert: false,
            tipeAlert: '',
            messageAlert: '',
57 58
            btncreate: false,
            btnedit: false,
d.arizona's avatar
d.arizona committed
59
            btndelete: false,
60
            load: false,
61
            judul: '',
62
            popupDel: false,
d.arizona's avatar
d.arizona committed
63
            loading: false
64
        }
65 66 67 68 69 70
        this.fileHandler = this.fileHandler.bind(this);
    }

    fileHandler = (event) => {
        let fileObj = event
        ExcelRenderer(fileObj, (err, resp) => {
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
71
            // console.log(resp)
72
            if (err) {
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
73
                // console.log(err);
74 75
            }
            else {
76 77 78 79 80
                let isi = resp.rows.slice(3)
                let payload = []
                isi.map((item, index) => {
                    if (item.length > 0) {
                        payload.push({
81
                            id: index + 1,
82
                            approval_type_name: item[0] === undefined ? "" : item[0],
83
                            orders: item[1] === undefined ? null: item[1],
84
                            email: item[2] === undefined ? "" : item[2],
85 86 87 88
                            // fullname: item[2] === undefined ? "" : item[2],
                            operator_type_name: item[3] === undefined ? "" : item[3],
                            start_date: item[4] === undefined ? "" : item[4],
                            end_date: item[5] === undefined ? "" : item[5],
89 90 91 92 93 94
                        })
                    }
                })
                let body = {
                    approval_matrix: payload
                }
95
                this.setState({ payload: body, buttonError: false, judul: resp.rows[1][0] })
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
96
                // console.log(this.state.payload)
97 98 99 100 101 102
            }
        });
    }

    checkUpload(){
        api.create().checkUploadAM(this.state.payload).then(response => {
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
103
            // console.log(response)
104 105 106 107 108 109 110 111
            let dataRow = []
            if (response.data) {
                if (response.ok) {
                    if (response.data.status === "success") {
                        dataRow = response.data.data.map((item, index) => {
                            return [
                                item.approval_type_name,
                                item.orders,
112
                                item.email,
113 114 115 116 117 118 119 120 121 122 123 124 125
                                item.fullname,
                                item.operator_type_name,
                                item.start_date,
                                item.end_date,
                                item.error
                            ]
                        })
                        let columns = [
                            {
                                name: "Approval Type",
                                options: {
                                    customBodyRender: (val, tableMeta) => {
                                        let check = null
126 127
                                        if (tableMeta.rowData[7] != null) {
                                            check = tableMeta.rowData[7].findIndex((val) => val.field.includes('approval_type_name'))
128 129
                                            if (check > -1) {
                                                this.setState({ buttonError: true })
a.bairuha's avatar
a.bairuha committed
130
                                            }
131
                                        }
132 133
                                        return (
                                            <div style={{ display: 'flex' }}>
134 135
                                                {tableMeta.rowData[7] != null && check > -1 ?
                                                    <a data-tip={tableMeta.rowData[7][check].message} data-for="approvaltype">
136
                                                        <span style={{ color: check != null && check > -1 ? "red" : 'black' }}>{val === "" ? "Empty" : val}</span>
137
                                                    </a> :
138
                                                    <span style={{ color: check != null && check > -1 ? "red" : 'black' }}>{val === "" ? "Empty" : val}</span>
139
                                                }
140 141 142 143 144 145 146 147 148 149 150
                                                <ReactTooltip border={true} id="approvaltype" place="bottom" type="light" effect="solid" />
                                            </div >
                                        );
                                    }
                                }
                            },
                            {
                                name: "Order",
                                options: {
                                    customBodyRender: (val, tableMeta) => {
                                        let check = null
151 152
                                        if (tableMeta.rowData[7] != null) {
                                            check = tableMeta.rowData[7].findIndex((val) => val.field.includes('orders'))
153 154
                                            if (check > -1) {
                                                this.setState({ buttonError: true })
a.bairuha's avatar
a.bairuha committed
155
                                            }
156
                                        }
157 158
                                        return (
                                            <div style={{ display: 'flex' }}>
159 160
                                                {tableMeta.rowData[7] != null && check > -1 ?
                                                    <a data-tip={tableMeta.rowData[7][check].message} data-for="order">
161
                                                        <span style={{ color: check != null && check > -1 ? "red" : 'black' }}>{val === null ? "Empty" : val}</span>
162
                                                    </a> :
163
                                                    <span style={{ color: check != null && check > -1 ? "red" : 'black' }}>{val === null ? "Empty" : val}</span>
164
                                                }
165 166 167 168 169 170
                                                <ReactTooltip border={true} id="order" place="bottom" type="light" effect="solid" />
                                            </div >
                                        );
                                    }
                                }
                            },
171 172 173 174 175 176 177 178 179 180 181 182 183 184
                            {
                                name: "Approver Email",
                                options: {
                                    customBodyRender: (val, tableMeta) => {
                                        let check = null
                                        if (tableMeta.rowData[7] != null) {
                                            check = tableMeta.rowData[7].findIndex((val) => val.field.includes('email'))
                                            if (check > -1) {
                                                this.setState({ buttonError: true })
                                            }
                                        }
                                        return (
                                            <div style={{ display: 'flex' }}>
                                                {tableMeta.rowData[7] != null && check > -1 ?
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
185
                                                    <LightTooltip title={tableMeta.rowData[7][check].message} arrow>
186
                                                        <span style={{ color: check != null && check > -1 ? "red" : 'black' }}>{val === "" ? "Empty" : val}</span>
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
187
                                                    </LightTooltip> :
188 189 190 191 192 193 194
                                                    <span style={{ color: check != null && check > -1 ? "red" : 'black' }}>{val === "" ? "Empty" : val}</span>
                                                }
                                            </div >
                                        );
                                    }
                                }
                            },
195 196 197 198 199
                            {
                                name: "Approver Name",
                                options: {
                                    customBodyRender: (val, tableMeta) => {
                                        let check = null
200 201
                                        if (tableMeta.rowData[7] != null) {
                                            check = tableMeta.rowData[7].findIndex((val) => val.field.includes('fullname'))
202 203
                                            if (check > -1) {
                                                this.setState({ buttonError: true })
a.bairuha's avatar
a.bairuha committed
204
                                            }
205
                                        }
206 207
                                        return (
                                            <div style={{ display: 'flex' }}>
208
                                                {tableMeta.rowData[7] != null && check > -1 ?
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
209 210 211 212
                                                    <LightTooltip title={tableMeta.rowData[7][check].message} arrow>
                                                        <span style={{ color: check != null && check > -1 ? "red" : 'black' }}>{val}</span>
                                                    </LightTooltip> :
                                                    <span style={{ color: check != null && check > -1 ? "red" : 'black' }}>{val}</span>
213
                                                }
214 215 216 217 218 219 220 221 222 223
                                            </div >
                                        );
                                    }
                                }
                            },
                            {
                                name: "Operator",
                                options: {
                                    customBodyRender: (val, tableMeta) => {
                                        let check = null
224 225
                                        if (tableMeta.rowData[7] != null) {
                                            check = tableMeta.rowData[7].findIndex((val) => val.field.includes('operator_type_name'))
226 227
                                            if (check > -1) {
                                                this.setState({ buttonError: true })
a.bairuha's avatar
a.bairuha committed
228
                                            }
229
                                        }
230 231
                                        return (
                                            <div style={{ display: 'flex' }}>
232
                                                {tableMeta.rowData[7] != null && check > -1 ?
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
233
                                                    <LightTooltip title={tableMeta.rowData[7][check].message} arrow>
234
                                                        <span style={{ color: check != null && check > -1 ? "red" : 'black' }}>{val === "" ? "Empty" : val}</span>
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
235
                                                    </LightTooltip> :
236
                                                    <span style={{ color: check != null && check > -1 ? "red" : 'black' }}>{val === "" ? "Empty" : val}</span>
237
                                                }
238 239 240 241 242 243 244 245 246 247
                                            </div >
                                        );
                                    }
                                }
                            },
                            {
                                name: "Valid From",
                                options: {
                                    customBodyRender: (val, tableMeta) => {
                                        let check = null
248 249
                                        if (tableMeta.rowData[7] != null) {
                                            check = tableMeta.rowData[7].findIndex((val) => val.field.includes('start_date'))
250 251
                                            if (check > -1) {
                                                this.setState({ buttonError: true })
a.bairuha's avatar
a.bairuha committed
252
                                            }
253
                                        }
254 255
                                        return (
                                            <div style={{ display: 'flex' }}>
256
                                                {tableMeta.rowData[7] != null && check > -1 ?
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
257
                                                    <LightTooltip title={tableMeta.rowData[7][check].message} arrow>
258
                                                        <span style={{ color: check != null && check > -1 ? "red" : 'black' }}>{val === "" ? "Empty" : val}</span>
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
259
                                                    </LightTooltip> :
260
                                                    <span style={{ color: check != null && check > -1 ? "red" : 'black' }}>{val === "" ? "Empty" : val}</span>
261
                                                }
262 263 264 265 266 267 268 269 270 271
                                            </div >
                                        );
                                    }
                                }
                            },
                            {
                                name: "Valid To",
                                options: {
                                    customBodyRender: (val, tableMeta) => {
                                        let check = null
272 273
                                        if (tableMeta.rowData[7] != null) {
                                            check = tableMeta.rowData[7].findIndex((val) => val.field.includes('end_date'))
274 275
                                            if (check > -1) {
                                                this.setState({ buttonError: true })
a.bairuha's avatar
a.bairuha committed
276 277
                                            }
                                        }
278 279
                                        return (
                                            <div style={{ display: 'flex' }}>
280
                                                {tableMeta.rowData[7] != null && check > -1 ?
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
281
                                                    <LightTooltip title={tableMeta.rowData[7][check].message} arrow>
282
                                                        <span style={{ color: check != null && check > -1 ? "red" : 'black' }}>{val === "" ? "Empty" : val}</span>
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
283
                                                    </LightTooltip> :
284
                                                    <span style={{ color: check != null && check > -1 ? "red" : 'black' }}>{val === "" ? "Empty" : val}</span>
285 286 287
                                                }
                                            </div >
                                        );
a.bairuha's avatar
a.bairuha committed
288
                                    }
289 290 291 292 293 294 295
                                }
                            },
                            {
                                name: "",
                                options: {
                                    display: false
                                }
a.bairuha's avatar
a.bairuha committed
296
                            }
297 298 299
                        ]

                        // console.log(dataRow);
300
                        this.setState({
301 302 303 304
                            dataLoaded: true,
                            cols: columns,
                            rows: dataRow, visibleUpload: false, 
                            visibleAM: false
305
                        });
306 307
                    } else {
                        this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning' }, () => {
a.bairuha's avatar
a.bairuha committed
308
                            if (response.data.message.includes("Someone Logged In")) {
309 310 311 312 313 314
                                setTimeout(() => {
                                    localStorage.removeItem(Constant.TOKEN)
                                    window.location.reload();
                                }, 1000);
                            }
                        })
315
                    }
316 317 318 319 320 321 322 323
                } else {
                    this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'error' })
                }
            } else {
                this.setState({
                    dataLoaded: false,
                    alert: true, messageAlert: response.problem, tipeAlert: 'error'
                });
324
            }
325
        })
326 327 328
    }

    componentDidMount() {
329
        this.getData()
330 331 332 333 334 335 336 337
        this.getPermission()
    }

    getPermission() {
        let payload = {
            menu: "approval matrix"
        }
        api.create().getPermission(payload).then(response => {
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
338
            // console.log(response)
339 340 341 342 343
            if (response.data) {
                if (response.data.status === "success") {
                    this.setState({
                        btncreate: response.data.data.create,
                        btnedit: response.data.data.edit,
d.arizona's avatar
d.arizona committed
344
                        btndelete: response.data.data.delete,
345 346 347 348 349 350 351
                        load: true
                    })
                } else {
                    this.setState({
                        load: true
                    })
                }
352 353
            } else {
                this.setState({ load: true })
354 355
            }
        })
356 357 358
    }

    getData() {
d.arizona's avatar
d.arizona committed
359
        this.setState({loading: true})
360 361
        api.create().getAM().then((response) => {
            // console.log(response)
Deni Rinaldi's avatar
Deni Rinaldi committed
362
            if (response.data) {
a.bairuha's avatar
a.bairuha committed
363 364 365 366 367 368
                if (response.ok) {
                    if (response.data.status == 'success') {
                        let data = response.data.data
                        let listData = data.sort((a, b) => a.approval_matrix_id - b.approval_matrix_id).map((item, index) => {
                            return [index, item.approval_matrix_id, item.approval_type_name, item.orders, item.fullname, item.operator_type_name, item.status]
                        })
d.arizona's avatar
d.arizona committed
369 370 371 372 373
                        this.setState({ dataTable: listData, listData: response.data.data }, () => {
                            setTimeout(() => {
                              this.setState({loading: false})
                            }, 2000);
                          })
a.bairuha's avatar
a.bairuha committed
374
                    } else {
d.arizona's avatar
d.arizona committed
375
                        this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning', loading: false }, () => {
a.bairuha's avatar
a.bairuha committed
376
                            if (response.data.message.includes("Someone Logged In")) {
a.bairuha's avatar
a.bairuha committed
377 378 379 380 381 382 383
                                setTimeout(() => {
                                    localStorage.removeItem(Constant.TOKEN)
                                    window.location.reload();
                                }, 1000);
                            }
                        })
                    }
Deni Rinaldi's avatar
Deni Rinaldi committed
384
                } else {
d.arizona's avatar
d.arizona committed
385
                    this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'error', loading: false })
Deni Rinaldi's avatar
Deni Rinaldi committed
386
                }
387
            } else {
d.arizona's avatar
d.arizona committed
388
                this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error', loading: false })
389 390 391 392 393 394 395
            }
        })
    }

    openPopUp(index, type) {
        if (type === 'edit') {
            this.setState({
396
                rowData: index,
397 398
                visibleEdit: true
            })
399 400 401 402 403 404 405
        } else if (type === 'delete') {
            this.setState({
                rowData: index,
                popupDel: true
            })
        }
        else {
406 407 408 409
            this.setState({
                visibleCreate: true
            })
        }
410 411
    }

412 413 414 415 416 417 418
    handleInputChange(e) {
        this.setState({ search: e })
        let body = {
            "keyword": e
        }
        api.create().searchAM(body).then(response => {
            // console.log(response.data);
Deni Rinaldi's avatar
Deni Rinaldi committed
419
            if (response.data) {
a.bairuha's avatar
a.bairuha committed
420 421 422 423 424 425 426 427 428
                if (response.ok) {
                    if (response.data.status == 'success') {
                        let data = response.data.data
                        let listData = data.map((item, index) => {
                            return [index, item.approval_matrix_id, item.approval_type_name, item.orders, item.fullname, item.operator_type_name, item.status]
                        })
                        this.setState({ dataTable: listData, listData: response.data.data })
                    } else {
                        this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning' }, () => {
a.bairuha's avatar
a.bairuha committed
429
                            if (response.data.message.includes("Someone Logged In")) {
a.bairuha's avatar
a.bairuha committed
430 431 432 433 434 435 436
                                setTimeout(() => {
                                    localStorage.removeItem(Constant.TOKEN)
                                    window.location.reload();
                                }, 1000);
                            }
                        })
                    }
Deni Rinaldi's avatar
Deni Rinaldi committed
437
                } else {
a.bairuha's avatar
a.bairuha committed
438
                    this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'error' })
Deni Rinaldi's avatar
Deni Rinaldi committed
439
                }
440
            } else {
Deni Rinaldi's avatar
Deni Rinaldi committed
441
                this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error' })
442
            }
443 444 445
        })
    }

446 447 448
    createAM = (payload) => {
        this.setState({ visibleCreate: false })
        api.create().createAM(payload).then(response => {
Deni Rinaldi's avatar
Deni Rinaldi committed
449
            if (response.data) {
a.bairuha's avatar
a.bairuha committed
450 451 452 453 454
                if (response.ok) {
                    if (response.data.status == 'success') {
                        this.getData()
                        this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'success' })
                    } else {
r.kurnia's avatar
r.kurnia committed
455
                        this.setState({ loading: false, alert: true, messageAlert: response.data.message, tipeAlert: 'warning' }, () => {
a.bairuha's avatar
a.bairuha committed
456
                            if (response.data.message.includes("Someone Logged In")) {
a.bairuha's avatar
a.bairuha committed
457 458 459 460 461 462 463
                                setTimeout(() => {
                                    localStorage.removeItem(Constant.TOKEN)
                                    window.location.reload();
                                }, 1000);
                            }
                        })
                    }
Deni Rinaldi's avatar
Deni Rinaldi committed
464
                } else {
r.kurnia's avatar
r.kurnia committed
465
                    this.setState({ loading: false, alert: true, messageAlert: response.data.message, tipeAlert: 'error' })
Deni Rinaldi's avatar
Deni Rinaldi committed
466
                }
467
            } else {
r.kurnia's avatar
r.kurnia committed
468
                this.setState({ loading: false, alert: true, messageAlert: response.problem, tipeAlert: 'error' })
469 470 471 472
            }
        })
    }

473 474 475
    updateAM = (payload) => {
        this.setState({ visibleEdit: false })
        api.create().updateAM(payload).then(response => {
Deni Rinaldi's avatar
Deni Rinaldi committed
476
            if (response.data) {
a.bairuha's avatar
a.bairuha committed
477 478 479 480 481
                if (response.ok) {
                    if (response.data.status == 'success') {
                        this.getData()
                        this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'success' })
                    } else {
r.kurnia's avatar
r.kurnia committed
482
                        this.setState({ loading: false, alert: true, messageAlert: response.data.message, tipeAlert: 'warning' }, () => {
a.bairuha's avatar
a.bairuha committed
483
                            if (response.data.message.includes("Someone Logged In")) {
a.bairuha's avatar
a.bairuha committed
484 485 486 487 488 489 490
                                setTimeout(() => {
                                    localStorage.removeItem(Constant.TOKEN)
                                    window.location.reload();
                                }, 1000);
                            }
                        })
                    }
Deni Rinaldi's avatar
Deni Rinaldi committed
491
                } else {
r.kurnia's avatar
r.kurnia committed
492
                    this.setState({ loading: false, alert: true, messageAlert: response.data.message, tipeAlert: 'error' })
Deni Rinaldi's avatar
Deni Rinaldi committed
493
                }
494
            } else {
r.kurnia's avatar
r.kurnia committed
495
                this.setState({ loading: false, alert: true, messageAlert: response.problem, tipeAlert: 'error' })
496 497 498 499
            }
        })
    }

500 501 502
    updateVAM = (payload) => {
        this.setState({ visibleVisual: false, visibleAM: true })
        api.create().updateVAM(payload).then(response => {
Deni Rinaldi's avatar
Deni Rinaldi committed
503
            if (response.data) {
a.bairuha's avatar
a.bairuha committed
504 505 506 507 508
                if (response.ok) {
                    if (response.data.status == 'success') {
                        this.getData()
                        this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'success' })
                    } else {
r.kurnia's avatar
r.kurnia committed
509
                        this.setState({ loading: false, alert: true, messageAlert: response.data.message, tipeAlert: 'warning' }, () => {
a.bairuha's avatar
a.bairuha committed
510
                            if (response.data.message.includes("Someone Logged In")) {
a.bairuha's avatar
a.bairuha committed
511 512 513 514 515 516 517
                                setTimeout(() => {
                                    localStorage.removeItem(Constant.TOKEN)
                                    window.location.reload();
                                }, 1000);
                            }
                        })
                    }
Deni Rinaldi's avatar
Deni Rinaldi committed
518
                } else {
r.kurnia's avatar
r.kurnia committed
519
                    this.setState({ loading: false, alert: true, messageAlert: response.data.message, tipeAlert: 'error' })
Deni Rinaldi's avatar
Deni Rinaldi committed
520
                }
521
            } else {
r.kurnia's avatar
r.kurnia committed
522
                this.setState({ loading: false, alert: true, messageAlert: response.problem, tipeAlert: 'error' })
523 524 525 526
            }
        })
    }

527 528
    downloadFile = async () => {
        let res = await fetch(
faisalhamdi's avatar
faisalhamdi committed
529
            `${process.env.REACT_APP_URL_MAIN_BE}/public/attachment/download_file?fileName=ApprovalMatrixTemplate&&fileType=xlsx`
530 531
        )
        res = await res.blob()
532
        // console.log(res)
533 534 535 536 537 538 539 540 541
        if (res.size > 0) {
            let url = window.URL.createObjectURL(res);
            let a = document.createElement('a');
            a.href = url;
            a.download = 'Template Approval Matrix.xlsx';
            a.click();
        }
    }

Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
542
    downloadDataTable = async () => {
543
        let res = await fetch(
faisalhamdi's avatar
faisalhamdi committed
544
            `${process.env.REACT_APP_URL_MAIN_BE}/public/approval_matrix/export_approval_matrix`
545 546
        )
        res = await res.blob()
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
547
        // console.log(res)
548 549 550 551 552 553 554 555 556 557 558
        if (res.size > 0) {
            let url = window.URL.createObjectURL(res);
            let a = document.createElement('a');
            a.href = url;
            a.download = 'Approval Matrix.xlsx';
            a.click();
        }
    }

    uploadAM() {
        api.create().uploadAM(this.state.payload).then(response => {
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
559
            // console.log(response)
Deni Rinaldi's avatar
Deni Rinaldi committed
560
            if (response.data) {
a.bairuha's avatar
a.bairuha committed
561 562 563 564 565
                if (response.ok) {
                    if (response.data.status === "success") {
                        this.getData()
                        this.setState({ visibleAM: true, alert: true, messageAlert: response.data.message, tipeAlert: 'success' })
                    } else {
r.kurnia's avatar
r.kurnia committed
566
                        this.setState({ loading: false, alert: true, messageAlert: response.data.message, tipeAlert: 'warning' }, () => {
a.bairuha's avatar
a.bairuha committed
567
                            if (response.data.message.includes("Someone Logged In")) {
a.bairuha's avatar
a.bairuha committed
568 569 570 571 572 573 574
                                setTimeout(() => {
                                    localStorage.removeItem(Constant.TOKEN)
                                    window.location.reload();
                                }, 1000);
                            }
                        })
                    }
Deni Rinaldi's avatar
Deni Rinaldi committed
575
                } else {
r.kurnia's avatar
r.kurnia committed
576
                    this.setState({ loading: false, alert: true, messageAlert: response.data.message, tipeAlert: 'error' })
Deni Rinaldi's avatar
Deni Rinaldi committed
577 578
                }
            } else {
r.kurnia's avatar
r.kurnia committed
579
                this.setState({ loading: false, alert: true, messageAlert: response.problem, tipeAlert: 'error' })
Deni Rinaldi's avatar
Deni Rinaldi committed
580
            }
581
        })
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
582 583
    }

Deni Rinaldi's avatar
Deni Rinaldi committed
584 585 586 587
    closeAlert() {
        this.setState({ alert: false })
    }

588 589 590
    deleteAM = (payload) => {
        this.setState({ popupDel: false })
        api.create().deleteAM(payload).then(response => {
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
591
            // console.log(response.data)
592 593 594 595 596 597
            if (response.data) {
                if (response.ok) {
                    if (response.data.status == 'success') {
                        this.getData()
                        this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'success' })
                    } else {
r.kurnia's avatar
r.kurnia committed
598
                        this.setState({ loading: false, alert: true, messageAlert: response.data.message, tipeAlert: 'warning' }, () => {
a.bairuha's avatar
a.bairuha committed
599
                            if (response.data.message.includes("Someone Logged In")) {
600 601 602 603 604 605 606 607
                                setTimeout(() => {
                                    localStorage.removeItem(Constant.TOKEN)
                                    window.location.reload();
                                }, 1000);
                            }
                        })
                    }
                } else {
r.kurnia's avatar
r.kurnia committed
608
                    this.setState({ loading: false, alert: true, messageAlert: response.data.message, tipeAlert: 'error' })
609 610
                }
            } else {
r.kurnia's avatar
r.kurnia committed
611
                this.setState({ loading: false, alert: true, messageAlert: response.problem, tipeAlert: 'error' })
612 613 614 615
            }
        })
    }

616 617 618 619
    render() {
        const columns = [{
            name: "Action",
            options: {
620
                filter: false,
621
                sort: false,
622
                customBodyRender: (val, tableMeta) => {
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
623
                    // console.log(tableMeta)
624
                    return (
d.arizona's avatar
d.arizona committed
625 626
                        <div style={{ display: 'flex' }}>
                            {this.state.btnedit && <span>
627
                                <a data-tip={'Edit'} data-for="edit">
d.arizona's avatar
d.arizona committed
628 629 630 631 632 633 634 635 636
                                <button
                                    style={{
                                        backgroundColor: 'transparent',
                                        cursor: 'pointer',
                                        borderColor: 'transparent',
                                        marginRight: 15
                                    }}
                                    // onClick={() => console.log(tableMeta)}
                                    onClick={() => this.openPopUp(tableMeta.rowData, 'edit')}
637 638 639 640 641
                                    >
                                        <img src={Images.editCopy} />
                                    </button>
                                </a>
                                <ReactTooltip border={true} id="edit" place="bottom" type="light" effect="solid" />
d.arizona's avatar
d.arizona committed
642 643
                            </span>}
                            {this.state.btndelete && <span>
644
                                <a data-tip={'Delete'} data-for="delete">
d.arizona's avatar
d.arizona committed
645 646 647 648 649 650 651 652
                                <button
                                    style={{
                                        backgroundColor: 'transparent',
                                        cursor: 'pointer',
                                        borderColor: 'transparent',
                                    }}
                                    // onClick={() => console.log(tableMeta)}
                                    onClick={() => this.openPopUp(tableMeta.rowData, 'delete')}
653 654 655 656 657
                                    >
                                        <img src={Images.delete} />
                                    </button>
                                </a>
                                <ReactTooltip border={true} id="delete" place="bottom" type="light" effect="solid" />
d.arizona's avatar
d.arizona committed
658 659
                            </span>}
                        </div >
660 661 662
                    );
                }
            }
Deni Rinaldi's avatar
Deni Rinaldi committed
663
        },
664 665 666 667 668 669
        {
            name: "ID",
            options: {
                customBodyRender: (val, tableMeta) => {
                    return (
                        <div style={{ display: 'flex' }}>
670
                            <span style={{ color: tableMeta.rowData[6] === "Active" ? "#656565" : 'rgba(0, 0, 0, 0.25)' }}>{val}</span>
671 672 673 674
                        </div >
                    );
                }
            }
Deni Rinaldi's avatar
Deni Rinaldi committed
675
        },
676
        {
677
            name: "Approval Type",
678 679 680 681
            options: {
                customBodyRender: (val, tableMeta) => {
                    return (
                        <div style={{ display: 'flex' }}>
682
                            <span style={{ color: tableMeta.rowData[6] === "Active" ? "#656565" : 'rgba(0, 0, 0, 0.25)' }}>{val}</span>
683 684 685
                        </div >
                    );
                }
Deni Rinaldi's avatar
Deni Rinaldi committed
686
            }
687 688 689 690 691 692 693
        },
        {
            name: "Order",
            options: {
                customBodyRender: (val, tableMeta) => {
                    return (
                        <div style={{ display: 'flex' }}>
694
                            <span style={{ color: tableMeta.rowData[6] === "Active" ? "#656565" : 'rgba(0, 0, 0, 0.25)' }}>{val}</span>
695 696 697
                        </div >
                    );
                }
Deni Rinaldi's avatar
Deni Rinaldi committed
698
            }
699 700
        },
        {
701
            name: "Approver Name",
702 703 704 705
            options: {
                customBodyRender: (val, tableMeta) => {
                    return (
                        <div style={{ display: 'flex' }}>
706
                            <span style={{ color: tableMeta.rowData[6] === "Active" ? "#656565" : 'rgba(0, 0, 0, 0.25)' }}>{val}</span>
707 708 709 710 711 712 713 714 715 716 717
                        </div >
                    );
                }
            }
        },
        {
            name: "Operator",
            options: {
                customBodyRender: (val, tableMeta) => {
                    return (
                        <div style={{ display: 'flex' }}>
718
                            <span style={{ color: tableMeta.rowData[6] === "Active" ? "#656565" : 'rgba(0, 0, 0, 0.25)' }}>{val}</span>
719 720 721 722 723 724 725 726 727 728 729
                        </div >
                    );
                }
            }
        },
        {
            name: "Status",
            options: {
                customBodyRender: (val, tableMeta) => {
                    return (
                        <div style={{ display: 'flex' }}>
730
                            <span style={{ color: tableMeta.rowData[6] === "Active" ? "#656565" : 'rgba(0, 0, 0, 0.25)' }}>{val}</span>
731 732 733 734 735
                        </div >
                    );
                }
            }
        }]
d.arizona's avatar
d.arizona committed
736 737 738 739 740 741 742 743 744 745
        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>
          );
746
        return (
747
            <div>
748
                {this.state.visibleAM === true ?
749
                    this.state.load && (
750
                    <div style={{ height: this.props.height }}>
751
                        <div className={"main-color"} style={{ height: 195, width: '100%' }} />
Deni Rinaldi's avatar
Deni Rinaldi committed
752 753 754 755 756 757
                        <Snackbar open={this.state.alert} autoHideDuration={6000} onClose={() => this.closeAlert()}>
                            <Alert onClose={() => this.closeAlert()} severity={this.state.tipeAlert}>
                                {this.state.messageAlert}
                            </Alert>
                        </Snackbar>
                        <div>
758 759 760
                            <div style={{ display: 'flex', flexDirection: 'row', justifyContent: 'space-between', paddingRight: 25, paddingLeft: 25, marginTop: -182 }}>
                                <label style={{ width: '50%', color: 'white', fontSize: 16, alignSelf: 'center' }}>Master Data - Approval Matrix</label>
                                {/* <div style={{ color: 'white', width: '50%', height: 37, display: 'flex', backgroundColor: 'white', borderWidth: 2, alignItems: 'center', borderRadius: 6, paddingLeft: 5, paddingRight: 5, alignSelf: 'center' }}>
761 762 763 764 765
                                    <img src={Images.searchBlack} style={{ marginRight: 10 }} />
                                    <InputBase
                                        style={{ width: '100%' }}
                                        placeholder="Search"
                                        value={this.state.search}
766
                                        onChange={(e) => this.handleInputChange(e.target.value)}
767 768
                                        inputProps={{ 'aria-label': 'naked' }}
                                    />
769 770
                                </div> */}
                                <div style={{ width: '50%', justifyContent: 'flex-end', display: 'flex', flexFlow: 'wrap' }}>
771 772 773 774 775 776 777 778 779 780 781 782 783 784
                                    <a data-tip={'Download Template'} data-for="template">
                                        <button
                                            style={{
                                                backgroundColor: 'transparent',
                                                cursor: 'pointer',
                                                borderColor: 'transparent',
                                                margin: 5
                                            }}
                                            onClick={() => this.downloadFile()}
                                        >
                                            <img src={Images.template} />
                                        </button>
                                    </a>
                                    <ReactTooltip border={true} id="template" place="bottom" type="light" effect="solid" />
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
                                    {this.state.btncreate && (
                                        <a data-tip={'Upload'} data-for="upload">
                                            <button
                                                style={{
                                                    backgroundColor: 'transparent',
                                                    cursor: 'pointer',
                                                    borderColor: 'transparent',
                                                    margin: 5
                                                }}
                                                onClick={() => this.setState({ visibleUpload: true })}
                                            >
                                                <img src={Images.upload} />
                                            </button>
                                        </a>
                                    )}
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
                                    <ReactTooltip border={true} id="upload" place="bottom" type="light" effect="solid" />
                                    <a data-tip={'Download'} data-for="download">
                                        <button
                                            style={{
                                                backgroundColor: 'transparent',
                                                cursor: 'pointer',
                                                borderColor: 'transparent',
                                                margin: 5
                                            }}
                                            onClick={() => this.downloadDataTable()}
                                        >
                                            <img src={Images.download} />
                                        </button>
                                    </a>
                                    <ReactTooltip border={true} id="download" place="bottom" type="light" effect="solid" />
815 816 817 818 819 820 821 822 823 824 825 826 827
                                        <a data-tip={'Visualization'} data-for="visualisasi">
                                            <button
                                                style={{
                                                    backgroundColor: 'transparent',
                                                    cursor: 'pointer',
                                                    borderColor: 'transparent',
                                                    margin: 5
                                                }}
                                                onClick={() => this.setState({ visibleVisual: true, visibleAM: false })}
                                            >
                                                <img src={Images.visualisasi} />
                                            </button>
                                        </a>
828
                                    <ReactTooltip border={true} id="visualisasi" place="bottom" type="light" effect="solid" />
829
                                    {this.state.btncreate && (
Deni Rinaldi's avatar
Deni Rinaldi committed
830
                                        <a data-tip={'Add New'} data-for="create">
831 832 833 834 835
                                            <button
                                                style={{
                                                    backgroundColor: 'transparent',
                                                    cursor: 'pointer',
                                                    borderColor: 'transparent',
Deni Rinaldi's avatar
Deni Rinaldi committed
836 837
                                                    margin: 5,
                                                    marginRight: 20
838 839 840 841 842 843 844
                                                }}
                                                onClick={() => this.setState({ visibleCreate: true })}
                                            >
                                                <img src={Images.add} />
                                            </button>
                                        </a>
                                    )}
845
                                    <ReactTooltip border={true} id="create" place="bottom" type="light" effect="solid" />
846
                                </div >
847
                            </div>
848
                            <div style={{ padding: 25, width: '100%' }}>
d.arizona's avatar
d.arizona committed
849
                                {this.state.loading && loadingComponent}
850 851 852 853 854 855 856 857 858 859 860
                                <MuiThemeProvider theme={getMuiTheme()}>
                                    <MUIDataTable
                                        theme={getMuiTheme()}
                                        data={this.state.dataTable}
                                        columns={columns}
                                        options={options}
                                    />
                                </MuiThemeProvider>
                            </div>
                        </div>
                    </div>
861
                    )
862
                    :
863
                    this.state.visibleVisual == true ?
Deni Rinaldi's avatar
Deni Rinaldi committed
864
                        <VisualisasiAM
865 866
                            btncreate={this.state.btncreate}
                            btnedit={this.state.btnedit}
Deni Rinaldi's avatar
Deni Rinaldi committed
867 868 869 870
                            onClickClose={() => this.setState({ visibleVisual: false, visibleAM: true })}
                            height={this.props.height}
                            updateVAM={this.updateVAM.bind(this)}
                        />
871
                    :
Deni Rinaldi's avatar
Deni Rinaldi committed
872
                        <div style={{ height: this.props.height }}>
873
                            <div class="main-color" style={{ height: 195, width: '100%' }} />
874
                            <div>
Deni Rinaldi's avatar
Deni Rinaldi committed
875
                                <div>
876
                                    <div style={{ display: 'flex', flexDirection: 'row', justifyContent: 'space-between', paddingRight: 25, paddingLeft: 25, marginTop: -165 }}>
Deni Rinaldi's avatar
Deni Rinaldi committed
877 878 879 880 881 882 883 884 885
                                        <label style={{ color: 'white', fontSize: 16, alignSelf: 'center' }}>Preview Data</label>
                                    </div>
                                    <div style={{ padding: 25 }}>
                                        {this.state.dataLoaded && (
                                            <MuiThemeProvider theme={getMuiTheme()}>
                                                <MUIDataTable
                                                    theme={getMuiTheme()}
                                                    data={this.state.rows}
                                                    columns={this.state.cols}
886
                                                    options={options}
Deni Rinaldi's avatar
Deni Rinaldi committed
887 888 889 890 891 892 893 894 895 896 897
                                                />
                                            </MuiThemeProvider>
                                        )}
                                    </div>
                                    <div style={{ display: 'flex', width: '100%', placeContent: 'flex-end', padding: 20 }}>
                                        <button
                                            type="button"
                                            onClick={() => this.setState({ visibleAM: true })}
                                            style={{ marginRight: 20 }}
                                        >
                                            <div style={{ width: 102, height: 30, border: 'solid 1px #354960', borderRadius: 5, alignItems: 'center', display: 'flex', justifyContent: 'center' }}>
898
                                                <span style={{ color: '#354960', fontSize: 11 }}>Cancel</span>
Deni Rinaldi's avatar
Deni Rinaldi committed
899 900 901 902
                                            </div>
                                        </button>
                                        <button
                                            type="button"
903 904 905 906
                                            // disabled={this.state.buttonError == true ? { buttonError: true, cursor: 'none' } : false}
                                            disabled={this.state.buttonError == true ? true : false}
                                            // onClick={() => this.state.buttonError ? this.setState({ popupError: true }) : this.uploadAM()}
                                            onClick={() => this.uploadAM()}
Deni Rinaldi's avatar
Deni Rinaldi committed
907 908 909
                                            style={{}}
                                        >
                                            <div style={{ width: 102, height: 30, backgroundColor: '#354960', borderRadius: 5, alignItems: 'center', display: 'flex', justifyContent: 'center' }}>
910
                                                <span style={{ color: '#fff', fontSize: 11 }}>Save</span>
Deni Rinaldi's avatar
Deni Rinaldi committed
911 912 913
                                            </div>
                                        </button>
                                    </div>
914 915 916 917
                                </div>
                            </div>
                        </div>
                }
918

919 920 921 922
                {this.state.visibleCreate && (
                    <CreateApprovalMatrix
                        onClickClose={() => this.setState({ visibleCreate: false })}
                        data={this.state.listData}
923
                        createAM={this.createAM.bind(this)}
924 925 926 927 928 929
                    />
                )}
                {this.state.visibleEdit && (
                    <EditApprovalMatrix
                        type={"edit"}
                        onClickClose={() => this.setState({ visibleEdit: false })}
930
                        data={this.state.rowData}
931
                        updateAM={this.updateAM.bind(this)}
932 933
                    />
                )}
934
                {this.state.popupDel && (
935
                    <PopUpDeleteAM
936 937 938 939 940 941 942
                        type={"delete"}
                        onClickClose={() => this.setState({ popupDel: false })}
                        data={this.state.rowData}
                        // getList={() => this.getData.bind(this)}
                        deleteAM={this.deleteAM.bind(this)}
                    />
                )}
943
                {this.state.popupError && (
Deni Rinaldi's avatar
Deni Rinaldi committed
944
                    <PopUpFailedSave onClickClose={() => this.setState({ popupError: false })} />
945
                )}
946 947 948
                {this.state.visibleUpload && (
                    <div className="test app-popup-show">
                        <div className="popup-content background-white border-radius" style={{ borderRadius: 8 }}>
949
                            <div className="popup-panel grid grid-2x main-color" style={{ height: 64, borderTopRightRadius: 8, borderTopLeftRadius: 8 }}>
950 951 952 953 954 955 956 957 958 959
                                <div className="col-1" style={{ maxWidth: "inherit", display: 'flex', alignItems: 'center' }}>
                                    <div className="popup-title">
                                        <span style={{ color: '#fff', fontSize: 16, fontWeight: 'bold' }}>Upload File</span>
                                    </div>
                                </div>
                                <div className="col-2 content-right" style={{ maxWidth: "inherit", alignSelf: 'center' }}>
                                    <button
                                        type="button"
                                        className="btn btn-circle btn-white"
                                        onClick={() => this.setState({ visibleUpload: false })}
960
                                    >
Deni Rinaldi's avatar
Deni Rinaldi committed
961
                                        <img src={Images.close} />
962
                                    </button>
963 964 965 966 967 968
                                </div>
                            </div>
                            <UploadFile
                                type={this.state.uploadStatus}
                                percentage={this.state.percentage}
                                result={this.state.result}
969
                                acceptedFiles={["xlsx"]}
970 971 972 973
                                onHandle={(dt) => {
                                    this.fileHandler(dt)
                                    this.setState({ uploadStatus: 'idle', percentage: '0' })
                                }}
974 975
                                // onUpload={() => this.setState({ visibleUpload: false, visibleAM: false })}
                                onUpload={() => {
r.kurnia's avatar
r.kurnia committed
976
                                    String(this.state.judul).includes("APPROVAL") && String(this.state.judul).includes("MATRIX") ?
977 978 979
                                        this.checkUpload() :
                                        this.setState({ alert: true, messageAlert: "Invalid Template", tipeAlert: 'warning' })
                                }}
980
                            />
981
                        </div>
982
                    </div>
983
                )}
984 985 986 987
            </div>
        );
    }
}