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";
|
"use client";
|
||||||
|
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useRef, useState } from "react";
|
||||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||||
|
|
||||||
import "@/components/trade-in/trade-in.css";
|
import "@/components/trade-in/trade-in.css";
|
||||||
|
|
@ -17,6 +17,7 @@ interface ScraperSetting {
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ScraperSettingUpdate {
|
interface ScraperSettingUpdate {
|
||||||
|
source: string;
|
||||||
request_delay_sec: number;
|
request_delay_sec: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -79,7 +80,10 @@ interface YandexValuationTriggerResp {
|
||||||
function useScraperSettings() {
|
function useScraperSettings() {
|
||||||
return useQuery<ScraperSetting[]>({
|
return useQuery<ScraperSetting[]>({
|
||||||
queryKey: ["scraper-settings"],
|
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,
|
refetchInterval: 30_000,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -89,12 +93,12 @@ function useUpdateScraperSetting() {
|
||||||
return useMutation<
|
return useMutation<
|
||||||
ScraperSetting,
|
ScraperSetting,
|
||||||
Error,
|
Error,
|
||||||
{ source: string; payload: ScraperSettingUpdate }
|
{ source: string; payload: Omit<ScraperSettingUpdate, "source"> }
|
||||||
>({
|
>({
|
||||||
mutationFn: ({ source, payload }) =>
|
mutationFn: ({ source, payload }) =>
|
||||||
apiFetch<ScraperSetting>(`/api/v1/admin/scraper-settings/${source}`, {
|
apiFetch<ScraperSetting>(`/api/v1/admin/scraper-settings/${source}`, {
|
||||||
method: "PUT",
|
method: "PUT",
|
||||||
body: JSON.stringify(payload),
|
body: JSON.stringify({ source, ...payload }),
|
||||||
}),
|
}),
|
||||||
onSuccess: () => {
|
onSuccess: () => {
|
||||||
qc.invalidateQueries({ queryKey: ["scraper-settings"] });
|
qc.invalidateQueries({ queryKey: ["scraper-settings"] });
|
||||||
|
|
@ -212,10 +216,14 @@ export default function YandexScraperPage() {
|
||||||
const settingsQ = useScraperSettings();
|
const settingsQ = useScraperSettings();
|
||||||
const updateSettingMut = useUpdateScraperSetting();
|
const updateSettingMut = useUpdateScraperSetting();
|
||||||
const [delayInput, setDelayInput] = useState<string>("5.0");
|
const [delayInput, setDelayInput] = useState<string>("5.0");
|
||||||
|
const delayInitializedRef = useRef(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const yandex = settingsQ.data?.find((s) => s.source === "yandex");
|
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]);
|
}, [settingsQ.data]);
|
||||||
|
|
||||||
// Around
|
// Around
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue