From e0e044945f02451ad2b1db8041e91a738589cd5d Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Thu, 22 Aug 2024 01:03:11 +0800 Subject: [PATCH] feat: add invite user form --- src/client/routes/settings/workspace.tsx | 99 ++++++++++++++++++++++-- 1 file changed, 92 insertions(+), 7 deletions(-) diff --git a/src/client/routes/settings/workspace.tsx b/src/client/routes/settings/workspace.tsx index 7822ae7..03337d5 100644 --- a/src/client/routes/settings/workspace.tsx +++ b/src/client/routes/settings/workspace.tsx @@ -5,28 +5,81 @@ import { CommonWrapper } from '@/components/CommonWrapper'; import { ScrollArea } from '@/components/ui/scroll-area'; import { useCurrentWorkspace } from '../../store/user'; import { CommonHeader } from '@/components/CommonHeader'; -import { Card, CardContent, CardHeader } from '@/components/ui/card'; +import { + Card, + CardContent, + CardFooter, + CardHeader, +} from '@/components/ui/card'; import { Typography } from 'antd'; -import { AppRouterOutput, trpc } from '@/api/trpc'; +import { + AppRouterOutput, + defaultErrorHandler, + defaultSuccessHandler, + trpc, +} from '@/api/trpc'; import { createColumnHelper, DataTable } from '@/components/DataTable'; -import { useMemo } from 'react'; +import { useMemo, useState } from 'react'; import { get } from 'lodash-es'; import { Badge } from '@/components/ui/badge'; +import { Input } from '@/components/ui/input'; +import { + Form, + FormControl, + FormDescription, + FormField, + FormItem, + FormLabel, + FormMessage, +} from '@/components/ui/form'; +import { useForm } from 'react-hook-form'; +import { Button } from '@/components/ui/button'; +import { useEventWithLoading } from '@/hooks/useEvent'; +import { zodResolver } from '@hookform/resolvers/zod'; +import { z } from 'zod'; export const Route = createFileRoute('/settings/workspace')({ beforeLoad: routeAuthBeforeLoad, component: PageComponent, }); +const inviteFormSchema = z.object({ + email: z.string().email(), +}); + +type InviteFormValues = z.infer; + type MemberInfo = AppRouterOutput['workspace']['members'][number]; const columnHelper = createColumnHelper(); function PageComponent() { const { t } = useTranslation(); - const { id, name } = useCurrentWorkspace(); - const { data: members = [] } = trpc.workspace.members.useQuery({ - workspaceId: id, + const { id: workspaceId, name } = useCurrentWorkspace(); + const { data: members = [], refetch: refetchMembers } = + trpc.workspace.members.useQuery({ + workspaceId, + }); + const form = useForm({ + resolver: zodResolver(inviteFormSchema), + defaultValues: { + email: '', + }, }); + const inviteMutation = trpc.workspace.invite.useMutation({ + onSuccess: defaultSuccessHandler, + onError: defaultErrorHandler, + }); + + const [handleInvite, isLoading] = useEventWithLoading( + async (values: InviteFormValues) => { + await inviteMutation.mutateAsync({ + workspaceId, + targetUserEmail: values.email, + }); + + refetchMembers(); + } + ); const columns = useMemo(() => { return [ @@ -72,13 +125,45 @@ function PageComponent() { {t('Workspace ID')}: - {id} + {workspaceId} +
+ + + + {t('Invite new members by email address')} + + + ( + + + + + + + + + )} + /> + + + + + + +
+ + {t('Members')}