Skip to content

Commit

Permalink
refactor : Refactor TopicCard and TopicCardContainer components
Browse files Browse the repository at this point in the history
eliminate useState,useEffect in TopicCardContainer
  • Loading branch information
jiwonh423 committed Jan 14, 2024
1 parent fed8719 commit 7036e7e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 22 deletions.
8 changes: 5 additions & 3 deletions frontend/src/components/TopicCard/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SyntheticEvent, useContext, useState } from 'react';
import { QueryObserverResult, RefetchOptions } from '@tanstack/react-query';
import { useContext, useState } from 'react';
import { styled } from 'styled-components';

import SeeTogetherSVG from '../../assets/seeTogetherBtn_filled.svg';
Expand Down Expand Up @@ -26,7 +27,9 @@ interface OnClickDesignatedProps {
interface TopicCardExtendedProps extends TopicCardProps {
cardType: 'default' | 'modal';
onClickDesignated?: ({ topicId, topicName }: OnClickDesignatedProps) => void;
getTopicsFromServer?: () => void;
getTopicsFromServer?: (
options?: RefetchOptions | undefined,
) => Promise<QueryObserverResult<TopicCardProps[], Error>>;
}

function TopicCard({
Expand All @@ -39,7 +42,6 @@ function TopicCard({
pinCount,
bookmarkCount,
isInAtlas,
isBookmarked,
onClickDesignated,
getTopicsFromServer,
}: TopicCardExtendedProps) {
Expand Down
19 changes: 2 additions & 17 deletions frontend/src/components/TopicCardContainer/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { Swiper, Tab } from 'map-befine-swiper';
import { useEffect, useState } from 'react';
import { styled } from 'styled-components';

import useGet from '../../apiHooks/useGet';
import useTopicsQuery from '../../hooks/api/useTopicsQuery';
import useKeyDown from '../../hooks/useKeyDown';
import { TopicCardProps } from '../../types/Topic';
import Box from '../common/Box';
import Flex from '../common/Flex';
import Space from '../common/Space';
Expand All @@ -26,20 +23,8 @@ function TopicCardContainer({
containerDescription,
routeWhenSeeAll,
}: TopicCardContainerProps) {
const [_, setTopics] = useState<TopicCardProps[] | null>(null);
const { topics, refetchTopics } = useTopicsQuery(url);
const { elementRef, onElementKeyDown } = useKeyDown<HTMLSpanElement>();
const { fetchGet } = useGet();

const setTopicsFromServer = async () => {
await fetchGet<TopicCardProps[]>(
url,
'์ง€๋„๋ฅผ ๊ฐ€์ ธ์˜ค๋Š”๋ฐ ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค. ์ž ์‹œ ํ›„ ๋‹ค์‹œ ์‹œ๋„ํ•ด์ฃผ์„ธ์š”.',
(response) => {
setTopics(response);
},
);
};
const { topics } = useTopicsQuery(url);

return (
<section>
Expand Down Expand Up @@ -110,7 +95,7 @@ function TopicCardContainer({
bookmarkCount={topic.bookmarkCount}
isInAtlas={topic.isInAtlas}
isBookmarked={topic.isBookmarked}
getTopicsFromServer={setTopicsFromServer}
getTopicsFromServer={refetchTopics}
/>
<CustomSpace />
</Flex>
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/hooks/api/useTopicsQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { useQuery } from '@tanstack/react-query';
import { getTopics } from '../../api';

const useTopicsQuery = (url: string) => {
const { data: topics } = useQuery({
const { data: topics, refetch: refetchTopics } = useQuery({
queryKey: ['topics', url],
queryFn: () => getTopics(url),
});
return { topics };
return { topics, refetchTopics };
};

export default useTopicsQuery;

0 comments on commit 7036e7e

Please sign in to comment.