Merge pull request #102 from SlimusMinus/fix-advert_slot
All checks were successful
deploy-frontend / deploy (push) Successful in 44s
All checks were successful
deploy-frontend / deploy (push) Successful in 44s
fixed empty advertising slot
This commit is contained in:
@@ -8,6 +8,8 @@ export default function AdSlot() {
|
||||
const slotRef = useRef(null);
|
||||
const [ad, setAd] = useState(null);
|
||||
const [closed, setClosed] = useState(false);
|
||||
// Показывать нечего (204 или ошибка) — слот убирается из ленты целиком
|
||||
const [empty, setEmpty] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (closed) return;
|
||||
@@ -25,18 +27,23 @@ export default function AdSlot() {
|
||||
headers: token ? { Authorization: `Bearer ${token}` } : undefined,
|
||||
})
|
||||
.then((res) => {
|
||||
// 204 No Content — свободных объявлений нет, оставляем пустой слот
|
||||
// 204 No Content — свободных объявлений нет
|
||||
if (res.status === 204) return null;
|
||||
if (!res.ok) throw new Error(`Error ${res.status}`);
|
||||
return res.json();
|
||||
})
|
||||
.then((data) => {
|
||||
if (!cancelled && data) {
|
||||
if (data.mediaUrl) data.mediaUrl = normalizeMediaUrl(data.mediaUrl);
|
||||
setAd(data);
|
||||
if (cancelled) return;
|
||||
if (!data) {
|
||||
setEmpty(true);
|
||||
return;
|
||||
}
|
||||
if (data.mediaUrl) data.mediaUrl = normalizeMediaUrl(data.mediaUrl);
|
||||
setAd(data);
|
||||
})
|
||||
.catch(() => {});
|
||||
.catch(() => {
|
||||
if (!cancelled) setEmpty(true);
|
||||
});
|
||||
},
|
||||
{ rootMargin: '200px' }
|
||||
);
|
||||
@@ -50,7 +57,9 @@ export default function AdSlot() {
|
||||
|
||||
const handleClose = () => setClosed(true);
|
||||
|
||||
if (closed) return null;
|
||||
// Слот грузится заранее (rootMargin 200px), т.е. ещё ниже экрана —
|
||||
// его исчезновение не сдвигает видимую часть ленты
|
||||
if (closed || empty) return null;
|
||||
|
||||
if (ad) {
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user