diff --git a/actions/pollActions.ts b/actions/pollActions.ts index c05b1c7c..92f2a2f3 100644 --- a/actions/pollActions.ts +++ b/actions/pollActions.ts @@ -22,7 +22,18 @@ export async function getPollData(entity_sets: string[]) { .from(poll_votes_on_entity) .innerJoin(entities, eq(entities.id, poll_votes_on_entity.poll_entity)) .where(and(inArray(entities.set, entity_sets))); - return { polls, voter_token }; + return { + polls: polls.map((p) => ({ + poll_votes_on_entity: { + ...p.poll_votes_on_entity, + voter_token: + voter_token === p.poll_votes_on_entity.voter_token + ? voter_token + : undefined, + }, + })), + voter_token, + }; } export async function voteOnPoll( @@ -46,16 +57,22 @@ export async function voteOnPoll( pollVote.find((v) => { return v.voter_token === voter_token; }) - ) - return; + ) { + await db + .delete(poll_votes_on_entity) + .where( + and( + eq(poll_votes_on_entity.voter_token, voter_token), + eq(poll_votes_on_entity.poll_entity, poll_entity), + ), + ); + } - await db - .insert(poll_votes_on_entity) - .values( - option_entities.map((option_entity) => ({ - option_entity, - poll_entity, - voter_token, - })), - ); + await db.insert(poll_votes_on_entity).values( + option_entities.map((option_entity) => ({ + option_entity, + poll_entity, + voter_token, + })), + ); } diff --git a/components/Blocks/PollBlock.tsx b/components/Blocks/PollBlock.tsx index 1793a9e3..a5ccf4c7 100644 --- a/components/Blocks/PollBlock.tsx +++ b/components/Blocks/PollBlock.tsx @@ -70,7 +70,7 @@ export const PollBlock = (props: BlockProps) => { entityID={props.entityID} close={() => { if (hasVoted) setPollState("results"); - setPollState("voting"); + else setPollState("voting"); }} /> ) : pollState === "results" ? ( @@ -98,7 +98,7 @@ export const PollBlock = (props: BlockProps) => { )} - {!hasVoted && } + {permissions.write && } void }) => { return { ...oldState, polls: [ - ...oldState.polls, + ...oldState.polls.filter( + (p) => + !( + p.poll_votes_on_entity.voter_token === + oldState.voter_token && + p.poll_votes_on_entity.poll_entity == props.entityID + ), + ), ...selectedPollOptions.map((option_entity) => ({ poll_votes_on_entity: { option_entity, @@ -418,7 +425,6 @@ const PollStateToggle = (props: { hasVoted: boolean; pollState: "editing" | "voting" | "results"; }) => { - if (props.pollState === "results" && props.hasVoted) return null; return ( ); };