new task add values

This commit is contained in:
JMARyA 2024-06-07 13:14:28 +02:00
parent 477ce5ca98
commit 312aed18c8
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
3 changed files with 111 additions and 14 deletions

View file

@ -280,23 +280,40 @@ impl VikunjaAPI {
self.delete_request(&format!("/tasks/{id}"));
}
pub fn new_task(&self, title: &str, project: &ProjectID) -> Task {
pub fn new_task(
&self,
title: &str,
project: &ProjectID,
description: Option<String>,
due_date: Option<String>,
fav: bool,
label: Option<String>,
priority: Option<isize>,
) -> Result<Task, String> {
let id = project.0;
let labels = if let Some(label) = label {
let label = self
.get_all_labels()
.into_iter()
.find(|x| x.title.trim() == label)
.map_or_else(|| Err(format!("Label '{label}' not found")), Ok)?;
vec![label]
} else {
vec![]
};
let data = serde_json::json!({
"title": title
"title": title,
"description": description,
"due_date": due_date,
"is_favorite": fav,
"priority": priority,
"labels": labels
});
// todo :
// description
// due_date
// end_date
// is_favorite
// labels
// priority
let resp = self.put_request(&format!("/projects/{id}/tasks"), &data);
serde_json::from_str(&resp).unwrap()
Ok(serde_json::from_str(&resp).unwrap())
}
pub fn done_task(&self, task_id: isize, done: bool) -> Option<Task> {

View file

@ -49,7 +49,12 @@ pub fn get_args() -> clap::ArgMatches {
arg!(-p --project <project> "Project to add task to")
.required(false)
.default_value("Inbox"),
),
)
.arg(arg!(-d --description <description> "Task Description").required(false))
.arg(arg!(--due <due> "Task Due").required(false))
.arg(arg!(-l --label <label> "Task Label").required(false))
.arg(arg!(--priority <priority> "Task Label").required(false))
.arg(arg!(-f --favorite "Mark task as favorite").required(false)),
)
.subcommand(
command!()

View file

@ -108,6 +108,43 @@ fn load_config() -> config::Config {
toml::from_str(content).unwrap()
}
fn parse_datetime(input: &str) -> Option<chrono::DateTime<chrono::Utc>> {
let formats = [
"%Y-%m-%d %H:%M:%S",
"%Y-%m-%d %H:%M",
"%Y-%m-%d",
"%Y-%m-%dT%H:%M:%S",
"%Y-%m-%dT%H:%M:%S%.f",
"%Y-%m-%dT%H:%M:%S%.fZ",
"%Y-%m-%dT%H:%M:%S%:z",
"%Y-%m-%dT%H:%M:%S%z",
"%+%",
];
let input = input.trim();
for format in &formats {
if let Ok(naive_date) = chrono::NaiveDate::parse_from_str(input, format) {
let naive_datetime = naive_date.and_hms_opt(0, 0, 0).unwrap();
return Some(chrono::TimeZone::from_utc_datetime(
&chrono::Utc,
&naive_datetime,
));
}
if let Ok(naive_datetime) = chrono::NaiveDateTime::parse_from_str(input, format) {
return Some(chrono::TimeZone::from_utc_datetime(
&chrono::Utc,
&naive_datetime,
));
}
if let Ok(datetime) = chrono::DateTime::parse_from_rfc3339(input) {
return Some(datetime.with_timezone(&chrono::Utc));
}
}
None
}
fn main() {
let arg = args::get_args();
@ -171,8 +208,46 @@ fn main() {
let title: &String = new_task_arg.get_one("title").unwrap();
let project: &String = new_task_arg.get_one("project").unwrap();
let project = ProjectID::parse(&api, project).unwrap();
let task = api.new_task(title.as_str(), &project);
ui::task::print_task_info(task.id, &api);
let description: Option<String> = new_task_arg
.get_one::<String>("description")
.map(std::borrow::ToOwned::to_owned);
let due_date: Option<String> = new_task_arg
.get_one::<String>("due")
.map(std::borrow::ToOwned::to_owned);
let due_date = due_date.map(|x| {
if let Some(parsed) = parse_datetime(&x) {
parsed.to_rfc3339()
} else {
print_color(crossterm::style::Color::Red, "Failed to parse due date");
println!();
std::process::exit(1);
}
});
let label: Option<String> = new_task_arg
.get_one::<String>("label")
.map(std::borrow::ToOwned::to_owned);
let priority: Option<String> = new_task_arg
.get_one::<String>("priority")
.map(std::borrow::ToOwned::to_owned);
let fav = new_task_arg.get_flag("favorite");
// todo : add args
let task = api.new_task(
title.as_str(),
&project,
description,
due_date,
fav,
label,
priority.map(|x| x.parse().unwrap()),
);
if let Err(msg) = task {
print_color(crossterm::style::Color::Red, &msg);
println!();
std::process::exit(1);
} else {
ui::task::print_task_info(task.unwrap().id, &api);
}
}
Some(("done", done_args)) => {
let task_id: &String = done_args.get_one("task_id").unwrap();