Skip to content

Commit d1f76b6

Browse files
authored
Merge pull request #25 from CarToi/feat/#24/survey-recommend-api
[feat] #24 설문 결과 보내고 받아오는 방식 변경
2 parents 3df3ef8 + 235dc7f commit d1f76b6

8 files changed

Lines changed: 88 additions & 35 deletions

File tree

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
import { useEffect, useState } from "react";
2-
import { fetchRecommendation } from "@/lib/apis/survey";
3-
import { RecommendationRequest, RecommendationResponse } from "@/lib/type";
1+
import { useState } from "react";
2+
import { useRouter } from "next/navigation";
3+
import { postSurvey } from "@/lib/apis/survey";
4+
import { SurveyRequest } from "@/lib/type";
45

5-
export function useSurveyRecommendation() {
6-
const [spaceData, setSpaceData] = useState<RecommendationResponse[]>([]);
6+
export function usePostSurvey() {
77
const [isLoading, setIsLoading] = useState(true);
88
const [isError, setIsError] = useState(false);
99

10-
const fetchData = async () => {
10+
const router = useRouter();
11+
12+
const handleSubmit = async () => {
1113
setIsLoading(true);
1214
setIsError(false);
1315

@@ -16,7 +18,7 @@ export function useSurveyRecommendation() {
1618
localStorage.getItem("onboardingAnswers") ?? "[]"
1719
);
1820

19-
const payload: RecommendationRequest = {
21+
const payload: SurveyRequest = {
2022
clientId,
2123
age: Number(onboarding[0]?.substr(0, 2) || 0),
2224
gender: onboarding[1],
@@ -27,8 +29,8 @@ export function useSurveyRecommendation() {
2729
};
2830

2931
try {
30-
const res = await fetchRecommendation(payload);
31-
setSpaceData(res);
32+
await postSurvey(payload);
33+
router.push("/recommend");
3234
} catch (err) {
3335
console.error(err);
3436
setIsError(true);
@@ -37,9 +39,5 @@ export function useSurveyRecommendation() {
3739
}
3840
};
3941

40-
useEffect(() => {
41-
fetchData();
42-
}, []);
43-
44-
return { spaceData, isLoading, isError };
42+
return { handleSubmit, isLoading, isError };
4543
}

src/app/_components/SurveyScreen/index.tsx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { useSurvey } from "./hooks/useSurvey";
44
import ProgressBar from "@/app/_components/SurveyScreen/ProgressBar";
55
import SurveyOption from "./SurveyOption";
66
import Button from "@/components/Button";
7+
import clsx from "clsx";
8+
import { usePostSurvey } from "./hooks/usePostSurvey";
9+
import ErrorScreen from "@/components/ErrorScreen";
710

811
interface SurveyScreenProps {
912
type: "onboarding" | "todayMood";
@@ -21,6 +24,13 @@ export default function SurveyScreen({ type, routing }: SurveyScreenProps) {
2124
handleNextClick,
2225
} = useSurvey({ type, routing });
2326

27+
const { handleSubmit, isLoading, isError } = usePostSurvey();
28+
29+
const isLastQuestion =
30+
type === "todayMood" && currentQuestion === questions.length - 1;
31+
32+
if (isError) return <ErrorScreen />;
33+
2434
return (
2535
<div className="flex h-screen w-full flex-col items-center justify-between">
2636
<ProgressBar progress={progress} />
@@ -55,11 +65,14 @@ export default function SurveyScreen({ type, routing }: SurveyScreenProps) {
5565
)}
5666
<Button
5767
color="blue"
58-
onClick={handleNextClick}
59-
className="text-body-large h-[62px] w-[120px] rounded-xl"
60-
disabled={!answers[currentQuestion]}
68+
onClick={isLastQuestion ? handleSubmit : handleNextClick}
69+
className={clsx(
70+
"text-body-large h-[62px] rounded-xl",
71+
isLastQuestion ? "w-[166px]" : "w-[120px]"
72+
)}
73+
disabled={!answers[currentQuestion] || !isLoading}
6174
>
62-
다음
75+
{isLastQuestion ? "추천길 보러가기" : "다음"}
6376
</Button>
6477
</div>
6578
</div>

src/app/recommend/_components/SatisfactionModalContent/hooks/useSatisfactionSubmit.ts renamed to src/app/recommend/_components/SatisfactionModalContent/hooks/usePatchSatisfaction.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { useState } from "react";
22
import { useRouter } from "next/navigation";
3-
import { UpdateRequest } from "@/lib/type";
4-
import { updateSatisfactionScore } from "@/lib/apis/survey";
3+
import { SatisfactionRequest } from "@/lib/type";
4+
import { patchSatisfaction } from "@/lib/apis/survey";
55

6-
export function useSatisfactionSubmit(onClose: () => void) {
6+
export function usePatchSatisfaction(onClose: () => void) {
77
const [isLoading, setIsLoading] = useState(false);
88
const [isError, setIsError] = useState(false);
99

@@ -15,13 +15,13 @@ export function useSatisfactionSubmit(onClose: () => void) {
1515

1616
const clientId = localStorage.getItem("userId") || "";
1717

18-
const payload: UpdateRequest = {
18+
const payload: SatisfactionRequest = {
1919
clientId,
2020
satisfactions,
2121
};
2222

2323
try {
24-
await updateSatisfactionScore(payload);
24+
await patchSatisfaction(payload);
2525
onClose();
2626
router.push("/submit-success");
2727
} catch (err) {

src/app/recommend/_components/SatisfactionModalContent/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useState } from "react";
2-
import { useSatisfactionSubmit } from "./hooks/useSatisfactionSubmit";
2+
import { usePatchSatisfaction } from "./hooks/usePatchSatisfaction";
33
import SatisfactionForm from "./SatisfactionForm";
44
import Button from "@/components/Button";
55
import ErrorScreen from "@/components/ErrorScreen";
@@ -11,7 +11,7 @@ export default function SatisfactionModalContent({
1111
}) {
1212
const [satisfactionScores, setSatisfactionScores] = useState([0, 0, 0]);
1313

14-
const { handleSubmit, isLoading, isError } = useSatisfactionSubmit(onClose);
14+
const { handleSubmit, isLoading, isError } = usePatchSatisfaction(onClose);
1515

1616
return (
1717
<div className="flex h-full flex-col gap-8 sm:gap-16">
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { useEffect, useState } from "react";
2+
import { getRecommendation } from "@/lib/apis/survey";
3+
import { RecommendationResponse } from "@/lib/type";
4+
5+
export function useGetRecommendation() {
6+
const [spaceData, setSpaceData] = useState<RecommendationResponse[]>([]);
7+
const [isLoading, setIsLoading] = useState(true);
8+
const [isError, setIsError] = useState(false);
9+
10+
const fetchData = async () => {
11+
setIsLoading(true);
12+
setIsError(false);
13+
14+
const clientId = localStorage.getItem("userId") || "";
15+
16+
try {
17+
const res = await getRecommendation(clientId);
18+
setSpaceData(res);
19+
} catch (err) {
20+
console.error(err);
21+
setIsError(true);
22+
} finally {
23+
setIsLoading(false);
24+
}
25+
};
26+
27+
useEffect(() => {
28+
fetchData();
29+
}, []);
30+
31+
return { spaceData, isLoading, isError };
32+
}

src/app/recommend/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use client";
22

33
import { useState } from "react";
4-
import { useSurveyRecommendation } from "./_hooks/useSurveyRecommendation";
4+
import { useGetRecommendation } from "./_hooks/useGetRecommendation";
55
import NavBar from "./_components/NavBar";
66
import RecommendationPanel from "./_components/RecommendationPanel";
77
import MapView from "./_components/MapView";
@@ -13,7 +13,7 @@ import TransitionScreen from "@/app/_components/TransitionScreen";
1313
export default function RecommendPage() {
1414
const [isOpen, setIsOpen] = useState(false);
1515

16-
const { spaceData, isLoading, isError } = useSurveyRecommendation();
16+
const { spaceData, isLoading, isError } = useGetRecommendation();
1717

1818
if (isLoading) return <TransitionScreen type="toRecommend" />;
1919

src/lib/apis/survey.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
11
import axios from "@/lib/axiosInstance";
2-
import { RecommendationRequest, UpdateRequest } from "../type";
2+
import { SurveyRequest, SatisfactionRequest } from "../type";
33

44
// 사용자 설문 결과 요청
5-
export async function fetchRecommendation(payload: RecommendationRequest) {
5+
export async function postSurvey(payload: SurveyRequest) {
66
try {
7-
const res = await axios.post("/survey/recommendation", payload);
7+
await axios.post("/survey/recommendation", payload);
8+
} catch (err) {
9+
console.error("사용자 설문 결과 보내기 실패:", err);
10+
throw err;
11+
}
12+
}
13+
14+
// 사용자 설문 결과 반환
15+
export async function getRecommendation(clientId: string) {
16+
try {
17+
const res = await axios.get(`/survey?clientId=${clientId}`);
818
return res.data;
919
} catch (err) {
10-
console.error("추천 API 실패:", err);
20+
console.error("추천 장소 데이터 불러오기 실패:", err);
1121
throw err;
1222
}
1323
}
1424

1525
// 사용자 설문 결과 만족도 반영 요청
16-
export async function updateSatisfactionScore(payload: UpdateRequest) {
26+
export async function patchSatisfaction(payload: SatisfactionRequest) {
1727
try {
1828
await axios.patch("/survey/update", payload);
1929
} catch (err) {
20-
console.error("만족도 API 호출 실패:", err);
30+
console.error("사용자 만족도 조사 결과 보내기 실패:", err);
2131
throw err;
2232
}
2333
}

src/lib/type.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export interface RecommendationRequest {
1+
export interface SurveyRequest {
22
clientId: string;
33
age: number;
44
gender: "남자" | "여자";
@@ -20,7 +20,7 @@ export interface RecommendationResponse {
2020
} | null;
2121
}
2222

23-
export interface UpdateRequest {
23+
export interface SatisfactionRequest {
2424
clientId: string;
2525
satisfactions: number[];
2626
}

0 commit comments

Comments
 (0)