._.
This commit is contained in:
parent
cc48e9667c
commit
514b773a90
@ -2,6 +2,7 @@ import React, { useState, useEffect } from "react";
|
|||||||
import SelectionDialog from "../components/SelectionDialog.jsx";
|
import SelectionDialog from "../components/SelectionDialog.jsx";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
import { getAuthToken } from "../api.jsx";
|
import { getAuthToken } from "../api.jsx";
|
||||||
|
import { getCoordinates } from "../geocoder.jsx";
|
||||||
import {
|
import {
|
||||||
getCities,
|
getCities,
|
||||||
createCity,
|
createCity,
|
||||||
@ -22,6 +23,8 @@ const Cities = () => {
|
|||||||
const [editingCityId, setEditingCityId] = useState(null);
|
const [editingCityId, setEditingCityId] = useState(null);
|
||||||
const [error, setError] = useState(null);
|
const [error, setError] = useState(null);
|
||||||
const [showDistrictDialog, setShowDistrictDialog] = useState(false);
|
const [showDistrictDialog, setShowDistrictDialog] = useState(false);
|
||||||
|
const [isGeocodeAvailable, setIsGeocodeAvailable] = useState(false);
|
||||||
|
const [isFetchingCoordinates, setIsFetchingCoordinates] = useState(false);
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -50,6 +53,28 @@ const Cities = () => {
|
|||||||
const handleInputChange = (e) => {
|
const handleInputChange = (e) => {
|
||||||
const { name, value } = e.target;
|
const { name, value } = e.target;
|
||||||
setNewCity({ ...newCity, [name]: value });
|
setNewCity({ ...newCity, [name]: value });
|
||||||
|
if (name === "name") {
|
||||||
|
setIsGeocodeAvailable(value.trim() !== "");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleGeocode = async () => {
|
||||||
|
try {
|
||||||
|
const { name } = newCity;
|
||||||
|
const coordinates = await getCoordinates(name);
|
||||||
|
if (coordinates) {
|
||||||
|
setNewCity({
|
||||||
|
...newCity,
|
||||||
|
x_coordinate: coordinates.latitude,
|
||||||
|
y_coordinate: coordinates.longitude,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
setError("Не удалось найти координаты для данного города.");
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Ошибка при получении координат:", error);
|
||||||
|
setError("Ошибка при попытке получить координаты.");
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSubmit = async (e) => {
|
const handleSubmit = async (e) => {
|
||||||
@ -86,6 +111,7 @@ const Cities = () => {
|
|||||||
x_coordinate: city.x_coordinate,
|
x_coordinate: city.x_coordinate,
|
||||||
y_coordinate: city.y_coordinate,
|
y_coordinate: city.y_coordinate,
|
||||||
});
|
});
|
||||||
|
setIsGeocodeAvailable(true);
|
||||||
setEditingCityId(city.id);
|
setEditingCityId(city.id);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -106,6 +132,7 @@ const Cities = () => {
|
|||||||
y_coordinate: "",
|
y_coordinate: "",
|
||||||
});
|
});
|
||||||
setEditingCityId(null);
|
setEditingCityId(null);
|
||||||
|
setIsGeocodeAvailable(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleDialogSelectDistrict = (selectedItem) => {
|
const handleDialogSelectDistrict = (selectedItem) => {
|
||||||
@ -138,32 +165,6 @@ const Cities = () => {
|
|||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="form-group">
|
|
||||||
<label htmlFor="xCoordinate">Координата X</label>
|
|
||||||
<input
|
|
||||||
type="text"
|
|
||||||
className="form-control"
|
|
||||||
id="xCoordinate"
|
|
||||||
name="x_coordinate"
|
|
||||||
placeholder="Введите координату X"
|
|
||||||
value={newCity.x_coordinate}
|
|
||||||
onChange={handleInputChange}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="form-group">
|
|
||||||
<label htmlFor="yCoordinate">Координата Y</label>
|
|
||||||
<input
|
|
||||||
type="text"
|
|
||||||
className="form-control"
|
|
||||||
id="yCoordinate"
|
|
||||||
name="y_coordinate"
|
|
||||||
placeholder="Введите координату Y"
|
|
||||||
value={newCity.y_coordinate}
|
|
||||||
onChange={handleInputChange}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="input-group mb-3">
|
<div className="input-group mb-3">
|
||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
@ -188,18 +189,52 @@ const Cities = () => {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="btn-group">
|
<div className="input-group mb-3">
|
||||||
<button type="submit" className="btn btn-primary">
|
<input
|
||||||
|
type="text"
|
||||||
|
className="form-control"
|
||||||
|
placeholder="X координата"
|
||||||
|
value={newCity.x_coordinate || ""}
|
||||||
|
readOnly
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="input-group mb-3">
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
className="form-control"
|
||||||
|
placeholder="Y координата"
|
||||||
|
value={newCity.y_coordinate || ""}
|
||||||
|
readOnly
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="btn-group mt-3">
|
||||||
|
<button
|
||||||
|
type="submit"
|
||||||
|
className="btn btn-primary"
|
||||||
|
disabled={isFetchingCoordinates}
|
||||||
|
>
|
||||||
{editingCityId ? "Обновить" : "Создать"}
|
{editingCityId ? "Обновить" : "Создать"}
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
className="btn btn-secondary"
|
className="btn btn-secondary"
|
||||||
onClick={resetForm}
|
onClick={resetForm}
|
||||||
|
disabled={isFetchingCoordinates}
|
||||||
>
|
>
|
||||||
Отменить
|
Отменить
|
||||||
</button>
|
</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="btn btn-info"
|
||||||
|
onClick={handleGeocode}
|
||||||
|
disabled={!isGeocodeAvailable || isFetchingCoordinates}
|
||||||
|
>
|
||||||
|
Узнать координаты
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{error && (
|
{error && (
|
||||||
<div className="alert alert-danger mt-3" role="alert">
|
<div className="alert alert-danger mt-3" role="alert">
|
||||||
{error}
|
{error}
|
||||||
@ -212,9 +247,9 @@ const Cities = () => {
|
|||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th>Название</th>
|
<th>Название</th>
|
||||||
<th>Координата X</th>
|
|
||||||
<th>Координата Y</th>
|
|
||||||
<th>Федеральный округ</th>
|
<th>Федеральный округ</th>
|
||||||
|
<th>X координата</th>
|
||||||
|
<th>Y координата</th>
|
||||||
<th>Действия</th>
|
<th>Действия</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
@ -222,9 +257,9 @@ const Cities = () => {
|
|||||||
{cities.map((city) => (
|
{cities.map((city) => (
|
||||||
<tr key={city.id}>
|
<tr key={city.id}>
|
||||||
<td>{city.name}</td>
|
<td>{city.name}</td>
|
||||||
|
<td>{city.federal_district_name}</td>
|
||||||
<td>{city.x_coordinate}</td>
|
<td>{city.x_coordinate}</td>
|
||||||
<td>{city.y_coordinate}</td>
|
<td>{city.y_coordinate}</td>
|
||||||
<td>{city.federal_district_name}</td>
|
|
||||||
<td>
|
<td>
|
||||||
<div className="btn-group">
|
<div className="btn-group">
|
||||||
<button
|
<button
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user