This commit is contained in:
Андрей Дувакин 2024-10-05 11:36:04 +05:00
parent bd71225a23
commit 5aa92b7ec0
2 changed files with 50 additions and 1 deletions

View File

@ -50,6 +50,7 @@ export const getAccessories = async () => {
export const createAccessory = async (accessoryData) => { export const createAccessory = async (accessoryData) => {
try { try {
console.log(accessoryData);
const response = await axios.post(`${API_URL}/accessories`, accessoryData, { const response = await axios.post(`${API_URL}/accessories`, accessoryData, {
headers: { headers: {
Authorization: `Bearer ${getAuthToken()}`, Authorization: `Bearer ${getAuthToken()}`,
@ -95,3 +96,18 @@ export const deleteAccessory = async (id) => {
throw error; throw error;
} }
}; };
export const getCities = async () => {
try {
const response = await axios.get(`${API_URL}/cities`, {
headers: {
Authorization: `Bearer ${getAuthToken()}`,
Accept: "application/json",
},
});
return response.data;
} catch (error) {
console.log("Ошибка при получении городов:", error);
throw error;
}
};

View File

@ -4,10 +4,12 @@ import {
createAccessory, createAccessory,
updateAccessory, updateAccessory,
deleteAccessory, deleteAccessory,
getCities,
} from "../api.jsx"; } from "../api.jsx";
const Accessories = () => { const Accessories = () => {
const [accessories, setAccessories] = useState([]); const [accessories, setAccessories] = useState([]);
const [cities, setCities] = useState([]);
const [newAccessory, setNewAccessory] = useState({ const [newAccessory, setNewAccessory] = useState({
name: "", name: "",
volume: "", volume: "",
@ -20,17 +22,28 @@ const Accessories = () => {
useEffect(() => { useEffect(() => {
fetchAccessories(); fetchAccessories();
fetchCities();
}, []); }, []);
const fetchAccessories = async () => { const fetchAccessories = async () => {
try { try {
const data = await getAccessories(); const data = await getAccessories();
console.log(data);
setAccessories(data); setAccessories(data);
} catch (error) { } catch (error) {
console.error("Ошибка при загрузке аксессуаров:", error); console.error("Ошибка при загрузке аксессуаров:", error);
} }
}; };
const fetchCities = async () => {
try {
const data = await getCities();
setCities(data);
} catch (error) {
console.error("Ошибка при загрузке городов:", error);
}
};
const handleInputChange = (e) => { const handleInputChange = (e) => {
const { name, value } = e.target; const { name, value } = e.target;
setNewAccessory({ ...newAccessory, [name]: value }); setNewAccessory({ ...newAccessory, [name]: value });
@ -43,7 +56,8 @@ const Accessories = () => {
!newAccessory.name || !newAccessory.name ||
!newAccessory.volume || !newAccessory.volume ||
!newAccessory.weight || !newAccessory.weight ||
!newAccessory.period !newAccessory.period ||
!newAccessory.city_id
) { ) {
setError("Пожалуйста, заполните все поля."); setError("Пожалуйста, заполните все поля.");
return; return;
@ -145,6 +159,23 @@ const Accessories = () => {
onChange={handleInputChange} onChange={handleInputChange}
/> />
</div> </div>
<div className="form-group">
<label htmlFor="citySelect">Город</label>
<select
className="form-control"
id="citySelect"
name="city_id"
value={newAccessory.city_id}
onChange={handleInputChange}
>
<option value="">Выберите город</option>
{cities.map((city) => (
<option key={city.id} value={city.id}>
{city.name}
</option>
))}
</select>
</div>
<div className="btn-group"> <div className="btn-group">
<button type="submit" className="btn btn-primary"> <button type="submit" className="btn btn-primary">
{editingAccessoryId ? "Обновить" : "Создать"} {editingAccessoryId ? "Обновить" : "Создать"}
@ -172,6 +203,7 @@ const Accessories = () => {
<th>Объем</th> <th>Объем</th>
<th>Вес</th> <th>Вес</th>
<th>Период</th> <th>Период</th>
<th>Город</th>
<th>Действия</th> <th>Действия</th>
</tr> </tr>
</thead> </thead>
@ -183,6 +215,7 @@ const Accessories = () => {
<td>{accessory.volume}</td> <td>{accessory.volume}</td>
<td>{accessory.weight}</td> <td>{accessory.weight}</td>
<td>{accessory.period}</td> <td>{accessory.period}</td>
<td>{accessory.city_name}</td>{" "}
<td> <td>
<div className="btn-group"> <div className="btn-group">
<button <button