Skip to content

Commit

Permalink
fix example
Browse files Browse the repository at this point in the history
  • Loading branch information
ranile committed Mar 31, 2021
1 parent bf8d00c commit c9cf845
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 46 deletions.
6 changes: 3 additions & 3 deletions examples/router/src/components/author_card.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{content::Author, generator::Generated};
use crate::{content::Author, generator::Generated, Routes};
use yew::prelude::*;
use yew_router::prelude::Link;

Expand Down Expand Up @@ -54,9 +54,9 @@ impl Component for AuthorCard {
</div>
</div>
<footer class="card-footer">
<Link classes="card-footer-item" route=format!("/authors/{}", author.seed)>
<Link<Routes> classes="card-footer-item" route=Routes::Author { id: author.seed }>
{ "Profile" }
</Link>
</Link<Routes>>
</footer>
</div>
}
Expand Down
10 changes: 5 additions & 5 deletions examples/router/src/components/post_card.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{content::Post, generator::Generated};
use crate::{content::Post, generator::Generated, Routes};
use yew::prelude::*;
use yew_router::prelude::Link;

Expand Down Expand Up @@ -43,12 +43,12 @@ impl Component for PostCard {
</figure>
</div>
<div class="card-content">
<Link classes="title is-block" route=format!("/posts/{}", post.seed)>
<Link<Routes> classes="title is-block" route=Routes::Post {id: post.seed}>
{ &post.title }
</Link>
<Link classes="subtitle is-block" route=format!("/authors/{}", post.author.seed)>
</Link<Routes>>
<Link<Routes> classes="subtitle is-block" route=Routes::Author {id: post.author.seed}>
{ &post.author.name }
</Link>
</Link<Routes>>
</div>
</div>
}
Expand Down
48 changes: 30 additions & 18 deletions examples/router/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@ use pages::{
post_list::PostList,
};

#[derive(Routable, PartialEq, Clone, Debug)]
pub enum Routes {
#[at("/posts/:id")]
Post { id: u64 },
#[at("/posts")]
Posts,
#[at("/authors/:id")]
Author { id: u64 },
#[at("/authors")]
Authors,
#[at("/")]
Home,
#[at("/404")]
NotFound,
}

pub enum Msg {
ToggleNavbar,
}
Expand Down Expand Up @@ -48,35 +64,31 @@ impl Component for Model {
{ self.view_nav() }

<main>
<Router not_found_route="/404">
<Route to="/posts/:id">
<Router<Routes> not_found_route="/404">
<Route to=Routes::POST>
<Post />
</Route>

<Route to="/posts">
<PostList />
</Route>

<Route to="/posts">
<Route to=Routes::POSTS>
<PostList />
</Route>

<Route to="/authors/:id">
<Route to=Routes::AUTHOR>
<Author />
</Route>

<Route to="/authors">
<Route to=Routes::AUTHORS>
<AuthorList />
</Route>

<Route to="/">
<Route to=Routes::HOME>
<Home />
</Route>

<Route to="404">
<Route to=Routes::NOT_FOUND>
<PageNotFound />
</Route>
</Router>
</Router<Routes>>
</main>
<footer class="footer">
<div class="content has-text-centered">
Expand Down Expand Up @@ -119,22 +131,22 @@ impl Model {
</div>
<div class=classes!("navbar-menu", active_class)>
<div class="navbar-start">
<Link classes="navbar-item" route="/">
<Link<Routes> classes="navbar-item" route=Routes::Home>
{ "Home" }
</Link>
<Link classes="navbar-item" route="/posts">
</Link<Routes>>
<Link<Routes> classes="navbar-item" route=Routes::Posts>
{ "Posts" }
</Link>
</Link<Routes>>

<div class="navbar-item has-dropdown is-hoverable">
<a class="navbar-link">
{ "More" }
</a>
<div class="navbar-dropdown">
<a class="navbar-item">
<Link classes="navbar-item" route="/authors">
<Link<Routes> classes="navbar-item" route=Routes::Authors>
{ "Meet the authors" }
</Link>
</Link<Routes>>
</a>
</div>
</div>
Expand Down
13 changes: 6 additions & 7 deletions examples/router/src/pages/author.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{content, generator::Generated};
use crate::{content, generator::Generated, Routes};
use yew::prelude::*;
use yew_router::RouterService;

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

fn create(_: Self::Properties, _link: ComponentLink<Self>) -> Self {
let seed = RouterService::current_route()
.parmas()
.get("id")
.unwrap()
.parse()
.unwrap();
let seed = match RouterService::current_route().route() {
Routes::Author { id } => *id,
_ => unreachable!()
};

Self {
author: content::Author::generate_from_seed(seed),
}
Expand Down
21 changes: 10 additions & 11 deletions examples/router/src/pages/post.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{content, generator::Generated};
use crate::{content, generator::Generated, Routes};
use content::PostPart;
use yew::prelude::*;
use yew_router::prelude::*;
Expand All @@ -11,12 +11,11 @@ impl Component for Post {
type Properties = ();

fn create(_: Self::Properties, _link: ComponentLink<Self>) -> Self {
let seed = RouterService::current_route()
.parmas()
.get("id")
.unwrap()
.parse()
.unwrap();
let seed = match RouterService::current_route().route() {
Routes::Post { id } => *id,
_ => unreachable!()
};

Self {
post: content::Post::generate_from_seed(seed),
}
Expand Down Expand Up @@ -49,9 +48,9 @@ impl Component for Post {
</h1>
<h2 class="subtitle">
{ "by " }
<Link classes="has-text-weight-semibold" route=format!("/authors/{}", post.author.seed)>
<Link<Routes> classes="has-text-weight-semibold" route=Routes::Author { id: post.author.seed }>
{ &post.author.name }
</Link>
</Link<Routes>>
</h2>
<div class="tags">
{ for keywords }
Expand All @@ -77,9 +76,9 @@ impl Post {
</figure>
<div class="media-content">
<div class="content">
<Link classes="is-size-5" route=format!("/authors/{}", quote.author.seed)>
<Link<Routes> classes="is-size-5" route=Routes::Author { id: quote.author.seed }>
<strong>{ &quote.author.name }</strong>
</Link>
</Link<Routes>>
<p class="is-family-secondary">
{ &quote.content }
</p>
Expand Down
8 changes: 7 additions & 1 deletion examples/router/src/pages/post_list.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::components::{pagination::Pagination, post_card::PostCard};
use crate::Routes;
use yew::prelude::*;
use yew_router::RouterService;
use std::collections::HashMap;

const ITEMS_PER_PAGE: u64 = 10;
const TOTAL_PAGES: u64 = std::u64::MAX / ITEMS_PER_PAGE;
Expand All @@ -23,7 +25,11 @@ impl Component for PostList {
fn update(&mut self, msg: Self::Message) -> ShouldRender {
match msg {
Msg::ShowPage(page) => {
RouterService::push(&format!("/posts/?page={}", page));
RouterService::push(Routes::Posts, {
let mut map = HashMap::new();
map.insert("page", page.to_string());
Some(map)
});
true
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/yew-router/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ impl RouterService {
/// Navigate to a specific route.
///
/// This should be used in cases where [`Link`](crate::prelude::Link) is insufficient.
pub fn push(route: impl Routable, query: Option<HashMap<&str, &str>>) {
pub fn push(route: impl Routable, query: Option<HashMap<&str, String>>) {
let mut url = route.to_route();
if let Some(query) = query {
url.push('?');
Expand Down

0 comments on commit c9cf845

Please sign in to comment.