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

fix: 투표 취소 에러 대응 #132

Merged
merged 3 commits into from
Jan 5, 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
13 changes: 12 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import React from 'react';
import React, { useEffect } from 'react';
import { StyleSheetManager, ThemeProvider } from 'styled-components';

import GlobalStyle from '@styles/global';
import { theme } from '@styles/theme';

import client from '@apis/fetch';

import Router from './routes';

const App = () => {
const queryClient = new QueryClient();
const isDev = import.meta.env.DEV;

useEffect(() => {
if (isDev) {
[...Array.from({ length: 20 }, (_, i) => i + 1)].map((topicId) => {
client.delete(`topics/${topicId}/vote`);
});
}
}, []);

return (
<StyleSheetManager shouldForwardProp={() => true}>
Expand Down
3 changes: 2 additions & 1 deletion src/apis/comment/useComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ const useComments = (topicId: number, enabled: boolean) => {
});
};

const useLatestComment = (topicId: number) => {
const useLatestComment = (topicId: number, enabled: boolean) => {
return useQuery({
queryKey: [COMMENT_KEY, 'latest', topicId],
queryFn: () => getComments({ topicId: topicId, page: 0, size: 1 }),
enabled: enabled,
});
};

Expand Down
12 changes: 12 additions & 0 deletions src/apis/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ class Fetch {
return data as TData;
}

async delete<T>(path: string): Promise<T> {
const response = await fetch(`${this.baseURL}${path}`, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
...(this.accessToken && { Authorization: `Bearer ${this.accessToken}` }),
},
});
const data: T = await response.json();
return data;
}

setAccessToken(token: string) {
this.accessToken = token;
}
Expand Down
5 changes: 4 additions & 1 deletion src/components/Home/TopicCard/TopicCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ const TopicCard = ({ topic }: TopicCardProps) => {

const { BottomSheet: CommentSheet, toggleSheet } = useBottomSheet({});
const voteMutation = useVoteTopic();
const { data: latestCommentData, isSuccess } = useLatestComment(topic.topicId);
const { data: latestCommentData, isSuccess } = useLatestComment(
topic.topicId,
topic.selectedOption !== null
);
const [latestComment, setLatestComment] = useState<LatestComment | undefined>();

useEffect(() => {
Expand Down