From 4ed7601437c80f08c31ea39b0c1c11b1be3c2d4a Mon Sep 17 00:00:00 2001 From: Adam Goth Date: Mon, 1 Nov 2021 12:46:12 +0100 Subject: [PATCH 1/2] Replace results badge with text design --- pages/polling/[poll-hash].tsx | 44 ++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/pages/polling/[poll-hash].tsx b/pages/polling/[poll-hash].tsx index 3b708f798..d90bffa26 100644 --- a/pages/polling/[poll-hash].tsx +++ b/pages/polling/[poll-hash].tsx @@ -48,10 +48,10 @@ import VoteBreakdown from 'modules/polling/components/VoteBreakdown'; import VoteBox from 'modules/polling/components/VoteBox'; import SystemStatsSidebar from 'modules/app/components/SystemStatsSidebar'; import ResourceBox from 'modules/app/components/ResourceBox'; -import PollOptionBadge from 'modules/polling/components/PollOptionBadge'; import MobileVoteSheet from 'modules/polling/components/MobileVoteSheet'; import VotesByAddress from 'modules/polling/components/VotesByAddress'; import { PollCategoryTag } from 'modules/polling/components/PollCategoryTag'; +import { getVoteColor } from 'modules/polling/helpers/getVoteColor'; function prefetchTally(poll) { if (typeof window !== 'undefined' && poll) { @@ -65,6 +65,30 @@ const editMarkdown = content => { return content.replace(/^

.*<\/h1>|^

.*<\/h2>/, ''); }; +const WinningOptionText = ({ + tally, + voteType +}: { + tally: PollTally; + voteType: string; +}): JSX.Element | null => { + if (!tally.winner) return null; + return ( + <> + + + + Winning Option:{' '} + + {tally.winningOptionName} + + + + + + ); +}; + const PollView = ({ poll, polls: prefetchedPolls }: { poll: Poll; polls: Poll[] }) => { const network = getNetwork(); const account = useAccountsStore(state => state.currentAccount); @@ -214,19 +238,6 @@ const PollView = ({ poll, polls: prefetchedPolls }: { poll: Poll; polls: Poll[] )} - - {hasPollEnded ? ( - - ) : null} @@ -327,6 +338,11 @@ const PollView = ({ poll, polls: prefetchedPolls }: { poll: Poll; polls: Poll[] ] ) ]} + banner={ + hasPollEnded && tally ? ( + + ) : undefined + } /> From b83f4026d909487c471f29e1912de20311eb20aa Mon Sep 17 00:00:00 2001 From: Rafael Ventura Date: Mon, 1 Nov 2021 14:17:49 +0100 Subject: [PATCH 2/2] Fix/685 (#212): Changes sort order of polls * Introduce correct sorting for polls * Lint --- .../api/__tests__/parsePollMetadata.spec.ts | 85 +++++++++++++++++++ modules/polling/api/parsePollMetadata.ts | 20 +++-- 2 files changed, 98 insertions(+), 7 deletions(-) create mode 100644 modules/polling/api/__tests__/parsePollMetadata.spec.ts diff --git a/modules/polling/api/__tests__/parsePollMetadata.spec.ts b/modules/polling/api/__tests__/parsePollMetadata.spec.ts new file mode 100644 index 000000000..04f03d9ab --- /dev/null +++ b/modules/polling/api/__tests__/parsePollMetadata.spec.ts @@ -0,0 +1,85 @@ +import { Poll } from '../../types'; +import { sortPolls } from '../parsePollMetadata'; + +describe('Parse poll metadata', () => { + const polls: Poll[] = [ + { + categories: ['a'], + content: '', + discussionLink: '', + endDate: new Date(2011, 10, 30), + multiHash: '', + options: {}, + pollId: 1, + startDate: new Date(2011, 10, 30), + slug: '', + summary: '', + title: '2011,10,30', + voteType: 'Plurality Voting', + ctx: {} as any + }, + { + categories: ['a'], + content: '', + discussionLink: '', + endDate: new Date(2011, 10, 30), + multiHash: '', + options: {}, + pollId: 2, + startDate: new Date(2011, 10, 31), + slug: '', + summary: '', + title: '2011,10,31', + voteType: 'Plurality Voting', + ctx: {} as any + }, + { + categories: ['a'], + content: '', + discussionLink: '', + endDate: new Date(2021, 10, 31), + multiHash: '', + options: {}, + pollId: 3, + startDate: new Date(2021, 10, 31), + slug: '', + summary: '', + title: '2021,10,31', + voteType: 'Plurality Voting', + ctx: {} as any + }, + { + categories: ['a'], + content: '', + discussionLink: '', + endDate: new Date(2021, 11, 31), + multiHash: '', + options: {}, + pollId: 4, + startDate: new Date(2021, 11, 31), + slug: '', + summary: '', + title: '2021,11,31', + voteType: 'Plurality Voting', + ctx: {} as any + } + ]; + + test('The first poll is the one ending sooner', () => { + const results = sortPolls(polls); + + expect(results[0].pollId).toEqual(2); + }); + + test('The second poll is the one with the same date as 1 but different start date', () => { + const results = sortPolls(polls); + + expect(results[1].pollId).toEqual(1); + }); + + test('The 4 poll is the one ending later', () => { + const results = sortPolls(polls); + + expect(results[3].pollId).toEqual(4); + }); +}); diff --git a/modules/polling/api/parsePollMetadata.ts b/modules/polling/api/parsePollMetadata.ts index 82bb02e5b..aa57487bd 100644 --- a/modules/polling/api/parsePollMetadata.ts +++ b/modules/polling/api/parsePollMetadata.ts @@ -5,6 +5,18 @@ import { backoffRetry, timeoutPromise } from 'lib/utils'; import matter from 'gray-matter'; import { parsePollMetadata } from '../helpers/parser'; +export function sortPolls(pollList: Poll[]): Poll[] { + return pollList.sort((a, b) => { + // closest to expiration shown first, if some have same expiration date, sort by startdate + const dateEndDiff = a.endDate.getTime() - b.endDate.getTime(); + + // Sort by more recent first if they end at the same time + const sortedByStartDate = a.startDate.getTime() < b.startDate.getTime() ? 1 : -1; + + return dateEndDiff < 0 ? -1 : dateEndDiff > 0 ? 1 : sortedByStartDate; + }); +} + export async function parsePollsMetadata(pollList): Promise { let numFailedFetches = 0; const failedPollIds: number[] = []; @@ -48,11 +60,5 @@ export async function parsePollsMetadata(pollList): Promise { IDs: ${failedPollIds}` ); - return ( - polls - // closest to expiration shown first - .sort((a, b) => - Date.now() - new Date(a.endDate).getTime() < Date.now() - new Date(b.endDate).getTime() ? -1 : 1 - ) - ); + return sortPolls(polls); }