Skip to content

Commit fc2824b

Browse files
committed
feat: Add async/await to generateStats function for improved loading experience
The code changes include adding the `async` keyword and `await` keyword to the `generateStats` function in the `GenerateStats` component. This allows for asynchronous fetching of data from the API endpoint, improving the loading experience for the user. Additionally, error handling has been added to display appropriate toast notifications in case of errors. Recent user commits: - Added skeleton loading component - Update Navbar component with responsive styling and search functionality - Add react-toastify dependency and implement toast notifications - Add mongoose dependency and create API endpoints for fetching and adding user data - Add PromotionCard component to Home page + api working Recent repository commits: - Update Card component with additional styling for responsiveness - Update Navbar component with responsive styling and search functionality - Add lazy loading for images and update grid layout in home page - card component created
1 parent de1f948 commit fc2824b

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

client/src/app/components/GenerateStats.tsx

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const GenerateStats = ({ showStats, setShowStats }: any) => {
1313
const [data, setData] = useState<any>(defaultData);
1414
const [loading, setLoading] = useState(false);
1515

16-
const generateStats = (e: React.FormEvent) => {
16+
const generateStats = async (e: React.FormEvent) => {
1717
setLoading(true);
1818
e.preventDefault();
1919

@@ -22,15 +22,21 @@ const GenerateStats = ({ showStats, setShowStats }: any) => {
2222
}
2323

2424
try {
25-
fetch("/api/" + username)
26-
.then((res) => res.json())
27-
.then((data) => {
28-
setData(data);
29-
toast("🫡Stats generated successfully");
30-
setLoading(false);
31-
});
25+
const res = await fetch("/api/" + username);
26+
if (!res.ok) {
27+
toast("👻 User not found");
28+
setLoading(false);
29+
return;
30+
}
31+
32+
const data = await res.json();
33+
setData(data);
34+
toast("🫡 Stats generated successfully");
3235
} catch (error) {
3336
console.error("An error occurred. Please try again later");
37+
toast("😞 An error occurred. Please try again later");
38+
} finally {
39+
setLoading(false);
3440
}
3541
};
3642

client/src/app/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export default function Home() {
3232

3333
useEffect(() => {
3434
fetchData();
35-
}, [datas.length]);
35+
}, [datas && datas.length]);
3636

3737
const searchedData = datas?.filter((data: any) =>
3838
data.profileData.fullName.toLowerCase().includes(search.toLowerCase())

0 commit comments

Comments
 (0)