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

[#115] Refactor: 메인페이지 관련 리팩토링 #119

Merged
merged 7 commits into from
Jun 21, 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
33 changes: 14 additions & 19 deletions src/apis/postList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ interface PostListSort {
unsorted: boolean;
sorted: boolean;
}

export interface PostList {
content: PostListContent[];
pageable: PostListPageable;
Expand All @@ -43,36 +44,30 @@ export interface PostList {
empty: boolean;
}

interface PostListInitialParams {
interface GetPostListParams {
page: string;
size: string;
keyword: string | null;
categoryId: string | null;
}

interface PostListParamsWithCategoryId extends PostListInitialParams {
categoryId: string;
keyword: null;
}

interface PostListParamsWithKeyword extends PostListInitialParams {
keyword: string;
categoryId: null;
}

export type GetPostListParams =
| PostListParamsWithCategoryId
| PostListParamsWithKeyword
| PostListInitialParams;

export const getPostList = async (params: GetPostListParams) =>
await api.get<PostList>({
url: '/posts/search',
params,
});

export const useGetPostListAPI = (params: GetPostListParams) => {
const PAGE_SIZE = '8';

export const useGetPostListAPI = (
keyword: string | null,
categoryId: string | null,
) => {
// TODO: 인피니티 스크롤 구현
const { data } = useSuspenseQuery({
queryKey: ['postList', ...Object.values(params)],
queryFn: () => getPostList(params),
queryKey: ['postList', { page: '0', keyword, categoryId }],
queryFn: () =>
getPostList({ page: '0', size: PAGE_SIZE, keyword, categoryId }),
});

return data.body;
Expand Down
10 changes: 1 addition & 9 deletions src/app/(main)/constants.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
interface CategoryMap {
all: null;
chatting: string;
sports: string;
entertaining: string;
study: string;
}

export const CATEGORY_MAP = {
all: null,
chatting: '1',
sports: '2',
entertaining: '3',
study: '4',
} satisfies CategoryMap;
};
23 changes: 0 additions & 23 deletions src/app/(main)/utils.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/app/_components/Chip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ReactNode } from 'react';
import { type VariantProps, cva } from 'class-variance-authority';

const chipCSS = cva(
'flex w-fit items-center whitespace-nowrap rounded-[20px] border border-gray-accent2 px-4 py-2 text-sm font-semibold',
'text-title-1-md flex w-fit items-center whitespace-nowrap rounded-[30px] border border-gray-accent2 px-3 py-1.5',
{
variants: {
variant: {
Expand Down
23 changes: 9 additions & 14 deletions src/app/_components/ChipContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { useState } from 'react';

import Link from 'next/link';

import Chip from '@/src/app/_components/Chip';
Expand All @@ -24,23 +22,20 @@ interface ChipContainerProps {
}

const ChipContainer = ({ currentChannel = 'all' }: ChipContainerProps) => {
const [currentPath, setCurrentPath] = useState(currentChannel);

const handleChipClick = (path: ChannelType) => () => {
setCurrentPath(path);
};

return (
<ul className='flex flex-nowrap gap-2 px-4 py-2'>
<ul className='flex items-center gap-2 px-4 py-2'>
{channelData.map(({ name, path }, index) => {
const variant = path === currentPath ? 'selected' : 'default';
const variant = path === currentChannel ? 'selected' : 'default';

return (
<li key={`${index}-${name}`}>
<Link href={{ pathname: '/', query: { channel: path } }}>
<Chip variant={variant} onClick={handleChipClick(path)}>
{name}
</Chip>
<Link
href={{
pathname: '/',
query: path === 'all' ? {} : { channel: path },
}}
>
<Chip variant={variant}>{name}</Chip>
</Link>
</li>
);
Expand Down
2 changes: 1 addition & 1 deletion src/app/_components/PostItem.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Image from 'next/image';

import { PostListContent } from '@/src/apis/postList';
import { isValidImageUrl } from '@/src/app/(main)/utils';
import Icon from '@/src/components/Icon';
import { isValidImageUrl } from '@/src/utils/isValidImageUrl';

interface PostItemProps {
post: PostListContent;
Expand Down
12 changes: 4 additions & 8 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Link from 'next/link';
import { useSearchParams } from 'next/navigation';

import { useGetPostListAPI } from '@/src/apis/postList';
import { searchParamsToGetPostListParams } from '@/src/app/(main)/utils';
import { CATEGORY_MAP } from '@/src/app/(main)/constants';
import ChipContainer, {
ChannelType,
} from '@/src/app/_components/ChipContainer';
Expand All @@ -17,14 +17,10 @@ import TopBar from '../components/Topbar';

const Main = () => {
const searchParams = useSearchParams();
const channel = searchParams.get('channel') as ChannelType;
const keyword = searchParams.get('keyword') as string;
const channel = (searchParams.get('channel') as ChannelType | null) ?? 'all';
const keyword = searchParams.get('keyword');

const { content: posts } = useGetPostListAPI({
page: '0',
size: '8',
...searchParamsToGetPostListParams(channel, keyword),
});
const { content: posts } = useGetPostListAPI(keyword, CATEGORY_MAP[channel]);

return (
<>
Expand Down
7 changes: 7 additions & 0 deletions src/utils/isValidImageUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const isValidImageUrl = (url: string | null): url is string => {
if (!url) return false;
if (url.startsWith('data:')) return true;
if (url.startsWith('http://') || url.startsWith('https://')) return true;

return false;
};
Loading