Skip to content

Commit

Permalink
Refactor: 코드리뷰 반영
Browse files Browse the repository at this point in the history
  • Loading branch information
cham0287 committed Oct 3, 2023
1 parent a495f81 commit 6c74c71
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/components/ArticleCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const ArticleCard = ({
<Link href={`/articles/${slug}`}>Read more...</Link>
<ul className="flex gap-1">
{tagList.map((tag) => (
<Tag text={tag} />
<Tag text={tag} key={tag} />
))}
</ul>
</div>
Expand Down
4 changes: 2 additions & 2 deletions src/components/Articles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ const Articles = () => {
{isLoading && <div>Loading...</div>}
{articleResponse && (
<div>
<Tabs tabs={[undefined, "Global Feed", articlesParams.tag]} />
<Tabs tabs={["Global Feed", articlesParams.tag]} />
<ul>
{articleResponse.articles.map((article) => (
<ArticleCard article={article} />
))}
</ul>
<PaginationBar count={articleResponse.articlesCount} />
</div>
)}
{articleResponse && <PaginationBar count={articleResponse.articlesCount} />}
</div>
<PopularTags />
</section>
Expand Down
16 changes: 16 additions & 0 deletions src/components/Comment.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
interface Props {
profileImg: string;
username: string;
createdAt: Date;
comment: string;
}

const Comment = ({ profileImg, username, createdAt, comment }: Props) => {
return (
<article>
<section>{comment}</section>
<section></section>
</article>
);
};
export default Comment;
20 changes: 7 additions & 13 deletions src/components/PaginationBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,17 @@ const PaginationBar: React.FC<PaginationBarProps> = ({ count }) => {
setCurrentPage(page);
};

const renderPaginationButtons = () => {
const pageBtns = [];
for (let i = 1; i <= Math.ceil(count / 10); i++) {
pageBtns.push(
<PageBtn key={i} onClick={() => handlePageClick(i)} isActive={i === currentPage}>
{i}
</PageBtn>,
);
}
return pageBtns;
const PaginationButtons = () => {
return [...Array(Math.ceil(count / 10) + 1)].map((_, index) => (
<PageBtn key={index + 1} onClick={() => handlePageClick(index)} isActive={index === currentPage - 1}>
{index + 1}
</PageBtn>
));
};

return (
<div>
<div className="[&>*:first-child]:rounded-l-md [&>*:last-child]:rounded-r-md mt-5">
{renderPaginationButtons()}
</div>
<div className="[&>*:first-child]:rounded-l-md [&>*:last-child]:rounded-r-md mt-5">{PaginationButtons()}</div>
</div>
);
};
Expand Down
7 changes: 3 additions & 4 deletions src/components/PopularTags.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import PopularTagBtn from "./buttons/PopularTagBtn";
import { useTabsStore, useArticlesParamsStore } from "@/stores/useStore";

const PopularTags = () => {
const { data: tags } = useQuery(["/api/tags"], () => getTags(), {
const { data: tags, isLoading } = useQuery(["/api/tags"], () => getTags(), {
refetchOnWindowFocus: false,
});
const {
Expand All @@ -24,17 +24,16 @@ const PopularTags = () => {
return (
<div className="bg-gray-200 p-2">
<h2 className="text-sm mb-1">Popular Tags</h2>
{tags ? (
{tags && (
<ul className="flex flex-wrap w-60 gap-1 ">
{tags.map((tag) => (
<PopularTagBtn selected={selectedTag === tag} key={tag} onClick={() => handleClickPopularTag(tag)}>
{tag}
</PopularTagBtn>
))}
</ul>
) : (
<p>Loading tags...</p>
)}
{isLoading && <p>Loading tags...</p>}
</div>
);
};
Expand Down
6 changes: 1 addition & 5 deletions src/components/ProvidersWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import { QueryClientProvider } from "@tanstack/react-query";
const queryClient = new QueryClient();

const ProvidersWrapper = ({ children }: { children: React.ReactNode }) => {
return (
<div>
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
</div>
);
return <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>;
};
export default ProvidersWrapper;
10 changes: 8 additions & 2 deletions src/components/Tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ import { activeTabStyle, inactiveTabStyle } from "./styles/tabs.css";
import { useTabsStore, useArticlesParamsStore } from "@/stores/useStore";

interface TabsProps {
tabs: (string | undefined)[];
tabs: string[];
}

const Tabs = ({ tabs }: TabsProps) => {
return <ul className="flex">{tabs.map((tab) => tab && <Tab key={tab} text={tab} />)}</ul>;
return (
<ul className="flex">
{tabs.map((tab) => (
<Tab key={tab} text={tab} />
))}
</ul>
);
};
export default Tabs;

Expand Down
1 change: 1 addition & 0 deletions src/components/atoms/Tag.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ interface Props {
const Tag = ({ text }: Props) => {
return <div className={TagStyle}>{text}</div>;
};

export default Tag;
3 changes: 1 addition & 2 deletions src/components/buttons/PopularTagBtn.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { ButtonHTMLAttributes, ReactNode } from "react";
import { ButtonHTMLAttributes } from "react";
import { popularTagBtnStyles, selectedPopularTagStyles } from "./styles/popularTagBtn.css";

interface PopularTagBtnProps extends ButtonHTMLAttributes<HTMLButtonElement> {
children: ReactNode;
selected: boolean;
}

Expand Down
9 changes: 9 additions & 0 deletions src/services/comments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { executeAPI } from "./app";

export const getComments = async (slug: string): Promise<any> => {
const result = await executeAPI({
method: "GET",
url: `/articles/${slug}/comments`,
});
return result;
};
22 changes: 11 additions & 11 deletions src/stores/useStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ interface TabState {
setActiveTab: (tab: string) => void;
}

type ArticlesState = {
articlesParams: ArticleQueryParams;
};

type ArticlesStoreActions = {
setSelectedTag: (tag: string) => void;
resetSelectedTag: () => void;
setCurrentPage: (selectedPage: number) => void;
resetCurrentPage: () => void;
};

export const useTabsStore = create<TabState>((set) => ({
activeTab: "Global Feed",
setActiveTab: (newTab: string) => set({ activeTab: newTab }),
Expand All @@ -19,17 +30,6 @@ const defaultArticlesState = {
limit: 10,
};

type ArticlesState = {
articlesParams: ArticleQueryParams;
};

type ArticlesStoreActions = {
setSelectedTag: (tag: string) => void;
resetSelectedTag: () => void;
setCurrentPage: (selectedPage: number) => void;
resetCurrentPage: () => void;
};

export const useArticlesParamsStore = create(
immer<ArticlesState & ArticlesStoreActions>((set) => ({
articlesParams: defaultArticlesState,
Expand Down

0 comments on commit 6c74c71

Please sign in to comment.