Browse Source

fix endline

pull/1/head
wahyu 1 year ago
parent
commit
7c83fd6c4b
  1. 738
      src/views/SimproV2/Divisi/index.js

738
src/views/SimproV2/Divisi/index.js

@ -1,369 +1,369 @@
import * as XLSX from 'xlsx'; import * as XLSX from 'xlsx';
import DialogForm from './DialogForm'; import DialogForm from './DialogForm';
import React, { useState, useEffect } from 'react'; import React, { useState, useEffect } from 'react';
import SweetAlert from 'react-bootstrap-sweetalert'; import SweetAlert from 'react-bootstrap-sweetalert';
import axios from "../../../const/interceptorApi" import axios from "../../../const/interceptorApi"
import { Card, CardBody, CardHeader, Col, Row, Input, Table } from 'reactstrap'; import { Card, CardBody, CardHeader, Col, Row, Input, Table } from 'reactstrap';
import { DIVISI_LIST, DIVISI_ADD, DIVISI_EDIT, DIVISI_DELETE, DIVISI_SEARCH } from '../../../const/ApiConst'; import { DIVISI_LIST, DIVISI_ADD, DIVISI_EDIT, DIVISI_DELETE, DIVISI_SEARCH } from '../../../const/ApiConst';
import { NotificationContainer, NotificationManager } from 'react-notifications'; import { NotificationContainer, NotificationManager } from 'react-notifications';
import { Pagination, Button, Tooltip } from 'antd'; import { Pagination, Button, Tooltip } from 'antd';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
const url = ""; const url = "";
const proyek_id = localStorage.getItem('proyek_id'); const proyek_id = localStorage.getItem('proyek_id');
const role_id = localStorage.getItem('role_id'); const role_id = localStorage.getItem('role_id');
const format = "DD-MM-YYYY"; const format = "DD-MM-YYYY";
const token = window.localStorage.getItem('token'); const token = window.localStorage.getItem('token');
const config = { const config = {
headers: headers:
{ {
Authorization: `Bearer ${token}`, Authorization: `Bearer ${token}`,
"Content-type": `application/json` "Content-type": `application/json`
} }
}; };
const column = [ const column = [
{ name: "Nama" }, { name: "Nama" },
{ name: "Deskripsi" }, { name: "Deskripsi" },
] ]
const ProjectType = ({ params }) => { const ProjectType = ({ params }) => {
const token = localStorage.getItem("token") const token = localStorage.getItem("token")
const HEADER = { const HEADER = {
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
"Authorization": `Bearer ${token}` "Authorization": `Bearer ${token}`
} }
} }
const pageName = params.name; const pageName = params.name;
const [alertDelete, setAlertDelete] = useState(false) const [alertDelete, setAlertDelete] = useState(false)
const [allDataMenu, setAllDataMenu] = useState([]) const [allDataMenu, setAllDataMenu] = useState([])
const [clickOpenModal, setClickOpenModal] = useState(false) const [clickOpenModal, setClickOpenModal] = useState(false)
const [currentPage, setCurrentPage] = useState(1) const [currentPage, setCurrentPage] = useState(1)
const [dataEdit, setDataEdit] = useState([]) const [dataEdit, setDataEdit] = useState([])
const [dataExport, setDataExport] = useState([]) const [dataExport, setDataExport] = useState([])
const [dataTable, setDatatable] = useState([]) const [dataTable, setDatatable] = useState([])
const [idDelete, setIdDelete] = useState(0) const [idDelete, setIdDelete] = useState(0)
const [openDialog, setOpenDialog] = useState(false) const [openDialog, setOpenDialog] = useState(false)
const [rowsPerPage, setRowsPerPage] = useState(10) const [rowsPerPage, setRowsPerPage] = useState(10)
const [search, setSearch] = useState('') const [search, setSearch] = useState('')
const [totalPage, setTotalPage] = useState(0) const [totalPage, setTotalPage] = useState(0)
const [typeDialog, setTypeDialog] = useState('Save') const [typeDialog, setTypeDialog] = useState('Save')
const [dataDivisions, setDataDivisions] = useState([]) const [dataDivisions, setDataDivisions] = useState([])
const { t } = useTranslation() const { t } = useTranslation()
useEffect(() => { useEffect(() => {
getListDivision() getListDivision()
}, []) }, [])
useEffect(() => { useEffect(() => {
getDataProjectType() getDataProjectType()
}, [currentPage, rowsPerPage, search]) }, [currentPage, rowsPerPage, search])
useEffect(() => { useEffect(() => {
const cekData = dataExport || [] const cekData = dataExport || []
if (cekData.length > 0) { if (cekData.length > 0) {
exportExcel() exportExcel()
} }
}, [dataExport]) }, [dataExport])
const getListDivision = async () => { const getListDivision = async () => {
const listDivions = await axios const listDivions = await axios
.get(DIVISI_LIST, HEADER) .get(DIVISI_LIST, HEADER)
.then(res => res) .then(res => res)
.catch((error) => error.response); .catch((error) => error.response);
if (listDivions && listDivions.data && listDivions.data.code == 200) { if (listDivions && listDivions.data && listDivions.data.code == 200) {
let arr = [] let arr = []
for (const v in listDivions.data.data) { for (const v in listDivions.data.data) {
arr.push(listDivions.data.data[v]) arr.push(listDivions.data.data[v])
} }
setDataDivisions(arr); setDataDivisions(arr);
} else { } else {
NotificationManager.error('Gagal Mengambil Data!!', 'Failed'); NotificationManager.error('Gagal Mengambil Data!!', 'Failed');
} }
} }
const getDataProjectType = async () => { const getDataProjectType = async () => {
let start = 0; let start = 0;
if (currentPage !== 1 && currentPage > 1) { if (currentPage !== 1 && currentPage > 1) {
start = (currentPage * rowsPerPage) - rowsPerPage start = (currentPage * rowsPerPage) - rowsPerPage
} }
const payload = { const payload = {
"columns": [ "columns": [
{ {
"name": "name", "name": "name",
"logic_operator": "ilike", "logic_operator": "ilike",
"value": search, "value": search,
"operator": "AND" "operator": "AND"
} }
], ],
"orders": { "orders": {
"ascending": true, "ascending": true,
"columns": [ "columns": [
'id' 'id'
] ]
}, },
"paging": { "paging": {
"length": rowsPerPage, "length": rowsPerPage,
"start": start "start": start
} }
} }
const result = await axios const result = await axios
.post(DIVISI_SEARCH, payload, HEADER) .post(DIVISI_SEARCH, payload, HEADER)
.then(res => res) .then(res => res)
.catch((error) => error.response); .catch((error) => error.response);
if (result && result.data && result.data.code == 200) { if (result && result.data && result.data.code == 200) {
result.data.data.map((res) => { result.data.data.map((res) => {
res.key = res.id.toString() res.key = res.id.toString()
}); });
setDatatable(result.data.data); setDatatable(result.data.data);
setTotalPage(result.data.totalRecord); setTotalPage(result.data.totalRecord);
} else { } else {
NotificationManager.error('Gagal Mengambil Data!!', 'Failed'); NotificationManager.error('Gagal Mengambil Data!!', 'Failed');
} }
} }
const handleExportExcel = async () => { const handleExportExcel = async () => {
let start = 0; let start = 0;
if (currentPage !== 1 && currentPage > 1) { if (currentPage !== 1 && currentPage > 1) {
start = (currentPage * rowsPerPage) - rowsPerPage start = (currentPage * rowsPerPage) - rowsPerPage
} }
const payload = { const payload = {
"columns": [ "columns": [
{ {
"name": "name", "name": "name",
"logic_operator": "like", "logic_operator": "like",
"value": search, "value": search,
"operator": "AND" "operator": "AND"
} }
], ],
"orders": { "orders": {
"ascending": true, "ascending": true,
"columns": [ "columns": [
'id' 'id'
] ]
}, },
"paging": { "paging": {
"length": rowsPerPage, "length": rowsPerPage,
"start": start "start": start
} }
} }
const result = await axios const result = await axios
.post(DIVISI_SEARCH, payload, HEADER) .post(DIVISI_SEARCH, payload, HEADER)
.then(res => res) .then(res => res)
.catch((error) => error.response); .catch((error) => error.response);
if (result && result.data && result.data.code == 200) { if (result && result.data && result.data.code == 200) {
let resData = result.data.data; let resData = result.data.data;
const excelData = []; const excelData = [];
resData.map((val, index) => { resData.map((val, index) => {
let dataRow = { let dataRow = {
"Nama Divisi": val.name, "Nama Divisi": val.name,
"Deskripsi": val.description, "Deskripsi": val.description,
} }
excelData.push(dataRow) excelData.push(dataRow)
}) })
await setDataExport(excelData) await setDataExport(excelData)
} else { } else {
NotificationManager.error('Gagal Export Data!!', 'Failed'); NotificationManager.error('Gagal Export Data!!', 'Failed');
} }
} }
const exportExcel = () => { const exportExcel = () => {
const dataExcel = dataExport || []; const dataExcel = dataExport || [];
const fileName = `Data ${pageName}.xlsx`; const fileName = `Data ${pageName}.xlsx`;
const ws = XLSX.utils.json_to_sheet(dataExcel); const ws = XLSX.utils.json_to_sheet(dataExcel);
const wb = XLSX.utils.book_new(); const wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, `Data ${pageName}`); XLSX.utils.book_append_sheet(wb, ws, `Data ${pageName}`);
XLSX.writeFile(wb, fileName); XLSX.writeFile(wb, fileName);
setDataExport([]) setDataExport([])
} }
const handleSearch = e => { const handleSearch = e => {
const value = e.target.value const value = e.target.value
setSearch(value); setSearch(value);
setCurrentPage(1) setCurrentPage(1)
}; };
const handleOpenDialog = (type) => { const handleOpenDialog = (type) => {
setOpenDialog(true) setOpenDialog(true)
setTypeDialog(type) setTypeDialog(type)
} }
const handleEdit = (data) => { const handleEdit = (data) => {
setDataEdit(data) setDataEdit(data)
handleOpenDialog('Edit'); handleOpenDialog('Edit');
} }
const handleDelete = async (id) => { const handleDelete = async (id) => {
await setAlertDelete(true) await setAlertDelete(true)
await setIdDelete(id) await setIdDelete(id)
} }
const handleCloseDialog = (type, data) => { const handleCloseDialog = (type, data) => {
if (type === "save") { if (type === "save") {
saveProjectType(data); saveProjectType(data);
} else if (type === "edit") { } else if (type === "edit") {
editMaterialR(data); editMaterialR(data);
} }
setDataEdit([]) setDataEdit([])
setOpenDialog(false) setOpenDialog(false)
} }
const saveProjectType = async (data) => { const saveProjectType = async (data) => {
const formData = data const formData = data
const result = await axios.post(DIVISI_ADD, formData, HEADER) const result = await axios.post(DIVISI_ADD, formData, HEADER)
.then(res => res) .then(res => res)
.catch((error) => error.response); .catch((error) => error.response);
if (result && result.data && result.data.code === 200) { if (result && result.data && result.data.code === 200) {
getDataProjectType() getDataProjectType()
NotificationManager.success(`Data berhasil ditambah`, 'Success!!'); NotificationManager.success(`Data berhasil ditambah`, 'Success!!');
} else { } else {
NotificationManager.error(`${result.data.message}`, 'Failed!!'); NotificationManager.error(`${result.data.message}`, 'Failed!!');
} }
} }
const editMaterialR = async (data) => { const editMaterialR = async (data) => {
let urlEdit = DIVISI_EDIT(data.id) let urlEdit = DIVISI_EDIT(data.id)
const formData = data const formData = data
const result = await axios.put(urlEdit, formData, HEADER) const result = await axios.put(urlEdit, formData, HEADER)
.then(res => res) .then(res => res)
.catch((error) => error.response); .catch((error) => error.response);
if (result && result.data && result.data.code === 200) { if (result && result.data && result.data.code === 200) {
getDataProjectType(); getDataProjectType();
NotificationManager.success(`Data berhasil diedit`, 'Success!!'); NotificationManager.success(`Data berhasil diedit`, 'Success!!');
} else { } else {
NotificationManager.error(`Data gagal di edit`, `Failed!!`); NotificationManager.error(`Data gagal di edit`, `Failed!!`);
} }
} }
const toggleAddDialog = () => { const toggleAddDialog = () => {
setOpenDialog(!openDialog) setOpenDialog(!openDialog)
} }
const onConfirmDelete = async () => { const onConfirmDelete = async () => {
let url = DIVISI_DELETE(idDelete); let url = DIVISI_DELETE(idDelete);
const result = await axios.delete(url, config) const result = await axios.delete(url, config)
.then(res => res) .then(res => res)
.catch((error) => error.response); .catch((error) => error.response);
if (result && result.data && result.data.code === 200) { if (result && result.data && result.data.code === 200) {
getDataProjectType() getDataProjectType()
setIdDelete(0) setIdDelete(0)
setAlertDelete(false) setAlertDelete(false)
NotificationManager.success(`Data berhasil dihapus!`, 'Success!!'); NotificationManager.success(`Data berhasil dihapus!`, 'Success!!');
} else { } else {
setIdDelete(0) setIdDelete(0)
setAlertDelete(false) setAlertDelete(false)
NotificationManager.error(`Data gagal dihapus!}`, 'Failed!!'); NotificationManager.error(`Data gagal dihapus!}`, 'Failed!!');
} }
} }
const cancelDelete = () => { const cancelDelete = () => {
setAlertDelete(false) setAlertDelete(false)
setIdDelete(0) setIdDelete(0)
} }
const onShowSizeChange = (current, pageSize) => { const onShowSizeChange = (current, pageSize) => {
setRowsPerPage(pageSize) setRowsPerPage(pageSize)
} }
const onPagination = (current, pageSize) => { const onPagination = (current, pageSize) => {
setCurrentPage(current) setCurrentPage(current)
} }
const dataNotAvailable = () => { const dataNotAvailable = () => {
if (dataTable.length === 0) { if (dataTable.length === 0) {
return ( return (
<tr> <tr>
<td align="center" colSpan="3">{t('noData')}</td> <td align="center" colSpan="3">{t('noData')}</td>
</tr> </tr>
) )
} }
} }
return ( return (
<div> <div>
<NotificationContainer /> <NotificationContainer />
<SweetAlert <SweetAlert
show={alertDelete} show={alertDelete}
warning warning
showCancel showCancel
confirmBtnText="Delete" confirmBtnText="Delete"
confirmBtnBsStyle="danger" confirmBtnBsStyle="danger"
title={t('deleteConfirm')} title={t('deleteConfirm')}
onConfirm={onConfirmDelete} onConfirm={onConfirmDelete}
onCancel={cancelDelete} onCancel={cancelDelete}
focusCancelBtn focusCancelBtn
> >
{t('deleteMsg')} {t('deleteMsg')}
</SweetAlert> </SweetAlert>
<DialogForm <DialogForm
openDialog={openDialog} openDialog={openDialog}
closeDialog={handleCloseDialog} closeDialog={handleCloseDialog}
toggleDialog={() => toggleAddDialog} toggleDialog={() => toggleAddDialog}
typeDialog={typeDialog} typeDialog={typeDialog}
dataEdit={dataEdit} dataEdit={dataEdit}
clickOpenModal={clickOpenModal} clickOpenModal={clickOpenModal}
dataParent={allDataMenu} dataParent={allDataMenu}
dataDivisions={dataDivisions} dataDivisions={dataDivisions}
/> />
<Card> <Card>
<CardHeader style={{ display: "flex", justifyContent: "space-between" }}> <CardHeader style={{ display: "flex", justifyContent: "space-between" }}>
<h4 className="capitalize">{pageName}</h4> <h4 className="capitalize">{pageName}</h4>
<Row> <Row>
<Col> <Col>
<Input onChange={handleSearch} value={search} type="text" name="search" id="search" placeholder={t('searchDivision')} /> <Input onChange={handleSearch} value={search} type="text" name="search" id="search" placeholder={t('searchDivision')} />
</Col> </Col>
<Col> <Col>
<Tooltip title={t('divisionAdd')}> <Tooltip title={t('divisionAdd')}>
<Button style={{ background: "#4caf50", color: "#fff" }} onClick={() => handleOpenDialog('Save')}><i className="fa fa-plus"></i></Button> <Button style={{ background: "#4caf50", color: "#fff" }} onClick={() => handleOpenDialog('Save')}><i className="fa fa-plus"></i></Button>
</Tooltip> </Tooltip>
<Tooltip title={t('exportExcel')}> <Tooltip title={t('exportExcel')}>
<Button style={{ marginLeft: "5px" }} onClick={() => handleExportExcel()}><i className="fa fa-print"></i></Button> <Button style={{ marginLeft: "5px" }} onClick={() => handleExportExcel()}><i className="fa fa-print"></i></Button>
</Tooltip> </Tooltip>
</Col> </Col>
</Row> </Row>
</CardHeader> </CardHeader>
<CardBody> <CardBody>
<Table responsive striped hover> <Table responsive striped hover>
<thead> <thead>
<tr> <tr>
<th>Aksi</th> <th>Aksi</th>
{column.map((i, index) => { {column.map((i, index) => {
return ( return (
<th key={index} scope="row">{i.name}</th> <th key={index} scope="row">{i.name}</th>
) )
})} })}
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{dataNotAvailable()} {dataNotAvailable()}
{dataTable.map((n, index) => { {dataTable.map((n, index) => {
return ( return (
<tr key={n.id}> <tr key={n.id}>
<td className='nowrap'> <td className='nowrap'>
<Tooltip title={t('delete')}> <Tooltip title={t('delete')}>
<i id="TooltipDelete" className="fa fa-trash" style={{ color: 'red', marginRight: 10, cursor: "pointer" }} onClick={() => handleDelete(n.id)}></i> <i id="TooltipDelete" className="fa fa-trash" style={{ color: 'red', marginRight: 10, cursor: "pointer" }} onClick={() => handleDelete(n.id)}></i>
</Tooltip> </Tooltip>
<Tooltip title={t('edit')}> <Tooltip title={t('edit')}>
<i id="TooltipEdit" className="fa fa-edit" style={{ color: 'green', cursor: "pointer" }} onClick={() => handleEdit(n)}></i> <i id="TooltipEdit" className="fa fa-edit" style={{ color: 'green', cursor: "pointer" }} onClick={() => handleEdit(n)}></i>
</Tooltip> </Tooltip>
</td> </td>
<td>{n.name}</td> <td>{n.name}</td>
<td>{n.description}</td> <td>{n.description}</td>
</tr> </tr>
) )
})} })}
</tbody> </tbody>
</Table> </Table>
</CardBody> </CardBody>
</Card> </Card>
</div> </div>
) )
} }
export default ProjectType; export default ProjectType;

Loading…
Cancel
Save