flask-contacts/frontend/src/App.jsx

122 lines
3.1 KiB
React
Raw Normal View History

2021-12-15 06:57:35 +00:00
import * as React from 'react';
import Grid from '@mui/material/Grid';
import { Container, Dialog, Typography, IconButton, Tooltip } from '@mui/material';
import MUIDataTable from "mui-datatables";
import { useAxios } from './axios';
import { useMutation, useQuery, useQueryClient } from 'react-query';
2022-10-30 11:29:52 +00:00
import StudentDialog from './StudentDialog';
2021-12-15 06:57:35 +00:00
import AddIcon from '@mui/icons-material/Add';
function App() {
const client = useAxios();
const queryClient = useQueryClient()
const [open, setOpen] = React.useState(false);
2022-10-30 11:29:52 +00:00
const [openedStudent, setOpenedStudent] = React.useState({});
const { data, status } = useQuery('students', async () => {
const {data} = await client.get('/api/students');
return data.map(({
2021-12-15 06:57:35 +00:00
uid,
first_name,
last_name,
middle_name,
2022-10-30 11:29:52 +00:00
discipline,
mark,
2021-12-15 06:57:35 +00:00
}) => {
return {
'first_name': first_name,
'last_name': last_name,
'middle_name': middle_name,
2022-10-30 11:29:52 +00:00
'discipline': discipline,
'mark': mark,
2021-12-15 06:57:35 +00:00
'display_name': `${first_name} ${last_name} ${middle_name}`,
'uid': uid
};
})
});
const columns = [
{
name: "uid", label: "ID", options: {
display: false
}
},
{
name: "display_name", label: "Имя"
},
{
2022-10-30 11:29:52 +00:00
name: "mark", label: "Оценка"
2021-12-15 06:57:35 +00:00
},
{
2022-10-30 11:29:52 +00:00
name: "discipline", label: "Дисциплина"
2021-12-15 06:57:35 +00:00
},
];
const commitDelete = useMutation(async (uid) => {
2022-10-30 11:29:52 +00:00
await client.delete(`/api/students/${uid}`);
2021-12-15 06:57:35 +00:00
}, {
onSuccess: () => {
2022-10-30 11:29:52 +00:00
queryClient.invalidateQueries('students');
2021-12-15 06:57:35 +00:00
}
});
const handleRowClick = (rowData) => {
2022-10-30 11:29:52 +00:00
setOpenedStudent(data.find(({uid}) => uid === rowData[0]));
2021-12-15 06:57:35 +00:00
setOpen(true);
console.log(data.find(({uid}) => uid === rowData[0]));
};
const handleRowsDelete = (rowsDeleted) => {
const uids = rowsDeleted.data.map(({dataIndex}) => data[dataIndex]['uid']);
for (const uid of uids) {
commitDelete.mutate(uid);
}
};
const handleClose = () => {
setOpen(false);
};
if (data === undefined || status === 'loading') {
return <div>Loading...</div>
}
return (
<Container maxWidth="md">
<MUIDataTable
2022-10-30 11:29:52 +00:00
title={"Зачётная книжка"}
2021-12-15 06:57:35 +00:00
data={data}
columns={columns}
options={{
filterType: "textField",
print: false,
rowsPerPage: 10,
rowsPerPageOptions: [10, 20, 50, 100],
onRowClick: handleRowClick,
customToolbar: () => {
return <CustomToolbar />
},
onRowsDelete: handleRowsDelete
}}
/>
2022-10-30 11:29:52 +00:00
<StudentDialog
2021-12-15 06:57:35 +00:00
open={open}
onClose={handleClose}
2022-10-30 11:29:52 +00:00
student={openedStudent}
2021-12-15 06:57:35 +00:00
/>
</Container>
);
function CustomToolbar() {
if (data !== undefined) return (
2022-10-30 11:29:52 +00:00
<Tooltip title='Новая запись'>
2021-12-15 06:57:35 +00:00
<IconButton onClick={() => {
2022-10-30 11:29:52 +00:00
setOpenedStudent({});
2021-12-15 06:57:35 +00:00
setOpen(true);
}}>
<AddIcon />
</IconButton>
</Tooltip>
)
return (<></>)
}
}
export default App;