Skip to content

Commit 945871c

Browse files
authored
Merge pull request #11 from blaybus-hackathon/feat/admin-caregiver-pages
feat: axios
2 parents ac8d255 + 58d0ddd commit 945871c

File tree

4 files changed

+76
-6
lines changed

4 files changed

+76
-6
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"@radix-ui/react-navigation-menu": "^1.2.5",
1414
"@radix-ui/react-slot": "^1.1.2",
1515
"@tailwindcss/vite": "^4.0.6",
16+
"axios": "^1.7.9",
1617
"class-variance-authority": "^0.7.1",
1718
"clsx": "^2.1.1",
1819
"lucide-react": "^0.475.0",

src/Test.jsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
1+
import ApiRequestComponent from "./api/apiRequest";
22

33
export default function Test() {
44
return (
5-
<div className="max-w-md mx-auto flex flex-col items-center">
6-
7-
8-
5+
<div>
6+
<ApiRequestComponent />
97
</div>
108
);
119
}
1210

13-

src/api/apiRequest.jsx

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { useState } from "react";
2+
import api from "../api"; // axios 인스턴스 가져오기
3+
4+
const ApiRequestComponent = () => {
5+
const [endpoint, setEndpoint] = useState("");
6+
const [data, setData] = useState("");
7+
const [response, setResponse] = useState(null);
8+
9+
const handleSubmit = async (e) => {
10+
e.preventDefault();
11+
try {
12+
const res = await api.post(`/api/${endpoint}`, { data });
13+
setResponse(res.data);
14+
console.log("서버 응답:", res.data);
15+
} catch (error) {
16+
console.error("API 요청 오류:", error);
17+
setResponse(null);
18+
}
19+
};
20+
21+
return (
22+
<div>
23+
<form onSubmit={handleSubmit}>
24+
<div>
25+
<label>엔드포인트:</label>
26+
<input
27+
type="text"
28+
value={endpoint}
29+
onChange={(e) => setEndpoint(e.target.value)}
30+
placeholder="엔드포인트 입력"
31+
/>
32+
</div>
33+
<div>
34+
<label>데이터:</label>
35+
<input
36+
type="text"
37+
value={data}
38+
onChange={(e) => setData(e.target.value)}
39+
placeholder="보낼 데이터 입력"
40+
/>
41+
</div>
42+
<button type="submit">전송</button>
43+
</form>
44+
{response && <div>응답: {response}</div>}
45+
</div>
46+
);
47+
};
48+
49+
export default ApiRequestComponent;

src/api/index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import axios from "axios";
2+
3+
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL; // VITE 환경변수 사용
4+
5+
const api = axios.create({
6+
baseURL: API_BASE_URL,
7+
headers: {
8+
"Content-Type": "application/json",
9+
},
10+
});
11+
12+
// 예제 API 호출 함수
13+
export const fetchData = async ({ endpoint }) => {
14+
try {
15+
const response = await api.get(`/api/${endpoint}`);
16+
return response.data;
17+
} catch (error) {
18+
console.error("API 요청 오류:", error);
19+
throw error;
20+
}
21+
};
22+
23+
export default api;

0 commit comments

Comments
 (0)