diff --git a/src/App.css b/src/App.css index 73a237a..ffb4102 100644 --- a/src/App.css +++ b/src/App.css @@ -22,6 +22,59 @@ border-radius: 4px; } +.community-highlight, +.api-error { + margin-top: 2rem; + padding: 1.5rem 1rem; + border: 1px solid #ececec; + border-radius: 8px; + overflow-wrap: break-word; +} + +.community-highlight h2, +.api-error h2 { + font-size: 1.25rem; + margin: 0 0 0.75rem; +} + +.api-error p, +.community-highlight p { + margin: 0; +} + +.retry-button { + margin-top: 1rem; + padding: 0.65rem 1.25rem; + border: 2px solid #8e8dd3; + border-radius: 6px; + background: #8e8dd3; + color: #fff; + font-weight: 700; + font-size: 1rem; + cursor: pointer; +} + +.retry-button:hover { + opacity: 0.85; +} + +.retry-button:focus-visible { + outline: 3px solid currentColor; + outline-offset: 3px; +} + +@media (max-width: 600px) { + .community-highlight, + .api-error { + padding: 1.25rem 0.75rem; + } + + .retry-button { + width: 100%; + max-width: 280px; + } +} + /* Dark Mode Toggle */ .dark-toggle { padding: 3px; @@ -74,6 +127,11 @@ html[data-theme="dark"] .welcome p { color: #ddd; } +html[data-theme="dark"] .community-highlight, +html[data-theme="dark"] .api-error { + border-color: #222; +} + /* Error Page */ .error-container { display: flex; diff --git a/src/components/Welcome.jsx b/src/components/Welcome.jsx index 84f1f83..42de870 100644 --- a/src/components/Welcome.jsx +++ b/src/components/Welcome.jsx @@ -1,29 +1,92 @@ +import { useCallback, useEffect, useState } from "react"; +import Loading from "./Loading.jsx"; + +const COMMUNITY_API_URL = "https://jsonplaceholder.typicode.com/users/1"; + function Welcome() { + const [member, setMember] = useState(null); + const [isLoading, setIsLoading] = useState(true); + const [hasError, setHasError] = useState(false); + + const loadCommunityMember = useCallback(async () => { + setIsLoading(true); + setHasError(false); + setMember(null); + + try { + const response = await fetch(COMMUNITY_API_URL); + + if (!response.ok) { + throw new Error("Request failed"); + } + + const data = await response.json(); + setMember(data); + } catch (error) { + console.error("Community highlight request failed:", error); + setHasError(true); + } finally { + setIsLoading(false); + } + }, []); + + useEffect(() => { + loadCommunityMember(); + }, [loadCommunityMember]); + return ( -
-

Welcome to React Open Source Starter

- -

- This project is specially created to help new contributors make - their first open source contribution. -

- -

- You can create a new component, add a feature, fix a bug, improve - the UI, or make any other useful improvement — then submit a - Pull Request. -

- -

- Please check CONTRIBUTING.md for contribution - guidelines and instructions. -

- -

- Created and maintained by Vikram Sardiwal. -

-
-); +
+

Welcome to React Open Source Starter

+ +

+ This project is specially created to help new contributors make + their first open source contribution. +

+ +

+ You can create a new component, add a feature, fix a bug, improve + the UI, or make any other useful improvement — then submit a + Pull Request. +

+ +

+ Please check CONTRIBUTING.md for contribution + guidelines and instructions. +

+ +

+ Created and maintained by Vikram Sardiwal. +

+ + {isLoading && } + + {hasError && !isLoading && ( +
+

Unable to load the content

+

+ The request could not be completed. Please try again. +

+ +
+ )} + + {member && !isLoading && !hasError && ( +
+

Community highlight

+

+ Meet {member.name} + {member.company?.name ? ` from ${member.company.name}` : ""}. +

+
+ )} +
+ ); } -export default Welcome; \ No newline at end of file +export default Welcome;