mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 20:13:49 +00:00
Restructure account api routes, fix update profile
This commit is contained in:
@@ -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;
|
||||
27
src/requests/Account/sendUpdateUserAvatarRequest.ts
Normal file
27
src/requests/Account/sendUpdateUserAvatarRequest.ts
Normal 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;
|
||||
28
src/requests/Account/sendUpdateUserProfileRequest.ts.ts
Normal file
28
src/requests/Account/sendUpdateUserProfileRequest.ts.ts
Normal 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;
|
||||
Reference in New Issue
Block a user