diff --git a/src/main.rs b/src/main.rs index 68cf184..bbd629a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,24 +30,41 @@ use indexmap::map::IndexMap; pub struct ListContext<'a> { pub fields: Vec>, pub cards: Vec>, + pub page: usize, + pub pages: usize, } -#[get("/")] -fn route_index(store: State>) -> Template { +#[get("/?&")] +fn route_index(store: State>, page: Option, card: Option) -> Template { let rg = store.read(); + let per_page: usize = 20; + let mut page = page.unwrap_or_default(); + let n_pages = (rg.data.cards.len() as f64 / per_page as f64).ceil() as usize; + + if let Some(card_id) = card { + if let Some((n, _)) = rg.data.cards.iter().enumerate().find(|(_n, (id, _card))| **id == card_id) { + page = n / per_page; + } + } + let context = ListContext { fields: render_empty_fields(&rg), - cards: rg.data.cards.iter().filter_map(|(id, card)| { - if let Value::Object(map) = card { - Some(RenderedCard { - fields: render_card_fields(&rg, map), - id: *id, - }) - } else { - None - } - }).collect(), + pages: n_pages, + page, + cards: rg.data.cards.iter() + .skip(page * per_page) + .take(per_page) + .filter_map(|(id, card)| { + if let Value::Object(ref map) = card { + Some(RenderedCard { + fields: render_card_fields(&rg, map), + id: *id, + }) + } else { + None + } + }).collect(), }; Template::render("index", context) @@ -71,7 +88,7 @@ impl<'a> FromForm<'a> for MapFromForm { } } -fn collect_card_form(store : &Store, mut form : MapFromForm) -> IndexMap:: { +fn collect_card_form(store: &Store, mut form: MapFromForm) -> IndexMap:: { let mut card = IndexMap::new(); for (k, field) in &store.model.fields { @@ -179,7 +196,7 @@ struct AddCardContext<'a> { #[derive(Serialize)] struct EditCardContext<'a> { pub fields: Vec>, - pub id : usize, + pub id: usize, } #[get("/add")] @@ -198,16 +215,16 @@ fn route_add_save(form: Form, store: State>) -> Redir let mut rg = store.write(); let card = collect_card_form(&rg, form.into_inner()); - rg.add_card(card); + let id = rg.add_card(card); - Redirect::found(uri!(route_index)) + Redirect::found(uri!(route_index: page=_, card=id)) } #[get("/edit/")] -fn route_edit(id : usize, store: State>) -> Option