From 443e1f91f9803f597130ba333bb649eaea606224 Mon Sep 17 00:00:00 2001 From: Wang Zixiao Date: Mon, 27 May 2024 15:34:54 +0800 Subject: [PATCH] feat(ui): modify name in profile (#2249) --- .../profile/components/change-name.tsx | 124 ++++++++++++++++++ .../profile/components/profile.tsx | 4 + ee/tabby-ui/lib/hooks/use-me.ts | 1 + 3 files changed, 129 insertions(+) create mode 100644 ee/tabby-ui/app/(dashboard)/profile/components/change-name.tsx diff --git a/ee/tabby-ui/app/(dashboard)/profile/components/change-name.tsx b/ee/tabby-ui/app/(dashboard)/profile/components/change-name.tsx new file mode 100644 index 000000000000..34af29f5aa3f --- /dev/null +++ b/ee/tabby-ui/app/(dashboard)/profile/components/change-name.tsx @@ -0,0 +1,124 @@ +'use client' + +import React from 'react' +import { zodResolver } from '@hookform/resolvers/zod' +import { useForm } from 'react-hook-form' +import { toast } from 'sonner' +import * as z from 'zod' + +import { graphql } from '@/lib/gql/generates' +import { useMe } from '@/lib/hooks/use-me' +import { useMutation } from '@/lib/tabby/gql' +import { Button } from '@/components/ui/button' +import { + Form, + FormControl, + FormField, + FormItem, + FormLabel, + FormMessage +} from '@/components/ui/form' +import { IconSpinner } from '@/components/ui/icons' +import { Input } from '@/components/ui/input' +import { Separator } from '@/components/ui/separator' +import { ListSkeleton } from '@/components/skeleton' + +const updateNameMutation = graphql(/* GraphQL */ ` + mutation UpdateUserName($id: ID!, $name: String!) { + updateUserName(id: $id, name: $name) + } +`) + +interface ChangeNameFormProps { + showOldPassword?: boolean + onSuccess?: () => void + defaultValues: { + name?: string + } +} + +const ChangeNameForm: React.FC = ({ + onSuccess, + defaultValues +}) => { + const [{ data }] = useMe() + + const formSchema = z.object({ + name: z.string() + }) + + const form = useForm>({ + resolver: zodResolver(formSchema), + defaultValues + }) + const { isSubmitting } = form.formState + const { name } = form.watch() + + const updateName = useMutation(updateNameMutation, { + form, + onCompleted(values) { + if (values?.updateUserName) { + onSuccess?.() + } + } + }) + + const onSubmit = async (values: z.infer) => { + await updateName({ + id: data!.me.id, + name: values.name + }) + } + + const isNameModified = name !== defaultValues.name + return ( +
+ + ( + + Name + + + + + + )} + /> + + +
+ +
+ + + ) +} + +export const ChangeName = () => { + const [{ data }, reexecuteQuery] = useMe() + const onSuccess = () => { + toast.success('Name is updated') + reexecuteQuery() + } + + return data ? ( + + ) : ( + + ) +} diff --git a/ee/tabby-ui/app/(dashboard)/profile/components/profile.tsx b/ee/tabby-ui/app/(dashboard)/profile/components/profile.tsx index 5f9567003b50..5b9b9ef76dff 100644 --- a/ee/tabby-ui/app/(dashboard)/profile/components/profile.tsx +++ b/ee/tabby-ui/app/(dashboard)/profile/components/profile.tsx @@ -6,10 +6,14 @@ import { Avatar } from './avatar' import { ChangePassword } from './change-password' import { Email } from './email' import { ProfileCard } from './profile-card' +import { ChangeName } from './change-name' export default function Profile() { return (
+ + +