Skip to content

Commit

Permalink
update comment count after manipulating comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rtrembecky committed Nov 10, 2023
1 parent d4b53be commit c784f67
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/components/Problems/Discussion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ interface DiscussionProps {
problemId: number
problemNumber: number
closeDiscussion: () => void
invalidateSeriesQuery: () => Promise<void>
}

export const Discussion: FC<DiscussionProps> = ({problemId, problemNumber, closeDiscussion}) => {
export const Discussion: FC<DiscussionProps> = ({problemId, problemNumber, closeDiscussion, invalidateSeriesQuery}) => {
const [commentText, setCommentText] = useState('')
const [hiddenResponseText, setHiddenResponseText] = useState('')
const [hiddenResponseDialogId, sethiddenResponseDialogId] = useState(-1)

const queryKey = ['competition', 'problem', problemId, 'comments']
const {data: commentsData, isLoading: commentsIsLoading} = useQuery({
queryKey: ['competition', 'problem', problemId, 'comments'],
queryKey,
queryFn: () => axios.get<Comment[]>(`/api/competition/problem/${problemId}/comments`),
})
const comments = commentsData?.data.map((comment) => ({
Expand All @@ -42,26 +44,34 @@ export const Discussion: FC<DiscussionProps> = ({problemId, problemNumber, close

const queryClient = useQueryClient()

const invalidateCommentsAndCount = async () => {
await Promise.all([
queryClient.invalidateQueries({queryKey}),
// comment count comes from problem from series
invalidateSeriesQuery(),
])
}

const {mutate: addComment} = useMutation({
mutationFn: () => axios.post(`/api/competition/problem/${problemId}/add-comment`, {text: commentText}),
onSuccess: () => {
setCommentText('')
queryClient.invalidateQueries({queryKey: ['competition', 'problem', problemId, 'comments']})
invalidateCommentsAndCount()
},
})

const {mutate: publishComment} = useMutation({
mutationFn: (id: number) => axios.post(`/api/competition/comment/${id}/publish`),
onSuccess: () => {
queryClient.invalidateQueries({queryKey: ['competition', 'problem', problemId, 'comments']})
invalidateCommentsAndCount()
},
})

const {mutate: hideComment} = useMutation({
mutationFn: ({id, hiddenResponseText}: {id: number; hiddenResponseText: string}) =>
axios.post(`/api/competition/comment/${id}/hide`, {hidden_response: hiddenResponseText}),
onSuccess: () => {
queryClient.invalidateQueries({queryKey: ['competition', 'problem', problemId, 'comments']})
invalidateCommentsAndCount()
sethiddenResponseDialogId(-1)
setHiddenResponseText('')
},
Expand All @@ -70,7 +80,7 @@ export const Discussion: FC<DiscussionProps> = ({problemId, problemNumber, close
const {mutate: deleteComment} = useMutation({
mutationFn: (id: number) => axios.delete(`/api/competition/comment/${id}`),
onSuccess: () => {
queryClient.invalidateQueries({queryKey: ['competition', 'problem', problemId, 'comments']})
invalidateCommentsAndCount()
},
})

Expand Down
1 change: 1 addition & 0 deletions src/components/Problems/Problems.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ export const Problems: FC = () => {
problemId={displaySideContent.problemId}
problemNumber={displaySideContent.problemNumber}
closeDiscussion={() => setDisplaySideContent({type: '', problemId: -1, problemNumber: -1})}
invalidateSeriesQuery={invalidateSeriesQuery}
/>
)}
{/* {displaySideContent.type === 'uploadProblemForm' && (
Expand Down

0 comments on commit c784f67

Please sign in to comment.