Skip to content

Commit

Permalink
fix updateProfile
Browse files Browse the repository at this point in the history
  • Loading branch information
mhsiungw committed May 15, 2024
1 parent fb3d2ac commit 5b5a28b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 30 deletions.
23 changes: 18 additions & 5 deletions app/client/src/app/profile/[[...slug]]/_block/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,22 @@ export default function ProfileEditBlock({
const { links, firstName, lastName, email, avatarUrl } = profile;

const [state, formAction] = useFormState(
updateProfile.bind(null, profileId),
updateProfile.bind(
null,
profileId,
['links', 'firstName', 'lastName', 'email', 'avatarFile'].reduce(
(formData, key) => {
if (Array.isArray(profile[key])) {
formData.append(key, JSON.stringify(profile[key]));
} else {
formData.append(key, profile[key]);
}

return formData;
},
new FormData()
)
),
null
);

Expand All @@ -54,15 +69,13 @@ export default function ProfileEditBlock({
}, [profileId, router]);

useEffect(() => {
const storageProfile = getStorageProfile();

if (storageProfile) {
const storageProfile = getStorageProfile(); if (storageProfile) {
setProfile(() =>
update(
{},
{
$merge: mergeWith(
_profile,
{},
storageProfile,
(objVal, srcVal) => {
if (isArray(objVal) && !objVal.length) {
Expand Down
28 changes: 3 additions & 25 deletions app/client/src/lib/actions/profile.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,17 @@
'use server';

import { cookies } from 'next/headers';
import { set } from 'lodash';
import { getAPIUrl } from '../utils';

export async function updateProfile(profileId, _, formData) {
export async function updateProfile(profileId, formData) {
if (!profileId) {
return null;
}

const sanitizedData = Array.from(formData)
.filter(d => !d[0].includes('$ACTION_'))
.reduce((acc, [key, value]) => {
if (value instanceof File && value.size === 0) {
return acc;
}
set(acc, key, value);
return acc;
}, {});

const newFormData = new FormData();

Object.entries(sanitizedData).forEach(([key, value]) => {
if (Array.isArray(value)) {
newFormData.append(key, JSON.stringify(value));
} else {
newFormData.append(key, value);
}
});

try {
const res = await fetch(`${getAPIUrl()}/profile/${[profileId]}`, {
method: 'PUT',
body: newFormData,
body: formData,
headers: {
Cookie: cookies().toString()
}
Expand All @@ -44,8 +23,7 @@ export async function updateProfile(profileId, _, formData) {
} catch (err) {
return {
error: true,
message: 'Something went wrong',
data: newFormData
message: 'Something went wrong'
};
}
}
Expand Down

0 comments on commit 5b5a28b

Please sign in to comment.