Skip to content

Commit be71b28

Browse files
committed
Allow edit profile image
1 parent bde9bf7 commit be71b28

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"react-confetti": "^6.2.2",
2929
"react-dom": "18.2.0",
3030
"react-hook-form": "^7.54.2",
31+
"react-image-file-resizer": "^0.4.8",
3132
"react-qr-code": "^2.0.12",
3233
"react-social-icons": "^6.18.0",
3334
"react-use": "^17.6.0",

src/app/(authenticated)/profile/edit/EditProfileForm.tsx

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Image from "next/image";
55
import { Plus, Save, Upload, X } from "lucide-react";
66
import CountryList from "country-list";
77
import { ChangeEvent, useState } from "react";
8+
import Resizer from "react-image-file-resizer";
89

910
interface EditProfileFormProps {
1011
user: User;
@@ -44,7 +45,7 @@ export default function EditProfileForm({
4445
updateUser,
4546
}: EditProfileFormProps) {
4647
const [profilePicturePreview, setProfilePicturePreview] = useState<string>(
47-
user.img
48+
user.img,
4849
);
4950
const {
5051
control,
@@ -53,6 +54,7 @@ export default function EditProfileForm({
5354
register,
5455
setValue,
5556
getValues,
57+
resetField,
5658
watch,
5759
} = useForm<FormData>({
5860
defaultValues: {
@@ -108,11 +110,27 @@ export default function EditProfileForm({
108110
if (formData.lookingFor.partTime) lookingFor.push("Part-time");
109111
if (formData.lookingFor.fullTime) lookingFor.push("Full-time");
110112

113+
const imgFile = formData.imgFile.item(0);
114+
115+
async function resizeImageAndConvert(file: File): Promise<string> {
116+
return new Promise((resolve) =>
117+
Resizer.imageFileResizer(
118+
file,
119+
300, // Max width
120+
300, // Max height
121+
"JPEG", // Output compression
122+
100, // Quality
123+
0, // Rotation
124+
(uri) => resolve(uri as string),
125+
"base64", // Output format
126+
),
127+
);
128+
}
129+
111130
await updateUser({
112131
id: user.id,
113-
// TODO: Add profile picture
114132
role: user.role,
115-
img: user.img,
133+
img: (imgFile && (await resizeImageAndConvert(imgFile))) || user.img,
116134
name: formData.name,
117135
title: formData.title,
118136
nationality: formData.nationality,

src/components/SocialNetwork.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export function SocialNetwork({ text, type }: SocialNetworkProps) {
4141
return (
4242
<SocialIcon
4343
url={url}
44+
target="_blank"
4445
style={{ height: 32, width: 32 }}
4546
{...(socialNetwork.extraProps ?? {})}
4647
/>

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3064,6 +3064,11 @@ react-hook-form@^7.54.2:
30643064
resolved "https://registry.yarnpkg.com/react-hook-form/-/react-hook-form-7.54.2.tgz#8c26ed54c71628dff57ccd3c074b1dd377cfb211"
30653065
integrity sha512-eHpAUgUjWbZocoQYUHposymRb4ZP6d0uwUnooL2uOybA9/3tPUvoAKqEWK1WaSiTxxOfTpffNZP7QwlnM3/gEg==
30663066

3067+
react-image-file-resizer@^0.4.8:
3068+
version "0.4.8"
3069+
resolved "https://registry.yarnpkg.com/react-image-file-resizer/-/react-image-file-resizer-0.4.8.tgz#85f4ae4469fd2867d961568af660ef403d7a79af"
3070+
integrity sha512-Ue7CfKnSlsfJ//SKzxNMz8avDgDSpWQDOnTKOp/GNRFJv4dO9L5YGHNEnj40peWkXXAK2OK0eRIoXhOYpUzUTQ==
3071+
30673072
react-is@^16.13.1:
30683073
version "16.13.1"
30693074
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"

0 commit comments

Comments
 (0)