- apiFetch: proactive refresh, cross-tab single-flight via Web Locks, no logout on transient errors - AuthContext: save tokens returned on own login change, revoke refresh on logout, sync tabs - Ads: X-Session-Id, impressionToken in impression/click, IAB viewability, safe http(s) targetUrl - STOMP: refresh before CONNECT, stop reconnect loop on dead session, /user/queue/errors behind flag - Auth form: password 8 chars / 72 bytes; 429/413 messages via describeApiError - My posts with auth + moderation badges; post form step validation and field limits - Map: escape user text in Yandex hint/template Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
116 lines
4.3 KiB
JavaScript
116 lines
4.3 KiB
JavaScript
import { useMemo, useState } from "react";
|
|
import { useForm } from "react-hook-form";
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
import { z } from "zod";
|
|
import { useTranslation } from "react-i18next";
|
|
import { isSafeHttpUrl } from "../../utils/safeUrl";
|
|
import CloseIcon from "../icons/CloseIcon";
|
|
import { updateAdvertising } from "../../api/adminAdsApi";
|
|
|
|
/**
|
|
* Редактирование объявления: только targetUrl и weight.
|
|
* Тип, медиа и статус транскодинга фиксируются при создании —
|
|
* смена креатива делается через новое объявление.
|
|
*/
|
|
export default function AdvertisingEditModal({ ad, onClose, onUpdated }) {
|
|
const { t } = useTranslation();
|
|
const [submitting, setSubmitting] = useState(false);
|
|
const [submitError, setSubmitError] = useState("");
|
|
|
|
const schema = useMemo(
|
|
() => z.object({
|
|
// Бэкенд принимает только абсолютный http(s) (иначе 400): z.url()
|
|
// пропустил бы и "javascript:", и "ftp:"
|
|
targetUrl: z.string().trim().refine(isSafeHttpUrl, t("adminPanel.urlInvalid")),
|
|
weight: z.coerce.number().int(t("adminPanel.weightInvalid")).positive(t("adminPanel.weightInvalid")),
|
|
}),
|
|
[t]
|
|
);
|
|
|
|
const {
|
|
register,
|
|
handleSubmit,
|
|
formState: { errors },
|
|
} = useForm({
|
|
resolver: zodResolver(schema),
|
|
mode: "onSubmit",
|
|
defaultValues: {
|
|
targetUrl: ad.targetUrl || "",
|
|
weight: ad.weight ?? "",
|
|
},
|
|
});
|
|
|
|
const onSubmit = async (values) => {
|
|
if (submitting) return;
|
|
setSubmitting(true);
|
|
setSubmitError("");
|
|
try {
|
|
const updated = await updateAdvertising(ad.id, {
|
|
targetUrl: values.targetUrl,
|
|
weight: Number(values.weight),
|
|
});
|
|
onUpdated({ ...ad, ...updated });
|
|
} catch (err) {
|
|
setSubmitError(
|
|
err?.status === 400 ? t("adminPanel.adEditInvalid") : t("adminPanel.adEditFailed")
|
|
);
|
|
setSubmitting(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="f-modal-bg" onClick={e => e.target === e.currentTarget && !submitting && onClose()}>
|
|
<div className="f-modal f-modal-sm">
|
|
<div className="f-modal-head">
|
|
<span className="f-modal-title">{t('adminPanel.adEditTitle')}</span>
|
|
<button className="f-close-btn" type="button" onClick={onClose} disabled={submitting}>
|
|
<CloseIcon />
|
|
</button>
|
|
</div>
|
|
|
|
<form onSubmit={handleSubmit(onSubmit)} noValidate>
|
|
<div className="f-m-group">
|
|
<label className="f-m-label" htmlFor="admp-ad-edit-url">{t('adminPanel.targetUrl')}</label>
|
|
<input
|
|
id="admp-ad-edit-url"
|
|
className={`f-m-field${errors.targetUrl ? " err" : ""}`}
|
|
type="text"
|
|
placeholder="https://example.com/landing"
|
|
disabled={submitting}
|
|
{...register("targetUrl")}
|
|
/>
|
|
{errors.targetUrl && <div className="f-err-msg">{errors.targetUrl.message}</div>}
|
|
</div>
|
|
|
|
<div className="f-m-group">
|
|
<label className="f-m-label" htmlFor="admp-ad-edit-weight">{t('adminPanel.weight')}</label>
|
|
<input
|
|
id="admp-ad-edit-weight"
|
|
className={`f-m-field${errors.weight ? " err" : ""}`}
|
|
type="number"
|
|
min={1}
|
|
step={1}
|
|
placeholder="10"
|
|
disabled={submitting}
|
|
{...register("weight")}
|
|
/>
|
|
{errors.weight ? (
|
|
<div className="f-err-msg">{errors.weight.message}</div>
|
|
) : (
|
|
<div className="f-hint">{t('adminPanel.weightHint')}</div>
|
|
)}
|
|
</div>
|
|
|
|
{submitError && <div className="f-err-msg" style={{ marginTop: 4 }}>{submitError}</div>}
|
|
|
|
<button className="f-m-submit" type="submit" disabled={submitting}>
|
|
<span className="f-m-submit-row">
|
|
{submitting && <span className="f-m-spinner" />}
|
|
{submitting ? t('adminPanel.savingAd') : t('adminPanel.adEditSubmit')}
|
|
</span>
|
|
</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
);
|
|
} |