Skip to content

Commit 82f7799

Browse files
committed
cleanup code, update example
1 parent e838d89 commit 82f7799

File tree

16 files changed

+412
-313
lines changed

16 files changed

+412
-313
lines changed

examples/router/src/components/author_card.rs

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

45
#[derive(Clone, Debug, PartialEq, Properties)]
56
pub struct Props {
@@ -53,9 +54,9 @@ impl Component for AuthorCard {
5354
</div>
5455
</div>
5556
<footer class="card-footer">
56-
<AppAnchor classes="card-footer-item" route=format!("/authors/{}", author.seed)>
57+
<Link classes="card-footer-item" route=format!("/authors/{}", author.seed)>
5758
{ "Profile" }
58-
</AppAnchor>
59+
</Link>
5960
</footer>
6061
</div>
6162
}

examples/router/src/components/post_card.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use crate::{content::Post, generator::Generated, switch::AppAnchor};
1+
use crate::{content::Post, generator::Generated};
22
use yew::prelude::*;
3+
use yew_router::prelude::Link;
34

45
#[derive(Clone, Debug, PartialEq, Properties)]
56
pub struct Props {
@@ -42,12 +43,12 @@ impl Component for PostCard {
4243
</figure>
4344
</div>
4445
<div class="card-content">
45-
<AppAnchor classes="title is-block" route=format!("/posts/{}", post.seed)>
46+
<Link classes="title is-block" route=format!("/posts/{}", post.seed)>
4647
{ &post.title }
47-
</AppAnchor>
48-
<AppAnchor classes="subtitle is-block" route=format!("/authors/{}", post.author.seed)>
48+
</Link>
49+
<Link classes="subtitle is-block" route=format!("/authors/{}", post.author.seed)>
4950
{ &post.author.name }
50-
</AppAnchor>
51+
</Link>
5152
</div>
5253
</div>
5354
}

examples/router/src/main.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use yew::prelude::*;
2-
use yew_router::*;
2+
use yew_router::prelude::*;
33

44
mod components;
55
mod content;
@@ -9,8 +9,6 @@ use pages::{
99
author::Author, author_list::AuthorList, home::Home, page_not_found::PageNotFound, post::Post,
1010
post_list::PostList,
1111
};
12-
mod switch;
13-
use switch::AppAnchor;
1412

1513
pub enum Msg {
1614
ToggleNavbar,
@@ -50,22 +48,21 @@ impl Component for Model {
5048
{ self.view_nav() }
5149

5250
<main>
53-
<Router>
51+
<Router not_found_route="/404">
5452
<Route to="/posts/:id">
55-
<Post /> // id
53+
<Post />
5654
</Route>
5755

58-
// todo query params
59-
<Route to="/posts/?page={}">
60-
<PostList /> // page.max(1)
56+
<Route to="/posts">
57+
<PostList />
6158
</Route>
6259

6360
<Route to="/posts">
6461
<PostList />
6562
</Route>
6663

6764
<Route to="/authors/:id">
68-
<Author /> // id
65+
<Author />
6966
</Route>
7067

7168
<Route to="/authors">
@@ -122,22 +119,22 @@ impl Model {
122119
</div>
123120
<div class=classes!("navbar-menu", active_class)>
124121
<div class="navbar-start">
125-
<AppAnchor classes="navbar-item" route="/">
122+
<Link classes="navbar-item" route="/">
126123
{ "Home" }
127-
</AppAnchor>
128-
<AppAnchor classes="navbar-item" route="/posts">
124+
</Link>
125+
<Link classes="navbar-item" route="/posts">
129126
{ "Posts" }
130-
</AppAnchor>
127+
</Link>
131128

132129
<div class="navbar-item has-dropdown is-hoverable">
133130
<a class="navbar-link">
134131
{ "More" }
135132
</a>
136133
<div class="navbar-dropdown">
137134
<a class="navbar-item">
138-
<AppAnchor classes="navbar-item" route="/authors">
135+
<Link classes="navbar-item" route="/authors">
139136
{ "Meet the authors" }
140-
</AppAnchor>
137+
</Link>
141138
</a>
142139
</div>
143140
</div>

examples/router/src/pages/author.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ impl Component for Author {
1212
fn create(_: Self::Properties, _link: ComponentLink<Self>) -> Self {
1313
let seed = RouterService::current_route()
1414
.parmas()
15-
.find("id")
15+
.get("id")
1616
.unwrap()
1717
.parse()
1818
.unwrap();

examples/router/src/pages/post.rs

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

66
pub struct Post {
77
post: content::Post,
@@ -13,7 +13,7 @@ impl Component for Post {
1313
fn create(_: Self::Properties, _link: ComponentLink<Self>) -> Self {
1414
let seed = RouterService::current_route()
1515
.parmas()
16-
.find("id")
16+
.get("id")
1717
.unwrap()
1818
.parse()
1919
.unwrap();
@@ -49,9 +49,9 @@ impl Component for Post {
4949
</h1>
5050
<h2 class="subtitle">
5151
{ "by " }
52-
<AppAnchor classes="has-text-weight-semibold" route=format!("/authors/{}", post.author.seed)>
52+
<Link classes="has-text-weight-semibold" route=format!("/authors/{}", post.author.seed)>
5353
{ &post.author.name }
54-
</AppAnchor>
54+
</Link>
5555
</h2>
5656
<div class="tags">
5757
{ for keywords }
@@ -77,9 +77,9 @@ impl Post {
7777
</figure>
7878
<div class="media-content">
7979
<div class="content">
80-
<AppAnchor classes="is-size-5" route=format!("/authors/{}", quote.author.seed)>
80+
<Link classes="is-size-5" route=format!("/authors/{}", quote.author.seed)>
8181
<strong>{ &quote.author.name }</strong>
82-
</AppAnchor>
82+
</Link>
8383
<p class="is-family-secondary">
8484
{ &quote.content }
8585
</p>

examples/router/src/pages/post_list.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ impl Component for PostList {
2424
match msg {
2525
Msg::ShowPage(page) => {
2626
RouterService::push(&format!("/posts/?page={}", page));
27-
false
27+
true
2828
}
2929
}
3030
}
@@ -77,7 +77,6 @@ impl PostList {
7777
}
7878

7979
fn current_page(&self) -> u64 {
80-
// todo query params
81-
1
80+
RouterService::current_route().query().get("page").map(|it| it.parse().expect("invalid page")).unwrap_or(1)
8281
}
8382
}

examples/router/src/switch.rs

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use crate::RouterService;
2+
use yew::prelude::*;
3+
4+
#[derive(Properties, Clone, PartialEq)]
5+
pub struct LinkProps {
6+
#[prop_or_default]
7+
pub classes: String,
8+
pub route: String,
9+
pub children: Children,
10+
}
11+
12+
pub struct Link {
13+
link: ComponentLink<Self>,
14+
props: LinkProps,
15+
}
16+
17+
pub enum Msg {
18+
OnClick,
19+
}
20+
21+
impl Component for Link {
22+
type Message = Msg;
23+
type Properties = LinkProps;
24+
25+
fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
26+
Self { link, props }
27+
}
28+
29+
fn update(&mut self, msg: Self::Message) -> ShouldRender {
30+
match msg {
31+
Msg::OnClick => {
32+
let route = self.props.route.clone();
33+
RouterService::push(&route);
34+
false
35+
}
36+
}
37+
}
38+
39+
fn change(&mut self, mut props: Self::Properties) -> ShouldRender {
40+
std::mem::swap(&mut self.props, &mut props);
41+
props != self.props
42+
}
43+
44+
fn view(&self) -> Html {
45+
html! {
46+
<a class=self.props.classes.clone() onclick=self.link.callback(|_| Msg::OnClick)>{ self.props.children.clone() }</a>
47+
}
48+
}
49+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod link;
2+
pub mod route;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use yew::prelude::*;
2+
3+
#[derive(Properties, Clone, PartialEq, Debug)]
4+
pub struct RouteProps {
5+
pub to: String,
6+
pub children: Children,
7+
}
8+
9+
pub struct Route {
10+
props: RouteProps,
11+
}
12+
13+
impl Component for Route {
14+
type Message = ();
15+
type Properties = RouteProps;
16+
17+
fn create(props: Self::Properties, _: ComponentLink<Self>) -> Self {
18+
Self { props }
19+
}
20+
21+
fn update(&mut self, _msg: Self::Message) -> ShouldRender {
22+
false
23+
}
24+
25+
fn change(&mut self, mut props: Self::Properties) -> ShouldRender {
26+
std::mem::swap(&mut self.props, &mut props);
27+
props != self.props
28+
}
29+
30+
fn view(&self) -> Html {
31+
html! { for self.props.children.clone() }
32+
}
33+
}

0 commit comments

Comments
 (0)