scroll to the right page in redirects to card list

session-crate
Ondřej Hruška 5 years ago
parent f2a834c977
commit 08d858927d
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 22
      src/main.rs

@ -34,18 +34,30 @@ pub struct ListContext<'a> {
pub pages: usize,
}
const per_page: usize = 20;
fn find_page_with_card(store : &Store, card_id : usize) -> Option<usize> {
if let Some((n, _)) = store.data.cards.iter().enumerate().find(|(_n, (id, _card))| **id == card_id) {
Some(n / per_page)
} else {
None
}
}
#[get("/?<page>&<card>")]
fn route_index(store: State<RwLock<Store>>, page: Option<usize>, card: Option<usize>) -> 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;
page = find_page_with_card(&rg, card_id).unwrap_or(page);
}
if page >= n_pages {
page = n_pages - 1;
}
let context = ListContext {
@ -250,9 +262,11 @@ fn route_edit_save(id: usize, form: Form<MapFromForm>, store: State<RwLock<Store
fn route_delete(id: usize, store: State<RwLock<Store>>) -> Redirect {
let mut rg = store.write();
let page = find_page_with_card(&rg, id).unwrap_or(0);
rg.delete_card(id);
Redirect::found(uri!(route_index: page=_, card=_))
Redirect::found(uri!(route_index: page=page, card=_))
}
fn main() {

Loading…
Cancel
Save