Skip to content

Commit

Permalink
Feat: search user posts (#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
naraeng committed Mar 2, 2025
1 parent bd7b602 commit 5dce7ec
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 134 deletions.
Empty file removed src/apis/getUserPostSearch.ts
Empty file.
3 changes: 1 addition & 2 deletions src/apis/getUserPosts.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { GetUserPostsResponse } from '@/types/apis/get';
import { AxiosResponse } from 'axios';
import { clientAuth } from './client';
import { clientAuth } from '@/apis/client.ts';

export const getUserPosts = async (page: number, take: number) => {
const response: AxiosResponse<GetUserPostsResponse> = await clientAuth({
baseURL: import.meta.env.VITE_API_URL,
url: `board/mypost?page=${page}&take=${take}`,
method: 'get',
});
Expand Down
11 changes: 11 additions & 0 deletions src/apis/getUserPostsSearch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { GetUserPostsResponse } from '@/types/apis/get';
import { AxiosResponse } from 'axios';
import { clientAuth } from './client.ts';

export const getUserPostsSearch = async (page: number, take: number, q: string) => {
const response: AxiosResponse<GetUserPostsResponse> = await clientAuth({
url: `board/mypost/search?page=${page}&take=${take}&q=${q}`,
method: 'get',
});
return response.data;
};
3 changes: 1 addition & 2 deletions src/apis/getUserProfile.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { GetUserProfileResponse } from '@/types/apis/get';
import { AxiosResponse } from 'axios';
import { clientAuth } from './client';
import { clientAuth } from './client.ts';

export const getUserProfile = async () => {
const response: AxiosResponse<GetUserProfileResponse> = await clientAuth({
baseURL: 'http://13.125.101.7:8080',
url: '/users/mypage',
method: 'get',
});
Expand Down
2 changes: 1 addition & 1 deletion src/pages/mypage/component/DropdownUserMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ interface DropdownUserMenuProps {

export default function DropdownUserMenu({ selectedMenu, setSelectedMenu, setIsDropdown }: DropdownUserMenuProps) {
return (
<div className="w-36 rounded-xs border bg-gray-50 px-1 py-1 text-gray-700 shadow-lg xs:text-xs sm:text-xs md:text-sm">
<div className="rounded-xs border bg-gray-50 px-1 py-1 text-gray-700 shadow-lg marker:w-36 xs:text-xs sm:text-xs md:text-sm">
<ul>
<li
className={`cursor-pointer px-4 py-3 ${
Expand Down
Empty file.
1 change: 0 additions & 1 deletion src/pages/mypage/myPosts/hooks/useGetUserPosts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import axios, { AxiosError } from 'axios';
export const useGetUserPosts = (page: number, take: number): UseQueryResult<GetUserPostsResponse, AxiosError> => {
return useQuery<GetUserPostsResponse, AxiosError>({
queryKey: ['get-user-posts', page, take],
// queryFn: () => getUserPosts(page, take),
queryFn: async () => {
try {
return await getUserPosts(page, take);
Expand Down
25 changes: 25 additions & 0 deletions src/pages/mypage/myPosts/hooks/useSearchUserPosts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { getUserPostsSearch } from '@/apis/getUserPostsSearch';
import { GetUserPostsResponse } from '@/types/apis/get';
import { useQuery, UseQueryResult } from '@tanstack/react-query';
import axios, { AxiosError } from 'axios';

export const useSearchUserPosts = (
page: number,
take: number,
q: string
): UseQueryResult<GetUserPostsResponse, AxiosError> => {
return useQuery<GetUserPostsResponse, AxiosError>({
queryKey: ['get-user-posts', page, take, q],
queryFn: async () => {
try {
return await getUserPostsSearch(page, take, q);
} catch (error) {
console.error('API Error:', error);
if (axios.isAxiosError(error)) {
console.error('Error response:', error.response?.data);
}
throw error;
}
},
});
};
12 changes: 11 additions & 1 deletion src/pages/mypage/myPosts/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,27 @@ import { MyPostsContent } from './myPostsContent/myPostsContent';
import { useGetUserPosts } from './hooks/useGetUserPosts';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { useSearchUserPosts } from './hooks/useSearchUserPosts';

function MyPostsPage() {
const [page, setPage] = useState(0);
const [take] = useState(6);
const [searchText, setSearchText] = useState('');
const [query, setQuery] = useState('');

const userPostsData = useGetUserPosts(page, take);
const searchData = useSearchUserPosts(page, take, query);

const data = query ? searchData.data : userPostsData.data;
const isLoading = query ? searchData.isLoading : userPostsData.isLoading;
const error = query ? searchData.error : userPostsData.error;

const { data, isLoading, error } = useGetUserPosts(page, take);
const totalPages = data?.data?.pageInfo?.totalPages ?? 1;

const onClickSearch = () => {
console.log(searchText);
setPage(0);
setQuery(searchText);
};

const handlePageClick = (pageNumber: number) => {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/mypage/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default function MyPage() {
<ChevronDown className="h-4 w-6" color="#9CA3AF" />
</button>
{isDropdown && (
<div className="absolute top-10">
<div className="absolute top-10 z-50">
<DropdownUserMenu
selectedMenu={selectedMenu}
setSelectedMenu={setSelectedMenu}
Expand Down
Loading

0 comments on commit 5dce7ec

Please sign in to comment.