Skip to content

Commit

Permalink
feat: add pagination
Browse files Browse the repository at this point in the history
A future commit with make filtering by != done in the api request,
rather than handling it client side
  • Loading branch information
mark-pitblado committed Oct 26, 2024
1 parent cde333b commit a1e74d7
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
11 changes: 6 additions & 5 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ use crate::models::{Label, Task, TaskDetail};
use reqwest::Client;
use std::collections::HashMap;

pub async fn fetch_tasks(instance_url: &str, api_key: &str) -> Result<Vec<Task>, reqwest::Error> {
pub async fn fetch_tasks(
instance_url: &str,
api_key: &str,
page: usize,
) -> Result<Vec<Task>, reqwest::Error> {
let client = Client::new();
let url = format!(
"{}/api/v1/tasks/all?project=Inbox&sort_by=created",
instance_url
);
let url = format!("{}/api/v1/tasks/all?page={}", instance_url, page);

let res = client
.get(&url)
Expand Down
32 changes: 31 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub struct App {
pub task_detail: Option<TaskDetail>,
pub input_mode: InputMode,
pub new_task_title: String,
pub page: usize,
}

pub enum InputMode {
Expand All @@ -31,6 +32,17 @@ impl App {
task_detail: None,
input_mode: InputMode::Normal,
new_task_title: String::new(),
page: 1,
}
}

pub fn next_page(&mut self) {
self.page += 1;
}

pub fn previous_page(&mut self) {
if self.page > 1 {
self.page -= 1;
}
}

Expand Down Expand Up @@ -86,6 +98,22 @@ impl App {
KeyCode::Char('q') => return Ok(true),
KeyCode::Char('j') => self.next(),
KeyCode::Char('k') => self.previous(),
KeyCode::Char('n') => {
// Next page
self.next_page();
if let Ok(new_tasks) = fetch_tasks(instance_url, api_key, self.page).await {
self.tasks = new_tasks.into_iter().filter(|task| !task.done).collect();
self.state.select(Some(0));
}
}
KeyCode::Char('p') => {
// Previous page
self.previous_page();
if let Ok(new_tasks) = fetch_tasks(instance_url, api_key, self.page).await {
self.tasks = new_tasks.into_iter().filter(|task| !task.done).collect();
self.state.select(Some(0));
}
}
KeyCode::Char('a') => {
self.input_mode = InputMode::Editing;
self.new_task_title.clear();
Expand All @@ -108,7 +136,9 @@ impl App {
eprintln!("Error creating task: {}", err);
} else {
// Refresh the task list
if let Ok(all_tasks) = fetch_tasks(instance_url, api_key).await {
if let Ok(all_tasks) =
fetch_tasks(instance_url, api_key, self.page).await
{
self.tasks =
all_tasks.into_iter().filter(|task| !task.done).collect();
self.state.select(Some(0));
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let instance_url = config.vikunja.instance_url;
let api_key = config.vikunja.api_key;

let all_tasks = api::fetch_tasks(&instance_url, &api_key).await?;
let all_tasks = api::fetch_tasks(&instance_url, &api_key, 1).await?;

let incomplete_tasks: Vec<models::Task> =
all_tasks.into_iter().filter(|task| !task.done).collect();
Expand Down
4 changes: 4 additions & 0 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ fn get_legend(input_mode: &InputMode) -> Text<'static> {
Span::raw(": Down "),
Span::styled(" k ", Style::default().fg(Color::Red)),
Span::raw(": Up "),
Span::styled(" n ", Style::default().fg(Color::Red)),
Span::raw(": Next Page "),
Span::styled(" p ", Style::default().fg(Color::Red)),
Span::raw(": Previous Page "),
Span::styled(" Enter ", Style::default().fg(Color::Red)),
Span::raw(": View Details "),
Span::styled(" a ", Style::default().fg(Color::Red)),
Expand Down

0 comments on commit a1e74d7

Please sign in to comment.