|
1 | 1 | import { useState, useEffect, useMemo } from "react"; |
2 | 2 | import { useNavigate, useLocation } from "react-router-dom"; |
3 | | -import { getMyCollaborations } from "./api/calendar"; |
4 | | -import type { CampaignCollaboration } from "./api/calendar"; |
| 3 | +import { getMyCollaborations, searchCollaborations } from "./api/calendar"; |
| 4 | +import type { CampaignCollaboration, } from "./api/calendar"; |
5 | 5 | import FilterBottomSheet from "../components/FilterBottomSheet"; |
6 | 6 | import WeeklyCalendar from "../components/WeeklyCalendar"; |
7 | 7 | import MonthlyCalendar from "../components/MonthlyCalendar"; |
@@ -30,25 +30,31 @@ export default function CalendarContent() { |
30 | 30 | const isFiltered = activeFilter !== "전체"; |
31 | 31 |
|
32 | 32 | useEffect(() => { |
33 | | - const fetchAllCampaigns = async () => { |
| 33 | + const fetchCampaigns = async () => { |
34 | 34 | try { |
35 | 35 | setIsLoading(true); |
36 | 36 |
|
| 37 | + // 1. 키워드가 있을 때는 searchCollaborations API 사용 |
| 38 | + // 2. 키워드가 없을 때는 getMyCollaborations API 사용 |
| 39 | + |
| 40 | + const fetchFunction = keyword.trim() ? searchCollaborations : getMyCollaborations; |
| 41 | + |
37 | 42 | const [applied, sent, received] = await Promise.all([ |
38 | | - getMyCollaborations({ type: "APPLIED", keyword: keyword.trim() || undefined }), |
39 | | - getMyCollaborations({ type: "SENT", keyword: keyword.trim() || undefined }), |
40 | | - getMyCollaborations({ type: "RECEIVED", keyword: keyword.trim() || undefined }), |
| 43 | + fetchFunction({ type: "APPLIED", keyword: keyword.trim() || undefined }), |
| 44 | + fetchFunction({ type: "SENT", keyword: keyword.trim() || undefined }), |
| 45 | + fetchFunction({ type: "RECEIVED", keyword: keyword.trim() || undefined }), |
41 | 46 | ]); |
42 | 47 |
|
43 | 48 | setCampaigns([...applied, ...sent, ...received]); |
44 | 49 | } catch (error) { |
45 | | - console.error("로드 실패:", error); |
| 50 | + console.error("데이터 로드 실패:", error); |
| 51 | + setCampaigns([]); // 에러 시 빈 배열 처리 |
46 | 52 | } finally { |
47 | 53 | setIsLoading(false); |
48 | 54 | } |
49 | 55 | }; |
50 | 56 |
|
51 | | - fetchAllCampaigns(); |
| 57 | + fetchCampaigns(); |
52 | 58 | }, [keyword, location.key]); |
53 | 59 |
|
54 | 60 | // 날짜 계산을 별도 useMemo로 분리 |
@@ -92,27 +98,26 @@ export default function CalendarContent() { |
92 | 98 | } |
93 | 99 | }; |
94 | 100 |
|
| 101 | + // CalendarContent.tsx 내부 matchingList |
| 102 | + |
95 | 103 | const matchingList = useMemo(() => { |
96 | 104 | return campaigns.filter((item) => { |
| 105 | + // 탭 필터링 |
97 | 106 | const isCorrectSubTab = |
98 | 107 | matchingSubTab === "sent" ? item.type === "SENT" : |
99 | 108 | matchingSubTab === "received" ? item.type === "RECEIVED" : |
100 | 109 | item.type === "APPLIED"; |
101 | 110 |
|
102 | 111 | if (!isCorrectSubTab) return false; |
103 | 112 |
|
104 | | - if (activeFilter === "전체") return true; |
105 | | - |
106 | | - const statusMatches = getStatusLabel(item.status) === activeFilter; |
107 | | - |
108 | | - const keywordMatches = keyword ? item.brandName.includes(keyword) : true; |
109 | | - |
110 | | - return statusMatches && keywordMatches; |
111 | | - |
112 | | - |
| 113 | + // 상태 필터링 (activeFilter가 "전체"가 아닐 때만 적용) |
| 114 | + if (activeFilter !== "전체") { |
| 115 | + return getStatusLabel(item.status) === activeFilter; |
| 116 | + } |
113 | 117 |
|
| 118 | + return true; |
114 | 119 | }); |
115 | | - }, [campaigns, matchingSubTab, activeFilter, keyword]); |
| 120 | + }, [campaigns, matchingSubTab, activeFilter]); // keyword는 이제 API 결과(campaigns)에 반영되어 있으므로 제외 가능 |
116 | 121 |
|
117 | 122 | console.log("전체 데이터:", campaigns); |
118 | 123 | console.log("필터된 데이터:", matchingList); |
@@ -226,20 +231,18 @@ export default function CalendarContent() { |
226 | 231 | <h2 className="text-title1 font-semibold text-text-black">매칭 현황</h2> |
227 | 232 | <button |
228 | 233 | onClick={() => setIsFilterOpen(true)} |
229 | | - className={`flex items-center w-fit h-7 pl-3 pr-1.5 rounded-full border text-[14px] font-medium text-[#5B5D6B] ${ |
230 | | - isFiltered |
| 234 | + className={`flex items-center w-fit h-7 pl-3 pr-1.5 rounded-full border text-[14px] font-medium text-[#5B5D6B] ${isFiltered |
231 | 235 | ? "border-core-70 text-core-1 bg-core-70" |
232 | 236 | : "border-core-2 text-gray-2 bg-white" |
233 | | - }`} |
| 237 | + }`} |
234 | 238 | > |
235 | 239 | {activeFilter} |
236 | 240 | <svg |
237 | 241 | xmlns="http://www.w3.org/2000/svg" |
238 | 242 | viewBox="0 0 20 20" |
239 | 243 | fill="none" |
240 | | - className={`w-6 h-6 ${ |
241 | | - isFiltered ? "text-core-1" : "text-text-gray2" |
242 | | - }`} |
| 244 | + className={`w-6 h-6 ${isFiltered ? "text-core-1" : "text-text-gray2" |
| 245 | + }`} |
243 | 246 | > |
244 | 247 | <path |
245 | 248 | d="M6 8L10 12L14 8" |
|
0 commit comments