Restructure account api routes, fix update profile

This commit is contained in:
Aaron William Po
2023-12-02 22:42:07 -05:00
parent 0d23b02957
commit cf89dc92df
15 changed files with 366 additions and 103 deletions

View File

@@ -1,32 +0,0 @@
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;

View File

@@ -0,0 +1,27 @@
interface UpdateProfileRequestParams {
file: File;
userId: string;
}
const sendUpdateUserAvatarRequest = async ({
file,
userId,
}: UpdateProfileRequestParams) => {
const formData = new FormData();
formData.append('file', file);
const response = await fetch(`/api/users/${userId}/`, {
method: 'PUT',
body: formData,
});
if (!response.ok) {
throw new Error('Failed to update profile.');
}
const json = await response.json();
return json;
};
export default sendUpdateUserAvatarRequest;

View File

@@ -0,0 +1,28 @@
import UpdateProfileSchema from '@/services/User/schema/UpdateProfileSchema';
import { z } from 'zod';
interface UpdateProfileRequestParams {
userId: string;
bio: z.infer<typeof UpdateProfileSchema>['bio'];
}
const sendUpdateUserProfileRequest = async ({
bio,
userId,
}: UpdateProfileRequestParams) => {
const response = await fetch(`/api/users/${userId}/profile/update-bio`, {
method: 'PUT',
body: JSON.stringify({ bio }),
headers: { 'Content-Type': 'application/json' },
});
if (!response.ok) {
throw new Error('Failed to update profile.');
}
const json = await response.json();
return json;
};
export default sendUpdateUserProfileRequest;