From 201d33099ca73d2e6bb94df76756fe5fa83228ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=92scar=20Casajuana?= Date: Fri, 12 Apr 2024 14:04:56 +0200 Subject: [PATCH] Add latest polls leaderboard Changed some go json marshaling keys to be more coherent with the other calls --- rankings.go | 8 ++++---- webapp/src/components/MutedUsersList.tsx | 3 ++- webapp/src/components/Top.tsx | 26 +++++++++++++++++++++--- webapp/src/pages/Leaderboards.tsx | 13 +++++++----- webapp/src/queries/tops.ts | 14 +++++++++++-- 5 files changed, 49 insertions(+), 15 deletions(-) diff --git a/rankings.go b/rankings.go index c69a410d..67678734 100644 --- a/rankings.go +++ b/rankings.go @@ -66,10 +66,10 @@ func (v *vocdoniHandler) lastElectionsHandler(_ *apirest.APIdata, ctx *httproute CreatedTime time.Time `json:"createdTime"` ElectionID string `json:"electionId"` LastVoteTime time.Time `json:"lastVoteTime"` - Question string `json:"question"` - CastedVotes uint64 `json:"castedVotes"` - Username string `json:"username"` - Displayname string `json:"displayname"` + Question string `json:"title"` + CastedVotes uint64 `json:"voteCount"` + Username string `json:"createdByUsername"` + Displayname string `json:"createdByDisplayname"` } var elections []*Election diff --git a/webapp/src/components/MutedUsersList.tsx b/webapp/src/components/MutedUsersList.tsx index da2d2449..a7505f2f 100644 --- a/webapp/src/components/MutedUsersList.tsx +++ b/webapp/src/components/MutedUsersList.tsx @@ -19,6 +19,7 @@ import { useQuery } from 'react-query' import { fetchMutedUsers } from '../queries/profile' import { appUrl } from '../util/constants' import { useAuth } from './Auth/useAuth' +import { Profile } from './Auth/useAuthProvider' import { Check } from './Check' export const MutedUsersList: React.FC = (props: StackProps) => { @@ -74,7 +75,7 @@ export const MutedUsersList: React.FC = (props: StackProps) => { {data ? ( data?.map((user) => ( { return } +export const LatestPolls = (props: BoxProps) => { + const { bfetch } = useAuth() + const { data, error, isLoading } = useQuery('latestPolls', fetchLatestPolls(bfetch)) + + if (isLoading || error) { + return + } + + if (!data || !data.length) return null + + return +} + export const TopCreators = (props: BoxProps) => { const { bfetch } = useAuth() const { data, error, isLoading } = useQuery('topCreators', fetchTopCreators(bfetch)) @@ -64,8 +84,8 @@ export const TopPolls = ({ polls, title, ...rest }: { polls: Poll[]; title: stri style={{ textDecoration: 'none' }} > - - {poll.title} — by {poll.createdByUsername} + + {poll.title} — by {poll.createdByDisplayname || poll.createdByUsername} {poll.voteCount} votes diff --git a/webapp/src/pages/Leaderboards.tsx b/webapp/src/pages/Leaderboards.tsx index f15a2043..88da5681 100644 --- a/webapp/src/pages/Leaderboards.tsx +++ b/webapp/src/pages/Leaderboards.tsx @@ -1,15 +1,15 @@ import { Grid, GridItem } from '@chakra-ui/react' -import { TopCreators, TopTenPolls, TopVoters } from '../components/Top' +import { LatestPolls, TopCreators, TopTenPolls, TopVoters } from '../components/Top' export const Leaderboards = () => ( @@ -21,5 +21,8 @@ export const Leaderboards = () => ( + + + ) diff --git a/webapp/src/queries/tops.ts b/webapp/src/queries/tops.ts index 2b390590..aa2b9f59 100644 --- a/webapp/src/queries/tops.ts +++ b/webapp/src/queries/tops.ts @@ -1,7 +1,11 @@ export type Poll = { + electionId: string title: string createdByUsername: string + createdByDisplayname: string voteCount: number + createdTime: Date + lastVoteTime: Date } export const fetchPollsByVotes = (bfetch) => async (): Promise => { @@ -16,14 +20,20 @@ export type UserRanking = { count: number } -export const fetchTopVoters = (bfetch) => async (): Promise => { +export const fetchTopVoters = (bfetch) => async (): Promise => { const response = await bfetch(`${import.meta.env.APP_URL}/rankings/usersByCastedVotes`) const { users } = (await response.json()) as { users: UserRanking[] } return users } -export const fetchTopCreators = (bfetch) => async (): Promise => { +export const fetchTopCreators = (bfetch) => async (): Promise => { const response = await bfetch(`${import.meta.env.APP_URL}/rankings/usersByCreatedPolls`) const { users } = (await response.json()) as { users: UserRanking[] } return users } + +export const fetchLatestPolls = (bfetch) => async (): Promise => { + const response = await bfetch(`${import.meta.env.APP_URL}/rankings/lastElections`) + const { polls } = (await response.json()) as { polls: Poll[] } + return polls +}