mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 20:13:49 +00:00
Refactor: Organize api requests by type and remove toastContext
This commit is contained in:
34
src/requests/BeerImage/sendUploadBeerImageRequest.ts
Normal file
34
src/requests/BeerImage/sendUploadBeerImageRequest.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import beerPostQueryResult from '@/services/BeerPost/schema/BeerPostQueryResult';
|
||||
import { z } from 'zod';
|
||||
|
||||
interface SendUploadBeerImagesRequestArgs {
|
||||
beerPost: z.infer<typeof beerPostQueryResult>;
|
||||
images: FileList;
|
||||
}
|
||||
|
||||
const sendUploadBeerImagesRequest = async ({
|
||||
beerPost,
|
||||
images,
|
||||
}: SendUploadBeerImagesRequestArgs) => {
|
||||
const formData = new FormData();
|
||||
|
||||
[...images].forEach((file) => {
|
||||
formData.append('images', file);
|
||||
});
|
||||
|
||||
formData.append('caption', `Image of ${beerPost.name}`);
|
||||
formData.append('alt', beerPost.name);
|
||||
|
||||
const uploadResponse = await fetch(`/api/beers/${beerPost.id}/images`, {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
});
|
||||
|
||||
if (!uploadResponse.ok) {
|
||||
throw new Error('Failed to upload images');
|
||||
}
|
||||
|
||||
return uploadResponse.json();
|
||||
};
|
||||
|
||||
export default sendUploadBeerImagesRequest;
|
||||
Reference in New Issue
Block a user