side widget: add auto open time and hide on mobile params
This commit is contained in:
parent
0ffa358585
commit
41e02c82ef
18
src/widgets/shared/useAutoOpenTimer.ts
Normal file
18
src/widgets/shared/useAutoOpenTimer.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
|
||||
export function useAutoOpenTimer(autoOpenTime: number) {
|
||||
const [isWidgetHidden, setIsWidgetHidden] = useState<boolean>(autoOpenTime ? true : false);
|
||||
|
||||
useEffect(function setAutoOpenTimer() {
|
||||
if (!autoOpenTime) return;
|
||||
|
||||
const timeout = setTimeout(() => setIsWidgetHidden(false), autoOpenTime * 1000);
|
||||
|
||||
return () => {
|
||||
clearTimeout(timeout);
|
||||
};
|
||||
}, [autoOpenTime]);
|
||||
|
||||
return isWidgetHidden;
|
||||
}
|
@ -1,9 +1,10 @@
|
||||
import lightTheme from "@/utils/themes/light";
|
||||
import { Button, ThemeProvider, useMediaQuery } from "@mui/material";
|
||||
import { Button, Fade, ThemeProvider, useMediaQuery } from "@mui/material";
|
||||
import { useState } from "react";
|
||||
import { createPortal } from "react-dom";
|
||||
import QuizDialog from "../shared/QuizDialog";
|
||||
import RunningStripe from "../shared/RunningStripe";
|
||||
import { useAutoOpenTimer } from "../shared/useAutoOpenTimer";
|
||||
import { useQuizCompletionStatus } from "../shared/useQuizCompletionStatus";
|
||||
|
||||
|
||||
@ -19,6 +20,8 @@ interface Props {
|
||||
dimensions?: { width: string; height: string; };
|
||||
fullScreen?: boolean;
|
||||
buttonFlash?: boolean;
|
||||
autoOpenTime?: number;
|
||||
hideOnMobile?: boolean;
|
||||
}
|
||||
|
||||
export default function QuizSideButton({
|
||||
@ -29,17 +32,22 @@ export default function QuizSideButton({
|
||||
dimensions,
|
||||
fullScreen = false,
|
||||
buttonFlash = false,
|
||||
autoOpenTime = 0,
|
||||
hideOnMobile = false,
|
||||
}: Props) {
|
||||
const [isQuizShown, setIsQuizShown] = useState<boolean>(false);
|
||||
const isMobile = useMediaQuery("(max-width: 600px)");
|
||||
const isQuizCompleted = useQuizCompletionStatus(quizId);
|
||||
const [isFlashEnabled, setIsFlashEnabled] = useState<boolean>(buttonFlash);
|
||||
const isWidgetHidden = useAutoOpenTimer(autoOpenTime);
|
||||
|
||||
function openQuiz() {
|
||||
setIsQuizShown(true);
|
||||
setIsFlashEnabled(false);
|
||||
}
|
||||
|
||||
if (hideOnMobile && isMobile) return null;
|
||||
|
||||
return createPortal(
|
||||
<ThemeProvider theme={lightTheme}>
|
||||
<QuizDialog
|
||||
@ -71,35 +79,37 @@ export default function QuizSideButton({
|
||||
},
|
||||
]}
|
||||
/>
|
||||
<Button
|
||||
className="pena-quiz-widget-button"
|
||||
variant="contained"
|
||||
onClick={openQuiz}
|
||||
disableFocusRipple
|
||||
sx={[
|
||||
{
|
||||
display: isQuizShown ? "none" : "block",
|
||||
position: "fixed",
|
||||
height: "70px",
|
||||
width: "600px",
|
||||
maxWidth: `calc(100% - ${PADDING * 2}px)`,
|
||||
backgroundColor: buttonBackgroundColor,
|
||||
color: buttonTextColor,
|
||||
overflow: "hidden",
|
||||
},
|
||||
position === "left" && {
|
||||
bottom: PADDING,
|
||||
left: PADDING,
|
||||
},
|
||||
position === "right" && {
|
||||
bottom: PADDING,
|
||||
right: PADDING,
|
||||
},
|
||||
]}
|
||||
>
|
||||
{!isQuizCompleted && isFlashEnabled && <RunningStripe />}
|
||||
Пройти квиз
|
||||
</Button>
|
||||
<Fade in={!isWidgetHidden} timeout={400}>
|
||||
<Button
|
||||
className="pena-quiz-widget-button"
|
||||
variant="contained"
|
||||
onClick={openQuiz}
|
||||
disableFocusRipple
|
||||
sx={[
|
||||
{
|
||||
display: isQuizShown ? "none" : "block",
|
||||
position: "fixed",
|
||||
height: "70px",
|
||||
width: "600px",
|
||||
maxWidth: `calc(100% - ${PADDING * 2}px)`,
|
||||
backgroundColor: buttonBackgroundColor,
|
||||
color: buttonTextColor,
|
||||
overflow: "hidden",
|
||||
},
|
||||
position === "left" && {
|
||||
bottom: PADDING,
|
||||
left: PADDING,
|
||||
},
|
||||
position === "right" && {
|
||||
bottom: PADDING,
|
||||
right: PADDING,
|
||||
},
|
||||
]}
|
||||
>
|
||||
{!isQuizCompleted && isFlashEnabled && <RunningStripe />}
|
||||
Пройти квиз
|
||||
</Button>
|
||||
</Fade>
|
||||
</ThemeProvider>,
|
||||
document.body
|
||||
);
|
||||
|
Loading…
Reference in New Issue
Block a user