From 8e13dd5746b99f311bb44deb1425ae31f3679184 Mon Sep 17 00:00:00 2001 From: Wang Sijie Date: Thu, 21 Apr 2022 12:38:54 +0800 Subject: [PATCH] feat(console): user role select (#589) --- .../UserDetails/components/RoleSelect.tsx | 44 ++++++++++++++++ .../console/src/pages/UserDetails/index.tsx | 15 ++++-- packages/core/src/routes/admin-user.test.ts | 52 ++++++------------- packages/core/src/routes/admin-user.ts | 51 ++++++------------ packages/phrases/src/locales/en.ts | 4 ++ packages/phrases/src/locales/zh-cn.ts | 4 ++ 6 files changed, 94 insertions(+), 76 deletions(-) create mode 100644 packages/console/src/pages/UserDetails/components/RoleSelect.tsx diff --git a/packages/console/src/pages/UserDetails/components/RoleSelect.tsx b/packages/console/src/pages/UserDetails/components/RoleSelect.tsx new file mode 100644 index 000000000..333b2a9ef --- /dev/null +++ b/packages/console/src/pages/UserDetails/components/RoleSelect.tsx @@ -0,0 +1,44 @@ +import React, { useMemo } from 'react'; +import { useTranslation } from 'react-i18next'; + +import Select from '@/components/Select'; + +type Props = { + value?: string[]; + onChange?: (value: string[]) => void; +}; + +const roleDefault = 'default'; +const roleAdmin = 'admin'; + +const RoleSelect = ({ value, onChange }: Props) => { + const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' }); + + const options = useMemo( + () => [ + { value: roleDefault, title: t('user_details.roles.default') }, + { value: roleAdmin, title: t('user_details.roles.admin') }, + ], + [t] + ); + + const selectValue = useMemo(() => { + if (!value?.length) { + return roleDefault; + } + + if (value.length === 1 && value[0] === 'admin') { + return roleAdmin; + } + + throw new Error('Unsupported user role value'); + }, [value]); + + const handleChange = (value: string) => { + onChange?.(value === roleAdmin ? ['admin'] : []); + }; + + return