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 vote state #673

Merged
merged 1 commit into from
Jul 10, 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
14 changes: 5 additions & 9 deletions packages/berlin/src/pages/Cycle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
queryKey: ['votes', cycleId],
queryFn: () => fetchUserVotes(cycleId || ''),
enabled: !!user?.id && !!cycleId,
retry: false,
});

const availableHearts =
Expand All @@ -84,7 +83,7 @@
toast('Vote has closed. Redirecting to results.');
navigate(`/events/${eventId}/cycles/${cycleId}/results`);
}
}, [cycle?.status]);

Check warning on line 86 in packages/berlin/src/pages/Cycle.tsx

View workflow job for this annotation

GitHub Actions / Check linting

React Hook useEffect has missing dependencies: 'cycleId', 'eventId', and 'navigate'. Either include them or remove the dependency array

useEffect(() => {
if (cycle && cycle.startAt && cycle.endAt) {
Expand Down Expand Up @@ -129,17 +128,16 @@
}
}, [cycleState, time, formattedTime]);

const updateInitialVotesAndHearts = (votes: GetUserVotesResponse) => {
const givenVotes = votes
.map((option) => option.numOfVotes)
.reduce((prev, curr) => prev + curr, 0);
const updateInitialVotesAndHearts = (votes: GetUserVotesResponse | null | undefined) => {
const givenVotes =
votes?.map((option) => option.numOfVotes).reduce((prev, curr) => prev + curr, 0) ?? 0;

setAvailableHearts({
questionId: cycle?.forumQuestions[0].id || '',
hearts: Math.max(0, INITIAL_HEARTS - givenVotes),
});

setLocalUserVotes(votes);
setLocalUserVotes(votes ?? []);
};

const votesAreDifferent = useMemo(() => {
Expand All @@ -160,9 +158,7 @@
}, [localUserVotes, userVotes]);

useEffect(() => {
if (userVotes?.length) {
updateInitialVotesAndHearts(userVotes);
}
updateInitialVotesAndHearts(userVotes);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [userVotes]);

Expand Down
11 changes: 9 additions & 2 deletions packages/berlin/src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export const useAppStore = create<AppState>()(
persist(
(set) => ({
userStatus: 'INCOMPLETE',
eventRegistrationStatus: 'INCOMPLETE',
onboardingStatus: {
event: 'INCOMPLETE',
cycle: 'INCOMPLETE',
Expand All @@ -55,7 +54,15 @@ export const useAppStore = create<AppState>()(
availableHearts: {},
})),
}),
{ name: 'lexicon-store' },
{
name: 'lexicon-store',
// Partialize the store to only persist the required keys
partialize: (state) => ({
onboardingStatus: state.onboardingStatus,
userStatus: state.userStatus,
theme: state.theme,
}),
},
),
),
);
7 changes: 3 additions & 4 deletions packages/berlin/src/utils/voting.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { GetUserVotesResponse, PostVotesRequest } from 'api';
import { ResponseUserVotesType } from '../types/CycleType';
import toast from 'react-hot-toast';
import { INITIAL_HEARTS } from './constants';

export const handleLocalVote = (
optionId: string,
prevLocalUserVotes: ResponseUserVotesType | { optionId: string; numOfVotes: number }[],
prevLocalUserVotes: GetUserVotesResponse | { optionId: string; numOfVotes: number }[],
) => {
// find if the user has already voted for this option
const prevVote = prevLocalUserVotes.find((x) => x.optionId === optionId);
Expand All @@ -28,7 +27,7 @@ export const handleLocalVote = (

export const handleLocalUnVote = (
optionId: string,
prevLocalUserVotes: ResponseUserVotesType | { optionId: string; numOfVotes: number }[],
prevLocalUserVotes: GetUserVotesResponse | { optionId: string; numOfVotes: number }[],
) => {
// this will just find the option and decrease the number of votes by 1
const updatedLocalVotes = prevLocalUserVotes.map((prevLocalUserVote) => {
Expand All @@ -53,7 +52,7 @@ export const handleAvailableHearts = (availableHearts: number, type: 'vote' | 'u
export const handleSaveVotes = (
userVotes: GetUserVotesResponse | null | undefined,
localUserVotes:
| ResponseUserVotesType
| GetUserVotesResponse
| {
optionId: string;
numOfVotes: number;
Expand Down
Loading