Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement #[bind(...)] for Routable macro #1897

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions examples/router/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ pub enum Route {
#[at("/posts/:id")]
Post { id: u64 },
#[at("/posts")]
Posts,
Posts {
#[bind(query)]
page: u64,
},
#[at("/authors/:id")]
Author { id: u64 },
#[at("/authors")]
Authors,
#[at("/")]
Home,
#[not_found]
#[bind(not_found)]
#[at("/404")]
NotFound,
}
Expand Down Expand Up @@ -111,7 +114,7 @@ impl Model {
<Link<Route> classes=classes!("navbar-item") route=Route::Home>
{ "Home" }
</Link<Route>>
<Link<Route> classes=classes!("navbar-item") route=Route::Posts>
<Link<Route> classes=classes!("navbar-item") route=Route::Posts { page: 1 }>
{ "Posts" }
</Link<Route>>

Expand Down Expand Up @@ -139,8 +142,8 @@ fn switch(routes: &Route) -> Html {
Route::Post { id } => {
html! { <Post seed=*id /> }
}
Route::Posts => {
html! { <PostList /> }
Route::Posts { page } => {
html! { <PostList page=page /> }
}
Route::Author { id } => {
html! { <Author seed=*id /> }
Expand Down
31 changes: 13 additions & 18 deletions examples/router/src/pages/post_list.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::components::{pagination::Pagination, post_card::PostCard};
use crate::Route;
use serde::{Deserialize, Serialize};
use yew::prelude::*;

const ITEMS_PER_PAGE: u64 = 10;
Expand All @@ -10,37 +9,39 @@ pub enum Msg {
ShowPage(u64),
}

#[derive(Serialize, Deserialize)]
struct PageQuery {
page: u64,
#[derive(Properties, Clone, PartialEq)]
pub struct Props {
pub page: u64,
}

pub struct PostList {
link: ComponentLink<Self>,
props: Props,
}
impl Component for PostList {
type Message = Msg;
type Properties = ();
type Properties = Props;

fn create(_props: Self::Properties, link: ComponentLink<Self>) -> Self {
Self { link }
fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
Self { link, props }
}

fn update(&mut self, msg: Self::Message) -> ShouldRender {
match msg {
Msg::ShowPage(page) => {
yew_router::push_route_with_query(Route::Posts, PageQuery { page }).unwrap();
yew_router::push_route(Route::Posts { page });
true
}
}
}

fn change(&mut self, _props: Self::Properties) -> ShouldRender {
false
fn change(&mut self, mut props: Self::Properties) -> ShouldRender {
std::mem::swap(&mut self.props, &mut props);
props != self.props
}

fn view(&self) -> Html {
let page = self.current_page();
let page = self.props.page;

html! {
<div class="section container">
Expand All @@ -58,7 +59,7 @@ impl Component for PostList {
}
impl PostList {
fn view_posts(&self) -> Html {
let start_seed = (self.current_page() - 1) * ITEMS_PER_PAGE;
let start_seed = (self.props.page - 1) * ITEMS_PER_PAGE;
let mut cards = (0..ITEMS_PER_PAGE).map(|seed_offset| {
html! {
<li class="list-item mb-5">
Expand All @@ -81,10 +82,4 @@ impl PostList {
</div>
}
}

fn current_page(&self) -> u64 {
yew_router::parse_query::<PageQuery>()
.map(|it| it.page)
.unwrap_or(1)
}
}
2 changes: 1 addition & 1 deletion packages/yew-router-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ proc-macro = true
heck = "0.3.2"
proc-macro2 = "1.0.24"
quote = "1.0.9"
syn = { version = "1.0.64", features = ["full","extra-traits"] }
syn = { version = "1.0", features = ["full","extra-traits"] }

[dev-dependencies]
rustversion = "1.0"
Expand Down
12 changes: 7 additions & 5 deletions packages/yew-router-macro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod routable_derive;
use routable_derive::{routable_derive_impl, Routable};
use syn::parse_macro_input;
use routable_derive::routable_derive_impl;
use syn::{parse_macro_input, DeriveInput};

/// Derive macro used to mark an enum as Routable.
///
Expand All @@ -23,8 +23,10 @@ use syn::parse_macro_input;
/// NotFound,
/// }
/// ```
#[proc_macro_derive(Routable, attributes(at, not_found))]
#[proc_macro_derive(Routable, attributes(at, bind))]
pub fn routable_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = parse_macro_input!(input as Routable);
routable_derive_impl(input).into()
let input = parse_macro_input!(input as DeriveInput);
routable_derive_impl(input)
.unwrap_or_else(|it| it.to_compile_error())
.into()
}
Loading