Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | 15x 12x 12x 12x 12x 12x 12x 6x 6x 6x 6x 12x 12x 8x 8x 2x 8x 12x 15x 10x 10x 15x 40x 15x 15x 6x 6x 6x 2x 2x 4x 4x 4x 4x 15x 4x 4x 4x 4x 4x | import { Box, Grid, Stack } from "@mui/material";
import { ClockIcon } from "@phosphor-icons/react";
import { usePayBuilderForm } from "@sections/PayBuilder/provider/PayBuilderFormProvider";
import GiveText from "@shared/Text/GiveText";
import { useAppTheme } from "@theme/v2/Provider";
import { memo, useEffect, useRef, useState } from "react";
import { useQueryClient } from "react-query";
export const SweepstakeTime = ({
endDate,
campaignId,
}: {
endDate: number;
campaignId?: number;
}) => {
const theme = useAppTheme();
const queryClient = useQueryClient();
const { methods } = usePayBuilderForm();
const isFirstRender = useRef<boolean>(true);
const [time, setTime] = useState<{
days?: number;
hours?: number;
minutes?: number;
seconds?: number;
}>({
days: undefined,
hours: undefined,
minutes: undefined,
seconds: undefined,
});
useEffect(() => {
const i = countDown(
endDate,
(days: number, hours: number, minutes: number, seconds: number) => {
setTime({
days,
hours,
minutes,
seconds,
});
},
);
return () => {
clearInterval(i);
};
}, [endDate]);
const hasTimeEnded =
time.days === 0 &&
time.hours === 0 &&
time.minutes === 0 &&
time.seconds === 0;
useEffect(() => {
isFirstRender.current = false;
if (hasTimeEnded && !campaignId) {
methods?.setValue("About.hasEnded", true);
}
//if in first render we already have hasTimeEnded true dont need to rerender
Iif (hasTimeEnded && campaignId && !isFirstRender.current) {
queryClient.invalidateQueries(["public-form-id", campaignId]);
}
}, [hasTimeEnded]);
return (
<Stack gap={1}>
<Grid container alignItems="center" gap={1}>
<ClockIcon fill={theme.palette.icon?.["icon-secondary"]} size="20px" />
<GiveText fontSize="14px" sx={{ color: theme.palette.text.secondary }}>
{hasTimeEnded ? "Sweepstakes has ended" : " Sweepstakes ends in"}
</GiveText>
</Grid>
{!hasTimeEnded && <CountDown time={time} />}
</Stack>
);
};
const CountDown = ({ time }: any) => {
const theme = useAppTheme();
return (
<Grid container alignItems="center" gap={2}>
<TimeFrame
color={theme.palette.text.secondary}
value={time.days}
label="days"
/>
<TimeFrame
color={theme.palette.text.secondary}
value={time.hours}
label="Hrs"
/>
<TimeFrame
color={theme.palette.text.secondary}
value={time.minutes}
label="Min"
/>
<TimeFrame
color={theme.palette.text.secondary}
value={time.seconds}
label="Sec"
/>
</Grid>
);
};
const TimeFrame = memo(({ value, label, color }: any) => {
return (
<Box display="flex" alignItems="center">
<GiveText
fontSize="24px"
fontWeight="300"
sx={{
minWidth: `${value}`.length === 1 ? "22px" : "40px",
textAlign: "left",
whiteSpace: "nowrap",
overflow: "hidden",
textOverflow: "ellipsis",
paddingLeft: "2px",
}}
>
{value}
</GiveText>
<GiveText sx={{ color }} fontSize="11px">
{label}
</GiveText>
</Box>
);
});
TimeFrame.displayName = "TimeFrame";
const countDown = (target: number, cb: any) => {
const now = Date.now();
const timeLeft = target - now;
if (timeLeft <= 0) {
cb(0, 0, 0, 0);
return;
}
const { days, hours, minutes, seconds } = calculateTimeLeft(timeLeft);
cb(days, hours, minutes, seconds);
const interval = setInterval(() => {
const now = Date.now();
const timeLeft = target - now;
if (timeLeft <= 0) {
cb(0, 0, 0, 0);
clearInterval(interval);
return;
}
const { days, hours, minutes, seconds } = calculateTimeLeft(timeLeft);
cb(days, hours, minutes, seconds);
}, 1000);
return interval;
};
const calculateTimeLeft = (timeLeft: number) => {
const seconds = Math.floor((timeLeft / 1000) % 60);
const minutes = Math.floor((timeLeft / (1000 * 60)) % 60);
const hours = Math.floor((timeLeft / (1000 * 60 * 60)) % 24);
const days = Math.floor(timeLeft / (1000 * 60 * 60 * 24));
return { days, hours, minutes, seconds };
};
|