Skip to content

Commit 6dcaec4

Browse files
committed
fix: parse contributor struct
1 parent 391e2eb commit 6dcaec4

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

src/pages/contributors.rs

+24-22
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@ use leptos::{
55
use serde::{Deserialize, Serialize};
66

77
use crate::components::ContributorCard;
8+
use crate::{error, log};
89

9-
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
10+
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
1011
pub struct Contributor {
1112
login: String,
1213
avatar_url: String,
1314
html_url: String,
1415
bio: Option<String>,
1516
twitter_username: Option<String>,
1617
location: Option<String>,
17-
contributions: i32,
18+
contributions: Option<i32>,
1819
}
1920

2021
async fn fetch_contributors() -> Result<Vec<Contributor>> {
@@ -26,28 +27,30 @@ async fn fetch_contributors() -> Result<Vec<Contributor>> {
2627
.json::<Vec<Contributor>>()
2728
.await?;
2829

29-
let response = join_all(
30-
response
31-
.into_iter()
32-
.map(|contributor| fetch_contributor_info(contributor.login)),
33-
)
34-
.await;
35-
36-
let response = response
37-
.into_iter()
38-
.filter_map(std::result::Result::ok)
30+
let response = join_all(response.iter().map(|c| fetch_contributor_info(c)))
31+
.await
32+
.iter()
33+
.flat_map(|c| c.clone().ok())
3934
.collect::<Vec<Contributor>>();
4035

4136
Ok(response)
4237
}
4338

44-
async fn fetch_contributor_info(username: String) -> Result<Contributor> {
45-
let response = reqwasm::http::Request::get(&format!("https://api.github.com/users/{username}"))
46-
.send()
47-
.await?
48-
.json::<Contributor>()
49-
.await?;
50-
Ok(response)
39+
async fn fetch_contributor_info(contributor: &Contributor) -> Result<Contributor> {
40+
Ok(reqwasm::http::Request::get(&format!(
41+
"https://api.github.com/users/{}",
42+
contributor.login
43+
))
44+
.send()
45+
.await?
46+
.json::<Contributor>()
47+
.await
48+
.inspect(|c| log!("{c:?} - {contributor:?}"))
49+
.inspect_err(|e| error!("Error: {e:?}"))
50+
.map(|c| Contributor {
51+
contributions: contributor.contributions,
52+
..c
53+
})?)
5154
}
5255

5356
#[island]
@@ -62,14 +65,13 @@ pub fn Contributors() -> impl IntoView {
6265
brand_src=item.avatar_url.clone()
6366
twitter=item.twitter_username.clone()
6467
location=item.location.clone()
65-
contributions=item.contributions
68+
contributions=item.contributions.unwrap_or(1)
6669
/>
6770
}
6871
};
6972

7073
let contributors_view = move || {
71-
let mut contributors = contributors_results.get()?.ok()?;
72-
contributors.sort_by_key(|a| a.contributions);
74+
let contributors = contributors_results.get()?.ok()?;
7375
let result = contributors
7476
.iter()
7577
.map(contributorMapper)

0 commit comments

Comments
 (0)