From 45441d3a9e268dd4d1082641a2c8e06994907735 Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Sun, 26 May 2019 18:48:04 -0700 Subject: [PATCH] fix: show poll results, time remaining, allow refresh (#1233) more work towards #1130 --- bin/svgs.js | 4 +- src/routes/_actions/polls.js | 14 ++ src/routes/_api/polls.js | 7 + src/routes/_components/status/StatusPoll.html | 196 +++++++++++++++++- src/routes/_intl/formatTimeagoDate.js | 10 + 5 files changed, 224 insertions(+), 7 deletions(-) create mode 100644 src/routes/_actions/polls.js create mode 100644 src/routes/_api/polls.js diff --git a/bin/svgs.js b/bin/svgs.js index af69d1b..4570348 100644 --- a/bin/svgs.js +++ b/bin/svgs.js @@ -47,5 +47,7 @@ module.exports = [ { id: 'fa-share-square-o', src: 'src/thirdparty/font-awesome-svg-png/white/svg/share-square-o.svg' }, { id: 'fa-flag', src: 'src/thirdparty/font-awesome-svg-png/white/svg/flag.svg' }, { id: 'fa-suitcase', src: 'src/thirdparty/font-awesome-svg-png/white/svg/suitcase.svg' }, - { id: 'fa-bar-chart', src: 'src/thirdparty/font-awesome-svg-png/white/svg/bar-chart.svg' } + { id: 'fa-bar-chart', src: 'src/thirdparty/font-awesome-svg-png/white/svg/bar-chart.svg' }, + { id: 'fa-clock', src: 'src/thirdparty/font-awesome-svg-png/white/svg/clock-o.svg' }, + { id: 'fa-refresh', src: 'src/thirdparty/font-awesome-svg-png/white/svg/refresh.svg' } ] diff --git a/src/routes/_actions/polls.js b/src/routes/_actions/polls.js new file mode 100644 index 0000000..612b877 --- /dev/null +++ b/src/routes/_actions/polls.js @@ -0,0 +1,14 @@ +import { getPoll as getPollApi } from '../_api/polls' +import { store } from '../_store/store' +import { toast } from '../_components/toast/toast' + +export async function getPoll (pollId) { + let { currentInstance, accessToken } = store.get() + try { + let poll = await getPollApi(currentInstance, accessToken, pollId) + return poll + } catch (e) { + console.error(e) + toast.say(`Unable to refresh poll`) + } +} diff --git a/src/routes/_api/polls.js b/src/routes/_api/polls.js new file mode 100644 index 0000000..5610666 --- /dev/null +++ b/src/routes/_api/polls.js @@ -0,0 +1,7 @@ +import { get, DEFAULT_TIMEOUT } from '../_utils/ajax' +import { auth, basename } from './utils' + +export async function getPoll (instanceName, accessToken, pollId) { + let url = `${basename(instanceName)}/api/v1/polls/${pollId}` + return get(url, auth(accessToken), { timeout: DEFAULT_TIMEOUT }) +} diff --git a/src/routes/_components/status/StatusPoll.html b/src/routes/_components/status/StatusPoll.html index 16f067b..67f7dea 100644 --- a/src/routes/_components/status/StatusPoll.html +++ b/src/routes/_components/status/StatusPoll.html @@ -1,19 +1,55 @@ -
+
    {#each options as option}
  • -
    {option.title} ({option.share}%)
    +
    + {option.share}% {option.title} +
  • {/each}
+
+
+ + {votesCount} {votesCount === 1 ? 'vote' : 'votes'} +
+
+ + + {expiryText} + + +
+ +
diff --git a/src/routes/_intl/formatTimeagoDate.js b/src/routes/_intl/formatTimeagoDate.js index 0756e4b..d0ef1df 100644 --- a/src/routes/_intl/formatTimeagoDate.js +++ b/src/routes/_intl/formatTimeagoDate.js @@ -1,6 +1,7 @@ import { format } from '../_thirdparty/timeago/timeago' import { mark, stop } from '../_utils/marks' +// Format a date in the past export function formatTimeagoDate (date, now) { mark('formatTimeagoDate') // use Math.max() to avoid things like "in 10 seconds" when the timestamps are slightly off @@ -8,3 +9,12 @@ export function formatTimeagoDate (date, now) { stop('formatTimeagoDate') return res } + +// Format a date in the future +export function formatTimeagoFutureDate (date, now) { + mark('formatTimeagoFutureDate') + // use Math.min() for same reason as above + let res = format(date, Math.min(now, date)) + stop('formatTimeagoFutureDate') + return res +}