import React, { Component } from 'react'; import BootstrapTable from 'react-bootstrap-table-next'; import ToolkitProvider, { Search, CSVExport } from 'react-bootstrap-table2-toolkit'; import paginationFactory from 'react-bootstrap-table2-paginator'; import cellEditFactory from 'react-bootstrap-table2-editor'; import { Badge, Card, CardBody, CardHeader, Col, Row, Table, Button, Modal, ModalHeader, ModalBody, ModalFooter, Form, FormGroup, Label, Input, FormText } from 'reactstrap'; import { Icon, InlineIcon } from '@iconify/react'; import createOutline from '@iconify/icons-ion/create-outline'; import archiveOutline from '@iconify/icons-ion/archive-outline'; import addCircleOutline from '@iconify/icons-ion/add-circle-outline'; import { AppSwitch } from '@coreui/react'; import { UncontrolledTooltip } from 'reactstrap'; import './DataTable.css'; import SweetAlert from 'react-bootstrap-sweetalert'; import moment from "moment" import { resetWarningCache } from 'prop-types'; const dataColumns = [{ dataField: 'id', text: 'Id' }]; const { SearchBar } = Search; const { ExportCSVButton } = CSVExport; class DataTable extends Component { constructor(props) { const {columns} = props const val = columns.reduce((obj, item) => (obj[item.dataField] = item.state, obj) ,{}); super(props) this.state = { columns: [], data: [], selectRow: { mode: 'checkbox', clickToSelect: true, clickToEdit: false, bgColor: '#e4e5e6', onSelect: (row, isSelect, rowIndex, e) => this.onSelectRowTable({row, isSelect, e, rowIndex}), onSelectAll: (isSelect, rows, e) => this.onSelectRowTable({row: rows, isSelect, e}), }, editCellMode: false, onSelectAllMode: false, selectedRows: [], alert: false, alertWarning: false, successAlert: false, dangerAlert: false, messageAlert: "", openDialog : false, editData : false, ... val } this.mapTableInit = this.mapTableInit.bind(this); } getDataTable = () => { fetch(this.props.urlParamGet, { method: 'POST', header: JSON.stringify({ 'Content-Type': 'application/json' }) }).then((response) => response.json()) .then((responseJson) => { if(responseJson.code_message === "Success"){ let dataArray = [] for(let i = 0; i < responseJson.data.length; i++){ dataArray.push(responseJson.data[i]); } this.mapTableInit(dataArray) }else{ console.log("gagal login"); } }).catch((error) => { }); } componentDidMount() { this.getDataTable(); } mapTableInit(data) { let columns = []; if (data.length > 0) { for (let key in data[0]) { columns.push({ dataField: key, text: key, sort: true }) } } this.setState({ columns: data.length !== 0 ? columns:dataColumns, data: data }) } onSelectRowTable = ({row, isSelect, e}) => { const {selectedRows} = this.state if(typeof row === "object"){ if(isSelect){ this.setState({selectedRows: [...selectedRows, row]}) } else { const idx = selectedRows.indexOf(row) selectedRows.splice(idx, 1) this.setState({selectedRows}) } } else { console.log("type array") this.setState({selectedRows: row}) } } openDialog = () => this.setState({openDialog: true}) closeDialog = () => this.setState({openDialog: false}) saveDialog = async () => { const {editData} = this.state const {columns, urlParamInsert, urlParamUpdate} = this.props const urlParam = editData ? urlParamUpdate:urlParamInsert const val = columns.reduce((obj, item) => (obj[item.dataField] = item.state, obj) ,{}); const obj = columns.reduce((obj, item) => Object.assign(obj, { [item.dataField]: item.dataField === "last_updated" || item.dataField === "created_time" || item.dataField === "modified_time" ? moment().format("YYYY-MM-DD HH:mm:ssZ"): this.state[item.dataField] }), {}); const param = { method: 'POST', header: JSON.stringify({'Content-Type': 'application/json'}), body: JSON.stringify(obj) } this.setState({selectedRows: []}) try { const result = await fetch(urlParam, param).then(response => response.json()).then(res => res) if(result.data){ this.getDataTable(); this.setState({openDialog: false, alert: true, messageAlert: result.code_message, successAlert: true, dangerAlert: false, ...val}) } else { this.setState({openDialog: false, alert: true, messageAlert: result.code_message, successAlert: false, dangerAlert: true, ...val}) } } catch { this.setState({openDialog: false, alert: true, messageAlert: "Opss! looks like something wrong", successAlert: false, dangerAlert: true, ...val}) } } dialogContent = () => { const {editData, openDialog} = this.state const {columns} = this.props return (