From 8e92cb1fe3186f5f5bed96efec3d3361cdbc827e Mon Sep 17 00:00:00 2001 From: Darcy Ye Date: Tue, 13 Jun 2023 16:57:40 +0800 Subject: [PATCH] feat(console): create tenant modal (#4019) --- .../images/create-tenant-header-dark.svg | 36 +++++ .../assets/images/create-tenant-header.svg | 36 +++++ .../Main/CreateTenantModal/index.module.scss | 8 ++ .../pages/Main/CreateTenantModal/index.tsx | 125 ++++++++++++++++++ .../components/RadioGroup/Radio.module.scss | 4 +- .../TenantEnvTag/index.module.scss | 0 .../{components => }/TenantEnvTag/index.tsx | 0 .../Topbar/TenantSelector/index.tsx | 37 +++++- 8 files changed, 241 insertions(+), 5 deletions(-) create mode 100644 packages/console/src/assets/images/create-tenant-header-dark.svg create mode 100644 packages/console/src/assets/images/create-tenant-header.svg create mode 100644 packages/console/src/cloud/pages/Main/CreateTenantModal/index.module.scss create mode 100644 packages/console/src/cloud/pages/Main/CreateTenantModal/index.tsx rename packages/console/src/containers/AppContent/components/Topbar/TenantSelector/{components => }/TenantEnvTag/index.module.scss (100%) rename packages/console/src/containers/AppContent/components/Topbar/TenantSelector/{components => }/TenantEnvTag/index.tsx (100%) diff --git a/packages/console/src/assets/images/create-tenant-header-dark.svg b/packages/console/src/assets/images/create-tenant-header-dark.svg new file mode 100644 index 000000000..bb189e81c --- /dev/null +++ b/packages/console/src/assets/images/create-tenant-header-dark.svg @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/packages/console/src/assets/images/create-tenant-header.svg b/packages/console/src/assets/images/create-tenant-header.svg new file mode 100644 index 000000000..286eca112 --- /dev/null +++ b/packages/console/src/assets/images/create-tenant-header.svg @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/packages/console/src/cloud/pages/Main/CreateTenantModal/index.module.scss b/packages/console/src/cloud/pages/Main/CreateTenantModal/index.module.scss new file mode 100644 index 000000000..2ee656d27 --- /dev/null +++ b/packages/console/src/cloud/pages/Main/CreateTenantModal/index.module.scss @@ -0,0 +1,8 @@ +@use '@/scss/underscore' as _; + +.description { + color: var(--color-text-secondary); + font: var(--font-body-2); + margin-top: _.unit(0.5); +} + diff --git a/packages/console/src/cloud/pages/Main/CreateTenantModal/index.tsx b/packages/console/src/cloud/pages/Main/CreateTenantModal/index.tsx new file mode 100644 index 000000000..d1a69b7db --- /dev/null +++ b/packages/console/src/cloud/pages/Main/CreateTenantModal/index.tsx @@ -0,0 +1,125 @@ +import type { AdminConsoleKey } from '@logto/phrases'; +import { Theme } from '@logto/schemas'; +import { TenantTag, type TenantInfo } from '@logto/schemas/models'; +import { Controller, FormProvider, useForm } from 'react-hook-form'; +import { toast } from 'react-hot-toast'; +import { useTranslation } from 'react-i18next'; +import Modal from 'react-modal'; + +import CreateTenantHeaderIconDark from '@/assets/images/create-tenant-header-dark.svg'; +import CreateTenantHeaderIcon from '@/assets/images/create-tenant-header.svg'; +import { useCloudApi } from '@/cloud/hooks/use-cloud-api'; +import Button from '@/components/Button'; +import FormField from '@/components/FormField'; +import ModalLayout from '@/components/ModalLayout'; +import RadioGroup, { Radio } from '@/components/RadioGroup'; +import TextInput from '@/components/TextInput'; +import useTheme from '@/hooks/use-theme'; +import * as modalStyles from '@/scss/modal.module.scss'; + +import * as styles from './index.module.scss'; + +type Props = { + isOpen: boolean; + onClose: (tenant?: TenantInfo) => void; +}; + +const tagOptions: Array<{ title: AdminConsoleKey; value: TenantTag }> = [ + { + title: 'tenants.create_modal.environment_tag_development', + value: TenantTag.Development, + }, + { + title: 'tenants.create_modal.environment_tag_staging', + value: TenantTag.Staging, + }, + { + title: 'tenants.create_modal.environment_tag_production', + value: TenantTag.Production, + }, +]; + +function CreateTenantModal({ isOpen, onClose }: Props) { + const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' }); + const theme = useTheme(); + const methods = useForm>({ + defaultValues: { tag: TenantTag.Development }, + }); + const { + control, + handleSubmit, + formState: { errors, isSubmitting }, + register, + } = methods; + + const cloudApi = useCloudApi(); + + const onSubmit = handleSubmit(async (data) => { + try { + const { name, tag } = data; + const newTenant = await cloudApi + .post('/api/tenants', { json: { name, tag } }) + .json(); + onClose(newTenant); + } catch (error: unknown) { + toast.error(error instanceof Error ? error.message : String(error)); + } + }); + + return ( + { + onClose(); + }} + > + : + } + footer={ +