From ca722bb155c822d967a0ae6e1b5d1c138f09c10a Mon Sep 17 00:00:00 2001 From: Copple <10214025+kiwicopple@users.noreply.github.com> Date: Fri, 20 Jan 2023 16:13:03 +0100 Subject: [PATCH] fix the max functions --- components/Issues.tsx | 2 +- components/PullRequests.tsx | 2 +- components/Stars.tsx | 6 ++++-- lib/helpers.ts | 10 ++++++---- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/components/Issues.tsx b/components/Issues.tsx index 2c54189..79f5ae8 100644 --- a/components/Issues.tsx +++ b/components/Issues.tsx @@ -100,7 +100,7 @@ FORMAT JSON`, if (!data) return
Loading...
const chartData = data.data - const max = parseInt(findMax(chartData, 'tally').toString()) + const max = findMax(chartData, "tally") return ( diff --git a/components/PullRequests.tsx b/components/PullRequests.tsx index 15d6825..439518b 100644 --- a/components/PullRequests.tsx +++ b/components/PullRequests.tsx @@ -100,7 +100,7 @@ FORMAT JSON`, if (!data) return
Loading...
const chartData = data.data - const max = parseInt(findMax(chartData, 'tally').toString()) + const max = findMax(chartData, "tally") return ( diff --git a/components/Stars.tsx b/components/Stars.tsx index d728d5a..ca4ecd3 100644 --- a/components/Stars.tsx +++ b/components/Stars.tsx @@ -40,10 +40,12 @@ FORMAT JSON`, // console.log("data", data) // console.log("error", error) - if (!data) return
Loading...
+ if (!data || !data.data) return
Loading...
const starHistory = data.data - const max = parseInt(starHistory[starHistory.length - 1]["stars"]) + const max = starHistory.length + ? parseInt(starHistory[starHistory.length - 1]["stars"]) + : 0 return (
diff --git a/lib/helpers.ts b/lib/helpers.ts index e13467d..58d1a3d 100644 --- a/lib/helpers.ts +++ b/lib/helpers.ts @@ -16,11 +16,13 @@ export const useUrlFilters = (): UrlFilters => { } // Find the max value from an array of objects (or just an array of numbers) -export function findMax(objects: any[], key?: string) { +export function findMax(objects: any[], key?: string): number { let max = 0 for (let i = 0; i < objects.length; i++) { - const val = key? objects[i][key]: objects[i] - if (val > max) max = val + const val: number = key ? objects[i][key] : objects[i] + const valInt = parseInt(val.toString()) + if (valInt > max) max = valInt } + console.log("max", max) return max -} \ No newline at end of file +}