CreateApprovalMatrix.js 26.8 KB
Newer Older
1
import React, { Component } from 'react';
2 3
import { TextField, Typography } from '@material-ui/core';
import { DateTimePicker, KeyboardDatePicker, DatePicker } from "@material-ui/pickers";
4
import Autocomplete from '@material-ui/lab/Autocomplete';
5 6
import * as R from 'ramda'
import format from "date-fns/format";
7
import localeID from "date-fns/locale/id"
8
import api from "../../api";
d.arizona's avatar
d.arizona committed
9
import Images from '../../assets/Images';
a.bairuha's avatar
a.bairuha committed
10
import Constant from '../../library/Constant';
11 12

export default class CreateApprovalMatrix extends Component {
13 14 15 16
    constructor(props) {
        super(props)
        this.state = {
            userId: null,
17
            typeId: null,
18
            operatorId: null,
19 20
            order: '',
            approvedBy: null,
21
            types: null,
22
            operators: null,
23 24
            startDate: null,
            endDate: null,
d.arizona's avatar
d.arizona committed
25
            userData: [],
26
            value: null,
27 28 29
            date: new Date(),
            errorOrder: false,
            msgErrOrder: '',
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
30 31
            errorType: false,
            msgErrType: '',
32 33 34 35
            errorApproved: false,
            msgErrApproved: '',
            errorOperator: false,
            msgErrOperator: '',
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
36 37 38 39
            errorStartDate: false,
            errorEndDate: false,
            msgErrorStartDate: "",
            msgErrorEndDate: ""
40 41 42 43 44
        }
    }

    componentDidMount() {
        this.getUserData()
45
        this.getTypeData()
46
        this.getOperatorData()
47 48 49 50 51
        let date = format(new Date, 'yyyy-MM-dd')
        this.setState({
            startDate: date,
            endDate: date
        })
52 53 54 55
    }

    getUserData() {
        api.create().getApprovedByAM().then((response) => {
a.bairuha's avatar
a.bairuha committed
56 57 58 59 60 61 62
            if(response.data) {
                if (response.ok) { 
                    if (response.data.status == 'success') {
                        let data = response.data.data
                        let userData = data.map((item) => {
                            return {
                                user_id: item.user_id,
d.arizona's avatar
d.arizona committed
63
                                fullname: item.fullname == null? "(NO_NAME)" : item.fullname
a.bairuha's avatar
a.bairuha committed
64 65 66 67 68 69 70 71
                            }
                        })
                        let defaultProps = {
                            options: userData,
                            getOptionLabel: (option) => option.fullname,
                        };
                        this.setState({ approvedBy: defaultProps, userData: response.data.data})
                    } else {
d.arizona's avatar
d.arizona committed
72
                        if (response.data.message.includes("Someone Logged In") || response.data.message.includes("Token Expired")) {
a.bairuha's avatar
a.bairuha committed
73 74 75 76 77 78 79 80 81
                            this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning' }, () => {
                                setTimeout(() => {
                                    localStorage.removeItem(Constant.TOKEN)
                                    window.location.reload();
                                }, 1000);
                            })
                        } else {
                            this.setState({ alert: true, messageAlert: 'Approver Name: ' + response.data.message, tipeAlert: 'error' })
                        }
82
                    }
a.bairuha's avatar
a.bairuha committed
83 84 85
                } else {
                    this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error' })
                }
86
            } else {
a.bairuha's avatar
a.bairuha committed
87
                this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error' })
88 89 90
            }
        })
    }
