Skip to content

Commit f567b27

Browse files
authored
[리팩터링] CategorySortButton관련 스타일 및 로직 수정 (#320)
* feat(api): 전체 카테고리 서치 로직 추가 * feat(lib-utility): isArrayEmpty * feat(lib-utility): routerHelper * style(molecules-buttons): 카테고리 버튼 스타일 수정 * fix(organisms-list): 파일명 변경 sticky옵션으로 top-0에 붙도록 설정 * fix(hooks-react-query): api 수정 반영 * fix: 내보내기 수정 * fix: 필요없는 코드 삭제 * fix: 코드 수정 반영 * style(common-organisms): 위치값 수정 * fix: _app 위치값 삭제
1 parent 1168de1 commit f567b27

File tree

15 files changed

+147
-105
lines changed

15 files changed

+147
-105
lines changed

lib/api/category/categoryService.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import {
77
GetUserCategoriesResponseDto,
88
} from "@til-log.lab/tilog-api";
99

10+
import GetCategoryListInterface from "@Library/api/category/interface/GetCategoryListInterface";
11+
import ExceptionInterface from "@Library/api/exception/interface";
12+
1013
export default class CategoryService {
1114
constructor(
1215
private readonly categoryRepository: CategoryRepository,
@@ -15,7 +18,7 @@ export default class CategoryService {
1518
getCategories(
1619
categoryName?: string,
1720
options?: AxiosRequestConfig
18-
): Promise<AxiosResponse<GetCategoriesResponseDto, any>> {
21+
): Promise<AxiosResponse<GetCategoriesResponseDto, ExceptionInterface>> {
1922
return this.categoryRepository.categoriesControllerGetCategories(
2023
categoryName,
2124
options
@@ -24,10 +27,31 @@ export default class CategoryService {
2427
getUsersCategories(
2528
userId: number,
2629
options?: AxiosRequestConfig
27-
): Promise<AxiosResponse<GetUserCategoriesResponseDto, any>> {
30+
): Promise<AxiosResponse<GetUserCategoriesResponseDto, ExceptionInterface>> {
2831
return this.categoryRepository.categoriesControllerGetUsersCategories(
2932
userId,
3033
options
3134
);
3235
}
36+
37+
getCategoryList({
38+
userId,
39+
options,
40+
}: GetCategoryListInterface): Promise<
41+
AxiosResponse<
42+
GetUserCategoriesResponseDto | GetCategoriesResponseDto,
43+
ExceptionInterface
44+
>
45+
> {
46+
if (userId) {
47+
return this.categoryRepository.categoriesControllerGetUsersCategories(
48+
userId,
49+
options
50+
);
51+
}
52+
return this.categoryRepository.categoriesControllerGetCategories(
53+
undefined,
54+
options
55+
);
56+
}
3357
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { AxiosRequestConfig } from "axios";
2+
3+
export default interface GetCategoryListInterface {
4+
userId?: number;
5+
options?: AxiosRequestConfig;
6+
}

lib/utility/isArrayEmpty.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const isArrayEmpty = <ArrayType>(array: ArrayType[]): boolean => {
2+
if (array.length === 0) return true;
3+
return false;
4+
};
5+
6+
export default isArrayEmpty;

lib/utility/routerHelper.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const routerHelper = (href: string, targetName: string, target: string) => {
2+
return (
3+
href
4+
// 1. target이 & 일경우 그냥 다 날려주면 됩니다.
5+
.replace(`&${targetName}=${target}`, "")
6+
// 2. target이 ? 일경우 &를 찾아서 ?로 변경해줘야합니다.
7+
.replace(`?${targetName}=${target}`, "")
8+
.replace("&", "?")
9+
);
10+
};
11+
12+
export default routerHelper;

src/components/common/molecules/buttons/CategorySortButton.tsx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { useRouter } from "next/router";
22
import React from "react";
33

44
import LinkTo from "@Components/common/atom/LinkTo";
5+
import routerHelper from "@Library/utility/routerHelper";
56

67
interface CategorySortButtonProps {
78
categoryName: string;
@@ -18,22 +19,19 @@ const CategorySortButton = ({ categoryName }: CategorySortButtonProps) => {
1819
pathname: router.pathname,
1920
query: { ...router.query, category: categoryName },
2021
}}
21-
className="p-2 text-sm font-semibold rounded-sm hover:no-underline font-eng-sub-font-2 bg-neutral-50 dark:bg-neutral-800 ring-1 ring-neutral-200 dark:ring-neutral-700 hover:bg-neutral-100 hover:dark:bg-neutral-700"
22+
className="p-2 text-lg font-semibold hover:no-underline text-neutral-400 dark:text-neutral-50"
2223
>
23-
<span className={`text-${categoryName}`}># </span>
24-
<span className="text-neutral-900 dark:text-neutral-50">
25-
{categoryName}
26-
</span>
24+
{categoryName}
2725
</LinkTo>
2826
);
2927
}
3028
return (
3129
<LinkTo
3230
scroll={false}
33-
href={`${router.asPath.split("?")[0]}`}
34-
className="p-2 text-sm font-semibold rounded-sm hover:no-underline font-eng-sub-font-2 bg-neutral-50 dark:bg-neutral-800 ring-1 ring-neutral-200 dark:ring-neutral-700 hover:bg-neutral-100 hover:dark:bg-neutral-700"
31+
href={`${routerHelper(router.asPath, "category", categoryName)}`}
32+
className="p-2 text-lg font-semibold hover:no-underline text-neutral-800 dark:text-neutral-400"
3533
>
36-
<span className={`text-${categoryName}`}># {categoryName}</span>
34+
{categoryName}
3735
</LinkTo>
3836
);
3937
};

src/components/common/organisms/list/CategoryButtonList.tsx

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import Spinner from "@Components/common/atom/loading/Spinner";
2+
import CategorySortButton from "@Components/common/molecules/buttons/CategorySortButton";
3+
import useGetAllCategoryListQuery from "@Hooks/react-query/category/useGetAllCategoryListQuery";
4+
import isArrayEmpty from "@Library/utility/isArrayEmpty";
5+
6+
import { GetMeResponseDto } from "@til-log.lab/tilog-api";
7+
8+
const CategorySortButtonList = ({
9+
userId,
10+
}: {
11+
userId?: GetMeResponseDto["userId"];
12+
}) => {
13+
const categoryList = useGetAllCategoryListQuery({ userId });
14+
if (categoryList.isLoading) return <Spinner size="10" />;
15+
if (categoryList.isError) return <div>{categoryList.error.message.ko}</div>;
16+
if (!categoryList.data) return null;
17+
if (isArrayEmpty(categoryList.data.data.list))
18+
return <h2 className="text-base">카테고리가 존재하지 않아요.</h2>;
19+
20+
return (
21+
<div className="sticky top-0 z-10 border-b border-neutral-300 bg-neutral-50 dark:bg-neutral-900">
22+
<div className="m-auto max-w-[1280px] items-center px-5">
23+
<div className="py-3 overflow-y-auto scrollbar-hide">
24+
{categoryList.data.data.list.map((category) => (
25+
<CategorySortButton categoryName={category.categoryName} />
26+
))}
27+
</div>
28+
</div>
29+
</div>
30+
);
31+
};
32+
export default CategorySortButtonList;

src/components/common/organisms/list/UserCategoryButtonList.tsx

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/components/modules/blog/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import CategorySortButtonList from "@Components/common/organisms/list/CategorySortButtonList";
12
import PostCardList from "@Components/common/organisms/list/PostCardList";
2-
import UserCategoryButtonList from "@Components/common/organisms/list/UserCategoryButtonList";
33
import UserInfoProfile from "@Components/common/organisms/profile/UserInfoProfile";
44

5-
export { UserCategoryButtonList, UserInfoProfile, PostCardList };
5+
export { CategorySortButtonList, UserInfoProfile, PostCardList };

src/hooks/react-query/category/useAllGetUserCategoryListQuery.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)