1 import axios from 'axios';
2 import React from 'react';
3 import { Alert, Col, Container, Form, Navbar, Row } from 'react-bootstrap';
4 import { Helmet } from 'react-helmet';
5 import { useTranslation } from 'react-i18next';
6 import { useParams } from 'react-router-dom';
7 import toastr from 'toastr';
9 import User from '../app/User';
10 import ChannelSelect from '../components/common/ChannelSelect';
11 import GuessingGameControls from '../components/twitch-bot/GuessingGameControls';
12 import GuessingGuess from '../components/twitch-bot/GuessingGuess';
13 import GuessingWinner from '../components/twitch-bot/GuessingWinner';
14 import { patchGuess, patchWinner } from '../helpers/Channel';
16 export const Component = () => {
17 const [channel, setChannel] = React.useState(null);
18 const [guesses, setGuesses] = React.useState([]);
19 const [winners, setWinners] = React.useState([]);
21 const { channelId } = useParams();
22 const { t } = useTranslation();
24 React.useEffect(() => {
25 if (!channelId) return;
26 const fetchChannel = async () => {
27 const response = await axios.get(`/api/channels`, {
33 if (response.data.length) {
34 setChannel(response.data[0]);
40 React.useEffect(() => {
46 if (channel.guessing_type) {
47 axios.get(`/api/channels/${channel.id}/guessing-game/${channel.guessing_type}`)
49 res.data.guesses.forEach(g => {
50 setGuesses(gs => patchGuess(gs, g));
52 res.data.winners.forEach(w => {
53 setWinners(ws => patchGuess(ws, w));
57 window.Echo.private(`Channel.${channel.id}`)
58 .listen('.GuessingGuessCreated', (e) => {
59 setGuesses(gs => patchGuess(gs, e.model));
61 .listen('.GuessingWinnerCreated', (e) => {
62 setWinners(ws => patchWinner(ws, e.model));
64 .listen('.ChannelUpdated', (e) => {
65 setChannel(c => ({ ...c, ...e.model }));
68 window.Echo.leave(`Channel.${channel.id}`);
70 }, [channel && channel.id]);
72 React.useEffect(() => {
73 const cutoff = channel && channel.guessing_start;
75 setGuesses(gs => gs.filter(g => g.created_at >= cutoff));
76 setWinners(ws => ws.filter(w => w.created_at >= cutoff));
78 }, [channel && channel.guessing_start]);
80 const onCancel = React.useCallback(async () => {
82 const rsp = await axios.post(
83 `/api/channels/${channel.id}/guessing-game/gtbk`,
88 toastr.error(t('twitchBot.controlError'));
92 const onSolve = React.useCallback(async (solution) => {
94 const rsp = await axios.post(
95 `/api/channels/${channel.id}/guessing-game/gtbk`,
96 { action: 'solve', solution },
100 toastr.error(t('twitchBot.controlError'));
104 const onStart = React.useCallback(async () => {
106 const rsp = await axios.post(
107 `/api/channels/${channel.id}/guessing-game/gtbk`,
110 setChannel(rsp.data);
112 toastr.error(t('twitchBot.controlError'));
116 const onStop = React.useCallback(async () => {
118 const rsp = await axios.post(
119 `/api/channels/${channel.id}/guessing-game/gtbk`,
122 setChannel(rsp.data);
124 toastr.error(t('twitchBot.controlError'));
130 <title>Guessing Game Controls</title>
132 <Navbar id="header" bg="dark" variant="dark">
139 onChange={({ channel }) => { setChannel(channel); }}
140 readOnly={!!(channelId && channel)}
141 value={channel ? channel.id : channelId}
149 <GuessingGameControls
158 <h3 className="mt-3">{t('twitchBot.guessingGame.winners')}</h3>
159 {winners.map(winner =>
160 <GuessingWinner key={winner.id} winner={winner} />
164 <h3 className="mt-3">{t('twitchBot.guessingGame.guesses')}</h3>
165 {guesses.map(guess =>
166 <GuessingGuess guess={guess} key={guess.id} />
170 <Alert variant="info">
171 {t('twitchBot.selectChannel')}