91 92 93

    getTypeData() {
        api.create().getTypeAM().then((response) => {
a.bairuha's avatar
a.bairuha committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
            if (response.data) {
                if (response.ok) {
                    if (response.data.status == 'success') {
                        let data = response.data.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 {
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 118 119
                            this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning' }, () => {
                                setTimeout(() => {
                                    localStorage.removeItem(Constant.TOKEN)
                                    window.location.reload();
                                }, 1000);
                            })
                        } else {
                            this.setState({ alert: true, messageAlert: 'Approval Type: ' + response.data.message, tipeAlert: 'warning' })
                        }
120
                    }
a.bairuha's avatar
a.bairuha committed
121 122 123
                } else {
                    this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error' })
                }
124
            } else {
a.bairuha's avatar
a.bairuha committed
125
                this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error' })
126 127 128 129 130 131
            }
        })
    }

    getOperatorData() {
        api.create().getOperatorAM().then((response) => {
a.bairuha's avatar
a.bairuha committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
            if (response.data) {
                if (response.ok) {
                    if (response.data.status == 'success') {
                        let data = response.data.data
                        let operatorData = data.map((item) => {
                            return {
                                operator_type_id: item.operator_type_id,
                                operator_type_name: item.operator_type_name
                            }
                        })
                        let operatorProps = {
                            options: operatorData,
                            getOptionLabel: (option) => option.operator_type_name,
                          };
                        this.setState({ operators: operatorProps, operatorData: response.data.data })
                    } else {
d.arizona's avatar
d.arizona committed
148
                        if (response.data.message.includes("Someone Logged In") || response.data.message.includes("Token Expired")) {
a.bairuha's avatar
a.bairuha committed
149 150 151 152 153 154 155 156 157
                            this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning' }, () => {
                                setTimeout(() => {
                                    localStorage.removeItem(Constant.TOKEN)
                                    window.location.reload();
                                }, 1000);
                            })
                        } else {
                            this.setState({ alert: true, messageAlert: 'Operator: ' + response.data.message, tipeAlert: 'warning' })
                        }
158
                    }
a.bairuha's avatar
a.bairuha committed
159 160 161
                } else {
                    this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error' })
                }
162
            } else {
a.bairuha's avatar
a.bairuha committed
163
                this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error' })
164 165 166
            }
        })
    }
167 168 169 170 171

    handleChange(e, type) {
        let data = this.state
        let isDate = type !== '' ? true : false
        if (isDate && type == 'start_date') {
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
172 173
            this.setState({ startDate: format(e, 'yyyy-MM-dd'), endDate: null }, () => {
                this.clearError()
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
174
                // console.log(this.state.startDate)
175 176
            })
        } else if (isDate && type == 'end_date') {
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
177 178
            this.setState({ endDate: format(e, 'yyyy-MM-dd')},  () => {
                this.clearError()
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
179
                // console.log(this.state.endDate)
180 181 182 183 184 185 186
            })
        } else {
            // this.setState({...data, tempData: {...this.state.tempData, [e.target.name] :  e.target.value}})
        }

    }

Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
187 188 189 190 191 192
    clearError() {
        this.setState({
            errorOrder: false,
            msgErrOrder: "",
            errorType: false,
            msgErrType: '',
193 194 195 196
            errorApproved: false,
            msgErrApproved: '',
            errorOperator: false,
            msgErrOperator: '',
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
197 198 199 200 201 202 203
            errorStartDate: false,
            errorEndDate: false,
            msgErrorStartDate: "",
            msgErrorEndDate: ""
        })
    }

204
    validasi() {
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
205
        if (R.isNil(this.state.typeId)) {
206
            this.setState({ errorType: true, msgErrType: 'Approval Type Cannot be Empty' })
207
        }
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
208
        else if (R.isEmpty(this.state.order)) {
209
            this.setState({ errorOrder: true, msgErrOrder: 'Order Cannot be Empty'})
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
210
        }
211
        else if (R.isNil(this.state.userId)) {
212
            this.setState({ errorApproved: true, msgErrApproved: 'Approver Name Cannot be Empty' })
213
        } 
214
        else if (R.isNil(this.state.operatorId)) {
215
            this.setState({ errorOperator: true, msgErrOperator: 'Operator Cannot be Empty. Please Choose - Option' })
216
        } 
217
        else if (R.isNil(this.state.startDate)) {
218
            this.setState({ errorStartDate: true, msgErrorStartDate: 'Valid From Cannot be Empty' })
219
        } else if (R.isNil(this.state.endDate)) {
220
            this.setState({ errorEndDate: true, msgErrorEndDate: 'Valid To Cannot be Empty' })
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
221 222 223 224 225 226 227 228 229 230
        } else {
            let payload = {
                "approval_type_id": this.state.typeId.approval_type_id,
                "orders": this.state.order,
                "user_id": this.state.userId.user_id,
                "operator_type_id": this.state.operatorId.operator_type_id,
                "start_date": this.state.startDate,
                "end_date": this.state.endDate
            }
            this.props.createAM(payload)
231 232 233
        }
    }

