Skip to content

Commit

Permalink
feat(next/web): collaborator privileges
Browse files Browse the repository at this point in the history
  • Loading branch information
sdjdd committed Dec 25, 2023
1 parent b94b32d commit e4e973e
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 5 deletions.
54 changes: 54 additions & 0 deletions next/web/src/App/Admin/Settings/Collaborators/Privileges.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Checkbox, Spin } from 'antd';

import {
CollaboratorPrivileges,
useCollaboratorPrivileges,
useSetCollaboratorPrivileges,
} from '@/api/collaborator';

interface PrivilegeItem {
key?: keyof CollaboratorPrivileges;
title: string;
}

export function Privileges() {
const { data: privileges } = useCollaboratorPrivileges();
const { mutate, isLoading: isMutating } = useSetCollaboratorPrivileges();

const privilegeItems: PrivilegeItem[] = [
{
title: '查看分配给其的工单',
},
{
title: '创建内部回复',
},
{
key: 'createPublicReply',
title: '创建公开回复',
},
];

if (!privileges) {
return <Spin />;
}

return (
<ul>
{privilegeItems.map(({ key, title }, index) => {
const enabled = !!privileges['createPublicReply'];
return (
<li key={key || index} className="space-x-1">
<span className={key && (enabled ? undefined : 'line-through')}>{title}</span>
{key && (
<Checkbox
checked={enabled}
disabled={isMutating}
onChange={(e) => mutate({ ...privileges, [key]: e.target.checked })}
/>
)}
</li>
);
})}
</ul>
);
}
6 changes: 2 additions & 4 deletions next/web/src/App/Admin/Settings/Collaborators/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useCollaborators, useCreateCollaborator, useDeleteCollaborator } from '
import { Button, Modal, Table, Typography } from '@/components/antd';
import { UserSelect } from '@/components/common';
import { UserLabel } from '@/App/Admin/components';
import { Privileges } from './Privileges';

const { Column } = Table;
const { Title, Paragraph } = Typography;
Expand Down Expand Up @@ -77,10 +78,7 @@ export function Collaborators() {
<Typography>
<Title level={2}>协作者</Title>
<Paragraph>协作者可以:</Paragraph>
<ul>
<li>查看分配给其的工单</li>
<li>创建内部回复</li>
</ul>
<Privileges />
</Typography>

<div className="flex justify-end mb-5">
Expand Down
39 changes: 38 additions & 1 deletion next/web/src/api/collaborator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { useMutation, UseMutationOptions, useQuery, UseQueryOptions } from 'react-query';
import {
useMutation,
UseMutationOptions,
useQuery,
useQueryClient,
UseQueryOptions,
} from 'react-query';

import { http } from '@/leancloud';
import { UserSchema } from './user';
Expand All @@ -16,6 +22,20 @@ async function deleteCollaborator(userId: string) {
await http.delete(`/api/2/collaborators/${userId}`);
}

export interface CollaboratorPrivileges {
createPublicReply?: boolean;
}

async function getCollaboratorPrivileges() {
const res = await http.get<CollaboratorPrivileges>('/api/2/collaborators/privileges');
return res.data;
}

async function setCollaboratorPrivileges(value: CollaboratorPrivileges) {
const res = await http.put<CollaboratorPrivileges>('/api/2/collaborators/privileges', value);
return res.data;
}

export function useCollaborators(options?: UseQueryOptions<UserSchema[]>) {
return useQuery({
queryKey: ['collaborators'],
Expand All @@ -39,3 +59,20 @@ export function useDeleteCollaborator(options?: UseMutationOptions<void, Error,
...options,
});
}

export function useCollaboratorPrivileges() {
return useQuery({
queryKey: ['CollaboratorPrivileges'],
queryFn: getCollaboratorPrivileges,
});
}

export function useSetCollaboratorPrivileges() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: setCollaboratorPrivileges,
onSuccess: (data) => {
queryClient.setQueryData(['CollaboratorPrivileges'], data);
},
});
}

0 comments on commit e4e973e

Please sign in to comment.