|
1 | 1 | import { zodResolver } from "@hookform/resolvers/zod" |
2 | 2 | import { useMutation, useQueryClient } from "@tanstack/react-query" |
3 | | -import { useState } from "react" |
| 3 | +import { useState, useRef, type ChangeEvent } from "react" |
4 | 4 | import { useForm } from "react-hook-form" |
5 | 5 | import { z } from "zod" |
6 | 6 |
|
@@ -34,6 +34,40 @@ const UserInformation = () => { |
34 | 34 | const [editMode, setEditMode] = useState(false) |
35 | 35 | const { user: currentUser } = useAuth() |
36 | 36 |
|
| 37 | + const fileInputRef = useRef<HTMLInputElement>(null) |
| 38 | + |
| 39 | + const handleAvatarClick = () => { |
| 40 | + fileInputRef.current?.click() |
| 41 | + } |
| 42 | + |
| 43 | + const handleFileChange = async (event: ChangeEvent<HTMLInputElement>) => { |
| 44 | + const file = event.target.files?.[0] |
| 45 | + if (!file) return |
| 46 | + |
| 47 | + const formData = new FormData() |
| 48 | + formData.append("file", file) |
| 49 | + |
| 50 | + try { |
| 51 | + const token = localStorage.getItem("access_token") |
| 52 | + const response = await fetch(`${import.meta.env.VITE_API_URL}/api/v1/users/me/avatar`, { |
| 53 | + method: "POST", |
| 54 | + headers: { |
| 55 | + Authorization: `Bearer ${token}`, |
| 56 | + }, |
| 57 | + body: formData, |
| 58 | + }) |
| 59 | + |
| 60 | + if (!response.ok) { |
| 61 | + throw new Error("Failed to upload avatar") |
| 62 | + } |
| 63 | + |
| 64 | + showSuccessToast("Avatar updated successfully") |
| 65 | + queryClient.invalidateQueries({ queryKey: ["currentUser"] }) |
| 66 | + } catch (error) { |
| 67 | + showErrorToast("Error uploading avatar") |
| 68 | + } |
| 69 | + } |
| 70 | + |
37 | 71 | const form = useForm<FormData>({ |
38 | 72 | resolver: zodResolver(formSchema), |
39 | 73 | mode: "onBlur", |
@@ -83,6 +117,37 @@ const UserInformation = () => { |
83 | 117 | return ( |
84 | 118 | <div className="max-w-md"> |
85 | 119 | <h3 className="text-lg font-semibold py-4">User Information</h3> |
| 120 | + |
| 121 | + <div className="flex items-center gap-4 mb-6"> |
| 122 | + <div |
| 123 | + className="relative w-24 h-24 rounded-full overflow-hidden bg-gray-100 border border-gray-200" |
| 124 | + > |
| 125 | + {currentUser?.avatar_url ? ( |
| 126 | + <img |
| 127 | + src={`${import.meta.env.VITE_API_URL}${currentUser.avatar_url}`} |
| 128 | + alt="Avatar" |
| 129 | + className="w-full h-full object-cover" |
| 130 | + /> |
| 131 | + ) : ( |
| 132 | + <div className="flex items-center justify-center h-full text-3xl font-bold text-gray-300 uppercase"> |
| 133 | + {currentUser?.full_name?.charAt(0) || currentUser?.email?.charAt(0) || "?"} |
| 134 | + </div> |
| 135 | + )} |
| 136 | + </div> |
| 137 | + <div className="flex flex-col gap-2"> |
| 138 | + <Button variant="outline" size="sm" onClick={handleAvatarClick}> |
| 139 | + Change Avatar |
| 140 | + </Button> |
| 141 | + <input |
| 142 | + type="file" |
| 143 | + ref={fileInputRef} |
| 144 | + className="hidden" |
| 145 | + accept="image/*" |
| 146 | + onChange={handleFileChange} |
| 147 | + /> |
| 148 | + </div> |
| 149 | + </div> |
| 150 | + |
86 | 151 | <Form {...form}> |
87 | 152 | <form |
88 | 153 | onSubmit={form.handleSubmit(onSubmit)} |
|
0 commit comments