234 235 236 237
    render() {
        return (
            <div className="test app-popup-show" style={{ paddingTop: 100 }}>
                <div className="popup-content background-white border-radius" style={{ borderRadius: 8 }}>
238
                    <div className="popup-panel grid grid-2x main-color" style={{ height: 64, borderTopRightRadius: 8, borderTopLeftRadius: 8 }}>
239 240
                        <div className="col-1" style={{ maxWidth: "inherit", display: 'flex', alignItems: 'center' }}>
                            <div className="popup-title">
241
                                <span style={{ color: '#fff', fontSize: 16, fontWeight: 'bold' }}>Create Data</span>
242 243 244 245 246 247 248 249
                            </div>
                        </div>
                        <div className="col-2 content-right" style={{ maxWidth: "inherit", alignSelf: 'center' }}>
                            <button
                                type="button"
                                className="btn btn-circle btn-white"
                                onClick={() => this.props.onClickClose()}
                            >
d.arizona's avatar
d.arizona committed
250
                                <img src={Images.close}/>
251 252 253 254 255 256 257
                            </button>
                        </div>
                    </div>

                    <div className="border-bottom" style={{ padding: 20 }}>
                        <div className="grid grid-2x grid-mobile-none gap-15px">
                            <div className="column-1">
258
                                <div className="" style={{ backgroundColor: '#e8e8e8', padding: 10, borderRadius: 5 }}>
259 260 261 262
                                    <TextField
                                        style={{ width: '100%', height: '90%' }}
                                        id="id"
                                        label="ID"
263
                                        disabled
264 265 266 267 268 269 270 271 272 273 274
                                        inputProps={{
                                            style: {
                                                fontSize: 11
                                            }
                                        }}
                                        InputLabelProps={{
                                            style: {
                                                fontSize: 11,
                                                color: '#7e8085'
                                            }
                                        }}
275 276 277 278 279
                                    >
                                    </TextField>
                                </div>
                            </div>
                            <div className="column-2">
280
                                <div className="" style={{ padding: 10, borderRadius: 5 }}>
281 282 283
                                    <Autocomplete
                                        {...this.state.types}
                                        id="tipe"
284
                                        onChange={(event, newInputValue) => this.setState({typeId:newInputValue}, ()=> this.clearError())}
285 286 287 288
                                        debug
                                        renderInput={(params) => 
                                            <TextField 
                                                {...params} 
289
                                                label="Approval Type"
290 291 292
                                                onChange={(e) => this.handleChange(e, '')}
                                                error={this.state.errorType}
                                                helperText={this.state.msgErrType}
293 294 295
                                                InputLabelProps={{
                                                    style: {
                                                        fontSize: 11,
296
                                                        fontFamily: 'Nunito Sans, sans-serif',
297 298 299
                                                        color: '#7e8085'
                                                    }
                                                }}
300
                                                InputProps={{ ...params.InputProps, style: { fontSize: 11, fontFamily: 'Nunito Sans, sans-serif' } }}
301 302 303
                                            />
                                        }
                                        value={this.state.typeId}
304
                                    />
305 306 307 308 309
                                </div>
                            </div>
                        </div>
                        <div className="grid grid-2x grid-mobile-none gap-15px">
                            <div className="column-1">
310
                                <div className="margin-top-10px" style={{ padding: 10, borderRadius: 5 }}>
311 312 313 314
                                    <TextField
                                        style={{ width: '100%' }}
                                        id="order"
                                        label="Order"
315 316
                                        // type={"number"}
                                        value={this.state.order === null ? '' : this.state.order}
317 318
                                        error={this.state.errorOrder}
                                        helperText={this.state.msgErrOrder}
319 320 321 322 323 324 325 326 327
                                        // onChange={(e) => this.setState({ order: e.target.value }, () => this.clearError())}
                                        onChange={(e) => {
                                            let coba = String(e.target.value).replace(/[^\d]/g,'');
                                            this.setState({
                                                order: coba
                                            }, 
                                            () => this.clearError())}
                                            // this.handleChange(coba, 'value')}
                                        }
328 329 330 331 332 333 334 335 336 337 338
                                        inputProps={{
                                            style: {
                                                fontSize: 11
                                            }
                                        }}
                                        InputLabelProps={{
                                            style: {
                                                fontSize: 11,
                                                color: '#7e8085'
                                            }
                                        }}
339 340 341 342 343
                                    >
                                    </TextField>
                                </div>
                            </div>
                            <div className="column-2">
344
                                <div className="margin-top-10px" style={{ padding: 10, borderRadius: 5 }}>
345 346
                                <Autocomplete
                                    {...this.state.approvedBy}
347
                                    id="approvedby"
348
                                    onChange={(event, newInputValue) => this.setState({userId:newInputValue}, ()=> this.clearError())}
349 350 351 352
                                    // disableClearable
                                    debug
                                    renderInput={(params) => 
                                        <TextField {...params} 
353
                                            label="Approver Name" 
354 355 356
                                            onChange={(e) => this.handleChange(e, '')}
                                            error={this.state.errorApproved}
                                            helperText={this.state.msgErrApproved}
357 358 359 360 361 362 363 364 365
                                            InputLabelProps={{
                                                style: {
                                                    fontSize: 11,
                                                    color: '#7e8085'
                                                }
                                            }}
                                            InputProps={{ ...params.InputProps, style: { fontSize: 11 } }}
                                    />}
                                    value={this.state.userId}
366
                                />
367 368 369 370 371
                                </div>
                            </div>
                        </div>
                        <div className="grid grid-2x grid-mobile-none gap-15px">
                            <div className="column-1">
372 373 374
                                <div className="margin-top-10px" style={{ padding: 10, borderRadius: 5 }}>
                                    <Autocomplete
                                        {...this.state.operators}
375
                                        id="operator"
376 377
                                        // disableClearable
                                        debug
378
                                        onChange={(event, newInputValue) => this.setState({operatorId:newInputValue}, ()=> this.clearError())}
379 380 381
                                        renderInput={(params) => 
                                            <TextField {...params} 
                                                label="Operator" 
382
                                                onChange={(e) => this.handleChange(e, '')}
383 384
                                                error={this.state.errorOperator}
                                                helperText={this.state.msgErrOperator}
385 386 387 388 389 390 391 392 393
                                                InputLabelProps={{
                                                    style: {
                                                        fontSize: 11,
                                                        color: '#7e8085'
                                                    }
                                                }}
                                                InputProps={{ ...params.InputProps, style: { fontSize: 11 } }}
                                        />}
                                        value={this.state.operatorId}
394
                                    />
395 396 397 398 399 400 401 402
                                </div>
                            </div>
                            <div className="column-2">
                                
                            </div>
                        </div>
                        <div className="grid grid-2x grid-mobile-none gap-15px">
                            <div className="column-1">
403
                                <div className="margin-top-10px" style={{ padding: 10, borderRadius: 5 }}>
404 405 406
                                <DatePicker
                                    margin="normal"
                                    id="startDate"
407
                                    label="Valid From"
408 409 410 411 412 413
                                    format="dd MMMM yyyy"
                                    value={this.state.startDate == "" ? null : this.state.startDate}
                                    onChange={(e) => this.handleChange(e, 'start_date')}
                                    KeyboardButtonProps={{
                                        'aria-label': 'change date',
                                    }}
414 415 416 417 418 419 420 421 422 423 424
                                    inputProps={{
                                        style: {
                                            fontSize: 11
                                        }
                                    }}
                                    InputLabelProps={{
                                        style: {
                                            fontSize: 11,
                                            color: '#7e8085'
                                        }
                                    }}
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
425 426
                                    error={this.state.errorStartDate}
                                    helperText={this.state.msgErrorStartDate}
427 428
                                    style={{ padding: 0, margin: 0, width: '100%' }}
                                />
429 430 431
                                </div>
                            </div>
                            <div className="column-2">
432
                                <div className="margin-top-10px" style={{ padding: 10, borderRadius: 5 }}>
433 434 435
                                <DatePicker
                                    margin="normal"
                                    id="endDate"
436
                                    label="Valid To"
437 438
                                    format="dd MMMM yyyy"
                                    value={this.state.endDate == "" ? null : this.state.endDate}
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
439 440
                                    error={this.state.errorEndDate}
                                    helperText={this.state.msgErrorEndDate}
441
                                    onChange={(e) => this.handleChange(e, 'end_date')}
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
442
                                    minDate={this.state.startDate}
443 444 445
                                    KeyboardButtonProps={{
                                        'aria-label': 'change date',
                                    }}
446 447 448 449 450 451 452 453 454 455 456
                                    inputProps={{
                                        style: {
                                            fontSize: 11
                                        }
                                    }}
                                    InputLabelProps={{
                                        style: {
                                            fontSize: 11,
                                            color: '#7e8085'
                                        }
                                    }}
457 458
                                    style={{ padding: 0, margin: 0, width: '100%' }}
                                />
459 460 461 462 463
                                </div>
                            </div>
                        </div>
                        <div className="grid grid-2x grid-mobile-none gap-15px">
                            <div className="column-1">
464
                                <div className="margin-top-10px" style={{ backgroundColor: '#e8e8e8', padding: 10, borderRadius: 5 }}>
465 466
                                    <TextField
                                        style={{ width: '100%' }}
467 468 469
                                        id="status"
                                        label="Status"
                                        disabled
470
                                        defaultValue={"Active"}
471 472 473 474 475 476 477 478 479 480 481
                                        inputProps={{
                                            style: {
                                                fontSize: 11
                                            }
                                        }}
                                        InputLabelProps={{
                                            style: {
                                                fontSize: 11,
                                                color: '#7e8085'
                                            }
                                        }}
482 483 484 485
                                    >
                                    </TextField>
                                </div>
                            </div>
486 487
                            {/* <div className="column-2">
                                <div className="margin-top-10px" style={{ marginTop: 8 }}>
488 489 490 491 492 493 494 495
                                    <TextField
                                        style={{ width: '100%' }}
                                        id="max-value"
                                        label="Max Value"
                                        // value={this.state.periode}
                                    >
                                    </TextField>
                                </div>
496
                            </div> */}
497
                        </div>
a.bairuha's avatar
a.bairuha committed
498 499 500
                        <div className="margin-top-10px" style={{ paddingTop: 10, paddingBottom: 10, paddingLeft: 10, paddingRight: 30 }}>
                            <Typography style={{ fontSize: 11 }}>{`Created By : `}</Typography>
                            <Typography style={{ fontSize: 11 }}>{`Updated By :`}</Typography>
501 502 503 504 505
                        </div>
                    </div>
                    <div className="border-top grid grid-2x" style={{ height: 56, backgroundColor: '#f5f5f5', paddingLeft: 20, paddingRight: 20 }}>
                        <div className="column-1" style={{ alignSelf: 'center' }}>
                            <div onClick={() => this.props.onClickClose()} style={{ width: 102, height: 30, border: 'solid 1px #354960', borderRadius: 5, alignItems: 'center', display: 'flex', justifyContent: 'center', cursor:"pointer" }}>
506
                                <span style={{ color: '#354960', fontSize: 11 }} >Cancel</span>
507 508 509
                            </div>
                        </div>
                        <div className="column-2" style={{display: 'flex', justifyContent: 'flex-end', alignItems: 'center'}}>
510
                            <div onClick={() => this.validasi()} style={{ width: 102, height: 30, backgroundColor: '#354960', borderRadius: 5, alignItems: 'center', display: 'flex', justifyContent: 'center', cursor:"pointer" }}>
511
                                <span style={{ color: '#fff', fontSize: 11 }}>Save</span>
512 513 514 515 516 517 518 519
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        );
    }
}