From ecc8fd405ecc6bd20fcc94ea74f541bbeecea9cf Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Tue, 22 Feb 2022 04:59:52 -0500 Subject: [PATCH] Delete Rule WIP --- packages/components/src/api/ApiConfig.ts | 10 ++- .../twitch_event_rules/GetTwitchEventRule.ts | 62 +++++++++++++++++ packages/storyteller/src/App.tsx | 6 ++ .../tts_configs/TtsConfigsDeleteRulePage.tsx | 57 ++++++++++++++++ .../pages/tts_configs/TtsConfigsIndexPage.tsx | 7 +- .../tts_configs/TwitchEventRuleElement.tsx | 68 ++++++++++++------- 6 files changed, 181 insertions(+), 29 deletions(-) create mode 100644 packages/components/src/api/storyteller/twitch_event_rules/GetTwitchEventRule.ts create mode 100644 packages/storyteller/src/pages/tts_configs/TtsConfigsDeleteRulePage.tsx diff --git a/packages/components/src/api/ApiConfig.ts b/packages/components/src/api/ApiConfig.ts index 51698d3c13..40d4a43515 100644 --- a/packages/components/src/api/ApiConfig.ts +++ b/packages/components/src/api/ApiConfig.ts @@ -357,12 +357,16 @@ class ApiConfig { return `${this.getScheme()}://${this.getNewApiHost()}/twitch/event_rule/create`; } - deleteTwitchEventRule(eventRuleToken: string) : string { - return `${this.getScheme()}://${this.getNewApiHost()}/twitch/event_rule/delete/${eventRuleToken}`; + getTwitchEventRule(eventRuleToken: string) : string { + return `${this.getScheme()}://${this.getNewApiHost()}/twitch/event_rule/${eventRuleToken}/info`; } editTwitchEventRule(eventRuleToken: string) : string { - return `${this.getScheme()}://${this.getNewApiHost()}/twitch/event_rule/update/${eventRuleToken}`; + return `${this.getScheme()}://${this.getNewApiHost()}/twitch/event_rule/${eventRuleToken}/update`; + } + + deleteTwitchEventRule(eventRuleToken: string) : string { + return `${this.getScheme()}://${this.getNewApiHost()}/twitch/event_rule/${eventRuleToken}/delete`; } obsEventsWebsocket(twitchUsername: string) : string { diff --git a/packages/components/src/api/storyteller/twitch_event_rules/GetTwitchEventRule.ts b/packages/components/src/api/storyteller/twitch_event_rules/GetTwitchEventRule.ts new file mode 100644 index 0000000000..b8f431b407 --- /dev/null +++ b/packages/components/src/api/storyteller/twitch_event_rules/GetTwitchEventRule.ts @@ -0,0 +1,62 @@ +import { ApiConfig } from "../../ApiConfig"; +import { EventMatchPredicate } from "./shared/EventMatchPredicate"; +import { EventResponse } from "./shared/EventResponse"; +import { TwitchEventCategory } from "./shared/TwitchEventCategory"; + +export interface GetTwitchEventRuleSuccessResponse { + success: boolean, + twitch_event_rule: TwitchEventRule +} + +export interface TwitchEventRule { + token: string, + event_category: TwitchEventCategory, + event_match_predicate: EventMatchPredicate, + event_response: EventResponse, + user_specified_rule_order: number, + rule_is_disabled: boolean, + created_at: Date, + updated_at: Date, +} + +export interface GetTwitchEventRuleErrorResponse { + success: boolean, +} + +type GetTwitchEventRuleResponse = GetTwitchEventRuleSuccessResponse | GetTwitchEventRuleErrorResponse; + +export function GetTwitchEventRuleIsOk(response: GetTwitchEventRuleResponse): response is GetTwitchEventRuleSuccessResponse { + return response?.success === true; +} + +export function GetTwitchEventRuleIsError(response: GetTwitchEventRuleResponse): response is GetTwitchEventRuleErrorResponse { + return response?.success === false; +} + +export async function GetTwitchEventRule(ruleToken: string) : Promise +{ + const endpoint = new ApiConfig().getTwitchEventRule(ruleToken); + + return await fetch(endpoint, { + method: 'GET', + headers: { + 'Accept': 'application/json', + }, + credentials: 'include', + }) + .then(res => res.json()) + .then(res => { + if (!res) { + return { success : false }; // TODO: This loses error semantics and is deprecated + } + + if (res && 'success' in res) { + return res; + } else { + return { success : false }; + } + }) + .catch(e => { + return { success : false }; + }); +} diff --git a/packages/storyteller/src/App.tsx b/packages/storyteller/src/App.tsx index 9f881d44c3..83ebb81608 100644 --- a/packages/storyteller/src/App.tsx +++ b/packages/storyteller/src/App.tsx @@ -17,6 +17,7 @@ import { SessionWrapper } from '@storyteller/components/src/session/SessionWrapp import { TopNav } from './layout/TopNav'; import { initReactI18next } from 'react-i18next'; import { TtsConfigsCreateRulePage } from './pages/tts_configs/TtsConfigsCreateRulePage'; +import { TtsConfigsDeleteRulePage } from './pages/tts_configs/TtsConfigsDeleteRulePage'; i18n .use(initReactI18next) // passes i18n down to react-i18next @@ -104,6 +105,11 @@ class App extends React.Component { sessionWrapper={this.state.sessionWrapper} /> + + + (undefined); + + const getTwitchEventRule = useCallback(async (token: string) => { + const response = await GetTwitchEventRule(token); + + if (GetTwitchEventRuleIsOk(response)) { + setTwitchEventRule(response.twitch_event_rule); + } else if (GetTwitchEventRuleIsError(response)) { + // TODO + } + }, []); + + useEffect(() => { + getTwitchEventRule(token); + }, [getTwitchEventRule, token]); + + if (!props.sessionWrapper.isLoggedIn()) { + return

Must Log In

; + } + + if (twitchEventRule === undefined) { + return

Loading...

; + } + + return ( + <> +
+

Delete Rule ?

+
+
+ +
+ +
+
+ + ) +} + +export { TtsConfigsDeleteRulePage } \ No newline at end of file diff --git a/packages/storyteller/src/pages/tts_configs/TtsConfigsIndexPage.tsx b/packages/storyteller/src/pages/tts_configs/TtsConfigsIndexPage.tsx index d96916f8f9..16e011d331 100644 --- a/packages/storyteller/src/pages/tts_configs/TtsConfigsIndexPage.tsx +++ b/packages/storyteller/src/pages/tts_configs/TtsConfigsIndexPage.tsx @@ -3,8 +3,9 @@ import { SessionWrapper } from '@storyteller/components/src/session/SessionWrapp import { ListTwitchEventRules, ListTwitchEventRulesIsError, ListTwitchEventRulesIsOk, TwitchEventRule } from '@storyteller/components/src/api/storyteller/twitch_event_rules/ListTwitchEventRules'; import { TwitchEventRuleElement } from './TwitchEventRuleElement'; import { TwitchEventCategory } from '@storyteller/components/src/api/storyteller/twitch_event_rules/shared/TwitchEventCategory'; +import { DiscordLink } from '@storyteller/components/src/elements/DiscordLink'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; -import { faBox, faCircleNotch, faCoins, faDonate, faExclamation, faGem, faLightbulb, faTerminal } from '@fortawesome/free-solid-svg-icons'; +import { faBox, faDonate, faGem, faLightbulb, faTerminal } from '@fortawesome/free-solid-svg-icons'; interface Props { sessionWrapper: SessionWrapper, @@ -103,7 +104,9 @@ function TtsConfigsIndexPage(props: Props) {

We want to build the things you want

-

Can you think of anything we haven't provided? Please let us know in Discord so that we can build it!

+

Can you think of anything we haven't + provided? so + that we can build it!



diff --git a/packages/storyteller/src/pages/tts_configs/TwitchEventRuleElement.tsx b/packages/storyteller/src/pages/tts_configs/TwitchEventRuleElement.tsx index b3e6f48442..ab9ac6136e 100644 --- a/packages/storyteller/src/pages/tts_configs/TwitchEventRuleElement.tsx +++ b/packages/storyteller/src/pages/tts_configs/TwitchEventRuleElement.tsx @@ -1,13 +1,17 @@ -import React, { useCallback, useEffect, useState } from 'react'; +import React from 'react'; import { TwitchEventRule } from '@storyteller/components/src/api/storyteller/twitch_event_rules/ListTwitchEventRules'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; -import { faArrowDown, faArrowUp, faRulerVertical } from '@fortawesome/free-solid-svg-icons'; +import { faArrowDown, faArrowUp, faEdit, faTrash } from '@fortawesome/free-solid-svg-icons'; +import { Link } from 'react-router-dom'; interface Props { rule: TwitchEventRule, + hideButtons?: boolean, } function TwitchEventRuleElement(props: Props) { + const hideButtons = !!props.hideButtons; + let title = "Not Set"; let subtitle = <>; let description = <>; @@ -55,6 +59,43 @@ function TwitchEventRuleElement(props: Props) { ); } + const editUrl = `/tts_configs/edit/${props.rule.token}`; + const deleteUrl = `/tts_configs/delete/${props.rule.token}`; + + let buttons = <>; + if (!hideButtons) { + buttons = ( + <> +
+

+ +  Up + +

+

+ +  Down + +

+

+ + +  Edit + + +

+

+ + + Delete + + +

+
+ + ) + } + return (
@@ -66,28 +107,7 @@ function TwitchEventRuleElement(props: Props) { {description}

-
-

- - - -

-

- - - -

-

- - Edit - -

-

- - Delete - -

-
+ {buttons}