Skip to content

Commit

Permalink
Current state (fixing voting)
Browse files Browse the repository at this point in the history
  • Loading branch information
camilovegag committed May 15, 2024
1 parent c019832 commit 20df7b7
Showing 1 changed file with 21 additions and 31 deletions.
52 changes: 21 additions & 31 deletions packages/berlin/src/pages/Comments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
fetchComments,
postComment,
fetchOptionUsers,
fetchCycle,

Check failure on line 15 in packages/berlin/src/pages/Comments.tsx

View workflow job for this annotation

GitHub Actions / Check linting

'fetchCycle' is defined but never used. Allowed unused vars must match /^_ignored/u
} from 'api';

// Hooks
Expand Down Expand Up @@ -40,15 +41,15 @@ import CommentsColumns from '../components/columns/comments-columns';
import IconButton from '../components/icon-button';
import Textarea from '../components/textarea';

type LocalUserVotes = ResponseUserVotesType | { optionId: string; numOfVotes: number }[];

function Comments() {
const theme = useAppStore((state) => state.theme);
const queryClient = useQueryClient();
const { cycleId, optionId } = useParams();
const { user } = useUser();
const { availableHearts, setAvailableHearts } = useAppStore((state) => state);
const [localUserVotes, setLocalUserVotes] = useState<
ResponseUserVotesType | { optionId: string; numOfVotes: number }[]
>([]);
const [localUserVotes, setLocalUserVotes] = useState<LocalUserVotes>([]);
const [localOptionHearts, setLocalOptionHearts] = useState(0);
const [comment, setComment] = useState('');
const [sortOrder, setSortOrder] = useState('desc'); // 'asc' for ascending, 'desc' for descending
Expand Down Expand Up @@ -76,7 +77,7 @@ function Comments() {
queryKey: ['comments', optionId],
queryFn: () => fetchComments({ optionId: optionId || '' }),
enabled: !!optionId,
refetchInterval: 5000, // Poll every 5 seconds
// refetchInterval: 5000, // Poll every 5 seconds
});

const sortedComments = useMemo(() => {
Expand All @@ -97,20 +98,6 @@ function Comments() {
}
}, [optionId, userVotes]);

const { mutate: mutateVotes } = useMutation({
mutationFn: postVotes,
onSuccess: (body) => {
if (body?.errors?.length) {
toast.error(`Failed to save votes, ${body?.errors[0].message}`);
} else if (body?.data.length) {
queryClient.invalidateQueries({ queryKey: ['votes', cycleId] });
// this is to update the plural scores in each option
queryClient.invalidateQueries({ queryKey: ['cycles', cycleId] });
toast.success('Votes saved successfully!');
}
},
});

const { mutate: mutateComments } = useMutation({
mutationFn: postComment,
onSuccess: (body) => {
Expand All @@ -130,19 +117,24 @@ function Comments() {
handleUnvote(optionId, availableHearts, setAvailableHearts, setLocalUserVotes);
};

const handleSaveVotesWrapper = () => {
const { mutate: mutateVotes } = useMutation({
mutationFn: postVotes,
onSuccess: (body) => {
if (body?.errors?.length) {
toast.error(`Failed to save votes, ${body?.errors[0].message}`);
} else if (body?.data.length) {
queryClient.invalidateQueries({ queryKey: ['votes', cycleId] });
// this is to update the plural scores in each option
queryClient.invalidateQueries({ queryKey: ['cycles', cycleId] });
toast.success('Votes saved successfully!');
}
},
});

const handleSaveVoteWrapper = () => {
handleSaveVotes(userVotes, localUserVotes, mutateVotes);
};

const votesAreDifferent = useMemo(() => {
if (localUserVotes && userVotes) {
return (
localUserVotes[0]?.numOfVotes !==
userVotes?.find((vote) => vote.optionId === optionId)?.numOfVotes
);
}
}, [localUserVotes, optionId, userVotes]);

const handlePostComment = () => {
if (optionId && comment) {
mutateComments({ questionOptionId: optionId, value: comment });
Expand Down Expand Up @@ -194,9 +186,7 @@ function Comments() {
)}
</FlexColumn>

<Button onClick={handleSaveVotesWrapper} disabled={!votesAreDifferent}>
Save votes
</Button>
<Button onClick={handleSaveVoteWrapper}>Save votes</Button>
<Form>
<Textarea
label="Leave a comment:"
Expand Down

0 comments on commit 20df7b7

Please sign in to comment.