сделал добавление пациента
This commit is contained in:
parent
44c092ecb9
commit
defe869e3e
18
web-app/src/api/patients/AddPatient.jsx
Normal file
18
web-app/src/api/patients/AddPatient.jsx
Normal 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;
|
||||||
@ -1,155 +1,166 @@
|
|||||||
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;
|
||||||
|
|
||||||
const PatientsPage = () => {
|
const PatientsPage = () => {
|
||||||
const {user} = useAuth();
|
const {user} = useAuth();
|
||||||
const [searchText, setSearchText] = useState("");
|
const [searchText, setSearchText] = useState("");
|
||||||
const [sortOrder, setSortOrder] = useState("asc");
|
const [sortOrder, setSortOrder] = useState("asc");
|
||||||
const [patients, setPatients] = useState([]);
|
const [patients, setPatients] = useState([]);
|
||||||
const [error, setError] = useState(null);
|
const [error, setError] = useState(null);
|
||||||
|
|
||||||
const [current, setCurrent] = useState(1);
|
const [current, setCurrent] = useState(1);
|
||||||
const [pageSize, setPageSize] = useState(10);
|
const [pageSize, setPageSize] = useState(10);
|
||||||
|
|
||||||
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) {
|
||||||
const intervalId = setInterval(fetchPatients, 5000);
|
const intervalId = setInterval(fetchPatients, 5000);
|
||||||
return () => clearInterval(intervalId);
|
return () => clearInterval(intervalId);
|
||||||
}
|
}
|
||||||
}, [user, isModalVisible]);
|
}, [user, isModalVisible]);
|
||||||
|
|
||||||
|
|
||||||
const fetchPatients = async () => {
|
const fetchPatients = async () => {
|
||||||
if (!user || !user.token) return;
|
if (!user || !user.token) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const data = await getAllPatients(user.token);
|
||||||
|
setPatients(data);
|
||||||
|
} catch (err) {
|
||||||
|
setError(err.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (loading) {
|
||||||
|
setLoading(false)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const filteredPatients = patients
|
||||||
|
.filter((patient) => `${patient.first_name} ${patient.last_name}`.toLowerCase().includes(searchText.toLowerCase()))
|
||||||
|
.sort((a, b) => {
|
||||||
|
const fullNameA = `${a.last_name} ${a.first_name}`;
|
||||||
|
const fullNameB = `${b.last_name} ${b.first_name}`;
|
||||||
|
return sortOrder === "asc" ? fullNameA.localeCompare(fullNameB) : fullNameB.localeCompare(fullNameA);
|
||||||
|
});
|
||||||
|
|
||||||
|
const handleAddPatient = () => {
|
||||||
|
setSelectedPatient(null);
|
||||||
|
setIsModalVisible(true);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleEditPatient = (patient) => {
|
||||||
|
setSelectedPatient(patient);
|
||||||
|
setIsModalVisible(true);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleCancel = () => {
|
||||||
|
setIsModalVisible(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSubmit = async (newPatient) => {
|
||||||
|
if (selectedPatient) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const data = await getAllPatients(user.token);
|
await updatePatient(user.token, selectedPatient.id, newPatient);
|
||||||
setPatients(data);
|
} catch (error) {
|
||||||
} catch (err) {
|
if (error.response?.status === 401) {
|
||||||
setError(err.message);
|
throw new Error("Ошибка авторизации: пользователь неяден или токен недействителен");
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const filteredPatients = patients
|
|
||||||
.filter((patient) =>
|
|
||||||
`${patient.first_name} ${patient.last_name}`.toLowerCase().includes(searchText.toLowerCase())
|
|
||||||
)
|
|
||||||
.sort((a, b) => {
|
|
||||||
const fullNameA = `${a.last_name} ${a.first_name}`;
|
|
||||||
const fullNameB = `${b.last_name} ${b.first_name}`;
|
|
||||||
return sortOrder === "asc"
|
|
||||||
? fullNameA.localeCompare(fullNameB)
|
|
||||||
: fullNameB.localeCompare(fullNameA);
|
|
||||||
});
|
|
||||||
|
|
||||||
const handleAddPatient = () => {
|
|
||||||
setSelectedPatient(null);
|
|
||||||
setIsModalVisible(true);
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleEditPatient = (patient) => {
|
|
||||||
setSelectedPatient(patient);
|
|
||||||
setIsModalVisible(true);
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleCancel = () => {
|
|
||||||
setIsModalVisible(false);
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleSubmit = async (newPatient) => {
|
|
||||||
if (selectedPatient) {
|
|
||||||
|
|
||||||
try {
|
|
||||||
await updatePatient(user.token, selectedPatient.id, newPatient);
|
|
||||||
} catch (error) {
|
|
||||||
if (error.response?.status === 401) {
|
|
||||||
throw new Error("Ошибка авторизации: пользователь неяден или токен недействителен");
|
|
||||||
}
|
|
||||||
throw new Error(error.message);
|
|
||||||
}
|
}
|
||||||
|
throw new Error(error.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
if (!selectedPatient) {
|
|
||||||
setPatients((prevPatients) => [...prevPatients, {id: Date.now(), ...newPatient}]);
|
if (!selectedPatient) {
|
||||||
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);
|
}
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
setIsModalVisible(false);
|
||||||
<div style={{padding: 20}}>
|
};
|
||||||
<Row gutter={[16, 16]} style={{marginBottom: 20}}>
|
|
||||||
<Col xs={24} sm={16}>
|
|
||||||
<Input
|
|
||||||
placeholder="Поиск пациента"
|
|
||||||
onChange={(e) => setSearchText(e.target.value)}
|
|
||||||
style={{width: "100%"}}
|
|
||||||
/>
|
|
||||||
</Col>
|
|
||||||
<Col xs={24} sm={8}>
|
|
||||||
<Select
|
|
||||||
value={sortOrder}
|
|
||||||
onChange={(value) => setSortOrder(value)}
|
|
||||||
style={{width: "100%"}}
|
|
||||||
>
|
|
||||||
<Option value="asc">А-Я</Option>
|
|
||||||
<Option value="desc">Я-А</Option>
|
|
||||||
</Select>
|
|
||||||
</Col>
|
|
||||||
</Row>
|
|
||||||
|
|
||||||
<List
|
return (<div style={{padding: 20}}>
|
||||||
grid={{gutter: 16, column: 1}}
|
<Row gutter={[16, 16]} style={{marginBottom: 20}}>
|
||||||
dataSource={filteredPatients}
|
<Col xs={24} sm={16}>
|
||||||
renderItem={(patient) => (
|
<Input
|
||||||
<List.Item
|
placeholder="Поиск пациента"
|
||||||
onClick={() => {
|
onChange={(e) => setSearchText(e.target.value)}
|
||||||
handleEditPatient(patient);
|
style={{width: "100%"}}
|
||||||
}}
|
/>
|
||||||
>
|
</Col>
|
||||||
<PatientListCard patient={patient}/>
|
<Col xs={24} sm={8}>
|
||||||
</List.Item>
|
<Select
|
||||||
)}
|
value={sortOrder}
|
||||||
pagination={{
|
onChange={(value) => setSortOrder(value)}
|
||||||
current,
|
style={{width: "100%"}}
|
||||||
pageSize,
|
>
|
||||||
showSizeChanger: true,
|
<Option value="asc">А-Я</Option>
|
||||||
pageSizeOptions: ["5", "10", "20", "50"],
|
<Option value="desc">Я-А</Option>
|
||||||
onChange: (page, newPageSize) => {
|
</Select>
|
||||||
setCurrent(page);
|
</Col>
|
||||||
setPageSize(newPageSize);
|
</Row>
|
||||||
},
|
|
||||||
|
{loading ? (
|
||||||
|
<Spin indicator={<LoadingOutlined style={{fontSize: 48}} spin/>}/>
|
||||||
|
) : (
|
||||||
|
<List
|
||||||
|
grid={{gutter: 16, column: 1}}
|
||||||
|
dataSource={filteredPatients}
|
||||||
|
renderItem={(patient) => (<List.Item
|
||||||
|
onClick={() => {
|
||||||
|
handleEditPatient(patient);
|
||||||
}}
|
}}
|
||||||
/>
|
>
|
||||||
|
<PatientListCard patient={patient}/>
|
||||||
|
</List.Item>)}
|
||||||
|
pagination={{
|
||||||
|
current,
|
||||||
|
pageSize,
|
||||||
|
showSizeChanger: true,
|
||||||
|
pageSizeOptions: ["5", "10", "20", "50"],
|
||||||
|
onChange: (page, newPageSize) => {
|
||||||
|
setCurrent(page);
|
||||||
|
setPageSize(newPageSize);
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
<FloatButton
|
|
||||||
icon={<PlusOutlined/>}
|
|
||||||
style={{position: "fixed", bottom: 20, right: 20}}
|
|
||||||
onClick={handleAddPatient}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<PatientModal
|
<FloatButton
|
||||||
visible={isModalVisible}
|
icon={<PlusOutlined/>}
|
||||||
onCancel={handleCancel}
|
style={{position: "fixed", bottom: 20, right: 20}}
|
||||||
onSubmit={handleSubmit}
|
onClick={handleAddPatient}
|
||||||
patient={selectedPatient}
|
/>
|
||||||
/>
|
|
||||||
</div>
|
<PatientModal
|
||||||
);
|
visible={isModalVisible}
|
||||||
}
|
onCancel={handleCancel}
|
||||||
;
|
onSubmit={handleSubmit}
|
||||||
|
patient={selectedPatient}
|
||||||
|
/>
|
||||||
|
</div>);
|
||||||
|
};
|
||||||
|
|
||||||
export default PatientsPage;
|
export default PatientsPage;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user