EditProfile: fix formdata submission

This commit is contained in:
Alex Gleason 2022-05-02 22:45:58 -05:00
parent 300efe3259
commit 81938ec4d0
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
2 changed files with 23 additions and 9 deletions

View file

@ -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));

View file

@ -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(