Additional work on user profile edit

This commit is contained in:
Aaron William Po
2023-12-02 13:58:17 -05:00
parent df55217bd0
commit d57623e705
10 changed files with 361 additions and 159 deletions

View File

@@ -0,0 +1,32 @@
import { z } from 'zod';
import UpdateProfileSchema from '@/services/User/schema/UpdateProfileSchema';
const sendUpdateProfileRequest = async (data: z.infer<typeof UpdateProfileSchema>) => {
if (!(data.userAvatar instanceof FileList)) {
throw new Error('You must submit this form in a web browser.');
}
const { bio, userAvatar } = data;
const formData = new FormData();
if (userAvatar[0]) {
formData.append('image', userAvatar[0]);
}
formData.append('bio', bio);
const response = await fetch(`/api/users/profile`, {
method: 'PUT',
body: formData,
});
if (!response.ok) {
throw new Error('Something went wrong.');
}
const updatedUser = await response.json();
return updatedUser;
};
export default sendUpdateProfileRequest;