import React, { Component } from 'react'; import { TextField, Divider, Typography, Checkbox, withStyles, Collapse, Snackbar } from '@material-ui/core'; import api from '../../../api'; import Autocomplete from '@material-ui/lab/Autocomplete'; import { titleCase } from '../../../library/Utils'; import localeID from "date-fns/locale/id" import format from "date-fns/format"; import RemoveIcon from '@material-ui/icons/Remove'; import AddIcon from '@material-ui/icons/Add'; import { DatePicker } from '@material-ui/pickers'; import * as R from 'ramda' import Images from '../../../assets/Images'; import MuiAlert from '@material-ui/lab/Alert'; import Constant from '../../../library/Constant'; const CustomCheckbox = withStyles({ root: { color: '#51c6ea', '&$checked': { color: '#51c6ea', }, }, checked: {}, })((props) => ); const CustomCheckboxDisabled = withStyles({ root: { color: '#d5d5d5', '&$checked': { color: '#d5d5d5', }, }, checked: {}, })((props) => ); const Alert = withStyles({ })((props) => ); export default class AddUser extends Component { constructor(props) { super(props) this.state = { paramsId: this.props.data, tempData: null, menuData: null, checked: false, fullname: '', email: '', role: null, startDate: null, endDate: null, company: [], listCompany: [], listRole: null, selectedIndex: 0, date: new Date(), errorFullname: false, errorEmail: false, errorRoleName: false, errorStartDate: false, errorEndDate: false, msgErrorFN: '', msgErrorEM: '', msgErrorRN: '', msgErrorSD: '', msgErrorED: '', alert: false, tipeAlert: '', messageAlert: '' } } handleChecked() { this.setState({ checked: !this.state.checked }) } componentDidMount() { this.getRole() this.getPerusahaan() if (this.props.type === 'edit') { // } else { let date = format(new Date, 'yyyy-MM-dd') // console.log(date); this.setState({ startDate: date, endDate: date }) } } handleChange(e, type) { let data = this.state let isDate = type !== '' ? true : false if (isDate && type === 'start_date') { this.setState({ ...data, startDate: format(e, 'yyyy-MM-dd'), endDate: null, errorFullname: false, errorEmail: false, errorRoleName: false, errorStartDate: false, errorEndDate: false, msgErrorFN: '', msgErrorEM: '', msgErrorRN: '', msgErrorSD: '', msgErrorED: '', }) } else if (isDate && type === 'end_date') { this.setState({ ...data, endDate: format(e, 'yyyy-MM-dd'), errorFullname: false, errorEmail: false, errorRoleName: false, errorStartDate: false, errorEndDate: false, msgErrorFN: '', msgErrorEM: '', msgErrorRN: '', msgErrorSD: '', msgErrorED: '', }) } else { this.setState({ ...data, [e.target.name]: e.target.value, errorFullname: false, errorEmail: false, errorRoleName: false, errorStartDate: false, errorEndDate: false, msgErrorFN: '', msgErrorEM: '', msgErrorRN: '', msgErrorSD: '', msgErrorED: '', }) } } clearError() { let listCompany = this.state.listCompany let company = this.state.company const handlePushChild = (item) => { let indexIDzz = company.findIndex((val) => val === item.id) if (indexIDzz === -1) { company.push(item.id) } if (item.children !== null) { if (item.children.length > 0) { item.children.map((items,indexs) => { handlePushChild(items) }) } } } listCompany.map((item, index) => { company.push(item.id) handlePushChild(item) }) let uniqueCompany = company.filter((val, id, array) => { return array.indexOf(val) == id; }); company = this.state.role == null? [] : this.state.role.role_id === 1? uniqueCompany : [] // console.log(uniqueCompany) this.setState({ errorFullname: false, errorEmail: false, errorRoleName: false, errorStartDate: false, errorEndDate: false, msgErrorFN: '', msgErrorEM: '', msgErrorRN: '', msgErrorSD: '', msgErrorED: '', company }, () => { // console.log(this.state.company) }) } isEmail(email) { const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; return re.test(String(email).toLowerCase()); } validasi() { var isEmail = this.isEmail(this.state.email) if (R.isEmpty(this.state.fullname)) { this.setState({ errorFullname: true, msgErrorFN: 'Fullname Cannot be Empty' }) } else if (R.isEmpty(this.state.email)) { this.setState({ errorEmail: true, msgErrorEM: 'Email Cannot be Empty' }) } else if (!isEmail) { this.setState({ errorEmail: true, msgErrorEM: 'Please enter a valid email address' }) } else if (R.isNil(this.state.role)) { this.setState({ errorRoleName: true, msgErrorRN: 'Role Cannot be Empty' }) } else if (R.isNil(this.state.startDate)) { this.setState({ errorStartDate: true, msgErrorSD: 'Valid From Cannot be Empty' }) } else if (R.isNil(this.state.endDate)) { this.setState({ errorEndDate: true, msgErrorED: 'Valid To Cannot be Empty' }) } else if (this.state.company.length < 1) { this.setState({ alert: true, messageAlert: 'Authorization company cannot be empty', tipeAlert: 'warning' }) } // else if (this.state.privileges.length < 1) { // alert('Hak Akses belum di pilih !!') // } else { let payload = { "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 } this.props.createUser(payload) } } getRole() { api.create().getRoleActive().then((response) => { if (response.data) { if (response.ok) { 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 }) } else { this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning' }, () => { if (response.data.message.includes("Someone Logged In") || response.data.message.includes("Token Expired")) { setTimeout(() => { localStorage.removeItem(Constant.TOKEN) window.location.reload(); }, 1000); } }) } } else { this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'error' }) } } else { this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error' }) } }) } getPerusahaan() { api.create().getPerusahaanHierarki().then((response) => { if (response.data) { if (response.ok) { if (response.data.status === 'success') { this.setState({ listCompany: response.data.data }) // console.log(response.data.data) } else { this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning' }, () => { if (response.data.message.includes("Someone Logged In") || response.data.message.includes("Token Expired")) { setTimeout(() => { localStorage.removeItem(Constant.TOKEN) window.location.reload(); }, 1000); } }) } } else { this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'error' }) } } else { this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error' }) } }) } handleItemChecked(item) { let indexID = this.state.company.findIndex((val) => val === item.id) return indexID === -1 ? false : true } handleItemClick(item) { let indexID = this.state.company.findIndex((val) => val === item.id) let company = this.state.company const handlePushChild = (item) => { let indexIDzz = company.findIndex((val) => val === item.id) if (indexIDzz === -1) { company.push(item.id) } if (item.children !== null) { if (item.children.length > 0) { item.children.map((items,indexs) => { handlePushChild(items) }) } } } const handleSpliceChild = (item) => { let indexIDzz = company.findIndex((val) => val === item.id) if (indexIDzz !== -1) { company.splice(indexIDzz, 1) } if (item.children !== null) { if (item.children.length > 0) { item.children.map((items,indexs) => { handleSpliceChild(items) }) } } } if (indexID === -1) { company.push(item.id) if (item.children !== null) { if (item.children.length > 0) { item.children.map((items,indexs) => { handlePushChild(items) }) } } } else { company.splice(indexID, 1) if (item.children !== null) { if (item.children.length > 0) { item.children.map((items,indexs) => { handleSpliceChild(items) }) } } } // console.log(company) this.setState({ company}) } renderChildren = (item, pad) => { let padding = null if (pad !== undefined) { padding = pad } else { padding = 30 } return (
{item.children.length > 0 && (
    {item.children.map((data, index) => { return ( //
  • 0 ? padding : padding + 30) : padding + 30 }}> {R.isNil(data.children) ? null : data.children.length < 1 ? null : this.handleCollapse(data)} style={{ marginLeft: 7, marginRight: 2 }}> {data.collapse ? : } } {this.state.role ? this.state.role.role_id === 1 ? this.handleItemClick(item)} /> : this.handleItemClick(data)} /> : this.handleItemClick(data)} /> } {titleCase(data.company_name)}
    {!R.isNil(data.children) && this.renderChildren(data, padding + 30)}
    //
  • ) })}
)}
) } handleCollapse(item) { let path = this.searchIt({ children: this.state.listCompany }, item.id) let listCompany = this.state.listCompany let arrayPath = [] if (path.length > 1) { arrayPath = path.split('-'); arrayPath = arrayPath.map((item) => { return item }) } else { arrayPath.push(path) } let pathSelect = null if (arrayPath.length == 1) { pathSelect = listCompany[arrayPath[0]] } else if (arrayPath.length == 2) { pathSelect = listCompany[arrayPath[0]].children[arrayPath[1]] } else if (arrayPath.length == 3) { pathSelect = listCompany[arrayPath[0]].children[arrayPath[1]].children[arrayPath[2]] } else if (arrayPath.length == 4) { pathSelect = listCompany[arrayPath[0]].children[arrayPath[1]].children[arrayPath[2]].children[arrayPath[3]] } else if (arrayPath.length == 5) { pathSelect = listCompany[arrayPath[0]].children[arrayPath[1]].children[arrayPath[2]].children[arrayPath[3]].children[arrayPath[4]] } else if (arrayPath.length == 6) { pathSelect = listCompany[arrayPath[0]].children[arrayPath[1]].children[arrayPath[2]].children[arrayPath[3]].children[arrayPath[4]].children[arrayPath[5]] } else if (arrayPath.length == 7) { pathSelect = listCompany[arrayPath[0]].children[arrayPath[1]].children[arrayPath[2]].children[arrayPath[3]].children[arrayPath[4]].children[arrayPath[5]].children[arrayPath[6]] } pathSelect.collapse = !pathSelect.collapse // console.log(pathSelect.collapse) this.setState({ listCompany }, () => console.log(pathSelect)) } searchIt = (node, search, path = '', position = 0) => { if (node.id && node.id === search) { return path !== '' ? `${path}-${position}` : position; } if (!node.children) { return false } const index = node.children.findIndex((x) => x.id && x.id === search); if (index >= 0) { return path !== '' ? `${path}-${index}` : index; } for (let i = 0; i < node.children.length; i++) { const result = this.searchIt(node.children[i], search, path !== '' ? `${path}-${i}` : i, i); if (result) { return result; } } return false; }; closeAlert() { this.setState({ alert: false }) } render() { return (
this.closeAlert()}> this.closeAlert()} severity={this.state.tipeAlert}> {this.state.messageAlert}
Create Data
null} inputProps={{ style: { fontSize: 11, fontFamily: 'Nunito Sans, sans-serif', } }} InputLabelProps={{ style: { fontSize: 11, fontFamily: 'Nunito Sans, sans-serif', color: '#7e8085', } }} > {/* {periode.map((option) => ( {option.label} ))} */}
this.handleChange(e, '')} inputProps={{ style: { fontSize: 11, fontFamily: 'Nunito Sans, sans-serif', } }} InputLabelProps={{ style: { fontSize: 11, fontFamily: 'Nunito Sans, sans-serif', color: '#7e8085', } }} // defaultValue="Default Value" // helperText="Some important text" />
this.handleChange(e, '')} inputProps={{ style: { fontSize: 11, fontFamily: 'Nunito Sans, sans-serif', } }} InputLabelProps={{ style: { fontSize: 11, fontFamily: 'Nunito Sans, sans-serif', color: '#7e8085', } }} // defaultValue="Default Value" // helperText="Some important text" />
this.setState({ role: newInputValue }, () => this.clearError())} debug renderInput={(params) => this.handleChange(e, '')} error={this.state.errorRoleName} helperText={this.state.msgErrorRN} InputLabelProps={{ style: { fontSize: 11, fontFamily: 'Nunito Sans, sans-serif', color: '#7e8085' } }} InputProps={{ ...params.InputProps, style: { fontSize: 11, fontFamily: 'Nunito Sans, sans-serif' } }} />} value={this.state.role} />
this.handleChange(e, 'start_date')} KeyboardButtonProps={{ 'aria-label': 'change date', }} inputProps={{ style: { fontSize: 11, fontFamily: 'Nunito Sans, sans-serif', } }} InputLabelProps={{ style: { fontSize: 11, color: '#7e8085', fontFamily: 'Nunito Sans, sans-serif', } }} style={{ padding: 0, margin: 0, width: '100%' }} />
this.handleChange(e, 'end_date')} KeyboardButtonProps={{ 'aria-label': 'change date', }} inputProps={{ style: { fontSize: 11, fontFamily: 'Nunito Sans, sans-serif', } }} InputLabelProps={{ style: { fontSize: 11, color: '#7e8085', fontFamily: 'Nunito Sans, sans-serif', } }} style={{ padding: 0, margin: 0, width: '100%' }} />
{/* {periode.map((option) => ( {option.label} ))} */}
{/* {periode.map((option) => ( {option.label} ))} */}
{/*
{`Dibuat: `} {`Diubah: `}
*/}
Authorization Company
{this.state.listCompany.map((item, index) => { return (
{/*
  • */}
    {item.children && item.children.length > 0 && this.handleCollapse(item)} style={{ marginLeft: 7, marginRight: 2 }}> {item.collapse ? : } } {this.state.role ? this.state.role.role_id === 1 ? this.handleItemClick(item)} /> : this.handleItemClick(item)} /> : this.handleItemClick(item)} /> } {titleCase(item.company_name)}
    {!R.isNil(item.children) && this.renderChildren(item)} {/*
*/}
) })}
); } }