сделал добавление пациента

This commit is contained in:
Андрей Дувакин 2025-02-12 20:51:46 +05:00
parent 44c092ecb9
commit defe869e3e
2 changed files with 155 additions and 126 deletions

View File

@ -0,0 +1,18 @@
import axios from "axios";
import CONFIG from "../../core/Config.jsx";
const AddPatient = async (token, patient) => {
try {
const response = await axios.post(`${CONFIG.BASE_URL}/patients/`, patient, {
headers: {
Authorization: `Bearer ${token}`,
},
});
return response.data;
} catch (error) {
throw new Error(error.response.data.message);
}
};
export default AddPatient;

View File

@ -1,11 +1,12 @@
import {useEffect, useState} from "react"; import {useEffect, useState} from "react";
import {Input, Select, List, FloatButton, Row, Col, message} from "antd"; import {Input, Select, List, FloatButton, Row, Col, message, Spin} from "antd";
import {PlusOutlined} from "@ant-design/icons"; import {LoadingOutlined, PlusOutlined} from "@ant-design/icons";
import {useAuth} from "../AuthContext.jsx"; import {useAuth} from "../AuthContext.jsx";
import getAllPatients from "../api/patients/GetAllPatients.jsx"; import getAllPatients from "../api/patients/GetAllPatients.jsx";
import PatientListCard from "../components/PatientListCard.jsx"; import PatientListCard from "../components/PatientListCard.jsx";
import PatientModal from "../components/PatientModal.jsx"; import PatientModal from "../components/PatientModal.jsx";
import updatePatient from "../api/patients/UpdatePatient.jsx"; // Подключаем модальное окно import updatePatient from "../api/patients/UpdatePatient.jsx";
import addPatient from "../api/patients/AddPatient.jsx"; // Подключаем модальное окно
const {Option} = Select; const {Option} = Select;
@ -21,6 +22,7 @@ const PatientsPage = () => {
const [isModalVisible, setIsModalVisible] = useState(false); const [isModalVisible, setIsModalVisible] = useState(false);
const [selectedPatient, setSelectedPatient] = useState(null); const [selectedPatient, setSelectedPatient] = useState(null);
const [loading, setLoading] = useState(true);
useEffect(() => { useEffect(() => {
if (!isModalVisible) { if (!isModalVisible) {
@ -39,18 +41,18 @@ const PatientsPage = () => {
} catch (err) { } catch (err) {
setError(err.message); setError(err.message);
} }
if (loading) {
setLoading(false)
}
}; };
const filteredPatients = patients const filteredPatients = patients
.filter((patient) => .filter((patient) => `${patient.first_name} ${patient.last_name}`.toLowerCase().includes(searchText.toLowerCase()))
`${patient.first_name} ${patient.last_name}`.toLowerCase().includes(searchText.toLowerCase())
)
.sort((a, b) => { .sort((a, b) => {
const fullNameA = `${a.last_name} ${a.first_name}`; const fullNameA = `${a.last_name} ${a.first_name}`;
const fullNameB = `${b.last_name} ${b.first_name}`; const fullNameB = `${b.last_name} ${b.first_name}`;
return sortOrder === "asc" return sortOrder === "asc" ? fullNameA.localeCompare(fullNameB) : fullNameB.localeCompare(fullNameA);
? fullNameA.localeCompare(fullNameB)
: fullNameB.localeCompare(fullNameA);
}); });
const handleAddPatient = () => { const handleAddPatient = () => {
@ -78,19 +80,27 @@ const PatientsPage = () => {
} }
throw new Error(error.message); throw new Error(error.message);
} }
} }
if (!selectedPatient) { if (!selectedPatient) {
setPatients((prevPatients) => [...prevPatients, {id: Date.now(), ...newPatient}]);
message.success("Пациент успешно добавлен!"); try {
await addPatient(user.token, newPatient);
} catch (error) {
if (error.response?.status === 401) {
throw new Error("Ошибка авторизации: пользователь неяден или токен недействителен");
}
throw new Error(error.message);
}
} }
setIsModalVisible(false); setIsModalVisible(false);
}; };
return ( return (<div style={{padding: 20}}>
<div style={{padding: 20}}>
<Row gutter={[16, 16]} style={{marginBottom: 20}}> <Row gutter={[16, 16]} style={{marginBottom: 20}}>
<Col xs={24} sm={16}> <Col xs={24} sm={16}>
<Input <Input
@ -111,18 +121,19 @@ const PatientsPage = () => {
</Col> </Col>
</Row> </Row>
{loading ? (
<Spin indicator={<LoadingOutlined style={{fontSize: 48}} spin/>}/>
) : (
<List <List
grid={{gutter: 16, column: 1}} grid={{gutter: 16, column: 1}}
dataSource={filteredPatients} dataSource={filteredPatients}
renderItem={(patient) => ( renderItem={(patient) => (<List.Item
<List.Item
onClick={() => { onClick={() => {
handleEditPatient(patient); handleEditPatient(patient);
}} }}
> >
<PatientListCard patient={patient}/> <PatientListCard patient={patient}/>
</List.Item> </List.Item>)}
)}
pagination={{ pagination={{
current, current,
pageSize, pageSize,
@ -134,6 +145,8 @@ const PatientsPage = () => {
}, },
}} }}
/> />
)}
<FloatButton <FloatButton
icon={<PlusOutlined/>} icon={<PlusOutlined/>}
@ -147,9 +160,7 @@ const PatientsPage = () => {
onSubmit={handleSubmit} onSubmit={handleSubmit}
patient={selectedPatient} patient={selectedPatient}
/> />
</div> </div>);
); };
}
;
export default PatientsPage; export default PatientsPage;