diff --git a/src/api/getChallengeLeaderboard.js b/src/api/getChallengeLeaderboard.js new file mode 100644 index 0000000..7c4cdb2 --- /dev/null +++ b/src/api/getChallengeLeaderboard.js @@ -0,0 +1,13 @@ +import {API} from '../utils/globals'; + +const getChallengeLeaderboard = (setDataState, setLoading, challengeName) => { + fetch(`${API}/leaderboard/${challengeName}`) + .then(response => response.json()) + .then(data => { + setDataState(data); + if (setLoading) + setLoading(false); + }); +}; + +export default getChallengeLeaderboard; \ No newline at end of file diff --git a/src/api/getChallengeSubmissions.js b/src/api/getChallengeSubmissions.js deleted file mode 100644 index 8f682cf..0000000 --- a/src/api/getChallengeSubmissions.js +++ /dev/null @@ -1,13 +0,0 @@ -import {API} from "../utils/globals"; - -const getChallengeSubmissions = (setDataState, setLoading, challengeName) => { - fetch(`${API}/challenge-all-submissions/${challengeName}`) - .then(response => response.json()) - .then(data => { - setDataState(data); - if (setLoading) - setLoading(false); - }); -} - -export default getChallengeSubmissions; \ No newline at end of file diff --git a/src/components/elements/Pager.js b/src/components/elements/Pager.js index 19c14a5..f80e169 100644 --- a/src/components/elements/Pager.js +++ b/src/components/elements/Pager.js @@ -57,7 +57,7 @@ const Pager = (props) => { backgroundColor={(props.pageNr === 1) ? 'transparent' : theme.colors.dark}/> ); diff --git a/src/components/elements/Table/Table.js b/src/components/elements/Table/Table.js index cab0ea4..1c99a2f 100644 --- a/src/components/elements/Table/Table.js +++ b/src/components/elements/Table/Table.js @@ -1,6 +1,6 @@ import React from 'react'; import {FlexColumn, FlexRow, Grid} from '../../../utils/containers'; -import getChallengeSubmissions from '../../../api/getChallengeSubmissions'; +import getChallengeLeaderboard from '../../../api/getChallengeLeaderboard'; import {H3, Medium} from '../../../utils/fonts'; import _renderSubmissions from './_renderSubmissions'; import Pager from '../Pager'; @@ -12,7 +12,7 @@ import PropsTypes from 'prop-types'; const Table = (props) => { const headerElements = ['#', 'submitter', 'when', 'result', 'entries']; - const [challengeData, setChallengeData] = React.useState({}); + const [leaderboardData, setLeaderboardData] = React.useState({}); const [pageNr, setPageNr] = React.useState(1); const [loading, setLoading] = React.useState(true); @@ -21,16 +21,16 @@ const Table = (props) => { }); const challengeDataRequest = () => { - getChallengeSubmissions(setChallengeData, setLoading, props.challengeName); + getChallengeLeaderboard(setLeaderboardData, setLoading, props.challengeName); }; const renderSubmissions = () => { - return _renderSubmissions(pageNr, challengeData.submissions - ? challengeData.submissions : []); + return _renderSubmissions(pageNr, leaderboardData.entries + ? leaderboardData.entries : []); }; const nextPage = () => { - if (pageNr !== CALC_PAGES(challengeData.submissions ? challengeData.submissions : [])) { + if (pageNr !== CALC_PAGES(leaderboardData.entries ? leaderboardData.entries : [])) { let newPage = pageNr + 1; setPageNr(newPage); } @@ -86,13 +86,13 @@ const Table = (props) => { <> - {mobileRender()} + {!loading ? mobileRender() : ''} - {desktopRender()} + {!loading ? desktopRender() : ''} + pages={CALC_PAGES(leaderboardData.entries ? leaderboardData.entries : [])}/> ); }; diff --git a/src/components/elements/Table/_renderSubmissions.js b/src/components/elements/Table/_renderSubmissions.js index 7c350a6..77eae0d 100644 --- a/src/components/elements/Table/_renderSubmissions.js +++ b/src/components/elements/Table/_renderSubmissions.js @@ -17,7 +17,7 @@ const _renderSubmissions = (pageNr, submissions) => { if (submissions) { return ( - {submissions.slice(n, n + ELEMENTS_PER_PAGE).map(({submitter, when, evaluations, version}, index) => { + {submissions.slice(n, n + ELEMENTS_PER_PAGE).map(({submitter, when, evaluations, times}, index) => { return ( @@ -35,7 +35,7 @@ const _renderSubmissions = (pageNr, submissions) => { {evaluations[0] ? evaluations[0].score : 'xxx'} - {version ? version.length : 1} + {times ? times : 1} diff --git a/src/utils/globals.js b/src/utils/globals.js index c66119a..b0ea878 100644 --- a/src/utils/globals.js +++ b/src/utils/globals.js @@ -46,6 +46,7 @@ const RENDER_ICO = (type) => { }; const CALC_PAGES = (objects) => { + if (objects.length === 0) return 1; return Math.ceil(objects.length / ELEMENTS_PER_PAGE); };