diff --git a/app/src/lib/useVote.ts b/app/src/lib/useVote.ts index 71325b4a..4a9b171d 100644 --- a/app/src/lib/useVote.ts +++ b/app/src/lib/useVote.ts @@ -56,7 +56,8 @@ export function useVote( // leaves the buttons showing something that was not stored. const mutate = async ( run: (id: string) => Promise, - failure: string + failure: string, + handleClose?: () => void ) => { if (!variantId) return false; if (!userId) { @@ -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); + + setVote(submission); + await loadTally(variantId); return true; } catch { @@ -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 }; } diff --git a/app/src/routes/guides/$slug/$variantSlug/index.tsx b/app/src/routes/guides/$slug/$variantSlug/index.tsx index b19b14e8..fc6799c6 100644 --- a/app/src/routes/guides/$slug/$variantSlug/index.tsx +++ b/app/src/routes/guides/$slug/$variantSlug/index.tsx @@ -195,7 +195,7 @@ function RouteComponent() { if (await downvote(reason, note)) setDownvoteOpen(false); }} onRemove={async () => { - if (await removeVote()) setDownvoteOpen(false); + await removeVote(() => setDownvoteOpen(false)); }} /> diff --git a/app/src/routes/guides/$slug/index.tsx b/app/src/routes/guides/$slug/index.tsx index 449bcb3e..1d05aa4d 100644 --- a/app/src/routes/guides/$slug/index.tsx +++ b/app/src/routes/guides/$slug/index.tsx @@ -184,10 +184,12 @@ function RouteComponent() { : null } onSubmit={async (reason, note) => { - if (await downvote(reason, note)) setDownvoteOpen(false); + await downvote(reason, note, async () => + setDownvoteOpen(false) + ); }} onRemove={async () => { - if (await removeVote()) setDownvoteOpen(false); + await removeVote(() => setDownvoteOpen(false)); }} />