diff --git a/.env.example b/.env.example index d3769a6a..05f4c897 100644 --- a/.env.example +++ b/.env.example @@ -29,6 +29,10 @@ DJANGO_SUPERUSER_PASSWORD=admin # for running with docker, else put these variables inside .env in frontend folder +# KPI API url for Stargazer count and contributors +STARGAZERS_COUNT=https://api.github.com/repos/djangoindia/djangoindia.org +CONTRIBUTORS=https://api.github.com/repos/djangoindia/djangoindia.org/contributors + # Server-side URL (not prefixed with NEXT_PUBLIC_) API_URL=http://djangoindia-backend:8000/api/v1 # without docker: API_URL=http://localhost:8000/api/v1 diff --git a/frontend/src/components/DataCard/DataCard.tsx b/frontend/src/components/DataCard/DataCard.tsx index bfd22d86..cb611df2 100644 --- a/frontend/src/components/DataCard/DataCard.tsx +++ b/frontend/src/components/DataCard/DataCard.tsx @@ -1,8 +1,8 @@ 'use client'; import React, { useEffect, useState } from 'react'; - import { FaCity, FaGithub, FaUsers } from 'react-icons/fa'; import { useInView } from 'react-intersection-observer'; +import { url, requestOptions } from './apiDataCard'; // Define props type for StatCard component interface StatCardProps { @@ -14,27 +14,53 @@ interface StatCardProps { } export const DataCard = () => { + const [dataCount, setDataCount] = useState({stargazers_count: Number, subscribers_count: Number, contributors_count: Number}); + const [loading, setLoading] = useState(true); + useEffect(() => { + //Set up use effect so API runs on component load and stops once loading is set to false. + async function fetchDataCount(url1: string, url2: string, options: object): Promise { + try { + if (url1 && url2) { + let res = await fetch(url1, options); + let res2 = await fetch(url2, options); + let data = await res.json(); + let data2 = await res2.json(); + setDataCount({stargazers_count: Number(data.stargazers_count), subscribers_count: 250, contributors_count: Number(data2.length)}); + setLoading(false); + } + + } + catch(err){ + console.log('Error:', err) + } + } + if (loading){ + fetchDataCount(url.stargazers_count, url.contributors, requestOptions.options); + } + + },[]) + return (
} /> } /> } />
@@ -54,7 +80,7 @@ const StatCard: React.FC = ({ threshold: 0.4, }); const intervalTime = count === 31818 ? 1 : 100; - + useEffect(() => { let start = startCount; const interval = setInterval(() => { @@ -81,7 +107,7 @@ const StatCard: React.FC = ({
{count === 50 ? '' : ''} - {inView ? number : 0}+ + {inView ? number : 0} {title}
diff --git a/frontend/src/components/DataCard/apiDataCard.ts b/frontend/src/components/DataCard/apiDataCard.ts new file mode 100644 index 00000000..02741362 --- /dev/null +++ b/frontend/src/components/DataCard/apiDataCard.ts @@ -0,0 +1,8 @@ +//API URL and Headers exported for DataCard.tsx +export const requestOptions = { + options: {method: "GET", headers: {'X-GitHub-Api-Version': '2022-11-28'}}, +}; +export const url = { + stargazers_count: process.env.STARGAZERS_COUNT, + contributors: process.env.CONTRIBUTORS, +}; \ No newline at end of file