Skip to content

Commit de1f948

Browse files
committed
feat: Added skeleton loading component
1 parent 8a62c3a commit de1f948

File tree

4 files changed

+53
-6
lines changed

4 files changed

+53
-6
lines changed

client/src/app/components/GenerateStats.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@ import React, { useContext, useState } from "react";
33
import Card from "./Card";
44
import Image from "next/image";
55
import { data as defaultData } from "./Card";
6-
import { toast, ToastContainer } from "react-toastify";
6+
import { toast } from "react-toastify";
77
import { DataContext } from "../context/DataContext";
8+
import Skeleton from "./Skeleton";
89

910
const GenerateStats = ({ showStats, setShowStats }: any) => {
1011
const { datas, setDatas } = useContext(DataContext);
1112
const [username, setUsername] = useState("");
1213
const [data, setData] = useState<any>(defaultData);
14+
const [loading, setLoading] = useState(false);
1315

1416
const generateStats = (e: React.FormEvent) => {
17+
setLoading(true);
1518
e.preventDefault();
1619

1720
if (!username) {
@@ -24,6 +27,7 @@ const GenerateStats = ({ showStats, setShowStats }: any) => {
2427
.then((data) => {
2528
setData(data);
2629
toast("🫡Stats generated successfully");
30+
setLoading(false);
2731
});
2832
} catch (error) {
2933
console.error("An error occurred. Please try again later");
@@ -68,7 +72,13 @@ const GenerateStats = ({ showStats, setShowStats }: any) => {
6872
onClick={(e) => e.stopPropagation()}
6973
className="px-4 flex items-center justify-center flex-col gap-4"
7074
>
71-
<Card userData={data} />
75+
{loading ? (
76+
<>
77+
<Skeleton />
78+
</>
79+
) : (
80+
<Card userData={data} />
81+
)}
7282

7383
<form
7484
onSubmit={generateStats}

client/src/app/components/PromotionCard.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const PromotionCard = () => {
4343
target="_blank"
4444
className="hover:scale-105 transition-all ease-in-out duration-300 font-sourcecodepro "
4545
>
46-
<div className="flex justify-betwen flex-col items-center rounded h-[280px] border-2 border-[#f7f7f7] ">
46+
<div className="flex justify-betwen max-w-[400px] flex-col items-center rounded h-[280px] border-2 border-[#f7f7f7] ">
4747
{/* Heading and Logo */}
4848
<div className="flex gap-1 xl:gap-4 items-center justify-between ">
4949
<div className="flex items-center text-white justify-center px-4 py-2 w-full gap-2 rounded-md">
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import React from "react";
2+
3+
const Skeleton = () => {
4+
return (
5+
<div
6+
role="status"
7+
className="max-w-[400px] lg:w-[400px] rounded min-h-[280px] p-4 border border-gray-200 shadow animate-pulse md:p-6 dark:border-gray-700"
8+
>
9+
<div className="flex items-center justify-center h-48 mb-4 bg-gray-300 rounded dark:bg-gray-700">
10+
<svg
11+
className="w-10 h-10 text-gray-200 dark:text-gray-600"
12+
aria-hidden="true"
13+
xmlns="http://www.w3.org/2000/svg"
14+
fill="currentColor"
15+
viewBox="0 0 16 20"
16+
>
17+
<path d="M14.066 0H7v5a2 2 0 0 1-2 2H0v11a1.97 1.97 0 0 0 1.934 2h12.132A1.97 1.97 0 0 0 16 18V2a1.97 1.97 0 0 0-1.934-2ZM10.5 6a1.5 1.5 0 1 1 0 2.999A1.5 1.5 0 0 1 10.5 6Zm2.221 10.515a1 1 0 0 1-.858.485h-8a1 1 0 0 1-.9-1.43L5.6 10.039a.978.978 0 0 1 .936-.57 1 1 0 0 1 .9.632l1.181 2.981.541-1a.945.945 0 0 1 .883-.522 1 1 0 0 1 .879.529l1.832 3.438a1 1 0 0 1-.031.988Z" />
18+
<path d="M5 5V.13a2.96 2.96 0 0 0-1.293.749L.879 3.707A2.98 2.98 0 0 0 .13 5H5Z" />
19+
</svg>
20+
</div>
21+
<div className="h-2.5 bg-gray-200 rounded-full dark:bg-gray-700 w-full min-w-[300px] mb-4"></div>
22+
23+
<div className="h-2 bg-gray-200 rounded-full dark:bg-gray-700"></div>
24+
</div>
25+
);
26+
};
27+
28+
export default Skeleton;

client/src/app/page.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import PromotionCard from "./components/PromotionCard";
1010
import { DataContext } from "./context/DataContext";
1111
import { ToastContainer } from "react-toastify";
1212
import "react-toastify/dist/ReactToastify.css";
13+
import Skeleton from "./components/Skeleton";
1314

1415
export default function Home() {
1516
const { datas, setDatas } = useContext(DataContext);
@@ -31,7 +32,7 @@ export default function Home() {
3132

3233
useEffect(() => {
3334
fetchData();
34-
}, [datas]);
35+
}, [datas.length]);
3536

3637
const searchedData = datas?.filter((data: any) =>
3738
data.profileData.fullName.toLowerCase().includes(search.toLowerCase())
@@ -57,7 +58,14 @@ export default function Home() {
5758

5859
<div className="mt-32 max-w-7xl mx-auto place-items-center grid grid-cols-1 md:grid-cols-2 gap-y-8 xl:grid-cols-3 font-sourcecodepro gap-x-4">
5960
<PromotionCard />
60-
{searchedData &&
61+
{loading ? (
62+
<>
63+
{Array.from({ length: 6 }, (_, i) => (
64+
<Skeleton key={i} />
65+
))}
66+
</>
67+
) : (
68+
searchedData &&
6169
searchedData
6270
.sort(
6371
(a: any, b: any) =>
@@ -66,7 +74,8 @@ export default function Home() {
6674
)
6775
.map((userData: any, index: number) => (
6876
<Card userData={userData} index={index} key={index} />
69-
))}
77+
))
78+
)}
7079
</div>
7180

7281
<Footer />

0 commit comments

Comments
 (0)