Skip to content
Open
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
37 changes: 28 additions & 9 deletions app/src/lib/useVote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ export function useVote(
// leaves the buttons showing something that was not stored.
const mutate = async (
run: (id: string) => Promise<MyVote>,
failure: string
failure: string,
handleClose?: () => void
) => {
if (!variantId) return false;
if (!userId) {
Expand All @@ -66,7 +67,16 @@ export function useVote(

setSubmitting(true);
try {
setVote(await run(variantId));
const submission = await run(variantId);

handleClose?.();

const delay = (ms: number) =>
new Promise((resolve) => setTimeout(resolve, ms));
await delay(200);
Comment on lines +74 to +76

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The delay on the upvote button can optionally be removed by adding a check to see if submission === "up".

Suggested change
const delay = (ms: number) =>
new Promise((resolve) => setTimeout(resolve, ms));
await delay(200);
if (submission === "up") {
const delay = (ms: number) =>
new Promise((resolve) => setTimeout(resolve, ms));
await delay(200);
}


setVote(submission);

await loadTally(variantId);
return true;
} catch {
Expand All @@ -86,17 +96,26 @@ export function useVote(
return castVote(id, { direction: "up" });
}, "Could not save your vote.");

const downvote = (reason: DownvoteReason, note: string) =>
const downvote = (
reason: DownvoteReason,
note: string,
handleClose?: () => void
) =>
mutate(
(id) => castVote(id, { direction: "down", reason, note: note || null }),
"Could not save your downvote."
"Could not save your downvote.",
handleClose
);

const removeVote = () =>
mutate(async (id) => {
await retractVote(id);
return null;
}, "Could not remove your vote.");
const removeVote = (handleClose: () => void) =>
mutate(
async (id) => {
await retractVote(id);
return null;
},
"Could not remove your vote.",
handleClose
);

return { vote, tally, submitting, upvote, downvote, removeVote };
}
2 changes: 1 addition & 1 deletion app/src/routes/guides/$slug/$variantSlug/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ function RouteComponent() {
if (await downvote(reason, note)) setDownvoteOpen(false);
}}
onRemove={async () => {
if (await removeVote()) setDownvoteOpen(false);
await removeVote(() => setDownvoteOpen(false));
}}
/>

Expand Down
6 changes: 4 additions & 2 deletions app/src/routes/guides/$slug/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,12 @@
: null
}
onSubmit={async (reason, note) => {
if (await downvote(reason, note)) setDownvoteOpen(false);
await downvote(reason, note, async () =>

Check warning on line 187 in app/src/routes/guides/$slug/index.tsx

View workflow job for this annotation

GitHub Actions / app · typecheck + lint + build

Async arrow function has no 'await' expression
setDownvoteOpen(false)
);
}}
onRemove={async () => {
if (await removeVote()) setDownvoteOpen(false);
await removeVote(() => setDownvoteOpen(false));
}}
/>

Expand Down
Loading