turkmentv_front/components/lottery/LotteryWinnersSection.tsx

199 lines
6.3 KiB
TypeScript
Raw Normal View History

2024-12-24 12:19:08 +00:00
'use client';
2024-11-25 14:33:01 +00:00
2024-12-27 12:52:55 +00:00
import { useState, useEffect, useRef } from 'react';
2024-12-24 12:19:08 +00:00
import { useLotteryAuth } from '@/store/useLotteryAuth';
2024-12-27 12:52:55 +00:00
import { LotteryWinnerDataSimplified } from '@/typings/lottery/lottery.types';
import LotteryWinnersList from './winners/LotteryWinnersList';
2024-12-25 13:46:20 +00:00
import LotterySlotCounter from './slotCounter/LotterySlotCounter';
import ReactConfetti from 'react-confetti';
import { useWindowSize } from 'react-use';
2024-11-25 14:33:01 +00:00
2024-12-27 12:52:55 +00:00
const WEBSOCKET_URL = 'wss://sms.turkmentv.gov.tm/ws/lottery?dst=0506';
const PING_INTERVAL = 25000;
2024-12-27 15:23:39 +00:00
const SLOT_COUNTER_DURATION = 20000;
2024-12-27 12:52:55 +00:00
2024-11-25 14:33:01 +00:00
const LotteryWinnersSection = () => {
2024-12-27 12:52:55 +00:00
// UI States
const [winners, setWinners] = useState<LotteryWinnerDataSimplified[]>([]);
2024-12-25 13:46:20 +00:00
const [currentNumber, setCurrentNumber] = useState<string>('00-00-00-00-00');
const [isConfettiActive, setIsConfettiActive] = useState(false);
2024-12-27 12:52:55 +00:00
const [wsStatus, setWsStatus] = useState<'connecting' | 'connected' | 'error'>('connecting');
2024-12-25 13:46:20 +00:00
const { width, height } = useWindowSize();
2024-12-23 19:32:28 +00:00
const { lotteryData } = useLotteryAuth();
2024-12-27 15:23:39 +00:00
const [isSlotCounterAnimating, setIsSlotCounterAnimating] = useState(false);
const [pendingWinner, setPendingWinner] = useState<LotteryWinnerDataSimplified | null>(null);
2024-12-23 19:32:28 +00:00
2024-12-27 12:52:55 +00:00
// Refs
const wsRef = useRef<WebSocket | null>(null);
const pingIntervalRef = useRef<NodeJS.Timeout>();
const mountedRef = useRef(false);
2024-12-27 15:23:39 +00:00
console.log(isConfettiActive, 'isConfettiActive');
2024-12-27 12:52:55 +00:00
// Initialize winners from lottery data
2024-12-23 19:32:28 +00:00
useEffect(() => {
if (lotteryData?.data.winners) {
2024-12-27 12:52:55 +00:00
const simplifiedWinners = lotteryData.data.winners.map((winner) => ({
client: winner.client,
winner_no: winner.winner_no,
ticket: winner.ticket,
}));
setWinners(simplifiedWinners);
2024-12-25 13:46:20 +00:00
setCurrentNumber(lotteryData.data.winners.at(-1)?.ticket || '00-00-00-00-00');
2024-12-23 19:32:28 +00:00
}
2024-12-27 12:52:55 +00:00
}, [lotteryData]);
2024-12-23 19:32:28 +00:00
2024-12-27 12:52:55 +00:00
// Handle WebSocket connection
useEffect(() => {
mountedRef.current = true;
const setupWebSocket = () => {
try {
const socket = new WebSocket(WEBSOCKET_URL);
wsRef.current = socket;
socket.addEventListener('open', () => {
if (!mountedRef.current) return;
console.log('WebSocket Connected');
setWsStatus('connected');
pingIntervalRef.current = setInterval(() => {
if (socket.readyState === WebSocket.OPEN) {
socket.send(JSON.stringify({ type: 'ping' }));
}
}, PING_INTERVAL);
});
2024-12-27 15:23:39 +00:00
socket.addEventListener('message', async (event) => {
2024-12-27 12:52:55 +00:00
if (!mountedRef.current) return;
console.log('Message received:', event.data);
try {
const newWinner = JSON.parse(event.data);
const winnerData = {
2024-12-27 15:23:39 +00:00
client: newWinner.phone,
2024-12-27 12:52:55 +00:00
winner_no: newWinner.winner_no,
ticket: newWinner.ticket,
};
2024-12-27 15:23:39 +00:00
// Start the sequence
setIsSlotCounterAnimating(true);
setPendingWinner(winnerData);
setCurrentNumber(winnerData.ticket);
// Wait for slot counter animation
await new Promise((resolve) => setTimeout(resolve, SLOT_COUNTER_DURATION));
2024-12-27 12:52:55 +00:00
setIsConfettiActive(true);
2024-12-27 15:23:39 +00:00
setWinners((prev) => [...prev, winnerData]);
// Hide confetti after 5 seconds
// setTimeout(() => {
// if (mountedRef.current) {
// setIsConfettiActive(false);
// setIsSlotCounterAnimating(false);
// setPendingWinner(null);
// }
// }, 5000);
// Show confetti and add winner simultaneously
if (mountedRef.current) {
setIsConfettiActive(true);
setWinners((prev) => [...prev, winnerData]);
// Hide confetti after 5 seconds
setTimeout(() => {
if (mountedRef.current) {
setIsConfettiActive(false);
setIsSlotCounterAnimating(false);
setPendingWinner(null);
}
}, 5000);
}
2024-12-27 12:52:55 +00:00
} catch (error) {
console.error('Error processing message:', error);
2024-12-27 15:23:39 +00:00
setIsSlotCounterAnimating(false);
setPendingWinner(null);
2024-12-27 12:52:55 +00:00
}
});
socket.addEventListener('error', (error) => {
if (!mountedRef.current) return;
console.error('WebSocket Error:', error);
setWsStatus('error');
});
socket.addEventListener('close', () => {
if (!mountedRef.current) return;
console.log('WebSocket Closed');
setWsStatus('error');
if (pingIntervalRef.current) {
clearInterval(pingIntervalRef.current);
}
});
} catch (error) {
console.error('Error creating WebSocket:', error);
setWsStatus('error');
}
2024-12-25 13:46:20 +00:00
};
2024-12-27 15:23:39 +00:00
// Initial connection
setupWebSocket();
2024-12-25 13:46:20 +00:00
2024-12-27 12:52:55 +00:00
return () => {
mountedRef.current = false;
if (pingIntervalRef.current) {
clearInterval(pingIntervalRef.current);
}
if (wsRef.current) {
wsRef.current.close();
}
2024-12-23 19:32:28 +00:00
};
2024-12-27 15:23:39 +00:00
}, []);
2024-11-25 14:33:01 +00:00
return (
<section>
2024-12-25 13:46:20 +00:00
{wsStatus === 'error' && (
<div className="text-red-500 text-center mb-2">
Connection error. Please refresh the page.
</div>
)}
{isConfettiActive && (
<div className="fixed top-0 left-0 z-50">
<ReactConfetti
width={width}
height={height}
recycle={false}
numberOfPieces={1000}
tweenDuration={10000}
run={true}
colors={[
'linear-gradient(45deg, #5D5D72, #8589DE)',
'linear-gradient(45deg, #E1E0FF, #575992)',
'#8589DE',
'#575992',
'#E1E0FF',
'#BA1A1A',
]}
/>
</div>
)}
2024-11-25 14:33:01 +00:00
<div className="container">
2024-12-27 12:52:55 +00:00
<div className="flex flex-col items-center">
2024-12-25 13:46:20 +00:00
<div className="-mb-[90px] z-10">
2024-12-27 15:23:39 +00:00
<LotterySlotCounter numberString={currentNumber} isAnimating={isSlotCounterAnimating} />
2024-12-25 13:46:20 +00:00
</div>
<div className="flex gap-6 bg-lightPrimaryContainer rounded-[12px] flex-1 w-full items-center justify-center pt-[122px] pb-[62px]">
2024-12-23 19:32:28 +00:00
<LotteryWinnersList winners={winners} />
2024-11-25 14:33:01 +00:00
</div>
</div>
</div>
</section>
);
};
export default LotteryWinnersSection;