diff --git a/app/soapbox/actions/me.js b/app/soapbox/actions/me.js index 8bbcc5fd8..96afcab7d 100644 --- a/app/soapbox/actions/me.js +++ b/app/soapbox/actions/me.js @@ -59,18 +59,12 @@ const persistAuthAccount = (account, params) => { } }; -export function patchMe(params, formData = false) { +export function patchMe(params) { return (dispatch, getState) => { dispatch(patchMeRequest()); - const options = formData ? { - headers: { - 'Content-Type': 'multipart/form-data', - }, - } : {}; - return api(getState) - .patch('/api/v1/accounts/update_credentials', params, options) + .patch('/api/v1/accounts/update_credentials', params) .then(response => { persistAuthAccount(response.data, params); dispatch(patchMeSuccess(response.data)); diff --git a/app/soapbox/features/edit_profile/index.tsx b/app/soapbox/features/edit_profile/index.tsx index 61b43dc06..bacf99ca3 100644 --- a/app/soapbox/features/edit_profile/index.tsx +++ b/app/soapbox/features/edit_profile/index.tsx @@ -27,6 +27,25 @@ const hidesNetwork = (account: Account): boolean => { return Boolean(hide_followers && hide_follows && hide_followers_count && hide_follows_count); }; +/** Converts JSON objects to FormData. */ +// https://stackoverflow.com/a/60286175/8811886 +// @ts-ignore +const toFormData = (f => f(f))(h => f => f(x => h(h)(f)(x)))(f => fd => pk => d => { + if (d instanceof Object) { + // eslint-disable-next-line consistent-return + Object.keys(d).forEach(k => { + const v = d[k]; + if (pk) k = `${pk}[${k}]`; + if (v instanceof Object && !(v instanceof Date) && !(v instanceof File)) { + return f(fd)(k)(v); + } else { + fd.append(k, v); + } + }); + } + return fd; +})(new FormData())(); + const messages = defineMessages({ heading: { id: 'column.edit_profile', defaultMessage: 'Edit profile' }, header: { id: 'edit_profile.header', defaultMessage: 'Edit Profile' }, @@ -193,8 +212,9 @@ const EditProfile: React.FC = () => { const handleSubmit: React.FormEventHandler = (event) => { const promises = []; + const formData = toFormData(data); - promises.push(dispatch(patchMe(data, true))); + promises.push(dispatch(patchMe(formData))); if (features.muteStrangers) { promises.push(