]> git.localhorst.tv Git - alttp.git/blobdiff - resources/js/components/twitch-bot/Controls.js
chat bot settings
[alttp.git] / resources / js / components / twitch-bot / Controls.js
index 0aea916cf04d698a879bc6bed886a88ebb426e08..bdc89380fe75c07a4a1ad22e8dd2587661d1ac1b 100644 (file)
@@ -1,20 +1,34 @@
 import axios from 'axios';
 import React from 'react';
-import { Alert, Col, Form, Row } from 'react-bootstrap';
+import { Alert, Button, Col, Form, Row } from 'react-bootstrap';
 import { useTranslation } from 'react-i18next';
 import toastr from 'toastr';
 
+import ChatSettingsForm from './ChatSettingsForm';
 import ChannelSelect from '../common/ChannelSelect';
 import ToggleSwitch from '../common/ToggleSwitch';
 
 const Controls = () => {
        const [channel, setChannel] = React.useState(null);
+       const [chatText, setChatText] = React.useState('');
 
        const { t } = useTranslation();
 
-       const join = React.useCallback(async () => {
+       const chat = React.useCallback(async (text, bot_nick) => {
                try {
-                       const rsp = await axios.post(`/api/channels/${channel.id}/join`);
+                       await axios.post(`/api/channels/${channel.id}/chat`, {
+                               text,
+                               bot_nick,
+                       });
+                       toastr.success(t('twitchBot.chatSuccess'));
+               } catch (e) {
+                       toastr.error(t('twitchBot.chatError'));
+               }
+       }, [channel, chatText, t]);
+
+       const join = React.useCallback(async (bot_nick) => {
+               try {
+                       const rsp = await axios.post(`/api/channels/${channel.id}/join`, { bot_nick });
                        setChannel(rsp.data);
                        toastr.success(t('twitchBot.joinSuccess'));
                } catch (e) {
@@ -22,9 +36,9 @@ const Controls = () => {
                }
        }, [channel, t]);
 
-       const part = React.useCallback(async () => {
+       const part = React.useCallback(async (bot_nick) => {
                try {
-                       const rsp = await axios.post(`/api/channels/${channel.id}/part`);
+                       const rsp = await axios.post(`/api/channels/${channel.id}/part`, { bot_nick });
                        setChannel(rsp.data);
                        toastr.success(t('twitchBot.partSuccess'));
                } catch (e) {
@@ -32,6 +46,16 @@ const Controls = () => {
                }
        }, [channel, t]);
 
+       const saveChatSettings = React.useCallback(async (values) => {
+               try {
+                       const rsp = await axios.post(`/api/channels/${channel.id}/chat-settings`, values);
+                       setChannel(rsp.data);
+                       toastr.success(t('twitchBot.saveSuccess'));
+               } catch (e) {
+                       toastr.error(t('twitchBot.saveError'));
+               }
+       }, [channel, t]);
+
        return <>
                <Row className="mb-4">
                        <Form.Group as={Col} md={6}>
@@ -44,27 +68,80 @@ const Controls = () => {
                                        value={channel ? channel.id : ''}
                                />
                        </Form.Group>
-                       {channel ?
-                               <Form.Group as={Col} md={6}>
-                                       <Form.Label>{t('twitchBot.join')}</Form.Label>
+                       {channel ? <>
+                               <Form.Group as={Col} md={3}>
+                                       <Form.Label>{t('twitchBot.joinApp')}</Form.Label>
                                        <div>
                                                <Form.Control
                                                        as={ToggleSwitch}
                                                        onChange={({ target: { value } }) => {
                                                                if (value) {
-                                                                       join();
+                                                                       join('localhorsttv');
                                                                } else {
-                                                                       part();
+                                                                       part('localhorsttv');
                                                                }
                                                        }}
                                                        value={channel.join}
                                                />
                                        </div>
                                </Form.Group>
-                       : null}
+                               <Form.Group as={Col} md={3}>
+                                       <Form.Label>{t('twitchBot.joinChat')}</Form.Label>
+                                       <div>
+                                               <Form.Control
+                                                       as={ToggleSwitch}
+                                                       onChange={({ target: { value } }) => {
+                                                               if (value) {
+                                                                       join('horstiebot');
+                                                               } else {
+                                                                       part('horstiebot');
+                                                               }
+                                                       }}
+                                                       value={channel.chat}
+                                               />
+                                       </div>
+                               </Form.Group>
+                       </> : null}
                </Row>
                {channel ?
-                       <div />
+                       <Row>
+                               <Form.Group as={Col} md={6}>
+                                       <Form.Label>{t('twitchBot.chat')}</Form.Label>
+                                       <Form.Control
+                                               as="textarea"
+                                               onChange={({ target: { value } }) => {
+                                                       setChatText(value);
+                                               }}
+                                               value={chatText}
+                                       />
+                                       <div className="button-bar">
+                                               <Button
+                                                       className="mt-2"
+                                                       disabled={!chatText || !channel.join}
+                                                       onClick={() => {
+                                                               if (chatText) chat(chatText, 'localhorsttv');
+                                                       }}
+                                                       variant="twitch"
+                                               >
+                                                       {t('twitchBot.sendApp')}
+                                               </Button>
+                                               <Button
+                                                       className="mt-2"
+                                                       disabled={!chatText || !channel.chat}
+                                                       onClick={() => {
+                                                               if (chatText) chat(chatText, 'horstiebot');
+                                                       }}
+                                                       variant="twitch"
+                                               >
+                                                       {t('twitchBot.sendChat')}
+                                               </Button>
+                                       </div>
+                               </Form.Group>
+                               <Col md={6}>
+                                       <h3>{t('twitchBot.chatSettings')}</h3>
+                                       <ChatSettingsForm channel={channel} onSubmit={saveChatSettings} />
+                               </Col>
+                       </Row>
                :
                        <Alert variant="info">
                                {t('twitchBot.selectChannel')}