import React, { Component } from 'react'; import { TextField, Typography, withStyles, Snackbar } from '@material-ui/core'; import * as R from 'ramda'; import { DatePicker } from "@material-ui/pickers"; import format from "date-fns/format"; import Autocomplete from '@material-ui/lab/Autocomplete'; import MuiAlert from '@material-ui/lab/Alert'; import api from '../../../api'; import Images from '../../../assets/Images'; import Constant from '../../../library/Constant'; const Alert = withStyles({ })((props) => ); export default class CreatePerusahaan extends Component { constructor(props) { super(props) this.state = { id: '', company: '', companyID: '', parentCompany: '', unitBisnis: '', totalReport: '', startDate: '', endDate: '', status: '', types: null, getTypes: null, perusahaan: null, getPerusahaan: null, errorNP: false, errorPC: false, errorSD: false, errorED: false, errorJL: false, errorUB: false, msgErrorNP: '', msgErrorPC: '', msgErrorSD: '', msgErrorED: '', msgErrorJL: '', msgErrorUB: '', alert: false, tipeAlert: '', messageAlert: '', } } render() { let { type } = this.props return type === 'edit' ? this.renderEdit() : this.renderCreate() } componentDidMount() { if (this.props.type === 'edit') { this.getDetailPerusahaan() // console.log(this.props.data); } else { this.getCompanyActive() this.getAllUnitBisnis() let date = format(new Date, 'yyyy-MM-dd') // console.log(date); this.setState({ startDate: date, endDate: date }) } } getDetailPerusahaan() { api.create().getDetailPerusahaan(this.props.data[1]).then(response => { console.log(response) if (response.data) { if (response.ok) { if (response.data.status === "success") { this.setState({ businessID: response.data.data.business_unit_id, companyID: response.data.data.company_id, status: response.data.data.status, company: response.data.data.company_name, parentID: response.data.data.parent, parent: response.data.data.parent_name, startDate: response.data.data.start_date, endDate: response.data.data.end_date, totalReport: response.data.data.total_report, created: response.data.data.created, updated: response.data.data.updated === null ? "" : response.data.data.updated }, () => { // setTimeout(() => { this.getAllUnitBisnis() this.getCompanyActive() // }, 1000); }) } else { this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning' }, () => { if (response.data.message.includes("Someone Logged In")) { 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' }) } }) } getCompanyActive() { console.log(this.state.company) if (this.state.company !== null) { console.log(this.state.company) let payload = { "company_id": this.state.company.company_id } api.create().getPerusahaanActive(payload).then(response => { console.log(response) if (response.data) { if (response.ok) { if (response.data.status === 'success') { let data = response.data.data let currentIndex = null let perusahaanData = data.map((item, index) => { if (this.state.companyID !== item.company_id) { return { company_id: item.company_id, company_name: item.company_name } } else { currentIndex = index } }) if (currentIndex !== null) { perusahaanData.splice(currentIndex, 1) } let index = perusahaanData.sort((a, b) => a.company_id - b.company_id).findIndex((val) => val.company_id == this.state.parentID) let typeProps = { options: perusahaanData, getOptionLabel: (option) => option.company_name, }; if (this.props.type === 'edit') { this.setState({ perusahaan: typeProps, perusahaanData: response.data.data, getPerusahaan: index == -1 ? null : perusahaanData[index], msgErrorPC: index === -1 ? 'Company has been Inactive' : "", errorPC: index === -1 ? true : false }) } else { this.setState({ perusahaan: typeProps, perusahaanData: response.data.data, getPerusahaan: index == -1 ? null : perusahaanData[index] }) } } else { this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning' }, () => { if (response.data.message.includes("Someone Logged In")) { 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' }) } }) } } getAllUnitBisnis() { api.create().getUnitBisnisActive().then(response => { if (response.data) { if (response.ok) { if (response.data.status === 'success') { let data = response.data.data let typeData = data.map((item) => { return { business_unit_id: item.business_unit_id, business_unit_name: item.business_unit_name } }) // console.log(typeData) let index = typeData.findIndex((val) => val.business_unit_id == this.state.businessID) console.log(index) let typeProps = { options: typeData, getOptionLabel: (option) => option.business_unit_name, }; if (this.props.type === 'edit') { this.setState({ types: typeProps, typeData: response.data.data, getTypes: index == -1 ? null : typeData[index], msgErrorUB: index === -1 ? 'Business Unit has been Inactive' : "", errorUB: index === -1 ? true : false }) } else { this.setState({ types: typeProps, typeData: response.data.data, getTypes: index == -1 ? null : typeData[index] }) } } else { this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning' }, () => { if (response.data.message.includes("Someone Logged In")) { 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' }) } }) } // getParentCompany() { // api.create().getPerusahaanActive().then((response) => { // if (response.data.status === 'success') { // let data = response.data.data // let typeData = data.map((item) => { // return { // company_id: item.company_id, // company_name: item.company_name // } // }) // let typeProps = { // options: typeData.sort((a, b) => a.company_id - b.company_id), // getOptionLabel: (option) => option.company_name, // }; // this.setState({ perusahaan: typeProps, perusahaanData: response.data.data }) // } else { // alert(response.data.message) // } // }) // } handleChange(e, type) { let data = this.state let isDate = type !== '' ? true : false if (isDate && type == 'start_date') { this.setState({ startDate: format(e, 'yyyy-MM-dd'), endDate: null }, () => { this.clearError() console.log(this.state.startDate) }) } else if (isDate && type == 'end_date') { this.setState({ endDate: format(e, 'yyyy-MM-dd') }, () => { this.clearError() console.log(this.state.endDate) }) } else { // this.setState({...data, tempData: {...this.state.tempData, [e.target.name] : e.target.value}}) } } clearError() { this.setState({ errorNP: false, errorPC: false, errorSD: false, errorED: false, errorJL: false, errorUB: false, msgErrorNP: '', msgErrorPC: '', msgErrorSD: '', msgErrorED: '', msgErrorJL: '', msgErrorUB: '' }) } validasiEdit() { if (R.isEmpty(this.state.company)) { this.setState({ errorNP: true, msgErrorNP: 'Company Name Cannot be Empty' }) } else if (R.isNil(this.state.getPerusahaan)) { this.setState({ errorPC: true, msgErrorPC: 'Parent Company Cannot be Empty' }) } else if (R.isNil(this.state.getTypes)) { this.setState({ errorUB: true, msgErrorUB: 'Business Unit Cannot be Empty' }) } else if (R.isNil(this.state.startDate)) { this.setState({ errorSD: true, msgErrorSD: 'Valid From Cannot be Empty' }) } else if (R.isNil(this.state.endDate)) { this.setState({ errorED: true, msgErrorED: 'Valid To Cannot be Empty' }) } else { let payload = { "company_id": this.state.companyID, "company_name": this.state.company, "business_unit_id": this.state.getTypes.business_unit_id, // "parent": this.state.getPerusahaan == null ? null : this.state.getPerusahaan.company_id, "parent": this.state.getPerusahaan.company_id, "start_date": this.state.startDate, "end_date": this.state.endDate } this.props.updatePerusahaan(payload) } } validasi() { if (R.isEmpty(this.state.company)) { this.setState({ errorNP: true, msgErrorNP: 'Company Name Cannot be Empty.' }) } else if (R.isNil(this.state.getPerusahaan)) { this.setState({ errorPC: true, msgErrorPC: 'Parent Company Cannot be Empty.' }) } else if (R.isNil(this.state.getTypes)) { this.setState({ errorUB: true, msgErrorUB: 'Business Unit Cannot be Empty.' }) } else if (R.isNil(this.state.startDate)) { this.setState({ errorSD: true, msgErrorSD: 'Valid From Cannot be Empty.' }) } else if (R.isNil(this.state.endDate)) { this.setState({ errorED: true, msgErrorED: 'Valid To Cannot be Empty.' }) } else { let payload = { "company_name": this.state.company, // "parent": this.state.getPerusahaan == null ? null : this.state.getPerusahaan.company_id, "parent": this.state.getPerusahaan.company_id, "business_unit_id": this.state.getTypes.business_unit_id, "start_date": this.state.startDate, "end_date": this.state.endDate } console.log(payload); this.props.createPerusahaan(payload) } } closeAlert() { this.setState({ alert: false }) } renderEdit() { return (
this.closeAlert()}> this.closeAlert()} severity={this.state.tipeAlert}> {this.state.messageAlert}
Edit Data
this.setState({ getTypes: newInputValue }, () => this.clearError())} renderInput={(params) => } value={this.state.getTypes} />
this.handleChange(e, 'start_date')} KeyboardButtonProps={{ 'aria-label': 'change date', }} inputProps={{ style: { fontSize: 11 } }} InputLabelProps={{ style: { fontSize: 11, color: '#7e8085' } }} error={this.state.errorSD} helperText={this.state.msgErrorSD} style={{ padding: 0, margin: 0, width: '100%' }} />
this.setState({ company: e.target.value }, () => this.clearError())} >
this.setState({ getPerusahaan: newInputValue }, () => this.clearError())} renderInput={(params) => } value={this.state.getPerusahaan} />
this.handleChange(e, 'end_date')} KeyboardButtonProps={{ 'aria-label': 'change date', }} inputProps={{ style: { fontSize: 11 } }} InputLabelProps={{ style: { fontSize: 11, color: '#7e8085' } }} style={{ padding: 0, margin: 0, width: '100%' }} error={this.state.errorED} helperText={this.state.msgErrorED} />
Created By : {this.state.created}
Updated By : {this.state.updated}
) } renderCreate() { return (
Create Data
this.setState({ getTypes: newInputValue }, () => this.clearError())} renderInput={(params) => } value={this.state.types} />
this.handleChange(e, 'start_date')} KeyboardButtonProps={{ 'aria-label': 'change date', }} inputProps={{ style: { fontSize: 11 } }} InputLabelProps={{ style: { fontSize: 11, color: '#7e8085' } }} style={{ padding: 0, margin: 0, width: '100%' }} />
Created By : Updated By :
this.setState({ company: e.target.value }, () => this.clearError())} >
this.setState({ getPerusahaan: newInputValue }, () => this.clearError())} renderInput={(params) => } value={this.state.perusahaan} />
this.handleChange(e, 'end_date')} KeyboardButtonProps={{ 'aria-label': 'change date', }} inputProps={{ style: { fontSize: 11 } }} InputLabelProps={{ style: { fontSize: 11, color: '#7e8085' } }} style={{ padding: 0, margin: 0, width: '100%' }} />
); } }