AddUser.js 19.5 KB
Newer Older
d.arizona's avatar
d.arizona committed
1
import React, { Component } from 'react';
d.arizona's avatar
d.arizona committed
2
import { TextField, Divider, Typography, Checkbox, withStyles, Collapse } from '@material-ui/core';
d.arizona's avatar
d.arizona committed
3
import api from '../../../api';
d.arizona's avatar
d.arizona committed
4 5
import Autocomplete from '@material-ui/lab/Autocomplete';
import { titleCase } from '../../../library/Utils';
d.arizona's avatar
d.arizona committed
6 7
import localeID from "date-fns/locale/id"
import format from "date-fns/format";
d.arizona's avatar
d.arizona committed
8 9 10 11
import * as R from 'ramda'
import RemoveIcon from '@material-ui/icons/Remove';
import AddIcon from '@material-ui/icons/Add';
import { DatePicker } from '@material-ui/pickers';
d.arizona's avatar
d.arizona committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

const CustomCheckbox = withStyles({
    root: {
      color: '#51c6ea',
      '&$checked': {
        color: '#51c6ea',
      },
    },
    checked: {},
  })((props) => <Checkbox color="default" {...props} />);

export default class AddUser extends Component {
    
    constructor(props) {
        super(props)
        this.state = {
            paramsId: this.props.data,
            tempData: null,
            menuData: null,
d.arizona's avatar
d.arizona committed
31 32 33 34 35 36 37
            checked: false,
            fullname: '',
            email: '',
            role: null,
            startDate: null,
            endDate: null,
            company: [],
d.arizona's avatar
d.arizona committed
38
            listCompany: [],
d.arizona's avatar
d.arizona committed
39
            listRole: null,
d.arizona's avatar
d.arizona committed
40
            selectedIndex: 0,
d.arizona's avatar
d.arizona committed
41
            date: new Date(),
d.arizona's avatar
d.arizona committed
42 43 44 45 46 47 48 49 50 51
            errorFullname: false,
            errorEmail: false,
            errorRoleName: false,
            errorStartDate: false,
            errorEndDate: false,
            msgErrorFN: '',
            msgErrorEM: '',
            msgErrorRN: '',
            msgErrorSD: '',
            msgErrorED: '',
d.arizona's avatar
d.arizona committed
52 53 54 55 56 57 58 59
        }
    }

    handleChecked() {
        this.setState({checked: !this.state.checked})
    }
    
    componentDidMount() {
d.arizona's avatar
d.arizona committed
60
        this.getRole()
d.arizona's avatar
d.arizona committed
61
        this.getPerusahaan()
d.arizona's avatar
d.arizona committed
62 63
    }

d.arizona's avatar
d.arizona committed
64
    handleChange(e, type) {
d.arizona's avatar
d.arizona committed
65
        let data = this.state
d.arizona's avatar
d.arizona committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
        let isDate = type !== '' ? true : false
        if (isDate && type == 'start_date') {
            this.setState({ ...data, startDate: format(e, 'yyyy-MM-dd'), endDate: null,
                errorRoleName: false,
                errorStartDate: false,
                errorEndDate: false,
                msgErrorRN: '',
                msgErrorSD: '',
                msgErrorED: '',
            })
        } else if (isDate && type == 'end_date') {
            this.setState({ ...data, endDate: format(e, 'yyyy-MM-dd') ,
                errorRoleName: false,
                errorStartDate: false,
                errorEndDate: false,
                msgErrorRN: '',
                msgErrorSD: '',
                msgErrorED: '',
            })
        } else {
            this.setState({ ...data, [e.target.name]: e.target.value, 
                errorRoleName: false,
                errorStartDate: false,
                errorEndDate: false,
                msgErrorRN: '',
                msgErrorSD: '',
                msgErrorED: '', 
            })
        }
    }

