Работает отображение предметов

This commit is contained in:
Никита Потапов 2024-01-11 16:25:30 +04:00
parent 30fb4d3e6a
commit 1b843b3810
4 changed files with 73 additions and 1 deletions

View File

@ -0,0 +1,16 @@
import { PropTypes } from "prop-types";
const GroupTableRow = ({ group }) => {
return (
<tr>
<th scope='row'>{group.id}</th>
<td>{group.name}</td>
</tr>
);
};
GroupTableRow.propTypes = {
group: PropTypes.object
};
export default GroupTableRow;

View File

@ -0,0 +1,16 @@
import { PropTypes } from "prop-types";
const SubjectTableRow = ({ subject }) => {
return (
<tr>
<th scope='row'>{subject.id}</th>
<td>{subject.name}</td>
</tr>
);
};
SubjectTableRow.propTypes = {
subject: PropTypes.object
};
export default SubjectTableRow;

22
src/hooks/SubjectsHook.js Normal file
View File

@ -0,0 +1,22 @@
import { useEffect, useState } from "react";
import SubjectsApiService from "../services/SubjectsApiService";
export const useSubjects = () => {
const [subjectsRefresh, setSubjectsRefresh] = useState(false);
const [subjects, setSubjects] = useState([]);
const handleSubjectsRefresh = () => setSubjectsRefresh(!subjectsRefresh);
const getSubjects = async () => {
const data = await SubjectsApiService.getAll();
setSubjects(data ?? []);
};
useEffect(() => {
getSubjects();
}, [subjectsRefresh]);
return {
subjects,
handleSubjectsRefresh,
};
};

View File

@ -1,11 +1,29 @@
import { useSubjects } from "../../hooks/SubjectsHook";
import { useOnlyTeachers } from "../../hooks/UserHooks";
import { Container } from "react-bootstrap";
import SubjectTableRow from "../../components/subjecttablerow/subjecttablerow";
const SubjectsPage = () => {
useOnlyTeachers();
const { subjects, handleSubjectsChange } = useSubjects();
return (
<>
<h5>SubjectsPage</h5>
<Container>
<table className="table">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Название</th>
</tr>
</thead>
<tbody>
{
subjects.map((subject, index) => <SubjectTableRow key={index} subject={subject} />)
}
</tbody>
</table>
</Container>
</>
);
};