CreateApprovalMatrix.js 23.1 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';
10 11

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

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

    getUserData() {
        api.create().getApprovedByAM().then((response) => {
56 57 58 59
            if(response.status == null){
                alert(response.problem)
            }
            else if (response.data.status == 'success') {
60 61 62 63 64 65 66
                let data = response.data.data
                let userData = data.map((item) => {
                    return {
                        user_id: item.user_id,
                        fullname: item.fullname
                    }
                })
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
67
                // console.log(userData)
68 69 70 71
                let defaultProps = {
                    options: userData,
                    getOptionLabel: (option) => option.fullname,
                  };
72
                this.setState({ approvedBy: defaultProps, userData: response.data.data})
73
            } else {
74
                alert('Approver: ' +response.data.message)
75 76 77
            }
        })
    }
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

    getTypeData() {
        api.create().getTypeAM().then((response) => {
            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 {
95
                alert('Approval Type: ' +response.data.message)
96 97 98 99 100 101 102 103 104 105
            }
        })
    }

    getOperatorData() {
        api.create().getOperatorAM().then((response) => {
            if (response.data.status == 'success') {
                let data = response.data.data
                let operatorData = data.map((item) => {
                    return {
106 107
                        operator_type_id: item.operator_type_id,
                        operator_type_name: item.operator_type_name
108 109 110 111 112
                    }
                })
                // console.log(userData)
                let operatorProps = {
                    options: operatorData,
113
                    getOptionLabel: (option) => option.operator_type_name,
114
                  };
115
                this.setState({ operators: operatorProps, operatorData: response.data.data })
116
            } else {
117
                alert('Operator: ' +response.data.message)
118 119 120
            }
        })
    }
121 122 123 124 125

    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
126 127 128
            this.setState({ startDate: format(e, 'yyyy-MM-dd'), endDate: null }, () => {
                this.clearError()
                console.log(this.state.startDate)
129 130
            })
        } else if (isDate && type == 'end_date') {
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
131 132 133
            this.setState({ endDate: format(e, 'yyyy-MM-dd')},  () => {
                this.clearError()
                console.log(this.state.endDate)
134 135 136 137 138 139 140
            })
        } else {
            // this.setState({...data, tempData: {...this.state.tempData, [e.target.name] :  e.target.value}})
        }

    }

Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
141 142 143 144 145 146
    clearError() {
        this.setState({
            errorOrder: false,
            msgErrOrder: "",
            errorType: false,
            msgErrType: '',
147 148 149 150
            errorApproved: false,
            msgErrApproved: '',
            errorOperator: false,
            msgErrOperator: '',
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
151 152 153 154 155 156 157
            errorStartDate: false,
            errorEndDate: false,
            msgErrorStartDate: "",
            msgErrorEndDate: ""
        })
    }

158
    validasi() {
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
159
        if (R.isNil(this.state.typeId)) {
160
            this.setState({ errorType: true, msgErrType: 'Approval Type is Required' })
161
        }
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
162
        else if (R.isEmpty(this.state.order)) {
163
            this.setState({ errorOrder: true, msgErrOrder: 'Order is Required'})
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
164
        }
165 166
        else if (R.isNil(this.state.userId)) {
            // return alert("Pemberi Persetujuan tidak boleh kosong");
167
            this.setState({ errorApproved: true, msgErrApproved: 'Approver is Required' })
168 169
        } 
        else if (R.isNil(this.state.operatorId)) {
170
            this.setState({ errorOperator: true, msgErrOperator: 'Operator is Required' })
171
        } 
172
        else if (R.isNil(this.state.startDate)) {
173
            this.setState({ errorStartDate: true, msgErrorStartDate: 'Start Date is Required' })
174
        } else if (R.isNil(this.state.endDate)) {
175
            this.setState({ errorEndDate: true, msgErrorEndDate: 'End Date is Required' })
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
176 177 178 179 180 181 182 183 184 185 186 187
        } else {
            console.log('masuk');
            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)
            // console.log(payload)
188 189 190
        }
    }

