кнопка старта дисаблится по истечении времени

This commit is contained in:
Nastya 2025-10-21 21:33:08 +03:00
parent 6fa808884f
commit 59d03a9a64
2 changed files with 34 additions and 42 deletions

@ -1,24 +1,21 @@
import { Box, Typography, useTheme } from "@mui/material";
import { useQuizStore } from "@stores/useQuizStore";
import moment from "moment";
// Глобальная переменная для принудительного обновления
let forceUpdateCounter = 0;
// Запускаем глобальный таймер
if (typeof window !== "undefined" && !(window as any).overtimeTimerStarted) {
(window as any).overtimeTimerStarted = true;
setInterval(() => {
forceUpdateCounter++;
// Принудительно обновляем все компоненты OverTime
const event = new CustomEvent("overtime-tick", { detail: forceUpdateCounter });
window.dispatchEvent(event);
}, 1000);
}
import React from "react";
export const OverTime = () => {
const theme = useTheme();
const { settings } = useQuizStore();
const [currentTime, setCurrentTime] = React.useState(moment());
// Реактивный таймер с useEffect
React.useEffect(() => {
const interval = setInterval(() => {
setCurrentTime(moment());
}, 1000);
return () => clearInterval(interval);
}, []);
// Проверяем, включен ли overTime
const overTimeConfig = settings?.cfg?.overTime;
@ -30,9 +27,7 @@ export const OverTime = () => {
}
// Функция для расчета времени до окончания
const calculateTimeLeft = () => {
const now = moment(); // текущее время
const calculateTimeLeft = (now: moment.Moment) => {
// Для тестирования: добавляем 2 часа к текущему времени
const testEndsAt = moment().add(2, "hours");
const endsAt = overTimeConfig?.endsAt ? moment(overTimeConfig.endsAt) : testEndsAt;
@ -51,20 +46,7 @@ export const OverTime = () => {
};
};
const { days, hours, minutes, seconds } = calculateTimeLeft();
// Отладочная информация (используем глобальный счетчик для обновления)
const now = moment();
const endsAt = overTimeConfig?.endsAt ? moment(overTimeConfig.endsAt) : moment().add(2, "hours");
console.log("OverTime Debug:", {
currentTime: now.format("YYYY-MM-DD HH:mm:ss"),
endsAt: endsAt.format("YYYY-MM-DD HH:mm:ss"),
timeLeft: { days, hours, minutes, seconds },
rawEndsAt: overTimeConfig?.endsAt,
duration: moment.duration(endsAt.diff(now)).humanize(),
updateCounter: forceUpdateCounter,
});
const { days, hours, minutes, seconds } = calculateTimeLeft(currentTime);
return (
<Box

@ -1,4 +1,6 @@
import { Box, Button, ButtonBase, Link, Paper, Typography, useTheme } from "@mui/material";
import moment from "moment";
import React from "react";
import { QuizPreviewLayoutByType } from "./QuizPreviewLayoutByType";
import { OverTime } from "./OverTime";
@ -25,6 +27,16 @@ export const StartPageViewPublication = () => {
const { settings, show_badge, quizId, questions } = useQuizStore();
const { isMobileDevice } = useUADevice();
const setCurrentQuizStep = useQuizViewStore((state) => state.setCurrentQuizStep);
const [currentTime, setCurrentTime] = React.useState(moment());
// Реактивный таймер для обновления времени
React.useEffect(() => {
const interval = setInterval(() => {
setCurrentTime(moment());
}, 1000);
return () => clearInterval(interval);
}, []);
const size = useRootContainerSize();
const isMobile = size < 700;
@ -40,15 +52,6 @@ export const StartPageViewPublication = () => {
yandexMetrics.phoneNumberOpened();
};
console.log("------------------------------------------------");
console.log("Background type:", settings.cfg.startpage.background.type);
console.log("Is image type:", settings.cfg.startpage.background.type === "image");
console.log("Is video type:", settings.cfg.startpage.background.type === "video");
console.log("Video URL:", settings.cfg.startpage.background.video);
console.log("Desktop background:", settings.cfg.startpage.background.desktop);
console.log("Startpage type:", settings.cfg.startpageType);
console.log("------------------------------------------------");
const background =
settings.cfg.startpage.background.type === "image" ? (
<img
@ -67,7 +70,6 @@ export const StartPageViewPublication = () => {
) : settings.cfg.startpage.background.type === "video" ? (
settings.cfg.startpage.background.video ? (
(() => {
console.log("Rendering QuizVideo with URL:", settings.cfg.startpage.background.video);
return (
<QuizVideo
videoUrl={settings.cfg.startpage.background.video}
@ -184,6 +186,14 @@ export const StartPageViewPublication = () => {
(question) => question.type !== null && question.type !== "result"
).length;
// Проверяем, истекло ли время для overTime
const overTimeConfig = settings?.cfg?.overTime;
const isOverTimeEnabled = overTimeConfig?.enabled;
const isTimeExpired =
isOverTimeEnabled && overTimeConfig?.endsAt
? currentTime.isAfter(moment(overTimeConfig.endsAt)) || currentTime.isSame(moment(overTimeConfig.endsAt))
: false;
const onQuizStart = () => {
setCurrentQuizStep("question");
@ -291,7 +301,7 @@ export const StartPageViewPublication = () => {
<Box width={settings.cfg.startpageType === "standard" ? "100%" : "auto"}>
<Button
variant="contained"
disabled={realQuestionsCount === 0}
disabled={realQuestionsCount === 0 || isTimeExpired}
sx={{
fontSize: "18px",
padding: "10px 20px",