Skip to content

Commit c9cf845

Browse files
committed
fix example
1 parent bf8d00c commit c9cf845

File tree

7 files changed

+62
-46
lines changed

7 files changed

+62
-46
lines changed

examples/router/src/components/author_card.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{content::Author, generator::Generated};
1+
use crate::{content::Author, generator::Generated, Routes};
22
use yew::prelude::*;
33
use yew_router::prelude::Link;
44

@@ -54,9 +54,9 @@ impl Component for AuthorCard {
5454
</div>
5555
</div>
5656
<footer class="card-footer">
57-
<Link classes="card-footer-item" route=format!("/authors/{}", author.seed)>
57+
<Link<Routes> classes="card-footer-item" route=Routes::Author { id: author.seed }>
5858
{ "Profile" }
59-
</Link>
59+
</Link<Routes>>
6060
</footer>
6161
</div>
6262
}

examples/router/src/components/post_card.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{content::Post, generator::Generated};
1+
use crate::{content::Post, generator::Generated, Routes};
22
use yew::prelude::*;
33
use yew_router::prelude::Link;
44

@@ -43,12 +43,12 @@ impl Component for PostCard {
4343
</figure>
4444
</div>
4545
<div class="card-content">
46-
<Link classes="title is-block" route=format!("/posts/{}", post.seed)>
46+
<Link<Routes> classes="title is-block" route=Routes::Post {id: post.seed}>
4747
{ &post.title }
48-
</Link>
49-
<Link classes="subtitle is-block" route=format!("/authors/{}", post.author.seed)>
48+
</Link<Routes>>
49+
<Link<Routes> classes="subtitle is-block" route=Routes::Author {id: post.author.seed}>
5050
{ &post.author.name }
51-
</Link>
51+
</Link<Routes>>
5252
</div>
5353
</div>
5454
}

examples/router/src/main.rs

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,22 @@ use pages::{
1010
post_list::PostList,
1111
};
1212

