fix: Hot bug fix
This commit is contained in:
@@ -27,6 +27,8 @@ export const RouteEditPage = observer(() => {
|
||||
const { editRouteData } = routeStore;
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const { language } = languageStore;
|
||||
const [coordinates, setCoordinates] = useState<string>("");
|
||||
|
||||
useEffect(() => {
|
||||
const fetchData = async () => {
|
||||
const response = await routeStore.getRoute(Number(id));
|
||||
@@ -37,6 +39,15 @@ export const RouteEditPage = observer(() => {
|
||||
fetchData();
|
||||
}, [id, language]);
|
||||
|
||||
useEffect(() => {
|
||||
if (editRouteData.path && editRouteData.path.length > 0) {
|
||||
const formattedPath = editRouteData.path
|
||||
.map((coords) => coords.join(" "))
|
||||
.join("\n");
|
||||
setCoordinates(formattedPath);
|
||||
}
|
||||
}, [editRouteData.path]);
|
||||
|
||||
const handleSave = async () => {
|
||||
setIsLoading(true);
|
||||
await routeStore.editRoute(Number(id));
|
||||
@@ -44,6 +55,43 @@ export const RouteEditPage = observer(() => {
|
||||
setIsLoading(false);
|
||||
};
|
||||
|
||||
const validateCoordinates = (value: string) => {
|
||||
try {
|
||||
const lines = value.trim().split("\n");
|
||||
const coordinates = lines.map((line) => {
|
||||
const [lat, lon] = line
|
||||
.trim()
|
||||
.split(/[\s,]+/)
|
||||
.map(Number);
|
||||
return [lat, lon];
|
||||
});
|
||||
|
||||
if (coordinates.length === 0) {
|
||||
return "Введите хотя бы одну пару координат";
|
||||
}
|
||||
|
||||
if (
|
||||
!coordinates.every(
|
||||
(point) => Array.isArray(point) && point.length === 2
|
||||
)
|
||||
) {
|
||||
return "Каждая строка должна содержать две координаты";
|
||||
}
|
||||
|
||||
if (
|
||||
!coordinates.every((point) =>
|
||||
point.every((coord) => !isNaN(coord) && typeof coord === "number")
|
||||
)
|
||||
) {
|
||||
return "Координаты должны быть числами";
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch {
|
||||
return "Неверный формат координат";
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Paper className="w-full h-full p-3 flex flex-col gap-10">
|
||||
<LanguageSwitcher />
|
||||
@@ -105,15 +153,46 @@ export const RouteEditPage = observer(() => {
|
||||
className="w-full"
|
||||
label="Координаты маршрута"
|
||||
multiline
|
||||
minRows={3}
|
||||
value={editRouteData.path.map((p) => p.join(" ")).join("\n") || ""}
|
||||
onChange={(e) =>
|
||||
routeStore.setEditRouteData({
|
||||
path: e.target.value
|
||||
.split("\n")
|
||||
.map((line) => line.split(" ").map(Number)),
|
||||
})
|
||||
minRows={4}
|
||||
value={coordinates}
|
||||
onChange={(e) => {
|
||||
const newValue = e.target.value;
|
||||
setCoordinates(newValue);
|
||||
|
||||
const validationResult = validateCoordinates(newValue);
|
||||
if (validationResult === true) {
|
||||
const lines = newValue.trim().split("\n");
|
||||
const path = lines.map((line) => {
|
||||
const [lat, lon] = line
|
||||
.trim()
|
||||
.split(/[\s,]+/)
|
||||
.map(Number);
|
||||
return [lat, lon];
|
||||
});
|
||||
routeStore.setEditRouteData({ path });
|
||||
}
|
||||
}}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === "Enter") {
|
||||
const lines = coordinates.split("\n");
|
||||
const lastLine = lines[lines.length - 1];
|
||||
|
||||
// Если мы на последней строке и она не пустая
|
||||
if (lastLine && lastLine.trim()) {
|
||||
e.preventDefault();
|
||||
const newValue = coordinates + "\n";
|
||||
setCoordinates(newValue);
|
||||
}
|
||||
}
|
||||
}}
|
||||
error={validateCoordinates(coordinates) !== true}
|
||||
helperText={
|
||||
typeof validateCoordinates(coordinates) === "string"
|
||||
? validateCoordinates(coordinates)
|
||||
: "Введите координаты в формате: широта долгота (можно использовать запятые или пробелы)"
|
||||
}
|
||||
placeholder="55.7558 37.6173
|
||||
55.7539 37.6208"
|
||||
/>
|
||||
<TextField
|
||||
className="w-full"
|
||||
|
||||
Reference in New Issue
Block a user