feat(tradein-ui): Yandex admin scraper page — mirror Avito + global delay control #488
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