Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

백엔드 API 명세에 따른 리팩토링 #297

Merged
merged 3 commits into from
Aug 6, 2024
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
4 changes: 0 additions & 4 deletions frontend/src/api/categories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ import { customFetch } from './customFetch';

const API_URL = process.env.REACT_APP_API_URL;

export const CATEGORY = {
UNASSIGNED_ID: 1,
};

export const CATEGORY_API_URL = `${API_URL}/categories`;

export const getCategoryList = async (): Promise<CategoryListResponse> =>
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { CATEGORY, CATEGORY_API_URL, getCategoryList, postCategory } from './categories';
export { CATEGORY_API_URL, getCategoryList, postCategory } from './categories';
export { customFetch } from './customFetch';
export { QUERY_KEY } from './queryKeys';
export {
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/api/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ export const getTemplateList = async (
const url = new URL(TEMPLATE_API_URL);

if (categoryId) {
url.searchParams.append('category', categoryId.toString());
url.searchParams.append('categoryId', categoryId.toString());
}

if (tagId) {
url.searchParams.append('tag', tagId.toString());
url.searchParams.append('tags', tagId.toString());
}

url.searchParams.append('page', page.toString());
url.searchParams.append('pageNumber', page.toString());
url.searchParams.append('pageSize', pageSize.toString());

return await customFetch({
Expand Down
46 changes: 26 additions & 20 deletions frontend/src/components/CategoryMenu/CategoryMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useState } from 'react';
import { useMemo, useState } from 'react';

import { CATEGORY } from '@/api';
import { Text } from '@/components';
import { theme } from '@/style/theme';
import type { Category } from '@/types';
Expand All @@ -20,10 +19,27 @@ interface ButtonProps {
const CategoryMenu = ({ categories, onSelectCategory }: MenuProps) => {
const [selectedId, setSelectedId] = useState<number>(0);

const indexById: Record<number, number> = {
0: 0,
1: categories.length + 1,
};
const reorderedCategories = useMemo(() => {
if (categories.length === 0) {
return [];
}

const [first, ...rest] = categories;

return [...rest, first];
}, [categories]);

const indexById: Record<number, number> = useMemo(() => {
const map: Record<number, number> = {
0: 0,
};

reorderedCategories.forEach(({ id }, index) => {
map[id] = index + 1;
});

return map;
}, [reorderedCategories]);

const handleButtonClick = (id: number) => {
setSelectedId(id);
Expand All @@ -34,21 +50,11 @@ const CategoryMenu = ({ categories, onSelectCategory }: MenuProps) => {
<S.CategoryContainer>
<CategoryButton name='전체보기' disabled={selectedId === 0} onClick={() => handleButtonClick(0)} />

{categories.map(({ id, name }, index) => {
indexById[id] = index + 1;

return (
<CategoryButton key={id} name={name} disabled={selectedId === id} onClick={() => handleButtonClick(id)} />
);
})}

<CategoryButton
name='카테고리 미지정'
disabled={selectedId === CATEGORY.UNASSIGNED_ID}
onClick={() => handleButtonClick(CATEGORY.UNASSIGNED_ID)}
/>
{reorderedCategories.map(({ id, name }) => (
<CategoryButton key={id} name={name} disabled={selectedId === id} onClick={() => handleButtonClick(id)} />
))}

<S.HighlightBox selectedIndex={indexById[selectedId]} categoryCount={categories.length + 2} />
<S.HighlightBox selectedIndex={indexById[selectedId]} categoryCount={reorderedCategories.length + 1} />
</S.CategoryContainer>
);
};
Expand Down