From be314c58039dc5a7db12dfbc80670ea6780e2d1b Mon Sep 17 00:00:00 2001 From: jschwarz2030 <79289630+jschwarz2030@users.noreply.github.com> Date: Mon, 14 Oct 2024 15:46:02 -0500 Subject: [PATCH] Enable Challenge and Project leaderboards (#2461) --- src/App.js | 10 ++--- .../ChallengeOwnerLeaderboard.js | 5 --- src/components/ProjectDetail/ProjectDetail.js | 5 +-- src/services/Leaderboard/Leaderboard.js | 45 +++++++++---------- src/services/Server/APIRoutes.js | 6 ++- 5 files changed, 33 insertions(+), 38 deletions(-) diff --git a/src/App.js b/src/App.js index 06635d9a5..32305403a 100644 --- a/src/App.js +++ b/src/App.js @@ -6,9 +6,8 @@ import Profile from './pages/Profile/Profile' import Metrics from './pages/Metrics/Metrics' import Dashboard from './pages/Dashboard/Dashboard.js' import Leaderboard from './pages/Leaderboard/Leaderboard' -// Disable Pages till leaderboards support unique queries -// import ChallengeLeaderboard from './pages/Leaderboard/ChallengeLeaderboard' -// import ProjectLeaderboard from './pages/Leaderboard/ProjectLeaderboard' +import ChallengeLeaderboard from './pages/Leaderboard/ChallengeLeaderboard' +import ProjectLeaderboard from './pages/Leaderboard/ProjectLeaderboard' // import CountryLeaderboard from './pages/Leaderboard/CountryLeaderboard' import ChallengePane from './components/ChallengePane/ChallengePane' import ChallengeDetail from './components/ChallengeDetail/ChallengeDetail' @@ -137,10 +136,9 @@ export class App extends Component { - {/* Disable Pages till leaderboards support unique queries */} - {/* + - */} + {/* */} diff --git a/src/components/AdminPane/Manage/ChallengeOwnerLeaderboard/ChallengeOwnerLeaderboard.js b/src/components/AdminPane/Manage/ChallengeOwnerLeaderboard/ChallengeOwnerLeaderboard.js index c64930730..4931cc162 100644 --- a/src/components/AdminPane/Manage/ChallengeOwnerLeaderboard/ChallengeOwnerLeaderboard.js +++ b/src/components/AdminPane/Manage/ChallengeOwnerLeaderboard/ChallengeOwnerLeaderboard.js @@ -14,15 +14,10 @@ export default class ChallengeOwnerLeaderboard extends Component { render() { const userType = this.props.userType - if(userType === "mapper") return ( -
- ) - if (!this.props.leaderboard) { return null } - const showNumberTasks = this.props.leaderboard.length > 0 ? this.props.leaderboard[0].completedTasks > 0 : false diff --git a/src/components/ProjectDetail/ProjectDetail.js b/src/components/ProjectDetail/ProjectDetail.js index 4644e05e9..f426c4751 100644 --- a/src/components/ProjectDetail/ProjectDetail.js +++ b/src/components/ProjectDetail/ProjectDetail.js @@ -141,8 +141,7 @@ export class ProjectDetail extends Component { - {/* Disable Link tell project leaderboard page is reimplemented */} - {/* {_get(this.props, 'challenges.length', 0) > 0 && + {_get(this.props, 'challenges.length', 0) > 0 &&
  • - } */} + }
    diff --git a/src/services/Leaderboard/Leaderboard.js b/src/services/Leaderboard/Leaderboard.js index 0a5fcbaa5..b302098dc 100644 --- a/src/services/Leaderboard/Leaderboard.js +++ b/src/services/Leaderboard/Leaderboard.js @@ -3,7 +3,6 @@ import _isArray from 'lodash/isArray' import Endpoint from '../Server/Endpoint' import { startOfMonth, endOfDay } from 'date-fns' import { CHALLENGE_INCLUDE_LOCAL } from '../Challenge/Challenge' -import { setupCustomCache } from '../../utils/setupCustomCache' import { addError } from '../Error/Error' import AppErrors from '../Error/AppErrors' @@ -27,8 +26,6 @@ const CACHE_TIME = 60 * 60 * 1000; const GLOBAL_LEADERBOARD_CACHE = "globalLeaderboard"; const USER_LEADERBOARD_CACHE = "userLeaderboard"; -const leaderboardCache = setupCustomCache(CACHE_TIME); - /** * Retrieve leaderboard data from the server for the given date range and * filters, returning a Promise that resolves to the leaderboard data. Note @@ -47,17 +44,17 @@ export const fetchLeaderboard = (numberMonths=null, onlyEnabled=true, initializeLeaderboardParams(params, numberMonths, forProjects, forChallenges, forUsers, forCountries, startDate, endDate) - const cachedLeaderboard = leaderboardCache.get({}, params, GLOBAL_LEADERBOARD_CACHE); - - if (cachedLeaderboard) { - return cachedLeaderboard; - } - try { - const results = await new Endpoint(api.users.leaderboard, { params }).execute() - - if (results) { - leaderboardCache.set({}, params, results, GLOBAL_LEADERBOARD_CACHE) + let results; + + if (forProjects && forProjects.length > 0) { + const projectId = forProjects[0]; + results = await new Endpoint(api.user.projectLeaderboard, { params: { ...params, projectId } }).execute() + } else if (forChallenges && forChallenges.length > 0) { + const challengeId = forChallenges[0]; + results = await new Endpoint(api.user.challengeLeaderboard, { params: { ...params, challengeId } }).execute() + } else { + results = await new Endpoint(api.users.leaderboard, { params }).execute() } return results @@ -95,17 +92,19 @@ export const fetchLeaderboardForUser = (userId, bracket=0, numberMonths=1, initializeLeaderboardParams(params, numberMonths, forProjects, forChallenges, null, forCountries, startDate, endDate) - const cachedLeaderboard = leaderboardCache.get(variables, params, USER_LEADERBOARD_CACHE); - - if (cachedLeaderboard) { - return cachedLeaderboard; - } - try { - const results = await new Endpoint(api.users.userLeaderboard, {variables, params}).execute() - - if (results) { - leaderboardCache.set(variables, params, results, USER_LEADERBOARD_CACHE) + let results; + + if (forProjects && forProjects.length > 0) { + //disabling project user ranks for now, as it's not supported by the backend + //const projectId = forProjects[0]; + //results = await new Endpoint(api.user.projectLeaderboardForUser, { params: { ...params, projectId }, variables: { userId } }).execute() + return [] + } else if (forChallenges && forChallenges.length > 0) { + const challengeId = forChallenges[0]; + results = await new Endpoint(api.user.challengeLeaderboardForUser, { params: { ...params, challengeId }, variables: { userId } }).execute() + } else { + results = await new Endpoint(api.users.userLeaderboard, {variables, params}).execute() } return results; diff --git a/src/services/Server/APIRoutes.js b/src/services/Server/APIRoutes.js index 7df257bfb..c4b69b550 100644 --- a/src/services/Server/APIRoutes.js +++ b/src/services/Server/APIRoutes.js @@ -191,7 +191,11 @@ const apiRoutes = (factory) => { notifications: factory.get("/user/:userId/notifications"), markNotificationsRead: factory.put("/user/:userId/notifications"), deleteNotifications: factory.put("/user/:userId/notifications/delete"), - announcements: factory.get("/user/announcements") + announcements: factory.get("/user/announcements"), + challengeLeaderboard: factory.get("/data/user/challengeLeaderboard"), + projectLeaderboard: factory.get("/data/user/projectLeaderboard"), + challengeLeaderboardForUser: factory.get("/data/user/:userId/challengeLeaderboard"), + // projectLeaderboardForUser: factory.get("/data/user/:userId/projectLeaderboard") }, teams: { find: factory.get("/teams/find"),