    validasi() {
        this.createUser()
d.arizona's avatar
d.arizona committed
99 100
    }

d.arizona's avatar
d.arizona committed
101
    createUser() {
d.arizona's avatar
d.arizona committed
102
        let payload = {
d.arizona's avatar
d.arizona committed
103 104 105 106 107 108 109
            "role_id": this.state.role.role_id,
            "email": this.state.email,
            "fullname": this.state.fullname,
            "password": this.state.fullname,
            "company": this.state.company,
            "start_date": this.state.startDate,
            "end_date": this.state.endDate
d.arizona's avatar
d.arizona committed
110
        }
d.arizona's avatar
d.arizona committed
111 112
        api.create().createUser(payload).then((response) => {
            console.log(response)
d.arizona's avatar
d.arizona committed
113 114 115 116 117 118
            if (String(response.data.status).toLocaleUpperCase == 'success') {
                this.props.onClickClose()
                this.props.refresh()
            } else {
                alert(response.data.message)
            }
d.arizona's avatar
d.arizona committed
119
        })
d.arizona's avatar
d.arizona committed
120 121
    }

d.arizona's avatar
d.arizona committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    getRole() {
        api.create().getRole().then((response) => {
            if(response.data.status == 'success') {
                let data = response.data.data
                let roleData = data.map((item) => {
                    return {
                        role_id: item.role_id,
                        role_name: item.role_name
                    }
                })
                let defaultProps = {
                    options: roleData,
                    getOptionLabel: (option) => titleCase(option.role_name),
                  };
                this.setState({listRole: defaultProps})
d.arizona's avatar
d.arizona committed
137 138 139 140 141 142
            } else {
                alert(response.data.message)
            }
        })
    }

d.arizona's avatar
d.arizona committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
    getPerusahaan() {
        api.create().getPerusahaanHierarki().then((response) => {
            if(response.data.status == 'success') {
                this.setState({listCompany: response.data.data})
                console.log(response.data.data)
            }
        })
    }

    handleItemChecked(item) {
        let indexID = this.state.company.findIndex((val) => val == item.company_id)
        return indexID == -1 ? false : true
    }

    handleItemClick(item) {
        let indexID = this.state.company.findIndex((val) => val == item.company_id)
        let company = this.state.company
        if (indexID == -1) {
            company.push(item.company_id)
        } else {
            company.splice(indexID, 1)
        }
        this.setState({ company })
    }
d.arizona's avatar
d.arizona committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190

