|
| 1 | +import { useSetUserProfile, useProfile } from '@/lib/store'; |
| 2 | +import { Profile } from '@/types/dbtypes'; |
| 3 | +import UserIcon from '@/components/icons/UserIcon'; |
| 4 | +import { useSupabaseClient } from '@supabase/auth-helpers-react'; |
| 5 | +import { |
| 6 | + updateUserAvatar, |
| 7 | + updateUserProfile, |
| 8 | +} from '@/services/profile.service'; |
| 9 | +import { zodResolver } from '@hookform/resolvers/zod'; |
| 10 | +import { UpdateUserInput, updateUserSchema } from '@/types/client/user'; |
| 11 | +import { useForm } from 'react-hook-form'; |
| 12 | +import { useState, useRef, ChangeEvent, useEffect } from 'react'; |
| 13 | +import { toast } from 'react-toastify'; |
| 14 | +import CameraIcon from '@/components/icons/CameraIcon'; |
| 15 | +import PlusIcon from '@/components/icons/PlusIcon'; |
| 16 | +import Image from 'next/image'; |
| 17 | + |
| 18 | +export default function EditUserForm() { |
| 19 | + const [userImage, setUserImage] = useState<File | null>(null); |
| 20 | + const [serverError, setServerError] = useState<string>(''); |
| 21 | + const imageRef = useRef<HTMLInputElement | null>(null); |
| 22 | + const previewImage = userImage ? URL.createObjectURL(userImage) : ''; |
| 23 | + const user = useProfile(); |
| 24 | + const supabase = useSupabaseClient(); |
| 25 | + |
| 26 | + const handleFileChange = (e: ChangeEvent<HTMLInputElement>) => { |
| 27 | + if (!e.target.files) { |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + setUserImage(e.target.files[0]); |
| 32 | + }; |
| 33 | + |
| 34 | + const { |
| 35 | + register, |
| 36 | + handleSubmit, |
| 37 | + reset, |
| 38 | + formState: { errors }, |
| 39 | + } = useForm<UpdateUserInput>({ |
| 40 | + resolver: zodResolver(updateUserSchema), |
| 41 | + mode: 'onChange', |
| 42 | + defaultValues: { |
| 43 | + full_name: user && user.full_name, |
| 44 | + website: user && user.website, |
| 45 | + }, |
| 46 | + }); |
| 47 | + |
| 48 | + const onSubmit = async (formData: UpdateUserInput) => { |
| 49 | + const { data, error } = await updateUserProfile( |
| 50 | + supabase, |
| 51 | + user?.id!, |
| 52 | + formData.full_name!, |
| 53 | + formData.website! |
| 54 | + ); |
| 55 | + |
| 56 | + const fileExt = userImage?.name.split('.').pop(); |
| 57 | + const fileName = `${data?.id}.${fileExt}`; |
| 58 | + const filePath = `${fileName}?updated=${Date.now()}`; |
| 59 | + |
| 60 | + if (userImage) { |
| 61 | + await updateUserAvatar(supabase, filePath, userImage, data!.id); |
| 62 | + } |
| 63 | + |
| 64 | + console.log(filePath); |
| 65 | + |
| 66 | + if (error) { |
| 67 | + setServerError(error.message); |
| 68 | + setTimeout(() => { |
| 69 | + setServerError(''); |
| 70 | + }, 7000); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + if (data && !error) { |
| 75 | + toast.success('Profile updated successfully', { |
| 76 | + position: 'top-right', |
| 77 | + autoClose: 3000, |
| 78 | + }); |
| 79 | + } |
| 80 | + setUserImage(null); |
| 81 | + }; |
| 82 | + |
| 83 | + const handleStringDisplay = ( |
| 84 | + userString: string | null, |
| 85 | + emptyMessage: string |
| 86 | + ) => { |
| 87 | + if (userString === null || userString.length === 0) { |
| 88 | + return emptyMessage; |
| 89 | + } |
| 90 | + else if (userString.length >= 25) { |
| 91 | + return `${userString.slice(0, 25)}...`; |
| 92 | + } |
| 93 | + return userString; |
| 94 | + }; |
| 95 | + |
| 96 | + return ( |
| 97 | + <div className="flex flex-row ml-5 overflow-y-scroll"> |
| 98 | + <div className="flex flex-col w-12"> |
| 99 | + <div className="flex flex-row"> |
| 100 | + <h1 className="text-2xl font-semibold">User Profile</h1> |
| 101 | + </div> |
| 102 | + <div className=" border-t-2 my-1 border-grey-700"></div> |
| 103 | + <form className="h-[17.5rem] " onSubmit={handleSubmit(onSubmit)}> |
| 104 | + <div className="flex flex-row justify-center items-center mb-2 "> |
| 105 | + <div className="flex flex-col"> |
| 106 | + <label className="font-semibold text-xl mb-1 mx-auto"> |
| 107 | + Avatar |
| 108 | + </label> |
| 109 | + <div className="flex flex-col items-center"> |
| 110 | + <div |
| 111 | + className={`${ |
| 112 | + userImage |
| 113 | + ? 'p-4' |
| 114 | + : 'w-8 py-4 px-6 border-dashed border-2 border-grey-600' |
| 115 | + } flex items-center rounded-lg justify-center relative hover:cursor-pointer`} |
| 116 | + onClick={() => imageRef?.current?.click()} |
| 117 | + > |
| 118 | + <div className="flex flex-col justify-center items-center"> |
| 119 | + {userImage ? ( |
| 120 | + <Image |
| 121 | + alt="userIcon" |
| 122 | + src={previewImage} |
| 123 | + width={40} |
| 124 | + height={2} |
| 125 | + /> |
| 126 | + ) : ( |
| 127 | + <> |
| 128 | + <CameraIcon width={5} /> |
| 129 | + <span className="absolute -top-3 -right-3"> |
| 130 | + <PlusIcon color="#4abfe8" /> |
| 131 | + </span> |
| 132 | + </> |
| 133 | + )} |
| 134 | + </div> |
| 135 | + </div> |
| 136 | + <span className="text-xs font-semibold text-center tracking-wider mt-2"> |
| 137 | + UPLOAD IMAGE |
| 138 | + </span> |
| 139 | + </div> |
| 140 | + <input |
| 141 | + type="file" |
| 142 | + ref={imageRef} |
| 143 | + onChange={handleFileChange} |
| 144 | + className="hidden" |
| 145 | + accept="image/*" |
| 146 | + /> |
| 147 | + </div> |
| 148 | + </div> |
| 149 | + <div className="flex flex-row justify-between items-center mb-2"> |
| 150 | + <div className="flex flex-col justify-start "> |
| 151 | + <label className="font-semibold text-xl mb-1">Name</label> |
| 152 | + {user && ( |
| 153 | + <input |
| 154 | + defaultValue={user.full_name!} |
| 155 | + className="w-12 text-grey-300 text-medium bg-grey-800 rounded-lg focus:bg-slate-600 focus:text-white focus:outline-grey-100 py-1 pl-2" |
| 156 | + placeholder={handleStringDisplay( |
| 157 | + user.full_name, |
| 158 | + 'Add your name to your profile' |
| 159 | + )} |
| 160 | + {...register('full_name')} |
| 161 | + /> |
| 162 | + )} |
| 163 | + </div> |
| 164 | + </div> |
| 165 | + <div className="flex flex-row justify-between items-center mb-2"> |
| 166 | + <div className="flex flex-col justify-start"> |
| 167 | + <label className="font-semibold text-xl mb-1">Website</label> |
| 168 | + {user && ( |
| 169 | + <input |
| 170 | + defaultValue={user.website!} |
| 171 | + className="w-12 text-medium text-grey-300 bg-grey-800 rounded-lg focus:bg-slate-600 focus:text-white focus:outline-grey-100 py-1 pl-2" |
| 172 | + placeholder={handleStringDisplay( |
| 173 | + user.website, |
| 174 | + 'Add your website to your profile' |
| 175 | + )} |
| 176 | + {...register('website')} |
| 177 | + /> |
| 178 | + )} |
| 179 | + </div> |
| 180 | + </div> |
| 181 | + <div className="flex flex-row justify-end items-center"> |
| 182 | + <div className="flex flex-row"> |
| 183 | + <button |
| 184 | + className=" hover:text-frost-500 px-2 py-1 rounded-lg" |
| 185 | + type="submit" |
| 186 | + > |
| 187 | + Submit |
| 188 | + </button> |
| 189 | + </div> |
| 190 | + </div> |
| 191 | + </form> |
| 192 | + </div> |
| 193 | + </div> |
| 194 | + ); |
| 195 | +} |
0 commit comments