-
Notifications
You must be signed in to change notification settings - Fork 671
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add setting user profile avatar preview
Signed-off-by: Innei <[email protected]>
- Loading branch information
Showing
3 changed files
with
132 additions
and
148 deletions.
There are no files selected for viewing
118 changes: 118 additions & 0 deletions
118
src/renderer/src/modules/profile/profile-setting-form.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { zodResolver } from "@hookform/resolvers/zod" | ||
import { setWhoami, useWhoami } from "@renderer/atoms/user" | ||
import { Avatar, AvatarImage } from "@renderer/components/ui/avatar" | ||
import { Button } from "@renderer/components/ui/button" | ||
import { | ||
Form, | ||
FormControl, | ||
FormDescription, | ||
FormField, | ||
FormItem, | ||
FormLabel, | ||
FormMessage, | ||
} from "@renderer/components/ui/form" | ||
import { Input } from "@renderer/components/ui/input" | ||
import { apiClient } from "@renderer/lib/api-fetch" | ||
import { useMutation } from "@tanstack/react-query" | ||
import { useForm } from "react-hook-form" | ||
import { toast } from "sonner" | ||
import { z } from "zod" | ||
|
||
const formSchema = z.object({ | ||
handle: z.string().max(50), | ||
name: z.string().min(3).max(50), | ||
image: z.string().url(), | ||
}) | ||
|
||
export const ProfileSettingForm = () => { | ||
const user = useWhoami() | ||
|
||
const form = useForm<z.infer<typeof formSchema>>({ | ||
resolver: zodResolver(formSchema), | ||
defaultValues: { | ||
handle: user?.handle || "", | ||
name: user?.name || "", | ||
image: user?.image || "", | ||
}, | ||
}) | ||
|
||
const updateMutation = useMutation({ | ||
mutationFn: async (values: z.infer<typeof formSchema>) => | ||
apiClient["auth-app"]["update-account"].$patch({ | ||
json: values, | ||
}), | ||
onSuccess: (_, variables) => { | ||
if (user && variables) { | ||
setWhoami({ ...user, ...variables }) | ||
} | ||
toast("Profile updated.", { | ||
duration: 3000, | ||
}) | ||
}, | ||
}) | ||
|
||
function onSubmit(values: z.infer<typeof formSchema>) { | ||
updateMutation.mutate(values) | ||
} | ||
|
||
return ( | ||
<Form {...form}> | ||
<form onSubmit={form.handleSubmit(onSubmit)} className="mt-4 space-y-4"> | ||
<FormField | ||
control={form.control} | ||
name="handle" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>Handle</FormLabel> | ||
<FormControl> | ||
<Input {...field} /> | ||
</FormControl> | ||
<FormDescription>Your unique identifier.</FormDescription> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
<FormField | ||
control={form.control} | ||
name="name" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>Display Name</FormLabel> | ||
<FormControl> | ||
<Input {...field} /> | ||
</FormControl> | ||
<FormDescription>Your public display name.</FormDescription> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
|
||
<FormField | ||
control={form.control} | ||
name="image" | ||
render={({ field }) => ( | ||
<div className="flex gap-4"> | ||
<FormItem className="w-full"> | ||
<FormLabel>Avatar</FormLabel> | ||
<FormControl> | ||
<Input {...field} /> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
|
||
<Avatar className="-translate-y-2 self-end"> | ||
<AvatarImage src={field.value} /> | ||
</Avatar> | ||
</div> | ||
)} | ||
/> | ||
|
||
<div className="text-right"> | ||
<Button type="submit" isLoading={updateMutation.isPending}> | ||
Submit | ||
</Button> | ||
</div> | ||
</form> | ||
</Form> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters