Skip to content

Commit 28a1054

Browse files
authored
Merge pull request #31 from michaelcardoza/main
refactor: fetch api contributors
2 parents 067fbae + f1fc6f5 commit 28a1054

File tree

4 files changed

+53
-66
lines changed

4 files changed

+53
-66
lines changed

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ leptos_meta = { version = "0.5.0-beta", features = ["csr", "nightly"] }
99
leptos_router = { version = "0.5.0-beta", features = ["csr", "nightly"] }
1010
log = "0.4"
1111
gloo-net = { version = "0.2", features = ["http"] }
12+
reqwasm = "0.5"
13+
serde = { version = "1", features = ["derive"] }
1214

1315
# dependecies for client (enable when csr or hydrate set)
1416
wasm-bindgen = { version = "0.2" }

src/components/cards/contributor_card.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ use crate::components::icons::github_icon::GithubIcon;
44

55
#[component]
66
pub fn ContributorCard(
7-
#[prop(into)] name: &'static str,
8-
#[prop(into)] description: &'static str,
9-
#[prop(into)] link: &'static str,
10-
#[prop(into)] brand_src: &'static str,
7+
#[prop(into)] name: String,
8+
#[prop(into)] description: String,
9+
#[prop(into)] link: String,
10+
#[prop(into)] brand_src: String,
1111
) -> impl IntoView {
1212
view! {
1313
<article>
@@ -17,8 +17,8 @@ pub fn ContributorCard(
1717
class="group flex flex-col gap-y-6 border border-black p-6 hover:bg-orange-500 bg-orange-100 drop-shadow-[0_0_0_rgba(0,0,0)] hover:drop-shadow-[-4px_-4px_0_rgba(0,0,0)] transition justify-between h-full"
1818
>
1919
<div class="flex flex-col gap-y-2">
20-
<img src=brand_src width="60" class="rounded-full mb-4" alt=name/>
21-
<h2 class="font-work-sans text-black text-xl">{name}</h2>
20+
<img src=brand_src width="60" class="rounded-full mb-4" alt=name.clone()/>
21+
<h2 class="font-work-sans text-black text-xl">{name.clone()}</h2>
2222
<p class="font-work-sans text-black">{description}</p>
2323
</div>
2424
<span class="ml-auto">

src/components/header.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use leptos::*;
2+
use leptos_router::*;
23

34
use crate::components::button_link::ButtonLink;
45

@@ -10,13 +11,13 @@ pub fn Header() -> impl IntoView {
1011
<header class="border-b border-b-black/20">
1112
<div class="container mx-auto px-4 flex items-center justify-between flex-col lg:flex-row">
1213
<div class="flex justify-between w-full lg:w-auto">
13-
<a href="/" class="flex items-center gap-x-4">
14+
<A href="" exact=true class="flex items-center gap-x-4">
1415
<img
1516
src="https://www.rust-lang.org/static/images/rust-logo-blk.svg"
1617
class="max-h-20 rounded-full"
1718
alt="Rust Lang en Español"
1819
/>
19-
</a>
20+
</A>
2021
<button
2122
class="lg:hidden"
2223
on:click=move |_| { set_is_open.update(|n| *n = !*n) }
@@ -37,7 +38,7 @@ pub fn Header() -> impl IntoView {
3738
</a>
3839
</li>
3940
<li>
40-
<a href="/colaboradores">"Colaboradores"</a>
41+
<A href="/colaboradores">"Colaboradores"</A>
4142
</li>
4243
<li>
4344
<a href="#">"Blog"</a>

src/pages/contributors.rs

+41-57
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,50 @@
1-
use leptos::*;
1+
use leptos::{error::Result, *};
2+
use serde::{Deserialize, Serialize};
23

34
use crate::components::{cards::contributor_card::ContributorCard, footer::Footer, header::Header};
45

5-
struct ContributorItem {
6-
name: &'static str,
7-
description: &'static str,
8-
link: &'static str,
9-
brand_src: &'static str,
6+
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
7+
pub struct Contributor {
8+
login: String,
9+
avatar_url: String,
10+
html_url: String,
11+
}
12+
13+
async fn fetch_contributors() -> Result<Vec<Contributor>> {
14+
let response = reqwasm::http::Request::get(
15+
"https://api.github.com/repos/RustLangES/rustlanges.github.io/contributors",
16+
)
17+
.send()
18+
.await?
19+
.json::<Vec<Contributor>>()
20+
.await?
21+
.into_iter()
22+
.collect();
23+
Ok(response)
1024
}
1125

1226
#[component]
1327
pub fn Contributors() -> impl IntoView {
14-
let contributors: Vec<ContributorItem> = vec![
15-
ContributorItem {
16-
name: "Phosphorus Moscu",
17-
description: "Student in Computer Science degree at Universidad Nacional del Oeste | Developer Consultant at Globant",
18-
link: "https://github.com/Phosphorus-M",
19-
brand_src: "https://avatars.githubusercontent.com/u/19656993?v=4",
20-
},
21-
ContributorItem {
22-
name: "Sergio Alejandro Ribera Costa",
23-
description: "Enthusiastic developer Linux and Open Source lover",
24-
link: "https://github.com/SergioRibera",
25-
brand_src: "https://avatars.githubusercontent.com/u/56278796?v=4",
26-
},
27-
ContributorItem {
28-
name: "Michael Cardoza",
29-
description: "Software Developer",
30-
link: "https://github.com/michaelcardoza",
31-
brand_src: "https://avatars.githubusercontent.com/u/8800455?v=4",
32-
},
33-
ContributorItem {
34-
name: "Emilio Ruscitti",
35-
description: "Rust Dev",
36-
link: "https://github.com/Lemin-n",
37-
brand_src: "https://avatars.githubusercontent.com/u/88170949?v=4",
38-
},
39-
ContributorItem {
40-
name: "carbon based lifeform",
41-
description: "Rust Dev",
42-
link: "https://github.com/ph4un00b",
43-
brand_src: "https://avatars.githubusercontent.com/u/1057021?v=4",
44-
},
45-
ContributorItem {
46-
name: "CrawKatt",
47-
description: "Aprendiz de Rust, estudiante de AIEP, Chileno",
48-
link: "https://github.com/CrawKatt",
49-
brand_src: "https://avatars.githubusercontent.com/u/108593932?v=4",
50-
},
51-
];
28+
let contributors_results = create_local_resource(move || (), |_| fetch_contributors());
29+
30+
let contributors_view = move || {
31+
let data = contributors_results.read();
32+
let Some(Ok(items)) = data else { return None };
33+
let result = items
34+
.iter()
35+
.map(|item| {
36+
view! {
37+
<ContributorCard
38+
name=item.login.clone()
39+
description=""
40+
link=item.html_url.clone()
41+
brand_src=item.avatar_url.clone()
42+
/>
43+
}
44+
})
45+
.collect::<Fragment>();
46+
Some(result.into_view())
47+
};
5248

5349
view! {
5450
<div>
@@ -61,19 +57,7 @@ pub fn Contributors() -> impl IntoView {
6157
<span class="font-alfa-slab text-orange-500">"Colaboradores"</span>
6258
</h2>
6359
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 2xl:grid-cols-5 gap-6">
64-
{contributors
65-
.into_iter()
66-
.map(|contributor| {
67-
view! {
68-
<ContributorCard
69-
name=contributor.name
70-
description=contributor.description
71-
link=contributor.link
72-
brand_src=contributor.brand_src
73-
/>
74-
}
75-
})
76-
.collect::<Vec<_>>()}
60+
{contributors_view}
7761
</div>
7862
</div>
7963
</section>

0 commit comments

Comments
 (0)