Skip to content

Commit

Permalink
최근순을 최근 수정순과 최근 생성순으로 나누어 구현 (#961)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hain-tain authored Dec 17, 2024
1 parent ae43eab commit 0f72be3
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 41 deletions.
3 changes: 0 additions & 3 deletions frontend/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
export { API_URL } from './config';
export { QUERY_KEY } from './queryKeys';
export {
PAGE_SIZE,
SORTING_OPTIONS,
DEFAULT_SORTING_OPTION,
getTemplateList,
getTemplateExplore,
getTemplate,
Expand Down
21 changes: 1 addition & 20 deletions frontend/src/api/templates.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,7 @@
import { apiClient } from '@/api/config';
import { DEFAULT_SORTING_OPTION, PAGE_SIZE } from '@/models/templates';
import { END_POINTS } from '@/routes';
import type { TemplateRequest, TemplateEditRequest, TemplateUploadRequest, TemplateListRequest } from '@/types';
import { SortingOption } from '@/types';

export const PAGE_SIZE = 20;

export const SORTING_OPTIONS: SortingOption[] = [
{
key: 'modifiedAt,desc',
value: '최근 순',
},
{
key: 'modifiedAt,asc',
value: '오래된 순',
},
{
key: 'likesCount,desc',
value: '좋아요 순',
},
];

export const DEFAULT_SORTING_OPTION = SORTING_OPTIONS[0];

export const getTemplateList = async ({
keyword,
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/TemplateCard/TemplateCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface Props {
}

const TemplateCard = ({ template }: Props) => {
const { title, description, thumbnail, tags, modifiedAt, member, visibility } = template;
const { title, description, thumbnail, tags, createdAt, member, visibility } = template;
const [showAllTagList, toggleShowAllTagList] = useToggle();
const isPrivate = visibility === VISIBILITY_PRIVATE;

Expand Down Expand Up @@ -49,7 +49,7 @@ const TemplateCard = ({ template }: Props) => {
<S.TimeContainer>
<ClockIcon width={ICON_SIZE.X_SMALL} color={theme.color.light.secondary_600} />
<S.NoWrapTextWrapper>
<Text.Small color={theme.color.light.secondary_600}>{formatRelativeTime(modifiedAt)}</Text.Small>
<Text.Small color={theme.color.light.secondary_600}>{formatRelativeTime(createdAt)}</Text.Small>
</S.NoWrapTextWrapper>
</S.TimeContainer>
</Flex>
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/hooks/useQueryParams.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useCallback } from 'react';
import { useSearchParams } from 'react-router-dom';

import { DEFAULT_SORTING_OPTION } from '@/api';
import { DEFAULT_SORTING_OPTION } from '@/models/templates';

interface FilterState {
category: number;
Expand Down
12 changes: 10 additions & 2 deletions frontend/src/mocks/handlers/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,22 @@ export const templateHandlers = [
}

switch (sort) {
case 'modifiedAt,asc':
filteredTemplates.sort((a, b) => new Date(a.modifiedAt).getTime() - new Date(b.modifiedAt).getTime());
case 'createdAt,asc':
filteredTemplates.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
break;

case 'createdAt,desc':
filteredTemplates.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime());
break;

case 'modifiedAt,desc':
filteredTemplates.sort((a, b) => new Date(b.modifiedAt).getTime() - new Date(a.modifiedAt).getTime());
break;

case 'likesCount,desc':
filteredTemplates.sort((a, b) => b.likesCount - a.likesCount);
break;

default:
break;
}
Expand Down
Empty file.
22 changes: 22 additions & 0 deletions frontend/src/models/templates/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export const PAGE_SIZE = 20;

export const SORTING_OPTIONS = [
{
key: 'createdAt,desc',
value: '최근 생성 순',
},
{
key: 'modifiedAt,desc',
value: '최근 수정 순',
},
{
key: 'createdAt,asc',
value: '오래된 순',
},
{
key: 'likesCount,desc',
value: '좋아요 순',
},
] as const;

export const DEFAULT_SORTING_OPTION = SORTING_OPTIONS[0];
4 changes: 2 additions & 2 deletions frontend/src/pages/MyTemplatesPage/MyTemplatePage.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Suspense } from 'react';
import { useParams } from 'react-router-dom';

import { SORTING_OPTIONS } from '@/api';
import { SearchIcon } from '@/assets/images';
import { Flex, Input, PagingButtons, Dropdown, Heading } from '@/components';
import { useAuth } from '@/hooks/authentication';
import { SORTING_OPTIONS } from '@/models/templates';
import {
CategoryListSection,
CategoryListSectionSkeleton,
Expand Down Expand Up @@ -105,7 +105,7 @@ const MyTemplatePage = () => {
</S.SearchInput>
<Dropdown
{...dropdownProps}
options={SORTING_OPTIONS}
options={[...SORTING_OPTIONS]}
currentValue={sortingOption}
getOptionLabel={(option) => option.value}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { QueryErrorResetBoundary } from '@tanstack/react-query';
import { useEffect } from 'react';
import { ErrorBoundary } from 'react-error-boundary';

import { SORTING_OPTIONS } from '@/api';
import { SearchIcon } from '@/assets/images';
import {
Dropdown,
Expand All @@ -16,6 +15,7 @@ import {
TemplateCard,
} from '@/components';
import { useDropdown, useInput, useQueryParams, useWindowWidth } from '@/hooks';
import { SORTING_OPTIONS } from '@/models/templates';
import { TemplateListSectionLoading } from '@/pages/MyTemplatesPage/components';
import { HotTopicCarousel } from '@/pages/TemplateExplorePage/components';
import { useHotTopic } from '@/pages/TemplateExplorePage/hooks';
Expand Down Expand Up @@ -100,7 +100,7 @@ const TemplateExplorePage = () => {
</S.SearchInput>
<Dropdown
{...dropdownProps}
options={SORTING_OPTIONS}
options={[...SORTING_OPTIONS]}
currentValue={sortingOption}
getOptionLabel={(option) => option.value}
/>
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/queries/templates/useTemplateExploreQuery.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { keepPreviousData, useQuery } from '@tanstack/react-query';

import { PAGE_SIZE, QUERY_KEY, DEFAULT_SORTING_OPTION, getTemplateExplore } from '@/api';
import { QUERY_KEY, getTemplateExplore } from '@/api';
import { ApiError } from '@/api/Error';
import { DEFAULT_SORTING_OPTION, PAGE_SIZE } from '@/models/templates';
import type { SortingKey, TemplateListResponse } from '@/types';

interface Props {
Expand Down
10 changes: 5 additions & 5 deletions frontend/src/queries/templates/useTemplateListQuery.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { renderHook, waitFor } from '@testing-library/react';

import { DEFAULT_SORTING_OPTION, PAGE_SIZE } from '@/api';
import { AuthProvider } from '@/contexts';
import { templates as mockTemplates } from '@/mocks/fixtures/templateList.json';
import { DEFAULT_SORTING_OPTION, PAGE_SIZE } from '@/models/templates';

import { useTemplateListQuery } from './useTemplateListQuery';

Expand Down Expand Up @@ -43,7 +43,7 @@ describe('useTemplateListQuery', () => {
keyword: 'console.log',
categoryId: undefined,
tagIds: [],
sort: 'modifiedAt,desc',
sort: 'createdAt,desc',
page: 1,
size: 20,
}),
Expand All @@ -70,7 +70,7 @@ describe('useTemplateListQuery', () => {
keyword: '',
categoryId: 1,
tagIds: [],
sort: 'modifiedAt,desc',
sort: 'createdAt,desc',
page: 1,
size: 20,
}),
Expand All @@ -89,7 +89,7 @@ describe('useTemplateListQuery', () => {
keyword: '',
categoryId: undefined,
tagIds: [3, 5],
sort: 'modifiedAt,desc',
sort: 'createdAt,desc',
page: 1,
size: 20,
}),
Expand All @@ -114,7 +114,7 @@ describe('useTemplateListQuery', () => {
keyword: '',
categoryId: undefined,
tagIds: [],
sort: 'modifiedAt,desc',
sort: 'createdAt,desc',
page: 2,
size: 20,
}),
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/queries/templates/useTemplateListQuery.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { keepPreviousData, useQuery } from '@tanstack/react-query';

import { PAGE_SIZE, QUERY_KEY, getTemplateList, DEFAULT_SORTING_OPTION } from '@/api';
import { QUERY_KEY, getTemplateList } from '@/api';
import { useAuth } from '@/hooks/authentication';
import { DEFAULT_SORTING_OPTION, PAGE_SIZE } from '@/models/templates';
import type { TemplateListResponse, SortingKey } from '@/types';

interface Props {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/service/getSortingOptionByValue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DEFAULT_SORTING_OPTION, SORTING_OPTIONS } from '@/api';
import { DEFAULT_SORTING_OPTION, SORTING_OPTIONS } from '@/models/templates';

export const getSortingOptionByValue = (value: string) =>
SORTING_OPTIONS.find((el) => el.value === value) || DEFAULT_SORTING_OPTION;
3 changes: 2 additions & 1 deletion frontend/src/types/api.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SORTING_OPTIONS } from '@/models/templates';
import { Category, SourceCodes, Tag, TemplateListItem, TemplateVisibility } from '@/types';

export interface TemplateListResponse {
Expand Down Expand Up @@ -75,6 +76,6 @@ export interface GetMemberNameResponse {
name: string;
}

export type SortingKey = 'modifiedAt,asc' | 'modifiedAt,desc' | 'likesCount,desc';
export type SortingKey = (typeof SORTING_OPTIONS)[number]['key'];

export type SortingOption = { key: SortingKey; value: string };

0 comments on commit 0f72be3

Please sign in to comment.