VisualisasiAM.js 12.3 KB
Newer Older
1 2 3 4 5
import React, { Component } from 'react';
import { Container, Row, Col } from "react-bootstrap";
import { makeStyles, createMuiTheme, MuiThemeProvider } from '@material-ui/core/styles';
import { TextField } from '@material-ui/core';
import MenuItem from '@material-ui/core/MenuItem';
6 7 8
import ReactDragListView from 'react-drag-listview';
import Autocomplete from '@material-ui/lab/Autocomplete';
import api from "../../api";
9
import { none } from 'ramda';
a.bairuha's avatar
a.bairuha committed
10
import Constant from '../../library/Constant';
11 12 13 14 15 16 17 18 19 20 21 22 23


const type = [
  {
    value: 'KPI',
    label: 'KPI',
  },
  {
    value: 'Gatau',
    label: 'Gatau',
  },
];

24
export default class VisualisasiAM extends Component {
25 26
    constructor(props) {
        super(props)
27 28 29 30 31 32
        const data = [];
        for (let i = 1, len = 7; i < len; i++) {
            data.push({
                title: `rows${i}`
            });
        }
33 34
        this.state = {
            visibleAM: false,
35 36
            visibleVisual: true,
            data,
d.arizona's avatar
d.arizona committed
37 38 39
            listApproval: [],
            listEdit: [],
            listOriginal: []
40 41 42 43
        }
    }

    componentDidMount() {
44
        this.getTypeData()
45 46
    }

47 48
    getTypeData() {
        api.create().getTypeAM().then((response) => {
a.bairuha's avatar
a.bairuha committed
49
            if (response.data) {
a.bairuha's avatar
a.bairuha committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
                if (response.ok) {
                    if (response.data.status == 'success') {
                        let data = response.data.data
                        // console.log(data)
                        let typeData = data.map((item) => {
                            return {
                                approval_type_id: item.approval_type_id,
                                approval_type_name: item.approval_type_name
                            }
                        })
                        let typeProps = {
                            options: typeData,
                            getOptionLabel: (option) => option.approval_type_name,
                        };
                        this.setState({ types: typeProps, typeData: response.data.data })
                    } else {
                        this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning' }, () => {
d.arizona's avatar
d.arizona committed
67
                            if (response.data.message.includes("Someone Logged In") || response.data.message.includes("Token Expired")) {
a.bairuha's avatar
a.bairuha committed
68 69 70 71 72 73 74
                                setTimeout(() => {
                                    localStorage.removeItem(Constant.TOKEN)
                                    window.location.reload();
                                }, 1000);
                            }
                        })
                    }
a.bairuha's avatar
a.bairuha committed
75
                } else {
a.bairuha's avatar
a.bairuha committed
76
                    this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'error' })
a.bairuha's avatar
a.bairuha committed
77
                }
78
            } else {
a.bairuha's avatar
a.bairuha committed
79
                this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error' })
80 81 82
            }
        })
    }
83

84 85 86 87 88 89
    handleInputChange(e) {
        this.setState({ search: e })
        let body = {
            "keyword": this.state.typeName
        }
        api.create().searchAM(body).then(response => {
a.bairuha's avatar
a.bairuha committed
90
            if (response.data) {
a.bairuha's avatar
a.bairuha committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
                if (response.ok) {
                    if (response.data.status == 'success') {
                        let data = response.data.data
                        // console.log(data)
                        let listVisual = []
                        data.map((item, index) => {
                            let indexId = listVisual.findIndex((val) => val.orderId == item.orders)
                            if(indexId == -1){
                                listVisual.push({
                                    orderId: item.orders,
                                    data: [item]
                                })
                            } else {
                                listVisual[indexId].data.push(item)
                            }
                        })
                        this.setState({ listApproval: listVisual})
                    } else {
                        this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning' }, () => {
d.arizona's avatar
d.arizona committed
110
                            if (response.data.message.includes("Someone Logged In") || response.data.message.includes("Token Expired")) {
a.bairuha's avatar
a.bairuha committed
111 112 113 114 115 116 117
                                setTimeout(() => {
                                    localStorage.removeItem(Constant.TOKEN)
                                    window.location.reload();
                                }, 1000);
                            }
                        })
                    }
a.bairuha's avatar
a.bairuha committed
118
                } else {
a.bairuha's avatar
a.bairuha committed
119
                    this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'error' })
a.bairuha's avatar
a.bairuha committed
120
                }
