173 lines
5.5 KiB
Plaintext
173 lines
5.5 KiB
Plaintext
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 MedicalForm from './MedicalForm';
|
|
import { medicalform } from './Validationform';
|
|
import { useFormik } from "formik";
|
|
// import { WidthFull } from '@mui/icons-material';
|
|
import PropTypes from "prop-types";
|
|
|
|
|
|
const MedicalList = () => {
|
|
|
|
const initialValues = {
|
|
|
|
medicalname: "",
|
|
medicalcode: "",
|
|
QTY: ""
|
|
};
|
|
|
|
const axiosClientPrivate = useAxiosPrivate();
|
|
|
|
const {
|
|
values,
|
|
touched,
|
|
errors,
|
|
handleBlur,
|
|
handleChange,
|
|
setFieldValue,
|
|
handleSubmit,
|
|
resetForm
|
|
} = useFormik({
|
|
initialValues: initialValues,
|
|
validationSchema: medicalform,
|
|
onSubmit: (values, action) => {
|
|
console.log(values);
|
|
action.resetForm();
|
|
},
|
|
// onSubmit: async (values, {resetForm}) => {
|
|
// try {
|
|
// const response = await axiosClientPrivate.post('/ohcs', values);
|
|
// console.log('Response:', response.data);
|
|
// setFetchTrigger(prev => prev+1);
|
|
|
|
// resetForm();
|
|
// } catch (error) {
|
|
// console.log(values);
|
|
// console.error('Error:', error);
|
|
// }
|
|
// },
|
|
});
|
|
|
|
const handleDeleteRow = async (id) => {
|
|
alert(id)
|
|
try {
|
|
await axiosClientPrivate.delete(`/ohcs/${id}`);
|
|
|
|
const newData = rowData.filter(row => row.id !== id);
|
|
// setRowData(newData);
|
|
setFetchTrigger(prev => prev+1);
|
|
|
|
} catch (error) {
|
|
console.error('Error deleting row:', error);
|
|
}
|
|
};
|
|
|
|
const [rowData, setRowData] = useState([]);
|
|
|
|
const [colDefs, setColDefs] = useState([]);
|
|
|
|
const [openPopup, setOpenPopup] = useState(false);
|
|
|
|
const [fetchTrigger, setFetchTrigger] = useState(0);
|
|
|
|
|
|
const CustomActionComponent = ({id}) => {
|
|
CustomActionComponent.propTypes = {
|
|
id: PropTypes.number.isRequired,
|
|
};
|
|
return <div> <Button > <EditNoteRoundedIcon /></Button>
|
|
<Button color="error" onClick={() => handleDeleteRow(id)}> <DeleteSweepRoundedIcon /> </Button> </div>
|
|
|
|
};
|
|
|
|
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('ohcs', { signal: controller.signal });
|
|
const items = response.data;
|
|
|
|
|
|
if (items.length > 0) {
|
|
const columns = Object.keys(items[0]).map(key => ({
|
|
field: key,
|
|
headerName: key.charAt(0).toUpperCase() + key.slice(1),
|
|
filter: true,
|
|
floatingFilter: true,
|
|
sortable: true
|
|
}));
|
|
|
|
columns.push({
|
|
|
|
field: "Actions", cellRenderer: (params) =>{
|
|
const id = params.data.id;
|
|
return <CustomActionComponent id={id} />
|
|
}
|
|
});
|
|
|
|
setColDefs(columns);
|
|
}
|
|
|
|
setRowData(items);
|
|
|
|
} catch (err) {
|
|
console.error("Failed to fetch data: ", err);
|
|
setRowData([]);
|
|
}
|
|
};
|
|
|
|
getAllOhc();
|
|
|
|
return () => {
|
|
controller.abort();
|
|
};
|
|
|
|
}, [fetchTrigger]);
|
|
|
|
return (
|
|
<>
|
|
<Box
|
|
className="ag-theme-quartz"
|
|
style={{ height: 500 }}
|
|
>
|
|
|
|
<Stack sx={{ display: 'flex', flexDirection: 'row' }} marginY={1} paddingX={1}>
|
|
<ButtonGroup variant="contained" aria-label="Basic button group">
|
|
<Button variant="contained" endIcon={<AddCircleOutlineRoundedIcon />} onClick={() => { setOpenPopup(true) }}>Add New</Button>
|
|
<Button variant="contained" color="success" endIcon={<ImportExportRoundedIcon />}>Export Data</Button>
|
|
</ButtonGroup>
|
|
|
|
</Stack>
|
|
<AgGridReact
|
|
rowData={rowData}
|
|
columnDefs={colDefs}
|
|
animateRows={true}
|
|
pagination={pagination}
|
|
paginationPageSize={paginationPageSize}
|
|
paginationPageSizeSelector={paginationPageSizeSelector}
|
|
/>
|
|
</Box>
|
|
|
|
<Popup resetForm={resetForm} handleSubmit={handleSubmit} openPopup={openPopup} setOpenPopup={setOpenPopup} title="Medical Form ">
|
|
|
|
<MedicalForm values={values} touched={touched} errors={errors} handleBlur={handleBlur} handleChange={handleChange} setFieldValue={setFieldValue} handleSubmit={handleSubmit} />
|
|
|
|
</Popup>
|
|
</>
|
|
);
|
|
};
|
|
|
|
export default MedicalList;
|