Skip to content

Commit 2b569ff

Browse files
committed
feat: mbti test 결과를 select info에 반영
1 parent 2b59eb3 commit 2b569ff

File tree

3 files changed

+107
-66
lines changed

3 files changed

+107
-66
lines changed

src/components/button/ChatStartButton.tsx

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
11
import { useNavigate } from "react-router-dom";
22

3-
const ChatStartButton = ({mode} : {mode: "go-fast" | "go-virtual" | "go-chat"}) => {
3+
type ChatStartButtonProps = {
4+
mode: "go-fast" | "go-virtual" | "go-chat";
5+
mbti?: string;
6+
};
7+
8+
const ChatStartButton = ({ mode, mbti }: ChatStartButtonProps) => {
49
const navigate = useNavigate();
510

611
const handleNavigate = () => {
7-
switch(mode) {
8-
case "go-fast" : navigate("/select-info", {state:"fastFriend"});
9-
break;
10-
case "go-virtual" : navigate("/select-info", {state:"virtualFriend"});
11-
break;
12-
case "go-chat" : navigate("/chat");
13-
break;
14-
default : console.error("mode is invalid", mode);
15-
return;
12+
switch (mode) {
13+
case "go-fast":
14+
navigate("/select-info", { state: { type: "fastFriend", mbti } });
15+
break;
16+
case "go-virtual":
17+
navigate("/select-info", { state: { type: "virtualFriend", mbti } });
18+
break;
19+
case "go-chat":
20+
navigate("/chat");
21+
break;
22+
default:
23+
console.error("mode is invalid", mode);
24+
return;
1625
}
17-
}
26+
};
27+
1828
return (
1929
<button
2030
className="flex h-[56px] w-[320px] items-center justify-center rounded-lg bg-primary-normal font-bold text-white md:w-[335px] lg:w-[460px]"

src/pages/MbtiTestResult.tsx

Lines changed: 59 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,51 +7,66 @@ import useLayoutSize from "@/hooks/useLayoutSize";
77
import Header from "@/components/header/Header";
88

99
const MbtiTestResult = () => {
10+
const mbti = localStorage.getItem("mbti-test-mbti") ?? "";
11+
const result = MBTI_RESULT_INFO[mbti as keyof typeof MBTI_RESULT_INFO];
12+
const size = useLayoutSize();
13+
const imageURL =
14+
size === "sm"
15+
? `/image/mbti-test/360px/image_${mbti?.toLocaleLowerCase()}_360.png`
16+
: size === "md"
17+
? `/image/mbti-test/375px/image_${mbti?.toLocaleLowerCase()}_375.png`
18+
: `/image/mbti-test/500px/image_${mbti?.toLocaleLowerCase()}_500.png`;
1019

11-
const mbti = localStorage.getItem("mbti-test-mbti");
12-
const result = MBTI_RESULT_INFO[mbti as keyof typeof MBTI_RESULT_INFO];
13-
const size = useLayoutSize();
14-
const imageURL = size === "sm" ? `/image/mbti-test/360px/image_${mbti?.toLocaleLowerCase()}_360.png` : (size === "md" ? `/image/mbti-test/375px/image_${mbti?.toLocaleLowerCase()}_375.png` : `/image/mbti-test/500px/image_${mbti?.toLocaleLowerCase()}_500.png`);
15-
console.log(imageURL);
16-
17-
const handleClick = (e: MouseEvent) => {
18-
e.preventDefault();
19-
}
20+
if (!result) return <div>404 error occured</div>;
2021

21-
if (!result) return <div>404 error occured</div>;
22-
23-
return (
24-
<div className="flex flex-col sm:w-[360px] md:w-[375px] lg:w-[500px] h-[1008px]">
25-
<Header title="결과" showShareIcon={false}/>
26-
<main className="flex flex-col flex-grow items-center w-full px-5 bg-white h-screen py-5">
27-
<img src={imageURL} alt="mbti 테스트 결과 이미지" className="sm:w-[320px] md:w-[335px] lg:w-[460px] h-[292px]"/>
28-
<h2 className="mt-6 font-bold text-2xl">{mbti?.toUpperCase()}는 이런 성향이에요!</h2>
29-
<ul className="flex gap-3 mt-4">
30-
<li style={{ backgroundColor: result.bgColor, color: result.textColor }} className="px-4 py-1 rounded-[40px] font-bold">{result.tag[0]}</li>
31-
<li style={{ backgroundColor: result.bgColor, color: result.textColor }} className="px-4 py-1 rounded-[40px] font-bold">{result.tag[1]}</li>
32-
</ul>
33-
<div className="flex flex-col bg-white mt-4 p-[28px] border-[#EDEDED] border-1 rounded-[20px]">
34-
<h3 className="font-bold text-lg">좋아하는 대화 주제</h3>
35-
<ul className="ml-5 list-disc">
36-
<li>{result.topic[0]}</li>
37-
<li>{result.topic[1]}</li>
38-
</ul>
39-
<h3 className="mt-6 font-bold text-lg">좋아하는 대화 태도</h3>
40-
<ul className="ml-5 list-disc">
41-
<li>{result.attitude[0]}</li>
42-
<li>{result.attitude[1]}</li>
43-
</ul>
44-
</div>
45-
<div className="mt-[22px] w-full">
46-
<ChatStartButton mode={"go-virtual"}/>
47-
</div>
48-
<div className="flex gap-4 mt-5 w-full">
49-
<RestartButton/>
50-
<ShareButton />
51-
</div>
52-
</main>
22+
return (
23+
<div className="flex h-[1008px] flex-col sm:w-[360px] md:w-[375px] lg:w-[500px]">
24+
<Header title="결과" showShareIcon={false} />
25+
<main className="flex h-screen w-full flex-grow flex-col items-center bg-white px-5 py-5">
26+
<img
27+
src={imageURL}
28+
alt="mbti 테스트 결과 이미지"
29+
className="h-[292px] sm:w-[320px] md:w-[335px] lg:w-[460px]"
30+
/>
31+
<h2 className="mt-6 text-2xl font-bold">
32+
{mbti?.toUpperCase()}는 이런 성향이에요!
33+
</h2>
34+
<ul className="mt-4 flex gap-3">
35+
<li
36+
style={{ backgroundColor: result.bgColor, color: result.textColor }}
37+
className="rounded-[40px] px-4 py-1 font-bold"
38+
>
39+
{result.tag[0]}
40+
</li>
41+
<li
42+
style={{ backgroundColor: result.bgColor, color: result.textColor }}
43+
className="rounded-[40px] px-4 py-1 font-bold"
44+
>
45+
{result.tag[1]}
46+
</li>
47+
</ul>
48+
<div className="mt-4 flex flex-col rounded-[20px] border-1 border-[#EDEDED] bg-white p-[28px]">
49+
<h3 className="text-lg font-bold">좋아하는 대화 주제</h3>
50+
<ul className="ml-5 list-disc">
51+
<li>{result.topic[0]}</li>
52+
<li>{result.topic[1]}</li>
53+
</ul>
54+
<h3 className="mt-6 text-lg font-bold">좋아하는 대화 태도</h3>
55+
<ul className="ml-5 list-disc">
56+
<li>{result.attitude[0]}</li>
57+
<li>{result.attitude[1]}</li>
58+
</ul>
59+
</div>
60+
<div className="mt-[22px] w-full">
61+
<ChatStartButton mode={"go-fast"} mbti={mbti} />
62+
</div>
63+
<div className="mt-5 flex w-full gap-4">
64+
<RestartButton />
65+
<ShareButton />
5366
</div>
54-
);
55-
}
67+
</main>
68+
</div>
69+
);
70+
};
5671

57-
export default MbtiTestResult;
72+
export default MbtiTestResult;

src/pages/SelectInfo.tsx

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ChangeEvent, useState } from "react";
1+
import { ChangeEvent, useEffect, useState } from "react";
22
import { useLocation, useNavigate } from "react-router-dom";
33
import FormButton from "@/components/button/FormButton";
44
import Header from "@/components/header/Header";
@@ -41,15 +41,20 @@ function isVirtualFriendResponse(
4141
const SelectInfo = () => {
4242
const navigate = useNavigate();
4343
const location = useLocation();
44-
const mode = location.state; // mode: fastFriend, virtualFriend 두 종류 존재
45-
const isNameRequired = mode === "virtualFriend";
44+
const { type, mbti: testResultMBTI } = location.state; // type: fastFriend, virtualFriend 두 종류 존재
45+
const isNameRequired = type === "virtualFriend";
4646
const headerTitle =
47-
mode === "fastFriend" ? "상대방 정보선택" : "친구 저장하기";
47+
type === "fastFriend" ? "상대방 정보선택" : "친구 저장하기";
4848
const selectInfoTitle =
49-
mode === "fastFriend"
49+
type === "fastFriend"
5050
? `상대방의 MBTI를 선택하면\n대화를 시뮬레이션 해볼 수 있어요`
5151
: `친구의 MBTI를\n선택해주세요`;
5252

53+
const mbtiTestResult =
54+
typeof location.state === "object" && testResultMBTI !== null
55+
? testResultMBTI
56+
: undefined;
57+
5358
const [selectedMBTI, setSelectedMBTI] = useState<{
5459
[key: string]: string | null;
5560
}>({
@@ -65,6 +70,17 @@ const SelectInfo = () => {
6570
const [interest, setInterest] = useState<string[]>([]);
6671
const [toastMessage, setToastMessage] = useState<string | null>(null);
6772

73+
useEffect(() => {
74+
if (mbtiTestResult && mbtiTestResult.length === 4) {
75+
setSelectedMBTI({
76+
E: mbtiTestResult[0],
77+
N: mbtiTestResult[1],
78+
F: mbtiTestResult[2],
79+
P: mbtiTestResult[3]
80+
});
81+
}
82+
}, [mbtiTestResult]);
83+
6884
const mbtiOptions = ["E", "N", "F", "P", "I", "S", "T", "J"];
6985
const ageOptions = ["10대", "20대", "30대 이상"];
7086
const genderOptions = ["여자", "남자"];
@@ -157,7 +173,7 @@ const SelectInfo = () => {
157173
};
158174

159175
const selectedData =
160-
mode === "virtualFriend"
176+
type === "virtualFriend"
161177
? {
162178
...commonData,
163179
friendName: name,
@@ -172,7 +188,7 @@ const SelectInfo = () => {
172188
};
173189

174190
const apiUrl =
175-
mode === "virtualFriend" ? "api/virtual-friend" : "api/fast-friend";
191+
type === "virtualFriend" ? "api/virtual-friend" : "api/fast-friend";
176192

177193
try {
178194
const response = await instance.post<FriendResponse>(
@@ -181,19 +197,19 @@ const SelectInfo = () => {
181197
);
182198
const responseData = response.data.data;
183199

184-
if (mode === "virtualFriend" && isVirtualFriendResponse(responseData)) {
200+
if (type === "virtualFriend" && isVirtualFriendResponse(responseData)) {
185201
navigate("/chat", {
186202
state: {
187203
mbti,
188-
mode,
204+
mode: type,
189205
id: responseData.conversationId
190206
}
191207
});
192-
} else if (mode === "fastFriend" && typeof responseData === "number") {
208+
} else if (type === "fastFriend" && typeof responseData === "number") {
193209
navigate("/chat", {
194210
state: {
195211
mbti,
196-
mode,
212+
mode: type,
197213
id: responseData
198214
}
199215
});

0 commit comments

Comments
 (0)