121
            } else {
a.bairuha's avatar
a.bairuha committed
122
                this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error' })
123 124 125
            }
        })
    }
126

d.arizona's avatar
d.arizona committed
127 128
    handleChangeOrder(fromIndex, toIndex, that) {
        let listApproval = that.state.listApproval
d.arizona's avatar
d.arizona committed
129
        let orderIdFrom = listApproval[fromIndex].orderId
d.arizona's avatar
d.arizona committed
130
        listApproval[fromIndex].orderId = listApproval[toIndex].orderId
d.arizona's avatar
d.arizona committed
131
        listApproval[toIndex].orderId = orderIdFrom
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
132
        that.setState({listEdit: listApproval})
d.arizona's avatar
d.arizona committed
133 134 135 136
    }

    handleSave() {
        let data = []
137 138 139 140 141 142 143 144 145 146
        if (this.state.listEdit.length == 0) {
            this.state.listApproval.map((item,index) => {
                item.data.map((items,index) => {
                    data.push({
                        approval_matrix_id: items.approval_matrix_id,
                        approval_type_id: items.approval_type_id,
                        operator_type_id: items.operator_type_id,
                        orders: item.orderId,
                        user_id: items.user_id
                    })
d.arizona's avatar
d.arizona committed
147 148
                })
            })
149 150 151 152 153 154 155 156 157 158 159 160 161
        } else {
            this.state.listEdit.map((item,index) => {
                item.data.map((items,index) => {
                    data.push({
                        approval_matrix_id: items.approval_matrix_id,
                        approval_type_id: items.approval_type_id,
                        operator_type_id: items.operator_type_id,
                        orders: item.orderId,
                        user_id: items.user_id
                    })
                })
            })
        }
d.arizona's avatar
d.arizona committed
162
        setTimeout(() => {
163 164 165 166 167 168
            let payload = {
                approval_type_id: data[0].approval_type_id,
                approval_matrix_detail : data
            }
            this.setState({listOriginal: payload})
            this.props.updateVAM(this.state.listOriginal)
d.arizona's avatar
d.arizona committed
169
        }, 500);
d.arizona's avatar
d.arizona committed
170 171
    }

