-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge/feat 탐색페이지 api 연동
- Loading branch information
Showing
48 changed files
with
868 additions
and
358 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 5 additions & 6 deletions
11
src/apis/editors/getEditorsData.ts → src/apis/editors/getEditorsData.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 2 additions & 5 deletions
7
src/apis/editors/fetchEditorsData.ts → src/apis/editors/useGetEditorsData.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import instance from "../instance"; | ||
import { FollowingType } from "../../types/follow/FollowingType"; | ||
import { BaseResponse } from "../../types/BaseResponse"; | ||
|
||
export const getFollowing = async () => { | ||
try{ | ||
const response = await instance.get<BaseResponse<FollowingType>>('following') | ||
console.log('response', response) | ||
return response.data.result || undefined | ||
} catch (error) { | ||
console.error('Error fetching data'); | ||
return undefined; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import instance from "../instance"; | ||
import { FollowType } from "../../types/follow/FollowType"; | ||
|
||
export const postFollow = async (newFollow: FollowType): Promise<FollowType> => { | ||
return await instance.post('/follow', newFollow); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { getFollowing } from "./getFollowing"; | ||
import { FollowingType } from "../../types/follow/FollowingType"; | ||
|
||
export const fetchFollowing = async (token:string| undefined): Promise<FollowingType | undefined> => { | ||
try { | ||
if(token) { | ||
return await getFollowing(); | ||
} else { | ||
return undefined; | ||
} | ||
} catch (error) { | ||
console.error('Error fetching data:', error); | ||
throw new Error('정보를 불러올 수 없음.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// src/apis/follow/usePostFollow.ts | ||
import { useMutation, useQueryClient } from 'react-query'; | ||
import instance from '../instance'; | ||
import { FollowType } from '../../types/follow/FollowType'; | ||
|
||
export const usePostFollow = () => { | ||
const queryClient = useQueryClient(); | ||
|
||
const mutation = useMutation({ | ||
mutationFn: async (followData: FollowType) => { | ||
console.log('Sending request with data:', followData); | ||
return await instance.post('/follow', followData); | ||
}, | ||
onSuccess: () => { | ||
queryClient.invalidateQueries({ queryKey: ['followData'] }); | ||
}, | ||
onError: (error: Error) => { | ||
console.error('Follow request failed:', error); | ||
}, | ||
}); | ||
|
||
return mutation; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { useQuery } from 'react-query'; | ||
import { getKeyword } from './getKeywords'; | ||
import { KeywordType } from '../../types/keywords/KeywordType'; | ||
import { useAllKeywordStore } from '../../stores/keywordStore'; | ||
import { useKeywordStore } from '../../stores/keywordStore'; | ||
import { fetchFollowing } from '../follow/useGetFollowing'; | ||
import { useEffect } from 'react'; | ||
import { useLocation } from 'react-router-dom'; | ||
|
||
export const useGetKeywords = (token: string | undefined) => { | ||
const location = useLocation(); | ||
const { setAllKeywordList, allKeywordList } = useAllKeywordStore(); | ||
const {selectedList, setSelectedList} = useKeywordStore() | ||
|
||
const { data: followingData, isSuccess: isFollowingDataSuccess } = useQuery( | ||
['followingData', token], | ||
() => fetchFollowing(token), | ||
{ | ||
enabled: !!token, | ||
refetchOnWindowFocus: false, | ||
}, | ||
); | ||
// 페이지 이동 시 재요청 방지를 위해 enabled를 false로 설정 | ||
const { data, isSuccess, refetch } = useQuery<KeywordType[]>( | ||
'keywordsData', | ||
getKeyword, | ||
{ | ||
refetchOnWindowFocus: false, | ||
enabled: false, // 자동으로 데이터 요청 안 함 | ||
}, | ||
); | ||
|
||
useEffect(() => { | ||
if (allKeywordList.length === 0) { | ||
refetch(); | ||
} | ||
}, [refetch, allKeywordList.length]); | ||
|
||
useEffect(() => { | ||
if (isSuccess && data && allKeywordList.length === 0) { | ||
let Keywords = data; | ||
|
||
if (!token && location.pathname === '/timeline') { | ||
Keywords = data.map((keyword, index) => ({ | ||
...keyword, | ||
selected: index < 2 ? true : keyword.selected, | ||
})); | ||
} else if (token &&followingData && followingData.users === undefined) { | ||
Keywords = data.map((keyword, index) => ({ | ||
...keyword, | ||
selected: index < 2 ? true : keyword.selected, | ||
})); | ||
} | ||
|
||
setAllKeywordList(Keywords); | ||
|
||
// Filter keywords with selected as true and set them to selectedList | ||
const selectedKeywords = Keywords.filter(keyword => keyword.selected); | ||
setSelectedList(selectedKeywords); | ||
} | ||
}, [isSuccess, data, setAllKeywordList, token, allKeywordList.length]); | ||
|
||
return { allKeywordList, refetch }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { keywordMap } from "./keywordMap"; | ||
|
||
export const getKeywordMap = async (keyword: string) => { | ||
try { | ||
const response = await keywordMap(keyword); | ||
console.log(response) | ||
return response; | ||
} catch (error) { | ||
console.error('Error fetching keyword map:', error); | ||
return undefined; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import instance from "../instance"; | ||
import { BaseResponse } from "../../types/BaseResponse"; | ||
|
||
export const getKeyword = async () => { | ||
try { | ||
const response = await instance.get<BaseResponse<string[]>>('home/keyword'); | ||
console.log('keywords:',response.data) | ||
const keywords = response.data.result || []; | ||
|
||
return keywords.map((title, index) => ({ | ||
id: Math.random(), | ||
title: title, | ||
selected: false, | ||
})) | ||
|
||
} catch (error) { | ||
console.error('Error fetching data;',error); | ||
return []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import instance from "../instance"; | ||
import { APIKeywordMapType } from "../../types/keywords/APIKeywordMapType"; | ||
import { BaseResponse } from "../../types/BaseResponse"; | ||
|
||
export const keywordMap = async (keyword:string) => { | ||
try { | ||
const response = await instance.get<BaseResponse<APIKeywordMapType[] | undefined>>(`home/map/keyword`, { | ||
params: { keyword } | ||
}) | ||
return response.data.result; | ||
}catch(error) { | ||
console.error('Error fetching data:', error); | ||
return undefined; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import instance from "../instance"; | ||
import { ExploreMapType } from "../../types/mapData/ExploreMapType"; | ||
import { BaseResponse } from "../../types/BaseResponse"; | ||
import { KeywordType } from "../../types/keywords/KeywordType"; | ||
import { APIExploreMapType } from "../../types/mapData/APIExploreMapType "; | ||
|
||
export const exploreMap = async (searchType:string, size: number, page:number) => { | ||
try { | ||
const response = await instance.get<BaseResponse<APIExploreMapType[] | undefined>> (`/map/search`, { | ||
params: { | ||
searchType, | ||
size, | ||
page | ||
} | ||
}) | ||
|
||
const results = response.data.result?.map((item) => { | ||
const newKeyword : KeywordType[]= item.keyword.map((title: string, index) => ({ | ||
id: index, | ||
title: title, | ||
selected: false, | ||
})); | ||
console.log('newKeyword:', newKeyword); | ||
|
||
return { | ||
...item, | ||
keyword: newKeyword, | ||
}; | ||
}); | ||
|
||
return results; | ||
} catch(error) { | ||
console.error('Error fetchgin data:',error); | ||
return undefined; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import instance from "../instance"; | ||
import { FollowingMapType } from "../../types/mapData/FollowingMapType"; | ||
import { BaseResponse } from "../../types/BaseResponse"; | ||
|
||
export const followingMap = async () => { | ||
try { | ||
const response = await instance.get<BaseResponse<FollowingMapType[] | undefined>>('/home/map') | ||
console.log('followingmap:', response) | ||
return response.data.result; | ||
} catch (error) { | ||
console.error('Error fetching data:',error); | ||
return undefined; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { exploreMap } from "./exploreMap"; | ||
|
||
export const getExploreMap = async(searchType:string, size: number, page:number) => { | ||
try{ | ||
const response = await exploreMap(searchType,size,page); | ||
console.log(response) | ||
return response; | ||
} catch (error) { | ||
console.error('Error fetching random map:',error); | ||
return undefined; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { followingMap } from "./followingMap"; | ||
import { FollowingMapType } from "../../types/mapData/FollowingMapType"; | ||
|
||
export const getFollowingMap = async (token: string | undefined): Promise<FollowingMapType[] | undefined> => { | ||
try { | ||
if (!token) { | ||
return undefined; | ||
} | ||
// 여기서 followingMap 함수가 token을 사용하여 데이터를 가져온다고 가정 | ||
const response = await followingMap(); | ||
return response; // 성공적으로 데이터를 반환 | ||
} catch (error) { | ||
console.error('Error fetching following map data:', error); | ||
// 오류가 발생한 경우, undefined를 반환 | ||
return undefined; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.