Skip to content

Commit d0f1e84

Browse files
authored
Merge pull request #163 from Soohyuniii/feat/chat-api-연동-작업
feat: chat api 연동 작업
2 parents 1a2d7f8 + 2ab1aba commit d0f1e84

File tree

2 files changed

+84
-12
lines changed

2 files changed

+84
-12
lines changed

src/pages/Chat.tsx

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,21 @@ import ChatMessage from "@/components/ChatMessage";
55
import ChatActionBar from "@/components/ChatActionBar";
66
import pickMbtiImage from "@/utils/pickMbtiImage";
77
import instance from "@/api/axios";
8+
import { useLocation } from "react-router-dom";
89

910
interface Message {
1011
role: "user" | "assistant";
1112
content: string;
1213
}
1314

15+
interface ChatResponse {
16+
data: string;
17+
}
18+
1419
const Chat = () => {
20+
const { state } = useLocation();
21+
const { mbti, mode, id } = state;
22+
1523
const [messages, setMessages] = useState<Message[]>([]);
1624
const [input, setInput] = useState("");
1725
const [isOpen, setIsOpen] = useState(false);
@@ -21,8 +29,8 @@ const Chat = () => {
2129
bottomRef.current?.scrollIntoView({ behavior: "smooth" });
2230
}, [messages]);
2331

24-
const chatTitle = "ENFP와 대화"; //TODO: API 연동 후 수정 필요
25-
const assistantInfo = "ENFP"; //TODO: API 연동 후 수정 필요
32+
const chatTitle = `${mbti} 대화`;
33+
const assistantInfo = mbti;
2634
const assistantImgUrl = pickMbtiImage(assistantInfo);
2735

2836
const handleSend = async (messageToSend: string) => {
@@ -36,17 +44,27 @@ const Chat = () => {
3644
setInput("");
3745

3846
try {
39-
//TODO: API 분기처리 필요
40-
const response = await instance.post(
41-
"/api/fast-friend/message",
42-
JSON.stringify({ content: messageToSend })
43-
);
47+
let url = "";
48+
let payload = {};
49+
50+
if (mode === "fastFriend") {
51+
url = "/api/fast-friend/message";
52+
payload = { fastFriendId: id, content: messageToSend };
53+
} else {
54+
url = "/api/message";
55+
payload = {
56+
conversationId: id,
57+
messageContent: messageToSend
58+
};
59+
}
60+
61+
const response = await instance.post<ChatResponse>(url, payload);
4462

4563
setMessages([
4664
...newMessages,
4765
{
4866
role: "assistant",
49-
content: JSON.stringify(response.data) || "응답이 없어요"
67+
content: response.data.data || "응답이 없어요"
5068
}
5169
]);
5270
} catch (e) {

src/pages/SelectInfo.tsx

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,45 @@
11
import { ChangeEvent, useState } from "react";
2-
import { useLocation } from "react-router-dom";
2+
import { useLocation, useNavigate } from "react-router-dom";
33
import FormButton from "@/components/button/FormButton";
44
import Header from "@/components/Header";
55
import { getMBTIgroup, mapAgeToNumber } from "@/utils/helpers";
66
import instance from "@/api/axios";
77
import ToastMessage from "@/components/ToastMessage";
88

9+
type FastFriendResponse = {
10+
header: {
11+
code: number;
12+
message: string;
13+
};
14+
data: number;
15+
};
16+
17+
type VirtualFriendResponse = {
18+
header: {
19+
code: number;
20+
message: string;
21+
};
22+
data: {
23+
conversationId: number;
24+
virtualFriendId: number;
25+
mbti: string;
26+
virtualFriendName: string;
27+
virtualFriendAge: number;
28+
virtualFriendSex: "MALE" | "FEMALE";
29+
virtualFriendRelationship: string;
30+
};
31+
};
32+
33+
type FriendResponse = FastFriendResponse | VirtualFriendResponse;
34+
35+
function isVirtualFriendResponse(
36+
data: number | VirtualFriendResponse["data"]
37+
): data is VirtualFriendResponse["data"] {
38+
return typeof data === "object" && data !== null && "conversationId" in data;
39+
}
40+
941
const SelectInfo = () => {
42+
const navigate = useNavigate();
1043
const location = useLocation();
1144
const mode = location.state; // mode: fastFriend, virtualFriend 두 종류 존재
1245
const isNameRequired = mode === "virtualFriend";
@@ -142,10 +175,31 @@ const SelectInfo = () => {
142175
mode === "virtualFriend" ? "api/virtual-friend" : "api/fast-friend";
143176

144177
try {
145-
const response = await instance.post(`/${apiUrl}`, selectedData);
146-
console.log("Success!!", response.data);
178+
const response = await instance.post<FriendResponse>(
179+
`/${apiUrl}`,
180+
selectedData
181+
);
182+
const responseData = response.data.data;
183+
184+
if (mode === "virtualFriend" && isVirtualFriendResponse(responseData)) {
185+
navigate("/chat", {
186+
state: {
187+
mbti,
188+
mode,
189+
id: responseData.conversationId
190+
}
191+
});
192+
} else if (mode === "fastFriend" && typeof responseData === "number") {
193+
navigate("/chat", {
194+
state: {
195+
mbti,
196+
mode,
197+
id: responseData
198+
}
199+
});
200+
}
147201
} catch (error) {
148-
console.error("Select Info Error", error);
202+
console.error(error);
149203
}
150204
};
151205

0 commit comments

Comments
 (0)