Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { useEffect, useState } from "react";
import { fetchRecommendation } from "@/lib/apis/survey";
import { RecommendationRequest, RecommendationResponse } from "@/lib/type";
import { useState } from "react";
import { useRouter } from "next/navigation";
import { postSurvey } from "@/lib/apis/survey";
import { SurveyRequest } from "@/lib/type";

export function useSurveyRecommendation() {
const [spaceData, setSpaceData] = useState<RecommendationResponse[]>([]);
export function usePostSurvey() {
const [isLoading, setIsLoading] = useState(true);
const [isError, setIsError] = useState(false);

const fetchData = async () => {
const router = useRouter();

const handleSubmit = async () => {
setIsLoading(true);
setIsError(false);

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

const payload: RecommendationRequest = {
const payload: SurveyRequest = {
clientId,
age: Number(onboarding[0]?.substr(0, 2) || 0),
gender: onboarding[1],
Expand All @@ -27,8 +29,8 @@ export function useSurveyRecommendation() {
};

try {
const res = await fetchRecommendation(payload);
setSpaceData(res);
await postSurvey(payload);
router.push("/recommend");
} catch (err) {
console.error(err);
setIsError(true);
Expand All @@ -37,9 +39,5 @@ export function useSurveyRecommendation() {
}
};

useEffect(() => {
fetchData();
}, []);

return { spaceData, isLoading, isError };
return { handleSubmit, isLoading, isError };
}
21 changes: 17 additions & 4 deletions src/app/_components/SurveyScreen/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { useSurvey } from "./hooks/useSurvey";
import ProgressBar from "@/app/_components/SurveyScreen/ProgressBar";
import SurveyOption from "./SurveyOption";
import Button from "@/components/Button";
import clsx from "clsx";
import { usePostSurvey } from "./hooks/usePostSurvey";
import ErrorScreen from "@/components/ErrorScreen";

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

const { handleSubmit, isLoading, isError } = usePostSurvey();

const isLastQuestion =
type === "todayMood" && currentQuestion === questions.length - 1;

if (isError) return <ErrorScreen />;

return (
<div className="flex h-screen w-full flex-col items-center justify-between">
<ProgressBar progress={progress} />
Expand Down Expand Up @@ -55,11 +65,14 @@ export default function SurveyScreen({ type, routing }: SurveyScreenProps) {
)}
<Button
color="blue"
onClick={handleNextClick}
className="text-body-large h-[62px] w-[120px] rounded-xl"
disabled={!answers[currentQuestion]}
onClick={isLastQuestion ? handleSubmit : handleNextClick}
className={clsx(
"text-body-large h-[62px] rounded-xl",
isLastQuestion ? "w-[166px]" : "w-[120px]"
)}
disabled={!answers[currentQuestion] || !isLoading}
>
다음
{isLastQuestion ? "추천길 보러가기" : "다음"}
</Button>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useState } from "react";
import { useRouter } from "next/navigation";
import { UpdateRequest } from "@/lib/type";
import { updateSatisfactionScore } from "@/lib/apis/survey";
import { SatisfactionRequest } from "@/lib/type";
import { patchSatisfaction } from "@/lib/apis/survey";

export function useSatisfactionSubmit(onClose: () => void) {
export function usePatchSatisfaction(onClose: () => void) {
const [isLoading, setIsLoading] = useState(false);
const [isError, setIsError] = useState(false);

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

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

const payload: UpdateRequest = {
const payload: SatisfactionRequest = {
clientId,
satisfactions,
};

try {
await updateSatisfactionScore(payload);
await patchSatisfaction(payload);
onClose();
router.push("/submit-success");
} catch (err) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useState } from "react";
import { useSatisfactionSubmit } from "./hooks/useSatisfactionSubmit";
import { usePatchSatisfaction } from "./hooks/usePatchSatisfaction";
import SatisfactionForm from "./SatisfactionForm";
import Button from "@/components/Button";
import ErrorScreen from "@/components/ErrorScreen";
Expand All @@ -11,7 +11,7 @@ export default function SatisfactionModalContent({
}) {
const [satisfactionScores, setSatisfactionScores] = useState([0, 0, 0]);

const { handleSubmit, isLoading, isError } = useSatisfactionSubmit(onClose);
const { handleSubmit, isLoading, isError } = usePatchSatisfaction(onClose);

return (
<div className="flex h-full flex-col gap-8 sm:gap-16">
Expand Down
32 changes: 32 additions & 0 deletions src/app/recommend/_hooks/useGetRecommendation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { useEffect, useState } from "react";
import { getRecommendation } from "@/lib/apis/survey";
import { RecommendationResponse } from "@/lib/type";

export function useGetRecommendation() {
const [spaceData, setSpaceData] = useState<RecommendationResponse[]>([]);
const [isLoading, setIsLoading] = useState(true);
const [isError, setIsError] = useState(false);

const fetchData = async () => {
setIsLoading(true);
setIsError(false);

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

try {
const res = await getRecommendation(clientId);
setSpaceData(res);
} catch (err) {
console.error(err);
setIsError(true);
} finally {
setIsLoading(false);
}
};

useEffect(() => {
fetchData();
}, []);

return { spaceData, isLoading, isError };
}
4 changes: 2 additions & 2 deletions src/app/recommend/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client";

import { useState } from "react";
import { useSurveyRecommendation } from "./_hooks/useSurveyRecommendation";
import { useGetRecommendation } from "./_hooks/useGetRecommendation";
import NavBar from "./_components/NavBar";
import RecommendationPanel from "./_components/RecommendationPanel";
import MapView from "./_components/MapView";
Expand All @@ -13,7 +13,7 @@ import TransitionScreen from "@/app/_components/TransitionScreen";
export default function RecommendPage() {
const [isOpen, setIsOpen] = useState(false);

const { spaceData, isLoading, isError } = useSurveyRecommendation();
const { spaceData, isLoading, isError } = useGetRecommendation();

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

Expand Down
22 changes: 16 additions & 6 deletions src/lib/apis/survey.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
import axios from "@/lib/axiosInstance";
import { RecommendationRequest, UpdateRequest } from "../type";
import { SurveyRequest, SatisfactionRequest } from "../type";

// 사용자 설문 결과 요청
export async function fetchRecommendation(payload: RecommendationRequest) {
export async function postSurvey(payload: SurveyRequest) {
try {
const res = await axios.post("/survey/recommendation", payload);
await axios.post("/survey/recommendation", payload);
} catch (err) {
console.error("사용자 설문 결과 보내기 실패:", err);
throw err;
}
}

// 사용자 설문 결과 반환
export async function getRecommendation(clientId: string) {
try {
const res = await axios.get(`/survey?clientId=${clientId}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GET 요청 적용 확인:)

return res.data;
} catch (err) {
console.error("추천 API 실패:", err);
console.error("추천 장소 데이터 불러오기 실패:", err);
throw err;
}
}

// 사용자 설문 결과 만족도 반영 요청
export async function updateSatisfactionScore(payload: UpdateRequest) {
export async function patchSatisfaction(payload: SatisfactionRequest) {
try {
await axios.patch("/survey/update", payload);
} catch (err) {
console.error("만족도 API 호출 실패:", err);
console.error("사용자 만족도 조사 결과 보내기 실패:", err);
throw err;
}
}
4 changes: 2 additions & 2 deletions src/lib/type.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface RecommendationRequest {
export interface SurveyRequest {
clientId: string;
age: number;
gender: "남자" | "여자";
Expand All @@ -20,7 +20,7 @@ export interface RecommendationResponse {
} | null;
}

export interface UpdateRequest {
export interface SatisfactionRequest {
clientId: string;
satisfactions: number[];
}