172 173 174 175
    render() {
        const that = this;
        const dragProps = {
            onDragEnd(fromIndex, toIndex) {
d.arizona's avatar
d.arizona committed
176
                const data = [...that.state.listApproval];
177
                const item = data.splice(fromIndex, 1)[0];
d.arizona's avatar
d.arizona committed
178 179 180 181 182
                if (fromIndex !== -1 && toIndex !== -1) { 
                    that.handleChangeOrder(fromIndex, toIndex, that)
                    data.splice(toIndex, 0, item);
                    that.setState({ listApproval: data });
                }
183 184 185 186
            },
            nodeSelector: 'li',
            handleSelector: 'a'
        };
187 188
        return (
            <div>    
189
                <div style={{ height: this.props.height }}>
190
                    <div class="main-color" style={{ height: 199, width: '100%' }} />
191
                        <div>
192
                            <div style={{ display: 'flex', paddingRight: 25, paddingLeft: 25, marginTop: -150 }}>
193
                                <label style={{ color: 'white', width: '20%', fontSize: 16, paddingTop: 8 }}>Visualization</label>
194 195 196
                            </div>
                            <div style={{ display: 'flex', paddingRight: 25, paddingLeft: 25 }}>
                                <label style={{ color: '#51c6ea', width: '20%', fontSize: 11 }}>Master Data / Approval Matrix / 
197
                                    <span style={{ color: 'white', width: '20%', fontSize: 11 }}> Visualization</span> 
198
                                </label>
199 200 201
                            </div>
                            <div style={{ padding: 25, width: '100%' }}>
                                <div style={{ width: '100%', padding: 25, backgroundColor: 'white', borderRadius: 6, border: 'solid 1px #c4c4c4'}}>
202
                                    <label style={{ color: '#4b4b4b', fontSize: '16px', fontWeight: 'bold' }}>Approval Hierarchy</label>
203
                                    <div className="" style={{ width: 250, marginTop: 8, marginBottom: 28 }}>
204 205 206 207 208 209
                                        <Autocomplete
                                            {...this.state.types}
                                            id="tipe"
                                            onChange={(event, newInputValue) => this.setState({typeName:newInputValue.approval_type_name}, () => {
                                                this.handleInputChange()
                                            })}
210
                                            disableClearable
211
                                            renderInput={(params) => <TextField {...params} label="Approval Type" margin="normal" />}
212
                                        />
213
                                    </div>
214
                                    <ReactDragListView {...dragProps}>
d.arizona's avatar
d.arizona committed
215
                                        <ol style={{paddingLeft: 15}}>
216
                                        {this.state.listApproval.sort((a,b) => a.orderId - b.orderId).map((item, index) => (
d.arizona's avatar
d.arizona committed
217 218 219
                                            <li key={index} style={{color: 'white', fontSize: 14}}>
                                                <a href='#' style={{ color: '#4b4b4b', fontSize: '14px', cursor: 'auto', outline: 'none', justifyContent: 'flex-start', display: 'flex'}}>
                                                    <label style={{color: '#4b4b4b', marginRight: 15}}>{Number(index+1)}</label>
220
                                                    <div className="grid grid-4x grid-mobile-none gap-20px margin-bottom-20px" style={{width: '75%'}}>
d.arizona's avatar
d.arizona committed
221 222 223 224
                                                        {item.data.map((items, indexs) => {return(<label>{items.fullname}</label>)})}
                                                        <label>{item.data.length == 1? '' : item.data[0].operator_type_name}</label>
                                                    </div>
                                                </a>
225 226 227 228
                                            </li>
                                        ))}
                                        </ol>
                                    </ReactDragListView>
229 230
                                </div>
                            </div>
231 232
                            <div className="row" style={{ float: 'right', display: 'flex', flexDirection: 'row', justifyContent: 'space-between', paddingRight: 15, paddingLeft: 15, paddingBottom: 25 }}>
                                {/* <div onClick={() => this.props.onClickClose()} style={{ width: 102, height: 30, marginLeft: 25, backgroundColor: 'dodgerblue', borderRadius: 5, alignItems: 'center', display: 'flex', justifyContent: 'center', cursor:"pointer" }}>
233
                                    <span style={{ color: '#fff', fontSize: 11 }}>Back</span>
234
                                </div> */}
235 236
                                <div className="row" style={{ float: 'right', marginRight: 25 }}>
                                    <div onClick={() => this.props.onClickClose()} style={{ width: 102, height: 30, border: 'solid 1px #354960', borderRadius: 5, alignItems: 'center', display: 'flex', justifyContent: 'center', cursor:"pointer" }}>
237
                                        <span style={{ color: '#354960', fontSize: 11 }} >Cancel</span>
238
                                    </div>
239
                                    {this.props.btnedit && (
d.arizona's avatar
d.arizona committed
240
                                    <div onClick={() => this.handleSave()} style={{ width: 102, height: 30, marginLeft: 25, backgroundColor: '#354960', borderRadius: 5, alignItems: 'center', display: 'flex', justifyContent: 'center', cursor:"pointer" }}>
241
                                        <span style={{ color: '#fff', fontSize: 11 }}>Save</span>
242
                                    </div>
243
                                    )}
244 245 246
                                </div>
                            </div>
                        </div>
247
                </div>
248 249 250 251 252
            </div>
            
        );
    }
}