Skip to content

Commit

Permalink
fix: separate onVote
Browse files Browse the repository at this point in the history
  • Loading branch information
Jinho1011 committed Feb 13, 2024
1 parent 2c0c540 commit 4ef34b5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
10 changes: 2 additions & 8 deletions src/components/A/ATopicCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,19 @@ import { getDateDiff } from '@utils/date';

interface AlphaTopicCardProps extends TopicResponse {
chip?: 'popular' | 'close';
onVote: (topicId: number, side: 'CHOICE_A' | 'CHOICE_B') => void;
}

const AlphaTopicCard = React.memo((props: AlphaTopicCardProps) => {
const { BottomSheet: CommentSheet, toggleSheet } = useBottomSheet({});
const voteMutation = useVoteTopic({ side: 'TOPIC_A', sort: 'createdAt,DESC' });
const [A, B] = props.choices;

const handleCommentChipClick = () => {
toggleSheet();
};

const handleVote = (side: 'CHOICE_A' | 'CHOICE_B') => {
if (props.selectedOption === null) {
voteMutation.mutate({
topicId: props.topicId,
choiceOption: side,
votedAt: new Date().getTime() / 1000,
});
}
props.onVote(props.topicId, side);
};

return (
Expand Down
11 changes: 11 additions & 0 deletions src/routes/A/ATopics.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ChangeEvent, useState } from 'react';

import useTopics from '@apis/topic/useTopics';
import useVoteTopic from '@apis/topic/useVoteTopic';
import ATopicCard from '@components/A/ATopicCard';
import { Col, Row } from '@components/commons/Flex/Flex';
import Layout from '@components/commons/Layout/Layout';
Expand All @@ -15,6 +16,7 @@ import { Container } from './ATopics.styles';

const ATopics = () => {
const { data } = useTopics({ side: 'TOPIC_A', sort: 'createdAt,DESC' });
const voteMutation = useVoteTopic({ side: 'TOPIC_A', sort: 'createdAt,DESC' });
const [topicFilter, setTopicFilter] = useState('진행중');
const [isMineOnly, setIsMineOnly] = useState(false);
const [isLatest, setIsLatest] = useState(true);
Expand All @@ -25,6 +27,14 @@ const ATopics = () => {
setTopicFilter(e.target.value);
};

const handleVote = (topicId: number, side: 'CHOICE_A' | 'CHOICE_B') => {
voteMutation.mutate({
topicId: topicId,
choiceOption: side,
votedAt: new Date().getTime() / 1000,
});
};

return (
<Layout
hasBottomNavigation
Expand Down Expand Up @@ -88,6 +98,7 @@ const ATopics = () => {
selectedOption={topic.selectedOption}
commentCount={topic.commentCount}
createdAt={topic.createdAt}
onVote={handleVote}
/>
);
})}
Expand Down

0 comments on commit 4ef34b5

Please sign in to comment.