13+
#[derive(Routable, PartialEq, Clone, Debug)]
14+
pub enum Routes {
15+
#[at("/posts/:id")]
16+
Post { id: u64 },
17+
#[at("/posts")]
18+
Posts,
19+
#[at("/authors/:id")]
20+
Author { id: u64 },
21+
#[at("/authors")]
22+
Authors,
23+
#[at("/")]
24+
Home,
25+
#[at("/404")]
26+
NotFound,
27+
}
28+
1329
pub enum Msg {
1430
ToggleNavbar,
1531
}
@@ -48,35 +64,31 @@ impl Component for Model {
4864
{ self.view_nav() }
4965

5066
<main>
51-
<Router not_found_route="/404">
52-
<Route to="/posts/:id">
67+
<Router<Routes> not_found_route="/404">
68+
<Route to=Routes::POST>
5369
<Post />
5470
</Route>
5571

56-
<Route to="/posts">
57-
<PostList />
58-
</Route>
59-
60-
<Route to="/posts">
72+
<Route to=Routes::POSTS>
6173
<PostList />
6274
</Route>
6375

64-
<Route to="/authors/:id">
76+
<Route to=Routes::AUTHOR>
6577
<Author />
6678
</Route>
6779

68-
<Route to="/authors">
80+
<Route to=Routes::AUTHORS>
6981
<AuthorList />
7082
</Route>
7183

72-
<Route to="/">
84+
<Route to=Routes::HOME>
7385
<Home />
7486
</Route>
7587

76-
<Route to="404">
88+
<Route to=Routes::NOT_FOUND>
7789
<PageNotFound />
7890
</Route>
79-
</Router>
91+
</Router<Routes>>
8092
</main>
8193
<footer class="footer">
8294
<div class="content has-text-centered">
@@ -119,22 +131,22 @@ impl Model {
119131
</div>
120132
<div class=classes!("navbar-menu", active_class)>
121133
<div class="navbar-start">
122-
<Link classes="navbar-item" route="/">
134+
<Link<Routes> classes="navbar-item" route=Routes::Home>
123135
{ "Home" }
124-
</Link>
125-
<Link classes="navbar-item" route="/posts">
136+
</Link<Routes>>
137+
<Link<Routes> classes="navbar-item" route=Routes::Posts>
126138
{ "Posts" }
127-
</Link>
139+
</Link<Routes>>
128140

129141
<div class="navbar-item has-dropdown is-hoverable">
130142
<a class="navbar-link">
131143
{ "More" }
132144
</a>
133145
<div class="navbar-dropdown">
134146
<a class="navbar-item">
135-
<Link classes="navbar-item" route="/authors">
147+
<Link<Routes> classes="navbar-item" route=Routes::Authors>
136148
{ "Meet the authors" }
137-
</Link>
149+
</Link<Routes>>
138150
</a>
139151
</div>
140152
</div>

examples/router/src/pages/author.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{content, generator::Generated};
1+
use crate::{content, generator::Generated, Routes};
22
use yew::prelude::*;
33
use yew_router::RouterService;
44

@@ -10,12 +10,11 @@ impl Component for Author {
1010
type Properties = ();
1111

1212
fn create(_: Self::Properties, _link: ComponentLink<Self>) -> Self {
13-
let seed = RouterService::current_route()
14-
.parmas()
15-
.get("id")
16-
.unwrap()
17-
.parse()
18-
.unwrap();
13+
let seed = match RouterService::current_route().route() {
14+
Routes::Author { id } => *id,
15+
_ => unreachable!()
16+
};
17+
1918
Self {
2019
author: content::Author::generate_from_seed(seed),
2120
}

examples/router/src/pages/post.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{content, generator::Generated};
1+
use crate::{content, generator::Generated, Routes};
22
use content::PostPart;
33
use yew::prelude::*;
44
use yew_router::prelude::*;
@@ -11,12 +11,11 @@ impl Component for Post {
1111
type Properties = ();
1212

1313
fn create(_: Self::Properties, _link: ComponentLink<Self>) -> Self {
14-
let seed = RouterService::current_route()
15-
.parmas()
16-
.get("id")
17-
.unwrap()
18-
.parse()
19-
.unwrap();
14+
let seed = match RouterService::current_route().route() {
15+
Routes::Post { id } => *id,
16+
_ => unreachable!()
17+
};
18+
2019
Self {
2120
post: content::Post::generate_from_seed(seed),
2221
}
@@ -49,9 +48,9 @@ impl Component for Post {
4948
</h1>
5049
<h2 class="subtitle">
5150
{ "by " }
52-
<Link classes="has-text-weight-semibold" route=format!("/authors/{}", post.author.seed)>
51+
<Link<Routes> classes="has-text-weight-semibold" route=Routes::Author { id: post.author.seed }>
5352
{ &post.author.name }
54-
</Link>
53+
</Link<Routes>>
5554
</h2>
5655
<div class="tags">
5756
{ for keywords }
@@ -77,9 +76,9 @@ impl Post {
7776
</figure>
7877
<div class="media-content">
7978
<div class="content">
80-
<Link classes="is-size-5" route=format!("/authors/{}", quote.author.seed)>
79+
<Link<Routes> classes="is-size-5" route=Routes::Author { id: quote.author.seed }>
8180
<strong>{ &quote.author.name }</strong>
82-
</Link>
81+
</Link<Routes>>
8382
<p class="is-family-secondary">
8483
{ &quote.content }
8584
</p>

examples/router/src/pages/post_list.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::components::{pagination::Pagination, post_card::PostCard};
2+
use crate::Routes;
23
use yew::prelude::*;
34
use yew_router::RouterService;
5+
use std::collections::HashMap;
46

57
const ITEMS_PER_PAGE: u64 = 10;
68
const TOTAL_PAGES: u64 = std::u64::MAX / ITEMS_PER_PAGE;
@@ -23,7 +25,11 @@ impl Component for PostList {
2325
fn update(&mut self, msg: Self::Message) -> ShouldRender {
2426
match msg {
2527
Msg::ShowPage(page) => {
26-
RouterService::push(&format!("/posts/?page={}", page));
28+
RouterService::push(Routes::Posts, {
29+
let mut map = HashMap::new();
30+
map.insert("page", page.to_string());
31+
Some(map)
32+
});
2733
true
2834
}
2935
}

packages/yew-router/src/service.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ impl RouterService {
99
/// Navigate to a specific route.
1010
///
1111
/// This should be used in cases where [`Link`](crate::prelude::Link) is insufficient.
12-
pub fn push(route: impl Routable, query: Option<HashMap<&str, &str>>) {
12+
pub fn push(route: impl Routable, query: Option<HashMap<&str, String>>) {
1313
let mut url = route.to_route();
1414
if let Some(query) = query {
1515
url.push('?');

0 commit comments

Comments
 (0)