-
Notifications
You must be signed in to change notification settings - Fork 227
/
Copy pathContributors.jsx
90 lines (83 loc) · 2.53 KB
/
Contributors.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import React, { useEffect, useState } from 'react';
import axios from 'axios';
import './Contributors.css';
import Preloader from '../components/Preloader';
function Contributors() {
const [contributors, setContributors] = useState([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null); // Added error state
useEffect(() => {
async function fetchContributors() {
let allContributors = [];
let page = 1;
try {
while (true) {
const response = await axios.get(
`https://api.github.com/repos/Trisha-tech/OnlineBookSales/contributors`,
{
params: {
per_page: 100,
page,
},
}
);
const data = response.data;
if (data.length === 0) {
break;
}
allContributors = [...allContributors, ...data];
page++;
}
setContributors(allContributors);
} catch (error) {
console.error('Error fetching contributors:', error.message);
setError('Failed to load contributors. Please try again later.'); // Set error message
} finally {
setLoading(false);
}
}
fetchContributors();
}, []);
if (loading) {
return <Preloader />; // Show preloader while loading
}
if (error) {
return (
<div className="error-message">
<p>{error}</p>
</div>
); // Show error message if there's an error
}
return (
<div className="contributors-container">
<h1 className="contributors-title">Our Contributors</h1>
<div className="contributors-grid">
{contributors.length > 0 ? (
contributors.map((contributor) => (
<div key={contributor.id} className="contributor-card">
<a
href={contributor.html_url}
className="contributor-link"
target="_blank"
rel="noopener noreferrer"
>
<img
src={contributor.avatar_url}
alt={contributor.login}
className="contributor-avatar"
/>
</a>
<h2 className="contributor-name">{contributor.login}</h2>
<p className="contributor-contributions">
Contributions: {contributor.contributions}
</p>
</div>
))
) : (
<p>No contributors found.</p>
)}
</div>
</div>
);
}
export default Contributors;