From a766183f01d9977512b38c3975de7fd648ab9724 Mon Sep 17 00:00:00 2001 From: Paul Makles Date: Wed, 6 Jul 2022 13:15:33 +0100 Subject: [PATCH] feat: port `CreateBot` to modal form fixes #723 --- src/controllers/modals/ModalController.tsx | 4 +- .../modals/components/CreateBot.tsx | 42 +++++++++ .../modals/components/legacy/CreateBot.tsx | 86 ------------------- 3 files changed, 44 insertions(+), 88 deletions(-) create mode 100644 src/controllers/modals/components/CreateBot.tsx delete mode 100644 src/controllers/modals/components/legacy/CreateBot.tsx diff --git a/src/controllers/modals/ModalController.tsx b/src/controllers/modals/ModalController.tsx index b2667f98..0e4ac29a 100644 --- a/src/controllers/modals/ModalController.tsx +++ b/src/controllers/modals/ModalController.tsx @@ -21,6 +21,7 @@ import Changelog from "./components/Changelog"; import ChannelInfo from "./components/ChannelInfo"; import Clipboard from "./components/Clipboard"; import Confirmation from "./components/Confirmation"; +import CreateBot from "./components/CreateBot"; import CreateCategory from "./components/CreateCategory"; import CreateChannel from "./components/CreateChannel"; import CreateGroup from "./components/CreateGroup"; @@ -45,7 +46,6 @@ import ShowToken from "./components/ShowToken"; import SignOutSessions from "./components/SignOutSessions"; import SignedOut from "./components/SignedOut"; import UserPicker from "./components/UserPicker"; -import { CreateBotModal } from "./components/legacy/CreateBot"; import { OnboardingModal } from "./components/legacy/Onboarding"; import { UserProfile } from "./components/legacy/UserProfile"; import { Modal } from "./types"; @@ -258,7 +258,7 @@ export const modalController = new ModalControllerExtended({ create_invite: CreateInvite, create_role: CreateRole, create_server: CreateServer, - create_bot: CreateBotModal, + create_bot: CreateBot, custom_status: CustomStatus, delete_message: DeleteMessage, error: Error, diff --git a/src/controllers/modals/components/CreateBot.tsx b/src/controllers/modals/components/CreateBot.tsx new file mode 100644 index 00000000..e5bcf791 --- /dev/null +++ b/src/controllers/modals/components/CreateBot.tsx @@ -0,0 +1,42 @@ +import { Text } from "preact-i18n"; + +import { ModalForm } from "@revoltchat/ui"; + +import { useClient } from "../../client/ClientController"; +import { mapError } from "../../client/jsx/error"; +import { ModalProps } from "../types"; + +/** + * Bot creation modal + */ +export default function CreateBot({ + onCreate, + ...props +}: ModalProps<"create_bot">) { + const client = useClient(); + + return ( + } + schema={{ + name: "text", + }} + data={{ + name: { + field: () as React.ReactChild, + }, + }} + callback={async ({ name }) => { + const { bot } = await client.bots + .create({ name }) + .catch(mapError); + + onCreate(bot); + }} + submit={{ + children: , + }} + /> + ); +} diff --git a/src/controllers/modals/components/legacy/CreateBot.tsx b/src/controllers/modals/components/legacy/CreateBot.tsx deleted file mode 100644 index 7c977cb2..00000000 --- a/src/controllers/modals/components/legacy/CreateBot.tsx +++ /dev/null @@ -1,86 +0,0 @@ -import { SubmitHandler, useForm } from "react-hook-form"; -import { API } from "revolt.js"; - -import { Text } from "preact-i18n"; -import { useState } from "preact/hooks"; - -import { Category, Modal } from "@revoltchat/ui"; - -import { noopTrue } from "../../../../lib/js"; - -import { I18nError } from "../../../../context/Locale"; - -import FormField from "../../../../pages/login/FormField"; -import { useClient } from "../../../client/ClientController"; -import { takeError } from "../../../client/jsx/error"; -import { modalController } from "../../ModalController"; -import { ModalProps } from "../../types"; - -interface FormInputs { - name: string; -} - -export function CreateBotModal({ - onCreate, - ...props -}: ModalProps<"create_bot">) { - const client = useClient(); - const { handleSubmit, register, errors } = useForm(); - const [error, setError] = useState(undefined); - - const onSubmit: SubmitHandler = async ({ name }) => { - try { - const { bot } = await client.bots.create({ name }); - onCreate(bot); - modalController.close(); - } catch (err) { - setError(takeError(err)); - } - }; - - return ( - } - actions={[ - { - confirmation: true, - palette: "accent", - onClick: async () => { - await handleSubmit(onSubmit)(); - return true; - }, - children: , - }, - { - palette: "plain", - onClick: noopTrue, - children: , - }, - ]}> - {/* Preact / React typing incompatabilities */} -
{ - e.preventDefault(); - handleSubmit( - onSubmit, - // eslint-disable-next-line @typescript-eslint/no-explicit-any - )(e as any); - }}> - - {error && ( - - {" "} - · - - )} - -
- ); -}