import React, { Component } from 'react'; import { TextField, Typography } from '@material-ui/core'; import * as R from 'ramda'; import { DateTimePicker, KeyboardDatePicker, DatePicker } from "@material-ui/pickers"; import format from "date-fns/format"; export default class CreatePerusahaan extends Component { constructor(props) { super(props) this.state = { id: '', company: '', parentCompany: '', unitBisnis: '', totalReport: '', startDate: '', endDate: '' } } render() { let { type } = this.props return type === 'edit' ? this.renderEdit() : this.renderCreate() } componentDidMount() { if (this.props.type === 'edit') { let data = this.props.data this.setState({ id: data.company_id, company: data.company_name, parentCompany: data.parentName, unitBisnis: data.businessUnitName, totalReport: data.total_report, startDate: data.start_date, endDate: data.end_date }) } } 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') }, () => { console.log(this.state.startDate) }) } else if (isDate && type == 'end_date') { this.setState({ endDate: format(e, 'yyyy-MM-dd') }, () => { console.log(this.state.endDate) }) } else { // this.setState({...data, tempData: {...this.state.tempData, [e.target.name] : e.target.value}}) } } validasi() { if (R.isEmpty(this.state.company)) return alert("Nama perusahaan harus diisi."); if (R.isEmpty(this.state.unitBisnis)) return alert("Unit Bisnis is Required."); if (R.isEmpty(this.state.parentCompany)) return alert("Parent Company is Required."); if (R.isEmpty(this.state.totalReport)) return alert("Total Report is Required."); if (!R.isEmpty(this.state.startDate) && !R.isEmpty(this.state.endDate) && (this.state.startDate > this.state.endDate)) return alert("Masa Berlaku Tidak Boleh Kurang Dari Tanggal Mulai"); if (R.isEmpty(this.state.startDate)) return alert("Tanggal Mulai is Required."); if (R.isEmpty(this.state.endDate)) return alert("Tanggal Berakhir is Required."); console.log('masuk'); if (this.props.type == 'edit') { let payload = { "company_id": this.state.id, "company_name": this.state.company, "business_unit_id": this.state.unitBisnis, "parent": this.state.parentCompany, "total_report": this.state.totalReport, "start_date": this.state.startDate, "end_date": this.state.endDate } this.props.updatePerusahaan(payload) } else if (this.props.type == 'create') { let payload = { "company_name": this.state.company, "parent": this.state.parentCompany, "business_unit_id": this.state.unitBisnis, "total_report": this.state.totalReport, "start_date": this.state.startDate, "end_date": this.state.endDate } this.props.createPerusahaan(payload) } } renderEdit() { return (