Skip to content

Commit

Permalink
πŸ“ Apply project and task edits
Browse files Browse the repository at this point in the history
  • Loading branch information
shantanuraj committed Dec 11, 2024
1 parent 3b2a552 commit a41bbb1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 32 deletions.
41 changes: 34 additions & 7 deletions src/commands/edit.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::api::client::ApiClient;
use crate::models;
use crate::constants::DEFAULT_ENTITY_ID;
use crate::models::{self, TimeEntry};
use crate::parcel::Parcel;
use crate::picker;
use colored::Colorize;
Expand All @@ -18,12 +19,38 @@ impl EditCommand {
match entities.running_time_entry() {
None => println!("{}", "No time entry is running at the moment".yellow()),
Some(running_time_entry) => {
let updated_time_entry = running_time_entry
.launch_in_editor()
.inspect_err(|e| {
eprintln!("{}", e.to_string().red());
})
.unwrap();
let updated_time_entry = {
let workspace_id = running_time_entry.workspace_id;

let new_entry = running_time_entry
.launch_in_editor()
.inspect_err(|e| {
eprintln!("{}", e.to_string().red());
})
.unwrap();

let project = new_entry.project.and_then(|project| {
if project.id == DEFAULT_ENTITY_ID {
entities.project_for_name(workspace_id, &project.name)
} else {
Some(project)
}
});

let task = new_entry.task.and_then(|task| {
if task.id == DEFAULT_ENTITY_ID {
entities.task_for_name(workspace_id, &task.name)
} else {
Some(task)
}
});

TimeEntry {
project,
task,
..new_entry
}
};

let updated_entry_id = api_client
.update_time_entry(updated_time_entry.clone())
Expand Down
33 changes: 8 additions & 25 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,11 +367,11 @@ impl Parcel for TimeEntry {
}

if let Some(project) = &self.project {
serialized.push_str(&format!("Project: {} -- {}\n\n", project.name, project.id));
serialized.push_str(&format!("Project: {}\n\n", project.name));
}

if let Some(task) = &self.task {
serialized.push_str(&format!("Task: {} -- {}\n\n", task.name, task.id));
serialized.push_str(&format!("Task: {}\n\n", task.name));
}

serialized.into_bytes()
Expand All @@ -388,44 +388,27 @@ impl Parcel for TimeEntry {
}
let mut parts = line.splitn(2, ": ");
let key = parts.next().unwrap();
let value = parts.next().unwrap_or("NOT FOUND");

let value = parts.next().map(|v| v.trim()).unwrap_or("NOT FOUND");

match key {
"Start" => time_entry.start = value.parse().unwrap(),
"Stop" => time_entry.stop = Some(value.parse().unwrap()),
"Billable" => time_entry.billable = value.parse().unwrap(),
"Tags" => time_entry.tags = value.split(", ").map(String::from).collect(),
"Project" => {
let project_parts: Vec<&str> = value.split(" -- ").collect();
if project_parts.is_empty() {
continue;
}
let project_name = project_parts[0].to_string();
let project_id = if project_parts.len() > 1 {
project_parts[1].parse().unwrap()
} else {
constants::DEFAULT_ENTITY_ID
};
let project_name = value.to_string();
time_entry.project = Some(Project {
id: project_id,
id: constants::DEFAULT_ENTITY_ID,
name: project_name,
workspace_id: time_entry.workspace_id,
..Project::default()
});
}
"Task" => {
let task_parts: Vec<&str> = value.split(" -- ").collect();
if task_parts.is_empty() {
continue;
}
let task_name = task_parts[0].to_string();
let task_id = if task_parts.len() > 1 {
task_parts[1].parse().unwrap()
} else {
constants::DEFAULT_ENTITY_ID
};
let task_name = value.to_string();
time_entry.task = Some(Task {
id: task_id,
id: constants::DEFAULT_ENTITY_ID,
name: task_name,
workspace_id: time_entry.workspace_id,
project: time_entry.project.clone().unwrap_or_default(),
Expand Down

0 comments on commit a41bbb1

Please sign in to comment.