Skip to content

Commit

Permalink
feat(next/web): allow customer service agent to create user
Browse files Browse the repository at this point in the history
  • Loading branch information
sdjdd committed Mar 29, 2024
1 parent c0995f9 commit 4d66199
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 20 deletions.
55 changes: 44 additions & 11 deletions next/web/src/App/Admin/Settings/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useMemo } from 'react';
import { Route, Routes } from 'react-router-dom';

import { SubMenu, MenuDataItem } from '@/components/Page';
import { SubMenu } from '@/components/Page';

import { NewUser } from './Users';
import { Members } from './Members';
Expand Down Expand Up @@ -35,12 +36,19 @@ import { NewTicketFormNoteTranslation } from './TicketFormNotes/NewTicketFormNot
import { EditTicketFormNoteTranslation } from './TicketFormNotes/EditTranslation';
import { EditSupportEmail, NewSupportEmail, SupportEmailList } from './SupportEmails';
import { useCurrentUserIsAdmin } from '@/leancloud';
import { Result } from '@/components/antd';
import { EvaluationConfig } from './Evaluation';
import { MergeUser } from './Users/MergeUser';
import { ExportTicketTask } from './ExportTicket/Tasks';

const SettingRoutes = () => (
<Routes>
<Route path="/users">
<Route path="new" element={<NewUser />} />
</Route>
</Routes>
);

const AdminSettingRoutes = () => (
<Routes>
<Route path="/users">
<Route index element="Under Construction" />
Expand Down Expand Up @@ -146,14 +154,24 @@ const SettingRoutes = () => (
</Routes>
);

const routeGroups: MenuDataItem[] = [
interface MenuItem {
name: string;
path?: string;
key?: string;
children?: Omit<MenuItem, 'children'>[];
adminOnly?: boolean;
}

const routeGroups: MenuItem[] = [
{
name: '用户',
key: 'users',
adminOnly: false,
children: [
{
name: '创建用户',
path: 'users/new',
adminOnly: false,
},
{
name: '合并用户',
Expand Down Expand Up @@ -267,14 +285,29 @@ const routeGroups: MenuDataItem[] = [
},
];

function filterDeep<T extends { children?: T[] }>(
items: T[],
predicate: (item: T) => boolean
): T[] {
return items
.map((item) => {
if (item.children) {
return {
...item,
children: filterDeep(item.children, predicate),
};
}
return item;
})
.filter(predicate);
}

export default function Setting() {
const isAdmin = useCurrentUserIsAdmin();
if (!isAdmin) {
return <Result status="403" title="403" subTitle="该页面需要管理员角色" />;
}
return (
<SubMenu menus={routeGroups}>
<SettingRoutes />
</SubMenu>
);
const menus = useMemo(() => {
if (isAdmin) return routeGroups;
return filterDeep(routeGroups, (menu) => menu.adminOnly === false);
}, [isAdmin]);

return <SubMenu menus={menus}>{isAdmin ? <AdminSettingRoutes /> : <SettingRoutes />}</SubMenu>;
}
12 changes: 4 additions & 8 deletions next/web/src/App/Admin/Sidebar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import { NavLink } from 'react-router-dom';
import { AiOutlineContainer, AiOutlineSetting } from 'react-icons/ai';
import { HiOutlinePlus, HiOutlineTicket } from 'react-icons/hi';
import { MdOutlineAnalytics } from 'react-icons/md';
import { BsChatSquareDots } from 'react-icons/bs';
import cx from 'classnames';

import { Tooltip } from '@/components/antd';
import { useCurrentUserIsAdmin, useCurrentUserIsCustomerService } from '@/leancloud';
import { useCurrentUserIsCustomerService } from '@/leancloud';
import { Feedback } from '../Feedback';
import { CurrentUserSection } from '../CurrentUserSection';
import { RequirePermission } from '@/components/RequirePermission';
Expand Down Expand Up @@ -36,7 +35,6 @@ function Path({ to, children, title }: { to: string; children: ReactNode; title?

export function Sidebar(props: ComponentPropsWithoutRef<'aside'>) {
const isCustomerService = useCurrentUserIsCustomerService();
const isAdmin = useCurrentUserIsAdmin();

return (
<aside
Expand Down Expand Up @@ -65,11 +63,9 @@ export function Sidebar(props: ComponentPropsWithoutRef<'aside'>) {
</Path>
</RequirePermission>
}
{isAdmin && (
<Path to="/admin/settings" title="设置">
<AiOutlineSetting className="m-auto w-5 h-5" />
</Path>
)}
<Path to="/admin/settings" title="设置">
<AiOutlineSetting className="m-auto w-5 h-5" />
</Path>
</section>
<section />
<section className="p-3">
Expand Down
2 changes: 1 addition & 1 deletion next/web/src/components/Page/SubMenu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type MenuDataItem = {
name: ReactNode;
path?: string;
key?: string;
children?: MenuDataItem[];
children?: Omit<MenuDataItem, 'children'>[];
};

interface SubMenuProps {
Expand Down

0 comments on commit 4d66199

Please sign in to comment.