import { Box, Button, ButtonGroup, Stack } from '@mui/material'; import { useEffect, useState } from 'react'; import { AgGridReact } from 'ag-grid-react'; import useAxiosPrivate from '../../utils/useAxiosPrivate'; import EditNoteRoundedIcon from '@mui/icons-material/EditNoteRounded'; import DeleteSweepRoundedIcon from '@mui/icons-material/DeleteSweepRounded'; // import ImportExportRoundedIcon from '@mui/icons-material/ImportExportRounded'; import AddCircleOutlineRoundedIcon from '@mui/icons-material/AddCircleOutlineRounded'; import Popup from './Popup'; //import InjuryClassForm from './InjuryClassForm'; import InjuryPartForm from './InjuryPartForm'; import { useFormik } from "formik"; //import { InjuryPartValidationForm } from './Validationform'; // import axios from 'axios'; import PropTypes from "prop-types"; import { ToastContainer, toast } from 'react-toastify'; import 'react-toastify/dist/ReactToastify.css'; import PictureAsPdfIcon from '@mui/icons-material/PictureAsPdf'; import DownloadIcon from '@mui/icons-material/Download'; import ExcelJS from 'exceljs'; import jsPDF from 'jspdf'; import 'jspdf-autotable'; import * as Yup from 'yup'; const InjuryPartValidationForm = Yup.object({ name: Yup.string().min(2).max(25).required("Please enter Injury Name"), description: Yup.string().min(2).max(25).required("Please enter Injury Discription"), code: Yup.string().min(2).max(25).required("Please enter Bussiness Injury Code"), }); const InjuryPartList = () => { const [rowData, setRowData] = useState([]); const [colDefs, setColDefs] = useState([]); const [openPopup, setOpenPopup] = useState(false); const axiosClientPrivate = useAxiosPrivate(); const [id,setId] = useState(1); const [showupdate,setShowupdate] = useState(false); const [fetchTrigger, setFetchTrigger] = useState(0) const initialValues = { name : "", description: "", code: "" }; const { values, touched, errors, handleBlur, handleChange, setFieldValue, handleSubmit, resetForm, } = useFormik({ initialValues: initialValues, validationSchema: InjuryPartValidationForm, // onSubmit: (values, action) => { // console.log(values); // action.resetForm(); // }, onSubmit: async (values, {resetForm}) => { console.log(values); try { const response = await axiosClientPrivate.post('/injury-parts', values); toast.success("Saved Successfully!",{ position:"top-center" }); // getting id(key,value) of last index // const id = rowData[rowData.length-1].id; // const obj = { // id : id+1, // ...values // } // console.log(obj); // setRowData(rowData => [...rowData, obj]); console.log('Response:', response.data); setFetchTrigger(prev => prev+1); resetForm(); } catch (error) { console.log(values); console.error('Error:', error); } }, }); // to delete a row const handleDeleteRow = async (id) => { alert(id) if(window.confirm('Are you sure you want to delete this data?')){ try { await axiosClientPrivate.delete(`/injury-parts/${id}`); setFetchTrigger(prev => prev+1); // setRowData(prevData => prevData.filter(row => row.id !== id)); } catch (error) { console.error('Error deleting row:', error); } } }; const CustomActionComponent = ({id}) => { CustomActionComponent.propTypes = { id: PropTypes.number.isRequired, }; return
}; const pagination = true; const paginationPageSize = 50; const paginationPageSizeSelector = [50, 100, 200, 500]; useEffect(() => { const controller = new AbortController(); const getAllOhc = async () => { try { const response = await axiosClientPrivate.get('http://localhost:8080/injury-parts?page=0&size=20', { signal: controller.signal }); const items = response.data.content; // console.log(items); if (items.length > 0) { const headerMappings = { name: "Name", description : "Description", code : "Code", }; const columns = Object.keys(items[0]).map(key => ({ field: key, headerName: headerMappings[key] || key.charAt(0).toUpperCase() + key.slice(1), filter: true, floatingFilter: true, sortable: true, width: key === 'id' ? 100 : undefined, })); columns.unshift({ field: "Actions", cellRenderer: (params) =>{ const id = params.data.id; return } }); setColDefs(columns); } setRowData(items); } catch (err) { console.error("Failed to fetch data: ", err); setRowData([]); } }; getAllOhc(); return () => { controller.abort(); }; }, [fetchTrigger]); const handleEdit = async (id) => { alert(id); try { const response = await axiosClientPrivate.get(`/injury-parts/${id}`); console.log(response.data); setFieldValue("id",response.data.id); setFieldValue("name",response.data.name); setFieldValue("description",response.data.description); setFieldValue("code",response.data.code); setFieldValue("lastModified", response.data.lastModified); setFieldValue("modifiedBy", response.data.modifiedBy); setId(id); setShowupdate(true); setOpenPopup(true); } catch (error) { console.error('Error fetching item for edit:', error); } }; const handleUpdate = async (id)=> { alert(id); console.log(values); const update = values; try{ // console.log(values); await axiosClientPrivate.put(`/injury-parts/${id}`,update); toast.success("Updated Successfully!",{ position:"top-center", autoClose: 3000, }); resetForm(); setFetchTrigger(prev => prev+1); // setRowData(rowData => [...rowData,values]); } catch(err){ console.log("after:- ",values); console.log(err); } } const exportpdf = async () => { const doc = new jsPDF(); const header = [["Id","Injury Part",'Injury Part Desc',"Injury Part Desc"]]; const tableData = rowData.map(item => [ item.id, item.name, item.description, item.code, ]); doc.autoTable({ head: header, body: tableData, startY: 20, theme: 'grid', margin: { top: 30 }, styles: { fontSize: 5 }, columnStyles: { 0: { cellWidth: 'auto' }, 1: { cellWidth: 'auto' } } }); doc.save("InjuryClassificationList.pdf"); }; const exportExcelfile = async () => { const workbook = new ExcelJS.Workbook(); const sheet = workbook.addWorksheet('My Sheet'); const headerStyle = { alignment: { horizontal: 'center' } }; sheet.getRow(1).font = { bold: true }; const columnWidths = { id: 10, name: 20, description: 20, code: 20, }; sheet.columns = [ { header: "Id", key: 'id', width: columnWidths.id, style: headerStyle }, { header: "Injury Part", key: 'name', width: columnWidths.name, style: headerStyle }, { header: "Injury Part Desc", key: 'description', width: columnWidths.description, style: headerStyle }, { header: "Injury Part Code", key: 'code', width: columnWidths.code, style: headerStyle }, ]; rowData.map(product =>{ sheet.addRow({ id: product.id, name: product.name, description: product.description, code: product.code, }) }); workbook.xlsx.writeBuffer().then(data => { const blob = new Blob([data], { type: "application/vnd.openxmlformats-officedocument.spreadsheet.sheet", }); const url = window.URL.createObjectURL(blob); const anchor = document.createElement('a'); anchor.href = url; anchor.download = 'InjuryClassificationList.xlsx'; anchor.click(); }) } return ( <> ); }; export default InjuryPartList;