Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
113 changes: 88 additions & 25 deletions src/components/Welcome.jsx
Original file line number Diff line number Diff line change
@@ -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 (
<section className="welcome">
<h1>Welcome to React Open Source Starter</h1>

<p>
This project is specially created to help new contributors make
their first open source contribution.
</p>

<p>
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.
</p>

<p>
Please check <code>CONTRIBUTING.md</code> for contribution
guidelines and instructions.
</p>

<p>
<strong>Created and maintained by Vikram Sardiwal.</strong>
</p>
</section>
);
<section className="welcome">
<h1>Welcome to React Open Source Starter</h1>

<p>
This project is specially created to help new contributors make
their first open source contribution.
</p>

<p>
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.
</p>

<p>
Please check <code>CONTRIBUTING.md</code> for contribution
guidelines and instructions.
</p>

<p>
<strong>Created and maintained by Vikram Sardiwal.</strong>
</p>

{isLoading && <Loading />}

{hasError && !isLoading && (
<div className="api-error" role="alert">
<h2>Unable to load the content</h2>
<p>
The request could not be completed. Please try again.
</p>
<button
type="button"
className="retry-button"
onClick={loadCommunityMember}
>
Try Again
</button>
</div>
)}

{member && !isLoading && !hasError && (
<div className="community-highlight">
<h2>Community highlight</h2>
<p>
Meet <strong>{member.name}</strong>
{member.company?.name ? ` from ${member.company.name}` : ""}.
</p>
</div>
)}
</section>
);
}

export default Welcome;
export default Welcome;