Skip to content

Commit

Permalink
Edit labels
Browse files Browse the repository at this point in the history
  • Loading branch information
alanvardy committed Dec 19, 2024
1 parent 6a1c365 commit a973428
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Added due to multi-select in `task edit`
- Make all updates selected in multi-select run asynchronously
- Added labels to multi-select in `task edit`

## 2024-12-19 v0.6.17

Expand Down
26 changes: 26 additions & 0 deletions src/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub enum TaskAttribute {
Description,
Priority,
Due,
Labels,
}
impl Display for TaskAttribute {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Expand All @@ -50,6 +51,7 @@ impl Display for TaskAttribute {
TaskAttribute::Description => write!(f, "Description"),
TaskAttribute::Priority => write!(f, "Priority"),
TaskAttribute::Due => write!(f, "Due"),
TaskAttribute::Labels => write!(f, "Labels"),
}
}
}
Expand All @@ -61,6 +63,7 @@ pub fn task_attributes() -> Vec<TaskAttribute> {
TaskAttribute::Description,
TaskAttribute::Priority,
TaskAttribute::Due,
TaskAttribute::Labels,
]
}

Expand Down Expand Up @@ -388,6 +391,20 @@ pub async fn update_task(
}
}
TaskAttribute::Due => tasks::spawn_schedule_task(config.clone(), task.clone()),
TaskAttribute::Labels => {
let label_string = input::string(
"Enter labels separated by spaces:",
config.mock_string.clone(),
)?;

let labels = label_string
.split_whitespace()
.map(|s| s.to_owned())
.collect();

let handle = spawn_update_task_labels(config.clone(), task.clone(), labels);
Ok(Some(handle))
}
}
}

Expand Down Expand Up @@ -627,6 +644,15 @@ pub fn spawn_update_task_description(
})
}

/// Updates task inside another thread
pub fn spawn_update_task_labels(config: Config, task: Task, labels: Vec<String>) -> JoinHandle<()> {
tokio::spawn(async move {
if let Err(e) = todoist::update_task_labels(&config, &task, labels, false).await {
config.tx().send(e).unwrap();
}
})
}

/// Updates task inside another thread
pub fn spawn_update_task_priority(
config: Config,
Expand Down
15 changes: 15 additions & 0 deletions src/todoist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,21 @@ pub async fn update_task_description(
Ok(String::from("✓"))
}

/// Update the labels of a task by ID
pub async fn update_task_labels(
config: &Config,
task: &Task,
labels: Vec<String>,
spinner: bool,
) -> Result<String, Error> {
let body = json!({ "labels": labels});
let url = format!("{}{}", REST_V2_TASKS_URL, task.id);

request::post_todoist_rest(config, url, body, spinner).await?;
// Does not pass back a task
Ok(String::from("✓"))
}

/// Complete the last task returned by "next task"
pub async fn complete_task(config: &Config, task_id: &str, spinner: bool) -> Result<String, Error> {
let body = json!({"commands": [{"type": "item_close", "uuid": request::new_uuid(), "temp_id": request::new_uuid(), "args": {"id": task_id}}]});
Expand Down

0 comments on commit a973428

Please sign in to comment.