B6: SPA navigation after case creation (useNavigate instead of window.location.href)
B7: stop AdminDashboard refetching the case list on selection change selectedCaseId was both read and written inside the list-loading effect and listed in its deps, causing a second fetch on first render and after every filter reset. Auto-select now uses a functional state update, so the effect no longer depends on selectedCaseId; editForm stays in sync via the existing selectedCase effect.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import React, { useState } from "react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import Step1Child from "./Step1Child";
|
||||
import Step2Health from "./Step2Health";
|
||||
import Step2bPsychotype from "./Step2bPsychotype";
|
||||
@@ -16,6 +17,7 @@ const INITIAL_DATA = {
|
||||
};
|
||||
|
||||
const CaseForm = ({ isMobile = false }) => {
|
||||
const navigate = useNavigate();
|
||||
const [currentStep, setCurrentStep] = useState(1);
|
||||
const [formData, setFormData] = useState(INITIAL_DATA);
|
||||
const [errors, setErrors] = useState([]);
|
||||
@@ -117,8 +119,9 @@ const CaseForm = ({ isMobile = false }) => {
|
||||
|
||||
const result = await response.json();
|
||||
|
||||
// Перенаправляем на страницу анализа с ID карточки
|
||||
window.location.href = `/analysis/${result.id}`;
|
||||
// Перенаправляем на страницу анализа с ID карточки.
|
||||
// SPA-переход: полная перезагрузка теряла состояние приложения.
|
||||
navigate(`/analysis/${result.id}`);
|
||||
|
||||
} catch (err) {
|
||||
setErrors([err.message || "Ошибка при отправке данных"]);
|
||||
|
||||
@@ -76,11 +76,16 @@ function AdminDashboard() {
|
||||
throw new Error(`Не удалось загрузить список кейсов (${response.status})`);
|
||||
}
|
||||
const payload = await response.json();
|
||||
setCases(payload.items || []);
|
||||
const items = payload.items || [];
|
||||
setCases(items);
|
||||
setTotal(payload.total || 0);
|
||||
if ((payload.items || []).length && !selectedCaseId) {
|
||||
setSelectedCaseId(payload.items[0].id);
|
||||
setEditForm(payload.items[0]);
|
||||
// Auto-select the first row only when nothing is selected yet.
|
||||
// Functional update so this effect does not have to read (and depend
|
||||
// on) selectedCaseId — depending on it caused a second fetch on first
|
||||
// render and after every filter reset. editForm is kept in sync by the
|
||||
// selectedCase effect below.
|
||||
if (items.length) {
|
||||
setSelectedCaseId((current) => current || items[0].id);
|
||||
}
|
||||
} catch (err) {
|
||||
if (err.name !== 'AbortError') {
|
||||
@@ -93,7 +98,7 @@ function AdminDashboard() {
|
||||
|
||||
loadCases();
|
||||
return () => controller.abort();
|
||||
}, [filters.ageMax, filters.ageMin, filters.status, page, selectedCaseId]);
|
||||
}, [filters.ageMax, filters.ageMin, filters.status, page]);
|
||||
|
||||
useEffect(() => {
|
||||
const controller = new AbortController();
|
||||
|
||||
Reference in New Issue
Block a user