191 192 193 194
    render() {
        return (
            <div className="test app-popup-show" style={{ paddingTop: 100 }}>
                <div className="popup-content background-white border-radius" style={{ borderRadius: 8 }}>
195
                    <div className="popup-panel grid grid-2x main-color" style={{ height: 64, borderTopRightRadius: 8, borderTopLeftRadius: 8 }}>
196 197
                        <div className="col-1" style={{ maxWidth: "inherit", display: 'flex', alignItems: 'center' }}>
                            <div className="popup-title">
198
                                <span style={{ color: '#fff', fontSize: 16, fontWeight: 'bold' }}>Add Data</span>
199 200 201 202 203 204 205 206
                            </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
207
                                <img src={Images.close}/>
208 209 210 211 212 213 214
                            </button>
                        </div>
                    </div>

                    <div className="border-bottom" style={{ padding: 20 }}>
                        <div className="grid grid-2x grid-mobile-none gap-15px">
                            <div className="column-1">
215
                                <div className="" style={{ backgroundColor: '#e8e8e8', padding: 10, borderRadius: 5 }}>
216 217 218 219
                                    <TextField
                                        style={{ width: '100%', height: '90%' }}
                                        id="id"
                                        label="ID"
220
                                        disabled
221 222 223 224 225 226 227 228 229 230 231
                                        inputProps={{
                                            style: {
                                                fontSize: 11
                                            }
                                        }}
                                        InputLabelProps={{
                                            style: {
                                                fontSize: 11,
                                                color: '#7e8085'
                                            }
                                        }}
232 233 234 235 236
                                    >
                                    </TextField>
                                </div>
                            </div>
                            <div className="column-2">
237
                                <div className="" style={{ padding: 10, borderRadius: 5 }}>
238 239 240
                                    <Autocomplete
                                        {...this.state.types}
                                        id="tipe"
241
                                        onChange={(event, newInputValue) => this.setState({typeId:newInputValue}, ()=> this.clearError())}
242 243 244 245
                                        debug
                                        renderInput={(params) => 
                                            <TextField 
                                                {...params} 
246
                                                label="Approval Type"
247 248 249
                                                onChange={(e) => this.handleChange(e, '')}
                                                error={this.state.errorType}
                                                helperText={this.state.msgErrType}
250 251 252
                                                InputLabelProps={{
                                                    style: {
                                                        fontSize: 11,
253
                                                        fontFamily: 'Nunito Sans, sans-serif',
254 255 256
                                                        color: '#7e8085'
                                                    }
                                                }}
257
                                                InputProps={{ ...params.InputProps, style: { fontSize: 11, fontFamily: 'Nunito Sans, sans-serif' } }}
258 259 260
                                            />
                                        }
                                        value={this.state.typeId}
261
                                    />
262 263 264 265 266
                                </div>
                            </div>
                        </div>
                        <div className="grid grid-2x grid-mobile-none gap-15px">
                            <div className="column-1">
267
                                <div className="margin-top-10px" style={{ padding: 10, borderRadius: 5 }}>
268 269 270 271
                                    <TextField
                                        style={{ width: '100%' }}
                                        id="order"
                                        label="Order"
272
                                        type={"number"}
273
                                        value={this.state.order}
274 275
                                        error={this.state.errorOrder}
                                        helperText={this.state.msgErrOrder}
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
276
                                        onChange={(e) => this.setState({ order: e.target.value }, () => this.clearError())}
277 278 279 280 281 282 283 284 285 286 287
                                        inputProps={{
                                            style: {
                                                fontSize: 11
                                            }
                                        }}
                                        InputLabelProps={{
                                            style: {
                                                fontSize: 11,
                                                color: '#7e8085'
                                            }
                                        }}
288 289 290 291 292
                                    >
                                    </TextField>
                                </div>
                            </div>
                            <div className="column-2">
293
                                <div className="margin-top-10px" style={{ padding: 10, borderRadius: 5 }}>
294 295
                                <Autocomplete
                                    {...this.state.approvedBy}
296
                                    id="approvedby"
297
                                    onChange={(event, newInputValue) => this.setState({userId:newInputValue}, ()=> this.clearError())}
298 299 300 301
                                    // disableClearable
                                    debug
                                    renderInput={(params) => 
                                        <TextField {...params} 
302
                                            label="Approver" 
303 304 305
                                            onChange={(e) => this.handleChange(e, '')}
                                            error={this.state.errorApproved}
                                            helperText={this.state.msgErrApproved}
306 307 308 309 310 311 312 313 314
                                            InputLabelProps={{
                                                style: {
                                                    fontSize: 11,
                                                    color: '#7e8085'
                                                }
                                            }}
                                            InputProps={{ ...params.InputProps, style: { fontSize: 11 } }}
                                    />}
                                    value={this.state.userId}
315
                                />
316 317 318 319 320
                                </div>
                            </div>
                        </div>
                        <div className="grid grid-2x grid-mobile-none gap-15px">
                            <div className="column-1">
321 322 323
                                <div className="margin-top-10px" style={{ padding: 10, borderRadius: 5 }}>
                                    <Autocomplete
                                        {...this.state.operators}
324
                                        id="operator"
325 326
                                        // disableClearable
                                        debug
327
                                        onChange={(event, newInputValue) => this.setState({operatorId:newInputValue}, ()=> this.clearError())}
328 329 330
                                        renderInput={(params) => 
                                            <TextField {...params} 
                                                label="Operator" 
331 332 333
                                                onChange={(e) => this.handleChange(e, '')}
                                                error={this.state.errorOperator}
                                                helperText={this.state.msgErrOperator}
334 335 336 337 338 339 340 341 342
                                                InputLabelProps={{
                                                    style: {
                                                        fontSize: 11,
                                                        color: '#7e8085'
                                                    }
                                                }}
                                                InputProps={{ ...params.InputProps, style: { fontSize: 11 } }}
                                        />}
                                        value={this.state.operatorId}
343
                                    />
344 345 346 347 348 349 350 351
                                </div>
                            </div>
                            <div className="column-2">
                                
                            </div>
                        </div>
                        <div className="grid grid-2x grid-mobile-none gap-15px">
                            <div className="column-1">
352
                                <div className="margin-top-10px" style={{ padding: 10, borderRadius: 5 }}>
353 354 355
                                <DatePicker
                                    margin="normal"
                                    id="startDate"
356
                                    label="Start Date"
357 358 359 360 361 362
                                    format="dd MMMM yyyy"
                                    value={this.state.startDate == "" ? null : this.state.startDate}
                                    onChange={(e) => this.handleChange(e, 'start_date')}
                                    KeyboardButtonProps={{
                                        'aria-label': 'change date',
                                    }}
363 364 365 366 367 368 369 370 371 372 373
                                    inputProps={{
                                        style: {
                                            fontSize: 11
                                        }
                                    }}
                                    InputLabelProps={{
                                        style: {
                                            fontSize: 11,
                                            color: '#7e8085'
                                        }
                                    }}
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
374 375
                                    error={this.state.errorStartDate}
                                    helperText={this.state.msgErrorStartDate}
376 377
                                    style={{ padding: 0, margin: 0, width: '100%' }}
                                />
378 379 380
                                </div>
                            </div>
                            <div className="column-2">
381
                                <div className="margin-top-10px" style={{ padding: 10, borderRadius: 5 }}>
382 383 384
                                <DatePicker
                                    margin="normal"
                                    id="endDate"
385
                                    label="End Date"
386 387
                                    format="dd MMMM yyyy"
                                    value={this.state.endDate == "" ? null : this.state.endDate}
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
388 389
                                    error={this.state.errorEndDate}
                                    helperText={this.state.msgErrorEndDate}
390
                                    onChange={(e) => this.handleChange(e, 'end_date')}
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
391
                                    minDate={this.state.startDate}
392 393 394
                                    KeyboardButtonProps={{
                                        'aria-label': 'change date',
                                    }}
395 396 397 398 399 400 401 402 403 404 405
                                    inputProps={{
                                        style: {
                                            fontSize: 11
                                        }
                                    }}
                                    InputLabelProps={{
                                        style: {
                                            fontSize: 11,
                                            color: '#7e8085'
                                        }
                                    }}
406 407
                                    style={{ padding: 0, margin: 0, width: '100%' }}
                                />
408 409 410 411 412
                                </div>
                            </div>
                        </div>
                        <div className="grid grid-2x grid-mobile-none gap-15px">
                            <div className="column-1">
413
                                <div className="margin-top-10px" style={{ backgroundColor: '#e8e8e8', padding: 10, borderRadius: 5 }}>
414 415
                                    <TextField
                                        style={{ width: '100%' }}
416 417 418
                                        id="status"
                                        label="Status"
                                        disabled
419
                                        defaultValue={"Active"}
420 421 422 423 424 425 426 427 428 429 430
                                        inputProps={{
                                            style: {
                                                fontSize: 11
                                            }
                                        }}
                                        InputLabelProps={{
                                            style: {
                                                fontSize: 11,
                                                color: '#7e8085'
                                            }
                                        }}
431 432 433 434
                                    >
                                    </TextField>
                                </div>
                            </div>
435 436
                            {/* <div className="column-2">
                                <div className="margin-top-10px" style={{ marginTop: 8 }}>
437 438 439 440 441 442 443 444
                                    <TextField
                                        style={{ width: '100%' }}
                                        id="max-value"
                                        label="Max Value"
                                        // value={this.state.periode}
                                    >
                                    </TextField>
                                </div>
445
                            </div> */}
446
                        </div>
447
                        <div className="margin-top-10px" style={{ padding: 10, paddingLeft: 0 }}>
Rifka Kurnia Irfiana's avatar
Rifka Kurnia Irfiana committed
448
                            <Typography style={{ fontSize: 11 }}>{`Created by  : `}</Typography>
449
                            {/* <Typography style={{ fontSize: 11 }}>Diubah	: Admin - 21 Jul 2020, 18:45</Typography> */}
450 451 452 453 454
                        </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" }}>
455
                                <span style={{ color: '#354960', fontSize: 11 }} >Cancel</span>
456 457 458
                            </div>
                        </div>
                        <div className="column-2" style={{display: 'flex', justifyContent: 'flex-end', alignItems: 'center'}}>
459
                            <div onClick={() => this.validasi()} style={{ width: 102, height: 30, backgroundColor: '#354960', borderRadius: 5, alignItems: 'center', display: 'flex', justifyContent: 'center', cursor:"pointer" }}>
460
                                <span style={{ color: '#fff', fontSize: 11 }}>Save</span>
461 462 463 464 465 466 467 468
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        );
    }
}