fix(tradein-ui): #488 BLOCK — scraper-settings response wrapper + PUT source field
deep-code-reviewer caught 2 critical bugs in both yandex/page.tsx (#488) and cian/page.tsx (#486 already merged): 1. useScraperSettings types response as ScraperSetting[] but backend returns {settings: [...]} -> .find is not a function -> settings panel never renders, slider input never auto-populates from DB. Fix: unwrap .settings in queryFn. 2. PUT body missing required `source` field -- backend Pydantic ScraperSettingPayload validates source==path source. Result: 422 on every save. Fix: include {source, ...payload} in PUT body. Plus M (non-blocking): useEffect race -- refetch every 30s clobbered user input mid-edit. Guarded with initializedRef first-load-only flag. Refs PR #488 review (deep-code-reviewer, 2026-05-23).
This commit is contained in:
parent
461161e3ec
commit
271b5daeea
1 changed files with 13 additions and 5 deletions
|
|
@ -1,6 +1,6 @@
|
|||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
|
||||
import "@/components/trade-in/trade-in.css";
|
||||
|
|
@ -17,6 +17,7 @@ interface ScraperSetting {
|
|||
}
|
||||
|
||||
interface ScraperSettingUpdate {
|
||||
source: string;
|
||||
request_delay_sec: number;
|
||||
}
|
||||
|
||||
|
|
@ -79,7 +80,10 @@ interface YandexValuationTriggerResp {
|
|||
function useScraperSettings() {
|
||||
return useQuery<ScraperSetting[]>({
|
||||
queryKey: ["scraper-settings"],
|
||||
queryFn: () => apiFetch<ScraperSetting[]>("/api/v1/admin/scraper-settings"),
|
||||
queryFn: () =>
|
||||
apiFetch<{ settings: ScraperSetting[] }>("/api/v1/admin/scraper-settings").then(
|
||||
(r) => r.settings,
|
||||
),
|
||||
refetchInterval: 30_000,
|
||||
});
|
||||
}
|
||||
|
|
@ -89,12 +93,12 @@ function useUpdateScraperSetting() {
|
|||
return useMutation<
|
||||
ScraperSetting,
|
||||
Error,
|
||||
{ source: string; payload: ScraperSettingUpdate }
|
||||
{ source: string; payload: Omit<ScraperSettingUpdate, "source"> }
|
||||
>({
|
||||
mutationFn: ({ source, payload }) =>
|
||||
apiFetch<ScraperSetting>(`/api/v1/admin/scraper-settings/${source}`, {
|
||||
method: "PUT",
|
||||
body: JSON.stringify(payload),
|
||||
body: JSON.stringify({ source, ...payload }),
|
||||
}),
|
||||
onSuccess: () => {
|
||||
qc.invalidateQueries({ queryKey: ["scraper-settings"] });
|
||||
|
|
@ -212,10 +216,14 @@ export default function YandexScraperPage() {
|
|||
const settingsQ = useScraperSettings();
|
||||
const updateSettingMut = useUpdateScraperSetting();
|
||||
const [delayInput, setDelayInput] = useState<string>("5.0");
|
||||
const delayInitializedRef = useRef(false);
|
||||
|
||||
useEffect(() => {
|
||||
const yandex = settingsQ.data?.find((s) => s.source === "yandex");
|
||||
if (yandex) setDelayInput(String(yandex.request_delay_sec));
|
||||
if (yandex && !delayInitializedRef.current) {
|
||||
setDelayInput(String(yandex.request_delay_sec));
|
||||
delayInitializedRef.current = true;
|
||||
}
|
||||
}, [settingsQ.data]);
|
||||
|
||||
// Around
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue