Skip to content

Commit 510e316

Browse files
Merge pull request #123 from mindfiredigital/dev
Update packages page
2 parents 1baa96f + 478f976 commit 510e316

File tree

1 file changed

+42
-10
lines changed

1 file changed

+42
-10
lines changed

src/app/packages/page.tsx

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type download = {
2323
const Stats = () => {
2424
const [startDate, setStartDate] = useState(moment().format("YYYY-MM-DD"));
2525
const [endDate, setEndDate] = useState(moment().format("YYYY-MM-DD"));
26-
26+
const [loading, setLoading] = useState(false); // State to track loading status
2727
const [count, setCount] = useState(0);
2828
const [selectedRange, setSelectedRange] = useState(false);
2929
const [isOpen, setIsOpen] = useState(false);
@@ -50,16 +50,19 @@ const Stats = () => {
5050

5151
// Function to fetch download statistics for a given package and period
5252
async function fetchDownloadStats(packageName: string, period: string) {
53+
setLoading(true);
5354
const url = `https://api.npmjs.org/downloads/range/${period}/@mindfiredigital/${packageName}`;
5455
const response = await fetch(url);
5556

5657
if (!response.ok) {
5758
console.log(
5859
`Failed to fetch download stats for ${packageName} (${period}): ${response.statusText}`
5960
);
61+
setLoading(false);
6062
}
6163

6264
const data = await response.json();
65+
setLoading(false);
6366
return data;
6467
}
6568

@@ -103,13 +106,16 @@ const Stats = () => {
103106
const range: { start: string; end: string } = getDateRange(
104107
event.target.value as string
105108
);
109+
106110
getStats(_package.name, `${range?.start}:${range?.end}`).then((res) => {
111+
setLoading(true);
107112
const count = calculateDownloads(res);
108113
// console.log(count);
109114

110115
packages.map((npmPackage) => {
111116
if (npmPackage.name === _package.name) {
112117
setCount(count);
118+
setLoading(false);
113119
}
114120
return npmPackage;
115121
});
@@ -221,11 +227,11 @@ const Stats = () => {
221227
<section className='bg-slate-50'>
222228
<div className='container mx-auto flex flex-col gap-4 items-center'>
223229
<h1 className='text-4xl leading-10 md:text-5xl md:!leading-[3.5rem] tracking-wide text-mindfire-text-black mt-10'>
224-
Download Statistics
230+
Our Packages
225231
</h1>
226232
<p className='mt-6 text-xl text-mf-light-grey tracking-wide mb-10 text-center'>
227-
We are a dynamic group of individuals who are passionate about what we
228-
do.
233+
Elevate your projects with Mindfire&apos;s game-changing open-source
234+
packages.
229235
</p>
230236
<div className='flex flex-col gap-4 flex-wrap lg:flex-row'>
231237
{packages.map((package_list) => (
@@ -468,9 +474,22 @@ const Stats = () => {
468474
quality={75}
469475
/>
470476
<div>
471-
<h6 className='text-mindfire-text-black font-semibold text-xl'>
472-
{count}
473-
</h6>
477+
{loading ? (
478+
// Render loading indicator while count is being fetched
479+
<div className='flex justify-center items-center w-5 h-5 border border-t-4 border-gray-700 rounded-full animate-spin'>
480+
<svg
481+
className='animate-spin h-5 w-5 mr-3 ...'
482+
viewBox='0 0 24 24'
483+
>
484+
{" "}
485+
</svg>
486+
</div>
487+
) : (
488+
// Render count when it is available
489+
<h6 className='text-mindfire-text-black font-semibold text-xl'>
490+
{count}
491+
</h6>
492+
)}
474493
</div>
475494
</div>
476495
<div className='mt-2 ml-2'>
@@ -496,9 +515,22 @@ const Stats = () => {
496515
quality={75}
497516
/>
498517
<div>
499-
<h6 className='text-mindfire-text-black font-semibold text-xl'>
500-
{count}
501-
</h6>
518+
{loading ? (
519+
// Render loading indicator while count is being fetched
520+
<div className='flex justify-center items-center w-5 h-5 border border-t-4 border-gray-700 rounded-full animate-spin'>
521+
<svg
522+
className='animate-spin h-5 w-5 mr-3 ...'
523+
viewBox='0 0 24 24'
524+
>
525+
{" "}
526+
</svg>
527+
</div>
528+
) : (
529+
// Render count when it is available
530+
<h6 className='text-mindfire-text-black font-semibold text-xl'>
531+
{count}
532+
</h6>
533+
)}
502534
</div>
503535
</div>
504536
<div className='mt-2 ml-2'>

0 commit comments

Comments
 (0)