diff --git a/src/api/leadtarget.ts b/src/api/leadtarget.ts index c05c4923..c85befcf 100644 --- a/src/api/leadtarget.ts +++ b/src/api/leadtarget.ts @@ -4,14 +4,14 @@ import { parseAxiosError } from "@utils/parse-error"; export type LeadTargetType = "mail" | "telegram" | "whatsapp" | "webhook"; export interface LeadTargetModel { - ID: number; - AccountID: string; - Type: LeadTargetType; - QuizID: number; - Target: string; - InviteLink?: string; - Deleted?: boolean; - CreatedAt?: string; + id: number; + accountID: string; + type: LeadTargetType; + quizID: number; + target: string; // содержит подстроку "zapier" или "postback" + inviteLink?: string; + deleted?: boolean; + createdAt?: string; } const API_URL = `${process.env.REACT_APP_DOMAIN}/squiz`; diff --git a/src/pages/IntegrationsPage/IntegrationsModal/Postback/index.tsx b/src/pages/IntegrationsPage/IntegrationsModal/Postback/index.tsx index 7e61decb..1de41a27 100644 --- a/src/pages/IntegrationsPage/IntegrationsModal/Postback/index.tsx +++ b/src/pages/IntegrationsPage/IntegrationsModal/Postback/index.tsx @@ -3,10 +3,11 @@ import { Dialog, IconButton, Typography, useMediaQuery, useTheme, Box, Button } import CloseIcon from "@mui/icons-material/Close"; import { Quiz } from "@/model/quiz/quiz"; import CustomTextField from "@/ui_kit/CustomTextField"; -import { createLeadTarget, getLeadTargetsByQuiz, deleteLeadTarget } from "@/api/leadtarget"; +import { createLeadTarget, getLeadTargetsByQuiz, deleteLeadTarget, updateLeadTarget } from "@/api/leadtarget"; import { useFormik } from "formik"; import InstructionYoutubeLink from "@/pages/IntegrationsPage/IntegrationsModal/InstructionYoutubeLink"; import { useSnackbar } from "notistack"; +import { useLeadTargets } from "@/pages/IntegrationsPage/hooks/useLeadTargets"; type PostbackModalProps = { isModalOpen: boolean; @@ -27,28 +28,26 @@ export const PostbackModal: FC = ({ const [isSaving, setIsSaving] = useState(false); const { enqueueSnackbar } = useSnackbar(); - const deleteLeadTargetsByQuizType = async (quizId: number, type: "webhook") => { - const [targets] = await getLeadTargetsByQuiz(quizId); - if (!targets || targets.length === 0) { - console.log("No targets found for deletion"); - return; - } - const toDelete = targets.filter(t => t.Type === type); - console.log("Targets to delete:", toDelete); - for (const t of toDelete) { - console.log("Deleting target with ID:", t.ID); - await deleteLeadTarget(t.ID); - } - }; - const handleSubmit = async (values: { token: string; domain: string }) => { const tokenValue = (values.token || "").trim(); const target = (values.domain || "").trim(); try { setIsSaving(true); + // 1) Асинхронно получаем текущие цели + const [items] = await getLeadTargetsByQuiz(quiz.backendId); + const existing = (items ?? []).filter((t) => t.type === "webhook"); + console.log("Saving flow -> existing webhook targets:", existing); if (!tokenValue && !target) { - await deleteLeadTargetsByQuizType(quiz.backendId, "webhook"); + const deletePromises = existing.map((t) => deleteLeadTarget(t.id)); + await Promise.all(deletePromises); enqueueSnackbar("Postback удален", { variant: "success" }); + } else if (existing.length > 0) { + const [first, ...extra] = existing; + await Promise.all([ + updateLeadTarget({ id: first.id, target }), + ...extra.map((t) => deleteLeadTarget(t.id)), + ]); + enqueueSnackbar("Postback обновлен", { variant: "success" }); } else { await createLeadTarget({ type: "webhook", @@ -58,6 +57,8 @@ export const PostbackModal: FC = ({ }); enqueueSnackbar("Postback сохранен", { variant: "success" }); } + + await refresh(); } catch (error) { enqueueSnackbar("Ошибка при сохранении", { variant: "error" }); } finally { @@ -65,11 +66,18 @@ export const PostbackModal: FC = ({ } }; + const { isLoading, postbackTarget, refresh } = useLeadTargets(quiz?.backendId, isModalOpen); + const formik = useFormik<{ token: string; domain: string }>({ - initialValues: { token: "", domain: "" }, + initialValues: { token: "", domain: postbackTarget?.target ?? "" }, onSubmit: handleSubmit, }); + useEffect(() => { + formik.setFieldValue("domain", postbackTarget?.target ?? ""); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [postbackTarget?.target]); + useEffect(() => { if (isModalOpen) { setTimeout(() => { @@ -153,17 +161,21 @@ export const PostbackModal: FC = ({ > Домен - formik.setFieldValue("domain", e.target.value)} - maxLength={150} - /> + {isLoading ? ( + + ) : ( + formik.setFieldValue("domain", e.target.value)} + maxLength={150} + /> + )}