Skip to content

Commit

Permalink
fix: 파일 오류 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
doh-yo committed Jul 19, 2024
1 parent 683c87e commit 4e951c4
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React from "react";
import { BrowserRouter, Route, Routes } from "react-router-dom";
import HomePage from "./pages/HomePage/HomePage";
import LoginPage from "./pages/LoginPage/LoginPage";
import LoginPage from "./pages/auth/LoginPage";
import MarketPage from "./pages/MarketPage/MarketPage";
import AddItemPage from "./pages/AddItemPage/AddItemPage";
import CommunityFeedPage from "./pages/CommunityFeedPage/CommunityFeedPage";
import Header from "./components/Layout/Header";
import ItemPage from "./pages/ItemPage/ItemPage";
import SignupPage from "./pages/LoginPage/SignupPage";
import SignupPage from "./pages/auth/SignupPage";

function App() {
return (
Expand Down
59 changes: 59 additions & 0 deletions src/api/itemApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,62 @@ export async function deleteProductComment(commentId: number) {
throw error;
}
}

export interface NewProduct {
images: string[];
tags: string[];
price: number;
description: string;
name: string;
}

export async function uploadProduct(newProduct: NewProduct) {
if (!newProduct) {
throw new Error("Invalid product data");
}

try {
const response = await fetch(`${baseURL}/products/`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(newProduct),
});

if (!response.ok) {
const errorBody = await response.json();
throw new Error(
`Failed to upload the post. ${errorBody.message || "Unknown error"}`
);
}

const body = await response.json();
return body;
} catch (error) {
console.error("Failed to upload product:", error);
throw error;
}
}

export async function uploadImage(imageFile: File) {
const formData = new FormData();
formData.append("image", imageFile);

try {
const response = await fetch(`${baseURL}/images/upload`, {
method: "POST",
body: formData,
});

if (!response.ok) {
throw new Error(`HTTP error: ${response.status}`);
}

const body = await response.json();
return body.url; // Assuming the response contains the URL of the uploaded image
} catch (error) {
console.error("Failed to upload image:", error);
throw error;
}
}
5 changes: 4 additions & 1 deletion src/components/UI/ImageUpload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,24 @@ const HiddenFileInput = styled.input`

interface ImageUploadProps {
title?: string;
setImageFiles: (images: File[]) => void;
}

const ImageUpload = ({ title }: ImageUploadProps) => {
const ImageUpload: React.FC<ImageUploadProps> = ({ title, setImageFiles }) => {
const [imagePreviewUrl, setImagePreviewUrl] = useState<string>("");

const handleImageChange = (event: ChangeEvent<HTMLInputElement>) => {
const file = event.target.files?.[0];
if (file) {
const imageUrl = URL.createObjectURL(file);
setImagePreviewUrl(imageUrl);
setImageFiles([file]); // File 객체를 넘겨줌
}
};

const handleDelete = () => {
setImagePreviewUrl("");
setImageFiles([]); // 빈 배열을 넘겨줌
};

return (
Expand Down
3 changes: 2 additions & 1 deletion src/pages/AddItemPage/AddItemPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const AddItemPage: React.FC = () => {
const [description, setDescription] = useState<string>("");
const [price, setPrice] = useState<string>("");
const [tags, setTags] = useState<string[]>([]);
const [imageFiles, setImageFiles] = useState<File[]>([]);

const addTag = (tag: string) => {
if (!tags.includes(tag)) {
Expand All @@ -59,7 +60,7 @@ const AddItemPage: React.FC = () => {
</TitleSection>

<InputSection>
<ImageUpload title="상품 이미지" />
<ImageUpload title="상품 이미지" setImageFiles={setImageFiles} />

<InputItem
id="name"
Expand Down

0 comments on commit 4e951c4

Please sign in to comment.