    render() {
        return (
            <div className="test app-popup-show">
                <div className="popup-content background-white border-radius" style={{ borderRadius: 8 }}>
                    <div className="popup-panel grid grid-2x" style={{ backgroundColor: '#51c6ea', height: 64, borderTopRightRadius: 8, borderTopLeftRadius: 8 }}>
                        <div className="col-1" style={{ maxWidth: "inherit", display: 'flex', alignItems: 'center' }}>
                            <div className="popup-title">
                                <span style={{ color: '#fff', fontSize: 16, fontWeight: 'bold' }}>Ubah Data</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.props.onClickClose()}
                            >
                                <i className="fa fa-lg fa-times" style={{ color: 'white' }} />
                            </button>
                        </div>
                    </div>

                    <div className="grid grid-2x grid-mobile-none gap-15px" style={{ padding: 20 }}>
                        <div className="column-1">
d.arizona's avatar
d.arizona committed
191
                            <div className="">
d.arizona's avatar
d.arizona committed
192 193 194 195 196 197 198
                                <TextField
                                    style={{ width: '100%' }}
                                    id="id"
                                    label="ID"
                                    disabled
                                    // id="outlined-read-only-input"
                                    variant="filled"
d.arizona's avatar
d.arizona committed
199
                                    value={'-'}
d.arizona's avatar
d.arizona committed
200 201 202 203 204 205 206 207 208 209 210 211
                                    onChange={(e) => null}
                                >
                                    {/* {periode.map((option) => (
                                        <MenuItem key={option.value} value={option.value}>
                                            {option.label}
                                        </MenuItem>
                                    ))} */}
                                </TextField>
                            </div>
                        </div>

                        <div className="column-2">
d.arizona's avatar
d.arizona committed
212 213 214 215 216 217
                            <div className="">
                                <TextField
                                style={{ width: '100%' , marginTop: 7}}
                                id="fullname"
                                name="fullname"
                                label="Nama Lengkap"
d.arizona's avatar
d.arizona committed
218 219
                                value={this.state.fullname}
                                onChange={(e) => this.handleChange(e, '')}
d.arizona's avatar
d.arizona committed
220 221 222 223 224 225 226 227 228
                                // defaultValue="Default Value"
                                // helperText="Some important text"
                                />
                            </div>
                        </div>
                    </div>

                    <div className="grid grid-2x grid-mobile-none gap-15px" style={{ paddingLeft: 20, paddingRight: 20 }}>
                        <div className="column-1">
d.arizona's avatar
d.arizona committed
229 230 231
                            <div className="margin-bottom-20px">
                                <TextField
                                style={{ width: '100%' , marginTop: 7}}
d.arizona's avatar
d.arizona committed
232 233 234
                                id="email"
                                name="email"
                                label="Email"
d.arizona's avatar
d.arizona committed
235 236
                                value={this.state.email}
                                onChange={(e) => this.handleChange(e, '')}
d.arizona's avatar
d.arizona committed
237 238 239 240 241
                                // defaultValue="Default Value"
                                // helperText="Some important text"
                                />
                            </div>
                        </div>
d.arizona's avatar
d.arizona committed
242 243 244 245 246 247 248 249 250 251 252 253 254

                        <div className="column-2">
                            <div className="margin-bottom-20px">
                                <Autocomplete
                                    {...this.state.listRole}
                                    id="role"
                                    onChange={(event, newInputValue) => this.setState({role: newInputValue})}
                                    debug
                                    renderInput={(params) => <TextField {...params} label="Role" margin="normal" style={{marginTop: 7}}/>}
                                    value={this.state.role}
                                />
                            </div>
                        </div>
d.arizona's avatar
d.arizona committed
255 256 257
                    </div>

                    <div className="grid grid-2x grid-mobile-none gap-15px" style={{ paddingLeft: 20, paddingRight: 20 }}>
d.arizona's avatar
d.arizona committed
258
                    <div className="column-1">
d.arizona's avatar
d.arizona committed
259
                            <div className="margin-bottom-20px">
d.arizona's avatar
d.arizona committed
260 261 262 263 264 265 266 267 268 269 270 271 272 273
                                <DatePicker
                                    margin="normal"
                                    id="startDate"
                                    label="Berlaku Mulai"
                                    format="dd MMMM yyyy"
                                    value={this.state.startDate}
                                    error={this.state.errorStartDate}
                                    helperText={this.state.msgErrorSD}
                                    onChange={(e) => this.handleChange(e, 'start_date')}
                                    KeyboardButtonProps={{
                                        'aria-label': 'change date',
                                    }}

                                    style={{ padding: 0, margin: 0, width: '100%' }}
d.arizona's avatar
d.arizona committed
274 275 276 277 278 279
                                />
                            </div>
                        </div>

                        <div className="column-2">
                            <div className="margin-bottom-20px">
d.arizona's avatar
d.arizona committed
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
                                <DatePicker
                                    margin="normal"
                                    id="endDate"
                                    label="Berlaku Hingga"
                                    format="dd MMMM yyyy"
                                    value={this.state.endDate}
                                    error={this.state.errorEndDate}
                                    helperText={this.state.msgErrorED}
                                    minDate={this.state.startDate}
                                    onChange={(e) => this.handleChange(e, 'end_date')}
                                    KeyboardButtonProps={{
                                        'aria-label': 'change date',
                                    }}

                                    style={{ padding: 0, margin: 0, width: '100%' }}
d.arizona's avatar
d.arizona committed
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
                                />
                            </div>
                        </div>
                    </div>

                    <div className="grid grid-2x grid-mobile-none gap-15px" style={{ paddingLeft: 20, paddingRight: 20 }}>
                        <div className="column-1">
                            <div className="margin-bottom-20px">
                                <TextField
                                    style={{ width: '100%' }}
                                    id="status"
                                    label="Status"
                                    disabled
                                    // id="outlined-read-only-input"
                                    variant="filled"
d.arizona's avatar
d.arizona committed
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
                                    value={'Aktif'}
                                >
                                    {/* {periode.map((option) => (
                                        <MenuItem key={option.value} value={option.value}>
                                            {option.label}
                                        </MenuItem>
                                    ))} */}
                                </TextField>
                            </div>
                        </div>

                        <div className="column-2">
                            <div className="margin-bottom-20px">
                                <TextField
                                    style={{ width: '100%' }}
                                    id="is_expired"
                                    name="is_expired"
                                    label="Expired"
                                    disabled
                                    // id="outlined-read-only-input"
                                    variant="filled"
                                    value={'T'}
d.arizona's avatar
d.arizona committed
332 333 334 335 336 337 338 339 340 341 342
                                >
                                    {/* {periode.map((option) => (
                                        <MenuItem key={option.value} value={option.value}>
                                            {option.label}
                                        </MenuItem>
                                    ))} */}
                                </TextField>
                            </div>
                        </div>
                    </div>
                    <div style={{flexDirection:'column', display: 'flex', paddingLeft: 20, paddingRight: 20 }}>
d.arizona's avatar
d.arizona committed
343
                        <Typography style={{fontSize: 12}}>{`Dibuat  : ${format(this.state.date, 'dd MMMM yyyy', {locale: localeID})}`}</Typography>
d.arizona's avatar
d.arizona committed
344
                        {/* <Typography style={{fontSize: 12}}>{`Diubah: ${this.state.tempData == null? '' : this.state.tempData.updated}`}</Typography> */}
d.arizona's avatar
d.arizona committed
345 346 347
                    </div>
                    <Divider style={{margin: 20}}/>
                    <div style={{paddingLeft: 20, paddingRight: 20}}>
d.arizona's avatar
d.arizona committed
348
                        <h5>Otorisasi Perusahaan</h5>
d.arizona's avatar
d.arizona committed
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
                        <div style={{paddingLeft: 10, overflow:'scroll', height: '25vh'}}>
                            {this.state.listCompany.map((item,index) => {
                                return(
                                    <div>
                                        <div style={{ display: 'flex', flexDirection: 'row', alignItems: 'center', justifyContent: 'flex-start' }}>
                                            {item.childCompany.length > 0 && <span onClick={() => this.setState({ selectedIndex: index == this.state.selectedIndex ? 0 : index })} style={{ marginLeft: 7, marginRight: 2 }}>
                                                {index == this.state.selectedIndex ? <RemoveIcon color={'action'} fontSize={'small'} /> : <AddIcon color={'action'} fontSize={'small'} />}
                                            </span>}
                                            <span>
                                                <CustomCheckbox
                                                    checked={this.handleItemChecked(item)}
                                                    onChange={() => this.handleItemClick(item)}
                                                />
                                            </span>
                                            <Typography style={{ fontSize: 12 }}>{titleCase(item.company_name)}</Typography>
                                        </div>
                                        {item.childCompany.length > 0 && item.childCompany.map((items,indexs) => {
                                            return (
                                                <Collapse in={index == this.state.selectedIndex} timeout="auto" unmountOnExit>
                                                    <div style={{ paddingLeft: 60, display: 'flex', flexDirection: 'row', alignItems: 'center', justifyContent: 'flex-start' }}>
                                                        {/* {item.sub_menu.length > 0 && <span onClick={() => this.setState({ selectedIndex: index == this.state.selectedIndex ? 0 : index })} style={{ marginLeft: 7, marginRight: 2 }}>
                                                            {index == this.state.selectedIndex ? <RemoveIcon color={'action'} fontSize={'small'} /> : <AddIcon color={'action'} fontSize={'small'} />}
                                                        </span>} */}
                                                        {/* <RemoveIcon color={'action'} fontSize={'small'} /> */}
                                                        <span>
                                                            <CustomCheckbox
                                                                checked={this.handleItemChecked(items)}
                                                                onChange={() => this.handleItemClick(items)}
                                                            />
                                                        </span>
                                                        <Typography style={{ fontSize: 12 }}>{titleCase(items.company_name)}</Typography>
                                                    </div>
                                                </Collapse>
                                                
                                            )
                                        })}
                                    </div>
                                )
                            })}
                        </div>
d.arizona's avatar
d.arizona committed
389 390 391
                    </div>
                    <div className="border-top grid grid-2x" style={{ height: 56, backgroundColor: '#f5f5f5', paddingLeft: 20, paddingRight: 20 }}>
                        <div className="column-1" style={{ alignSelf: 'center' }}>
d.arizona's avatar
d.arizona committed
392 393 394 395 396
                            <button onClick={() => this.props.onClickClose()}>
                                <div style={{ width: 102, height: 30, border: 'solid 1px #354960', borderRadius: 5, alignItems: 'center', display: 'flex', justifyContent: 'center' }}>
                                    <span style={{ color: '#354960', fontSize: 11 }}>Batal</span>
                                </div>
                            </button>
d.arizona's avatar
d.arizona committed
397 398
                        </div>
                        <div className="column-2" style={{display: 'flex', justifyContent: 'flex-end', alignItems: 'center'}}>
d.arizona's avatar
d.arizona committed
399 400 401 402 403
                            <button onClick={() => this.validasi()}>
                                <div style={{ width: 102, height: 30, backgroundColor: '#354960', borderRadius: 5, alignItems: 'center', display: 'flex', justifyContent: 'center' }}>
                                    <span style={{ color: '#fff', fontSize: 11 }}>Simpan</span>
                                </div>
                            </button>
d.arizona's avatar
d.arizona committed
404 405 406 407 408 409 410
                        </div>
                    </div>
                </div>
            </div>
        );
    }
}