|
|
|
@ -48,6 +48,16 @@ const createVoteCallbacks = (
|
|
|
|
|
type MutationKey = Parameters<typeof trpc.useMutation>[0];
|
|
|
|
|
type QueryKey = Parameters<typeof trpc.useQuery>[0][0];
|
|
|
|
|
|
|
|
|
|
const getVoteValue = (vote: Vote | null) => {
|
|
|
|
|
if (vote === Vote.UPVOTE) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
if (vote === Vote.DOWNVOTE) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useQuestionVote = (id: string) => {
|
|
|
|
|
const utils = trpc.useContext();
|
|
|
|
|
|
|
|
|
@ -58,23 +68,14 @@ export const useQuestionVote = (id: string) => {
|
|
|
|
|
// 'questions.questions.getQuestionsByFilterAndContent',
|
|
|
|
|
],
|
|
|
|
|
onMutate: async (previousVote, currentVote) => {
|
|
|
|
|
// Update question list
|
|
|
|
|
const questionQueries = utils.queryClient.getQueriesData([
|
|
|
|
|
'questions.questions.getQuestionsByFilterAndContent',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const getVoteValue = (vote: Vote | null) => {
|
|
|
|
|
if (vote === Vote.UPVOTE) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
if (vote === Vote.DOWNVOTE) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const voteValueChange =
|
|
|
|
|
getVoteValue(currentVote) - getVoteValue(previousVote);
|
|
|
|
|
|
|
|
|
|
if (questionQueries !== undefined) {
|
|
|
|
|
for (const [key, query] of questionQueries) {
|
|
|
|
|
if (query === undefined) {
|
|
|
|
|
continue;
|
|
|
|
@ -103,14 +104,16 @@ export const useQuestionVote = (id: string) => {
|
|
|
|
|
|
|
|
|
|
utils.queryClient.setQueryData(key, newQuery);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const prevQuestion = utils.queryClient.getQueryData([
|
|
|
|
|
'questions.questions.getQuestionById',
|
|
|
|
|
{
|
|
|
|
|
id,
|
|
|
|
|
},
|
|
|
|
|
]) as Question;
|
|
|
|
|
]) as Question | undefined;
|
|
|
|
|
|
|
|
|
|
if (prevQuestion !== undefined) {
|
|
|
|
|
const newQuestion = {
|
|
|
|
|
...prevQuestion,
|
|
|
|
|
numVotes: prevQuestion.numVotes + voteValueChange,
|
|
|
|
@ -120,6 +123,7 @@ export const useQuestionVote = (id: string) => {
|
|
|
|
|
['questions.questions.getQuestionById', { id }],
|
|
|
|
|
newQuestion,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
query: 'questions.questions.user.getVote',
|
|
|
|
|
setDownVoteKey: 'questions.questions.user.setDownVote',
|
|
|
|
|