wahyuun
1 year ago
1 changed files with 337 additions and 343 deletions
@ -1,343 +1,337 @@ |
|||||||
import * as XLSX from 'xlsx'; |
import * as XLSX from 'xlsx'; |
||||||
import DialogForm from './DialogForm'; |
import DialogForm from './DialogForm'; |
||||||
import React, { useState, useEffect, useMemo } from 'react'; |
import React, { useState, useEffect, useMemo } 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 { NotificationContainer, NotificationManager } from 'react-notifications'; |
import { NotificationContainer, NotificationManager } from 'react-notifications'; |
||||||
import { Pagination, Button, Tooltip } from 'antd'; |
import { Pagination, Button, Tooltip } from 'antd'; |
||||||
import { SATUAN_ADD, SATUAN_EDIT, SATUAN_DELETE, SATUAN_SEARCH } from '../../../const/ApiConst'; |
import { SATUAN_ADD, SATUAN_EDIT, SATUAN_DELETE, SATUAN_SEARCH } from '../../../const/ApiConst'; |
||||||
import { useTranslation } from 'react-i18next'; |
import { useTranslation } from 'react-i18next'; |
||||||
|
|
||||||
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 Satuan = ({ params }) => { |
const Satuan = ({ 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 { t } = useTranslation() |
const { t } = useTranslation() |
||||||
useEffect(() => { |
useEffect(() => { |
||||||
getDataSatuan() |
getDataSatuan() |
||||||
}, [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 column = [ |
const column = [ |
||||||
{ name: t('name') }, |
{ name: t('name') }, |
||||||
{ name: t('description') }, |
{ name: t('description') }, |
||||||
] |
] |
||||||
const getDataSatuan = async () => { |
const getDataSatuan = 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" |
||||||
}, |
} |
||||||
{ |
], |
||||||
"name": "description", |
"orders": { |
||||||
"logic_operator": "ilike", |
"ascending": true, |
||||||
"value": search, |
"columns": [ |
||||||
"operator": "AND" |
'id' |
||||||
} |
] |
||||||
], |
}, |
||||||
"orders": { |
"paging": { |
||||||
"ascending": true, |
"length": rowsPerPage, |
||||||
"columns": [ |
"start": start |
||||||
'id' |
} |
||||||
] |
} |
||||||
}, |
|
||||||
"paging": { |
const result = await axios |
||||||
"length": rowsPerPage, |
.post(SATUAN_SEARCH, payload, config) |
||||||
"start": start |
.then(res => res) |
||||||
} |
.catch((error) => error.response); |
||||||
} |
if (result && result.data && result.data.code == 200) { |
||||||
|
setDatatable(result.data.data); |
||||||
const result = await axios |
setTotalPage(result.data.totalRecord); |
||||||
.post(SATUAN_SEARCH, payload, config) |
} else { |
||||||
.then(res => res) |
NotificationManager.error('Gagal Mengambil Data!!', 'Failed'); |
||||||
.catch((error) => error.response); |
} |
||||||
if (result && result.data && result.data.code == 200) { |
} |
||||||
setDatatable(result.data.data); |
|
||||||
setTotalPage(result.data.totalRecord); |
const handleSearch = e => { |
||||||
} else { |
const value = e.target.value |
||||||
NotificationManager.error('Gagal Mengambil Data!!', 'Failed'); |
setSearch(value); |
||||||
} |
setCurrentPage(1) |
||||||
} |
}; |
||||||
|
|
||||||
const handleSearch = e => { |
const handleOpenDialog = (type) => { |
||||||
const value = e.target.value |
setOpenDialog(true) |
||||||
setSearch(value); |
setTypeDialog(type) |
||||||
setCurrentPage(1) |
} |
||||||
}; |
|
||||||
|
const handleExportExcel = async () => { |
||||||
const handleOpenDialog = (type) => { |
const payload = { |
||||||
setOpenDialog(true) |
"paging": { "start": 0, "length": -1 }, |
||||||
setTypeDialog(type) |
"columns": [ |
||||||
} |
{ "name": "name", "logic_operator": "ilike", "value": search, "operator": "AND" } |
||||||
|
], |
||||||
const handleExportExcel = async () => { |
"orders": { "columns": ["id"], "ascending": false } |
||||||
const payload = { |
} |
||||||
"paging": { "start": 0, "length": -1 }, |
const result = await axios |
||||||
"columns": [ |
.post(SATUAN_SEARCH, payload, config) |
||||||
{ "name": "name", "logic_operator": "ilike", "value": search, "operator": "AND" } |
.then(res => res) |
||||||
], |
.catch((error) => error.response); |
||||||
"orders": { "columns": ["id"], "ascending": false } |
if (result && result.data && result.data.code == 200) { |
||||||
} |
let resData = result.data.data; |
||||||
const result = await axios |
const excelData = []; |
||||||
.post(SATUAN_SEARCH, payload, config) |
resData.map((val, index) => { |
||||||
.then(res => res) |
let dataRow = { |
||||||
.catch((error) => error.response); |
"Nama": val.name, |
||||||
if (result && result.data && result.data.code == 200) { |
"Deskripsi": val.description, |
||||||
let resData = result.data.data; |
} |
||||||
const excelData = []; |
excelData.push(dataRow) |
||||||
resData.map((val, index) => { |
}) |
||||||
let dataRow = { |
await setDataExport(excelData); |
||||||
"Nama": val.name, |
} else { |
||||||
"Deskripsi": val.description, |
NotificationManager.error('Gagal Export Data!!', 'Failed'); |
||||||
} |
} |
||||||
excelData.push(dataRow) |
} |
||||||
}) |
|
||||||
await setDataExport(excelData); |
const exportExcel = () => { |
||||||
} else { |
const dataExcel = dataExport || []; |
||||||
NotificationManager.error('Gagal Export Data!!', 'Failed'); |
const fileName = `Data ${pageName}.xlsx`; |
||||||
} |
const ws = XLSX.utils.json_to_sheet(dataExcel); |
||||||
} |
const wb = XLSX.utils.book_new(); |
||||||
|
XLSX.utils.book_append_sheet(wb, ws, `Data ${pageName}`); |
||||||
const exportExcel = () => { |
XLSX.writeFile(wb, fileName); |
||||||
const dataExcel = dataExport || []; |
setDataExport([]) |
||||||
const fileName = `Data ${pageName}.xlsx`; |
} |
||||||
const ws = XLSX.utils.json_to_sheet(dataExcel); |
|
||||||
const wb = XLSX.utils.book_new(); |
const handleEdit = (data) => { |
||||||
XLSX.utils.book_append_sheet(wb, ws, `Data ${pageName}`); |
setDataEdit(data) |
||||||
XLSX.writeFile(wb, fileName); |
handleOpenDialog('Edit'); |
||||||
setDataExport([]) |
} |
||||||
} |
|
||||||
|
const handleDelete = async (id) => { |
||||||
const handleEdit = (data) => { |
await setAlertDelete(true) |
||||||
setDataEdit(data) |
await setIdDelete(id) |
||||||
handleOpenDialog('Edit'); |
} |
||||||
} |
|
||||||
|
const handleCloseDialog = (type, data) => { |
||||||
const handleDelete = async (id) => { |
if (type === "save") { |
||||||
await setAlertDelete(true) |
saveSatuan(data); |
||||||
await setIdDelete(id) |
} else if (type === "edit") { |
||||||
} |
editMaterialR(data); |
||||||
|
} |
||||||
const handleCloseDialog = (type, data) => { |
setDataEdit([]) |
||||||
if (type === "save") { |
setOpenDialog(false) |
||||||
saveSatuan(data); |
} |
||||||
} else if (type === "edit") { |
|
||||||
editMaterialR(data); |
const saveSatuan = async (data) => { |
||||||
} |
const formData = data |
||||||
setDataEdit([]) |
const result = await axios.post(SATUAN_ADD, formData, HEADER) |
||||||
setOpenDialog(false) |
.then(res => res) |
||||||
} |
.catch((error) => error.response); |
||||||
|
|
||||||
const saveSatuan = async (data) => { |
if (result && result.data && result.data.code === 200) { |
||||||
const formData = data |
getDataSatuan() |
||||||
const result = await axios.post(SATUAN_ADD, formData, HEADER) |
NotificationManager.success(`Satuan berhasil ditambah`, 'Success!!'); |
||||||
.then(res => res) |
} else { |
||||||
.catch((error) => error.response); |
NotificationManager.error(`${result.data.message}`, 'Failed!!'); |
||||||
|
} |
||||||
if (result && result.data && result.data.code === 200) { |
} |
||||||
getDataSatuan() |
|
||||||
NotificationManager.success(`Data project type berhasil ditambah`, 'Success!!'); |
const editMaterialR = async (data) => { |
||||||
} else { |
let urlEdit = SATUAN_EDIT(data.id) |
||||||
NotificationManager.error(`${result.data.message}`, 'Failed!!'); |
const formData = data |
||||||
} |
|
||||||
} |
const result = await axios.put(urlEdit, formData, HEADER) |
||||||
|
.then(res => res) |
||||||
const editMaterialR = async (data) => { |
.catch((error) => error.response); |
||||||
let urlEdit = SATUAN_EDIT(data.id) |
|
||||||
const formData = data |
if (result && result.data && result.data.code === 200) { |
||||||
|
getDataSatuan(); |
||||||
const result = await axios.put(urlEdit, formData, HEADER) |
NotificationManager.success(`Satuan berhasil diubah`, 'Success!!'); |
||||||
.then(res => res) |
} else { |
||||||
.catch((error) => error.response); |
NotificationManager.error(`Satuan gagal diubah`, `Failed!!`); |
||||||
|
} |
||||||
if (result && result.data && result.data.code === 200) { |
} |
||||||
getDataSatuan(); |
|
||||||
NotificationManager.success(`Data project type berhasil diedit`, 'Success!!'); |
const toggleAddDialog = () => { |
||||||
} else { |
setOpenDialog(!openDialog) |
||||||
NotificationManager.error(`Data project type gagal di edit`, `Failed!!`); |
} |
||||||
} |
|
||||||
} |
const onConfirmDelete = async () => { |
||||||
|
let url = SATUAN_DELETE(idDelete); |
||||||
const toggleAddDialog = () => { |
|
||||||
setOpenDialog(!openDialog) |
const result = await axios.delete(url, config) |
||||||
} |
.then(res => res) |
||||||
|
.catch((error) => error.response); |
||||||
const onConfirmDelete = async () => { |
|
||||||
let url = SATUAN_DELETE(idDelete); |
if (result && result.data && result.data.code === 200) { |
||||||
|
getDataSatuan() |
||||||
const result = await axios.delete(url, config) |
setIdDelete(0) |
||||||
.then(res => res) |
setAlertDelete(false) |
||||||
.catch((error) => error.response); |
NotificationManager.success(`Satuan berhasil dihapus!`, 'Success!!'); |
||||||
|
} else { |
||||||
if (result && result.data && result.data.code === 200) { |
setIdDelete(0) |
||||||
getDataSatuan() |
setAlertDelete(false) |
||||||
setIdDelete(0) |
NotificationManager.error(`Satuan gagal dihapus!}`, 'Failed!!'); |
||||||
setAlertDelete(false) |
} |
||||||
NotificationManager.success(`Data project type berhasil dihapus!`, 'Success!!'); |
} |
||||||
} else { |
|
||||||
setIdDelete(0) |
const cancelDelete = () => { |
||||||
setAlertDelete(false) |
setAlertDelete(false) |
||||||
NotificationManager.error(`Data project type gagal dihapus!}`, 'Failed!!'); |
setIdDelete(0) |
||||||
} |
} |
||||||
} |
|
||||||
|
const onShowSizeChange = (current, pageSize) => { |
||||||
const cancelDelete = () => { |
setRowsPerPage(pageSize) |
||||||
setAlertDelete(false) |
} |
||||||
setIdDelete(0) |
|
||||||
} |
const onPagination = (current, pageSize) => { |
||||||
|
setCurrentPage(current) |
||||||
const onShowSizeChange = (current, pageSize) => { |
} |
||||||
setRowsPerPage(pageSize) |
|
||||||
} |
const dataNotAvailable = () => { |
||||||
|
if (dataTable.length === 0) { |
||||||
const onPagination = (current, pageSize) => { |
return ( |
||||||
setCurrentPage(current) |
<tr> |
||||||
} |
<td align="center" colSpan="3">{t('noData')}</td> |
||||||
|
</tr> |
||||||
const dataNotAvailable = () => { |
) |
||||||
if (dataTable.length === 0) { |
} |
||||||
return ( |
} |
||||||
<tr> |
|
||||||
<td align="center" colSpan="3">{t('noData')}</td> |
return ( |
||||||
</tr> |
<div> |
||||||
) |
<NotificationContainer /> |
||||||
} |
<SweetAlert |
||||||
} |
show={alertDelete} |
||||||
|
warning |
||||||
return ( |
showCancel |
||||||
<div> |
confirmBtnText="Delete" |
||||||
<NotificationContainer /> |
confirmBtnBsStyle="danger" |
||||||
<SweetAlert |
title={t('deleteConfirm')} |
||||||
show={alertDelete} |
onConfirm={onConfirmDelete} |
||||||
warning |
onCancel={() => cancelDelete()} |
||||||
showCancel |
focusCancelBtn |
||||||
confirmBtnText="Delete" |
> |
||||||
confirmBtnBsStyle="danger" |
{t('deleteMsg')} |
||||||
title={t('deleteConfirm')} |
</SweetAlert> |
||||||
onConfirm={onConfirmDelete} |
<DialogForm |
||||||
onCancel={() => cancelDelete()} |
openDialog={openDialog} |
||||||
focusCancelBtn |
closeDialog={handleCloseDialog} |
||||||
> |
toggleDialog={() => toggleAddDialog} |
||||||
{t('deleteMsg')} |
typeDialog={typeDialog} |
||||||
</SweetAlert> |
dataEdit={dataEdit} |
||||||
<DialogForm |
clickOpenModal={clickOpenModal} |
||||||
openDialog={openDialog} |
dataParent={allDataMenu} |
||||||
closeDialog={handleCloseDialog} |
/> |
||||||
toggleDialog={() => toggleAddDialog} |
<Card> |
||||||
typeDialog={typeDialog} |
<CardHeader style={{ display: "flex", justifyContent: "space-between" }}> |
||||||
dataEdit={dataEdit} |
<h4 className="capitalize">{pageName}</h4> |
||||||
clickOpenModal={clickOpenModal} |
<Row> |
||||||
dataParent={allDataMenu} |
<Col> |
||||||
/> |
<Input onChange={handleSearch} value={search} type="text" name="search" id="search" placeholder={t('searchUom')} /> |
||||||
<Card> |
</Col> |
||||||
<CardHeader style={{ display: "flex", justifyContent: "space-between" }}> |
<Col> |
||||||
<h4 className="capitalize">{pageName}</h4> |
<Tooltip title={t('uomAdd')}> |
||||||
<Row> |
<Button style={{ background: "#4caf50", color: "#fff" }} onClick={() => handleOpenDialog('Save')}><i className="fa fa-plus"></i></Button> |
||||||
<Col> |
</Tooltip> |
||||||
<Input onChange={handleSearch} value={search} type="text" name="search" id="search" placeholder={t('searchUom')} /> |
<Tooltip title={t('exportExcel')}> |
||||||
</Col> |
<Button style={{ marginLeft: "5px" }} color="primary" onClick={() => handleExportExcel()}><i className="fa fa-print"></i></Button> |
||||||
<Col> |
</Tooltip> |
||||||
<Tooltip title={t('uomAdd')}> |
</Col> |
||||||
<Button style={{ background: "#4caf50", color: "#fff" }} onClick={() => handleOpenDialog('Save')}><i className="fa fa-plus"></i></Button> |
</Row> |
||||||
</Tooltip> |
</CardHeader> |
||||||
<Tooltip title={t('exportExcel')}> |
<CardBody> |
||||||
<Button style={{ marginLeft: "5px" }} color="primary" onClick={() => handleExportExcel()}><i className="fa fa-print"></i></Button> |
<Table responsive striped hover> |
||||||
</Tooltip> |
<thead> |
||||||
</Col> |
<tr> |
||||||
</Row> |
<th>{t('action')}</th> |
||||||
</CardHeader> |
{column.map((i, index) => { |
||||||
<CardBody> |
return ( |
||||||
<Table responsive striped hover> |
<th key={index} scope="row">{i.name}</th> |
||||||
<thead> |
) |
||||||
<tr> |
})} |
||||||
<th>{t('action')}</th> |
</tr> |
||||||
{column.map((i, index) => { |
</thead> |
||||||
return ( |
<tbody> |
||||||
<th key={index} scope="row">{i.name}</th> |
{dataNotAvailable()} |
||||||
) |
{dataTable.map((n, index) => { |
||||||
})} |
return ( |
||||||
</tr> |
<tr key={n.id}> |
||||||
</thead> |
<td className='nowrap'> |
||||||
<tbody> |
|
||||||
{dataNotAvailable()} |
<Tooltip title={t('delete')}> |
||||||
{dataTable.map((n, index) => { |
<i id="TooltipDelete" className="fa fa-trash" style={{ color: 'red', marginRight: 10, cursor: "pointer" }} onClick={() => handleDelete(n.id)}></i> |
||||||
return ( |
</Tooltip> |
||||||
<tr key={n.id}> |
|
||||||
<td className='nowrap'> |
<Tooltip title={t('ubah')}> |
||||||
|
<i id="TooltipEdit" className="fa fa-edit" style={{ color: 'green', cursor: "pointer" }} onClick={() => handleEdit(n)}></i> |
||||||
<Tooltip title={t('delete')}> |
</Tooltip> |
||||||
<i id="TooltipDelete" className="fa fa-trash" style={{ color: 'red', marginRight: 10, cursor: "pointer" }} onClick={() => handleDelete(n.id)}></i> |
</td> |
||||||
</Tooltip> |
<td>{n.name}</td> |
||||||
|
<td>{n.description}</td> |
||||||
<Tooltip title={t('ubah')}> |
</tr> |
||||||
<i id="TooltipEdit" className="fa fa-edit" style={{ color: 'green', cursor: "pointer" }} onClick={() => handleEdit(n)}></i> |
) |
||||||
</Tooltip> |
})} |
||||||
</td> |
</tbody> |
||||||
<td>{n.name}</td> |
</Table> |
||||||
<td>{n.description}</td> |
<Pagination |
||||||
</tr> |
showSizeChanger |
||||||
) |
onShowSizeChange={onShowSizeChange} |
||||||
})} |
onChange={onPagination} |
||||||
</tbody> |
defaultCurrent={currentPage} |
||||||
</Table> |
pageSize={rowsPerPage} |
||||||
<Pagination |
total={totalPage} |
||||||
showSizeChanger |
pageSizeOptions={["10", "15", "20", "25", "30", "35", "40"]} |
||||||
onShowSizeChange={onShowSizeChange} |
/> |
||||||
onChange={onPagination} |
</CardBody> |
||||||
defaultCurrent={currentPage} |
</Card> |
||||||
pageSize={rowsPerPage} |
</div> |
||||||
total={totalPage} |
) |
||||||
pageSizeOptions={["10", "15", "20", "25", "30", "35", "40"]} |
} |
||||||
/> |
|
||||||
</CardBody> |
export default Satuan; |
||||||
</Card> |
|
||||||
</div> |
|
||||||
) |
|
||||||
} |
|
||||||
|
|
||||||
export default Satuan; |
|
||||||
|
Loading…
Reference in new issue