Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aggregate homepage stats from APIs #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ REACT_APP_AUTH_API_BASE_URL=https://next.akatsuki.gg
REACT_APP_SEARCH_API_BASE_URL=https://next.akatsuki.gg/api
REACT_APP_LEADERBOARD_API_BASE_URL=https://next.akatsuki.gg/api
REACT_APP_USER_API_BASE_URL=https://next.akatsuki.gg/api
REACT_APP_AGGREGATE_USER_STATS_API_BASE_URL=https://next.akatsuki.gg/api
tsunyoku marked this conversation as resolved.
Show resolved Hide resolved
REACT_APP_SCORES_API_BASE_URL=https://next.akatsuki.gg/api
REACT_APP_AGGREGATE_SCORE_STATS_API_BASE_URL=https://next.akatsuki.gg/api
tsunyoku marked this conversation as resolved.
Show resolved Hide resolved
REACT_APP_PROFILE_HISTORY_API_BASE_URL=https://next.akatsuki.gg/api
REACT_APP_USER_RELATIONSHIPS_API_BASE_URL=https://next.akatsuki.gg/api
REACT_APP_BANCHO_API_BASE_URL=https://c.akatsuki.gg/api
Expand Down
17 changes: 17 additions & 0 deletions src/adapters/akatsuki-api/aggregate-score-stats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import axios from "axios"

const aggregateScoreStatsApiInstance = axios.create({
baseURL: process.env.REACT_APP_AGGREGATE_SCORE_STATS_API_BASE_URL,
})

export const fetchTotalScoresSet = async (): Promise<number> => {
try {
const response = await aggregateScoreStatsApiInstance.get(
"/v1/aggregate-score-stats/total-scores-set"
)
return response.data
} catch (e: any) {
console.log(e)
throw new Error(e.response.data.user_feedback)
}
}
29 changes: 29 additions & 0 deletions src/adapters/akatsuki-api/aggregate-user-stats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import axios from "axios"

const aggregateUserStatsApiInstance = axios.create({
baseURL: process.env.REACT_APP_AGGREGATE_USER_STATS_API_BASE_URL,
})

export const fetchTotalPPEarned = async (): Promise<number> => {
try {
const response = await aggregateUserStatsApiInstance.get(
"/v1/aggregate-user-stats/total-pp-earned"
)
return response.data
} catch (e: any) {
console.log(e)
throw new Error(e.response.data.user_feedback)
}
}

export const fetchTotalRegisteredUsers = async (): Promise<number> => {
try {
const response = await aggregateUserStatsApiInstance.get(
"/v1/aggregate-user-stats/total-registered-users"
)
return response.data
} catch (e: any) {
console.log(e)
throw new Error(e.response.data.user_feedback)
}
}
48 changes: 45 additions & 3 deletions src/pages/HomePage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { Box, Button, Container, Grid, Stack, Typography } from "@mui/material"
import { useEffect, useState } from "react"
import { Link } from "react-router-dom"

import { fetchTotalScoresSet } from "../adapters/akatsuki-api/aggregate-score-stats"
import {
fetchTotalPPEarned,
fetchTotalRegisteredUsers,
} from "../adapters/akatsuki-api/aggregate-user-stats"
import { HomepageStatDisplay } from "../components/home/HomepageStatDisplay"
import HomepageBanner from "../components/images/banners/homepage_banner.svg"
import { HomepagePPIcon } from "../components/images/icons/HomepagePPIcon"
Expand All @@ -10,9 +16,45 @@ import { WhiteoutAkatsukiLogo } from "../components/images/logos/WhiteoutAkatsuk

export const HomePage = () => {
// TODO: fetch these from a backend API
const totalPPEarned = 1_483_238
const totalScoresSet = 92_383_238
const totalUsersRegistered = 172_395
const [totalPPEarned, setTotalPPEarned] = useState(0)
const [totalScoresSet, setTotalScoresSet] = useState(0)
const [totalUsersRegistered, setTotalUsersRegistered] = useState(0)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might want to hide the uis in this null state

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using 0 as a default state rather than null or undefined smells weird to me...


useEffect(() => {
;(async () => {
try {
const totalPPEarned = await fetchTotalPPEarned()
setTotalPPEarned(totalPPEarned)
} catch (error) {
console.error("Failed to fetch total PP earned:", error)
return
}
})()
}, [totalPPEarned])

useEffect(() => {
;(async () => {
try {
const totalScoresSet = await fetchTotalScoresSet()
setTotalScoresSet(totalScoresSet)
} catch (error) {
console.error("Failed to fetch total scores set:", error)
return
}
})()
}, [totalScoresSet])

useEffect(() => {
;(async () => {
try {
const totalUsersRegistered = await fetchTotalRegisteredUsers()
setTotalUsersRegistered(totalUsersRegistered)
} catch (error) {
console.error("Failed to fetch total users registered:", error)
return
}
})()
}, [totalUsersRegistered])

return (
<Box>
Expand Down
2 changes: 2 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ declare namespace NodeJS {
readonly REACT_APP_SEARCH_API_BASE_URL: string
readonly REACT_APP_LEADERBOARD_API_BASE_URL: string
readonly REACT_APP_USER_API_BASE_URL: string
readonly REACT_APP_AGGREGATE_USER_STATS_API_BASE_URL: string
readonly REACT_APP_SCORES_API_BASE_URL: string
readonly REACT_APP_AGGREGATE_SCORE_STATS_API_BASE_URL: string
readonly REACT_APP_PROFILE_HISTORY_API_BASE_URL: string
readonly REACT_APP_BANCHO_API_BASE_URL: string
readonly REACT_APP_USER_RELATIONSHIPS_API_BASE_URL: string
Expand Down