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: votes #851

Merged
merged 1 commit into from
Jan 11, 2023
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
2 changes: 1 addition & 1 deletion frontend/src/components/Board/Card/CardFooter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ const CardFooter = React.memo<FooterProps>(
<StyledButtonIcon
disabled={
!isMainboard ||
user?.votesCount === maxVotes ||
(maxVotes && user?.votesCount === maxVotes) ||
(maxVotes && disableVotes.maxVotesReached) ||
(hideCards && createdBy?._id !== userId)
}
Expand Down
17 changes: 14 additions & 3 deletions frontend/src/hooks/useVotes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ const useVotes = () => {
);
};

const getPrevData = async (id: string | undefined): Promise<BoardType | undefined> => {
const query = getBoardQueryKey(id);
await queryClient.cancelQueries(query);
const prevData = queryClient.getQueryData<{ board: BoardType }>(query);
return prevData?.board;
};

const getFirstCardItemIndexWithVotes = (cardItems: CardItemType[]) =>
cardItems.findIndex((cardItem) => cardItem.votes.length > 0);

Expand Down Expand Up @@ -231,19 +238,23 @@ const useVotes = () => {
};

const updateVote = async (variables: VoteDto) => {
const { newBoardData } = await updateVoteOptimistic(
const { newBoardData, prevBoardData } = await updateVoteOptimistic(
variables.count > 0 ? Action.Add : Action.Remove,
variables,
);

if (newBoardData?.maxVotes && variables.userId === userId) {
toastRemainingVotesMessage('', newBoardData);
}

return { newBoardData, prevBoardData };
};

const handleVote = useMutation(handleVotes, {
onSuccess: async (data, variables) => {
updateVote(variables);
onMutate: async (variables) => {
const { newBoardData, prevBoardData } = await updateVote(variables);

return { previousBoard: prevBoardData, data: newBoardData };
},
onError: (_, variables, context) => {
queryClient.invalidateQueries(['board', { id: variables.boardId }]);
Expand Down