| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#![allow(clippy::needless_pass_by_value)] |
| 33 |
|
| 34 |
use chrono::{DateTime, Local, Utc}; |
| 35 |
use goingson_core::{ |
| 36 |
Annotation, DbValue as _, LinkedTaskRef, Priority, Recurrence, Subtask, Task, TaskId, |
| 37 |
TaskStatus, TimeSession, UpdateTask, |
| 38 |
}; |
| 39 |
use quasi_declare::declare; |
| 40 |
use quasi_router::screen::{Choice, Figure, Tag}; |
| 41 |
use quasi_router::{Action, RegionKind, Response, RouteError, Router}; |
| 42 |
|
| 43 |
use super::parse_optional_id; |
| 44 |
use crate::commands::{StreakInfo, compute_streak}; |
| 45 |
use crate::state::{AppState, DESKTOP_USER_ID}; |
| 46 |
|
| 47 |
#[cfg(test)] |
| 48 |
mod tests; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
const fn status_tone(status: &TaskStatus) -> makeover_layout::Tone { |
| 56 |
match status { |
| 57 |
TaskStatus::Completed => makeover_layout::Tone::Success, |
| 58 |
TaskStatus::Started => makeover_layout::Tone::Info, |
| 59 |
TaskStatus::Pending | TaskStatus::Deleted => makeover_layout::Tone::Neutral, |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
const fn priority_tone(priority: &Priority) -> makeover_layout::Tone { |
| 68 |
match priority { |
| 69 |
Priority::High => makeover_layout::Tone::Danger, |
| 70 |
Priority::Medium => makeover_layout::Tone::Warning, |
| 71 |
Priority::Low => makeover_layout::Tone::Neutral, |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
fn short_date(at: DateTime<Utc>) -> String { |
| 81 |
at.with_timezone(&Local).format("%b %-d").to_string() |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
fn task_id(request: &quasi_router::Request) -> Result<TaskId, RouteError> { |
| 86 |
let raw = request |
| 87 |
.captures |
| 88 |
.get("id") |
| 89 |
.ok_or_else(|| RouteError::not_found("no task id"))?; |
| 90 |
Ok(TaskId::from( |
| 91 |
uuid::Uuid::parse_str(raw).map_err(|_| RouteError::not_found("not a task id"))?, |
| 92 |
)) |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
fn load(state: &AppState, id: TaskId) -> Result<Task, RouteError> { |
| 105 |
let task = state |
| 106 |
.tasks |
| 107 |
.get_by_id(id, DESKTOP_USER_ID) |
| 108 |
.map_err(|error| RouteError::internal(error.to_string()))? |
| 109 |
.filter(|task| task.status != TaskStatus::Deleted) |
| 110 |
.ok_or_else(|| RouteError::not_found("no such task"))?; |
| 111 |
Ok(task) |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
fn streak_for(state: &AppState, task: &Task) -> Result<Option<StreakInfo>, RouteError> { |
| 122 |
if !task.has_recurrence() && task.recurrence_parent_id.is_none() { |
| 123 |
return Ok(None); |
| 124 |
} |
| 125 |
let root = task.recurrence_parent_id.unwrap_or(task.id); |
| 126 |
let chain = state |
| 127 |
.tasks |
| 128 |
.list_recurrence_chain(root, DESKTOP_USER_ID) |
| 129 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 130 |
Ok(Some(compute_streak(&chain))) |
| 131 |
} |
| 132 |
|
| 133 |
declare! { |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
shape habit_figures(streak: &StreakInfo) -> Node; |
| 145 |
|
| 146 |
stats [] { |
| 147 |
figure Figure::new("{streak.current_streak}d", "Current Streak"); |
| 148 |
figure Figure::new("{streak.best_streak}d", "Best Streak"); |
| 149 |
figure Figure::new("{completion_rate(streak)}%", "Completion Rate"); |
| 150 |
figure Figure::new( |
| 151 |
"{streak.total_completed}/{streak.total_instances}", |
| 152 |
"Total Completed" |
| 153 |
); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
fn completion_rate(streak: &StreakInfo) -> i64 { |
| 159 |
streak.completion_rate_30d.round() as i64 |
| 160 |
} |
| 161 |
|
| 162 |
declare! { |
| 163 |
|
| 164 |
shape habit_section(streak: &StreakInfo, task: TaskId) -> Vec<Node>; |
| 165 |
|
| 166 |
section "Completion History"; |
| 167 |
include habit_figures(streak); |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
region "task-heatmap-{task}" as RegionKind::ceded("task-heatmap") {} |
| 179 |
} |
| 180 |
|
| 181 |
declare! { |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
shape badges(task: &Task) -> Vec<Node>; |
| 187 |
|
| 188 |
badge task.status.as_str() { |
| 189 |
tone status_tone(&task.status); |
| 190 |
} |
| 191 |
|
| 192 |
badge task.priority.as_str() { |
| 193 |
tone priority_tone(&task.priority); |
| 194 |
} |
| 195 |
|
| 196 |
badge "Focus" when task.is_focus { |
| 197 |
tone Info; |
| 198 |
} |
| 199 |
|
| 200 |
badge "Overdue" when task.is_overdue() { |
| 201 |
tone Danger; |
| 202 |
} |
| 203 |
|
| 204 |
badge "Snoozed" when task.is_snoozed() { |
| 205 |
tone Warning; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
declare! { |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
shape metadata(task: &Task) -> Vec<Node>; |
| 228 |
|
| 229 |
extend badges(task); |
| 230 |
|
| 231 |
rich &task.description unless task.description.is_empty(); |
| 232 |
|
| 233 |
list { |
| 234 |
for project in task.project_name.iter() { |
| 235 |
row "Project" { |
| 236 |
meta project; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
row "Due" when task.due.is_some() { |
| 241 |
meta task.due_formatted(); |
| 242 |
} |
| 243 |
|
| 244 |
row "Recurrence" when task.has_recurrence() { |
| 245 |
meta task.recurrence.as_str(); |
| 246 |
} |
| 247 |
|
| 248 |
for contact in task.contact_name.iter() { |
| 249 |
row "Contact" { |
| 250 |
meta contact; |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
row "Tags" unless task.tags.is_empty() { |
| 257 |
for tag in task.tags.iter() { |
| 258 |
token Tag::badge(tag); |
| 259 |
} |
| 260 |
} |
| 261 |
} unless no_details(task); |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
fn no_details(task: &Task) -> bool { |
| 266 |
task.project_name.is_none() |
| 267 |
&& task.due.is_none() |
| 268 |
&& !task.has_recurrence() |
| 269 |
&& task.contact_name.is_none() |
| 270 |
&& task.tags.is_empty() |
| 271 |
} |
| 272 |
|
| 273 |
declare! { |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
shape subtask_row(task: TaskId, subtask: &Subtask) -> Row; |
| 293 |
|
| 294 |
row &subtask.text { |
| 295 |
toggling subtask.is_completed |
| 296 |
Action::post("/tasks/{task}/subtasks/{subtask.id}/toggle") |
| 297 |
unless is_linked(subtask); |
| 298 |
|
| 299 |
selectable subtask.is_completed when is_linked(subtask); |
| 300 |
token Tag::badge("Linked") when is_linked(subtask); |
| 301 |
|
| 302 |
act undo_or_done(subtask) |
| 303 |
to post "/tasks/{task}/subtasks/{subtask.id}/toggle" |
| 304 |
when is_linked(subtask) { |
| 305 |
disabled; |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
fn is_linked(subtask: &Subtask) -> bool { |
| 312 |
subtask.linked_task_id.is_some() |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
fn undo_or_done(subtask: &Subtask) -> &'static str { |
| 317 |
if subtask.is_completed { "Undo" } else { "Done" } |
| 318 |
} |
| 319 |
|
| 320 |
declare! { |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
shape subtasks_section(task: &Task) -> Vec<Node>; |
| 335 |
|
| 336 |
section "Subtasks {task.subtasks_completed()}/{task.subtask_count()}"; |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
proportion counted(task.subtasks_completed()) counted(task.subtask_count()) |
| 341 |
unless task.subtasks.is_empty() { |
| 342 |
tone Success; |
| 343 |
label "subtasks"; |
| 344 |
} |
| 345 |
|
| 346 |
list { |
| 347 |
for subtask in task.subtasks.iter() { |
| 348 |
include subtask_row(task.id, subtask); |
| 349 |
} |
| 350 |
} unless task.subtasks.is_empty(); |
| 351 |
|
| 352 |
form post "/tasks/{task.id}/subtasks" { |
| 353 |
submit "Add"; |
| 354 |
|
| 355 |
field Text "text" "Subtask" { |
| 356 |
required; |
| 357 |
placeholder "Add subtask..."; |
| 358 |
} |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
fn counted(n: usize) -> u32 { |
| 364 |
u32::try_from(n).unwrap_or(u32::MAX) |
| 365 |
} |
| 366 |
|
| 367 |
declare! { |
| 368 |
|
| 369 |
shape session_row(session: &TimeSession) -> Row; |
| 370 |
|
| 371 |
row when_started(session) { |
| 372 |
|
| 373 |
|
| 374 |
meta ran_for(session); |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
fn when_started(session: &TimeSession) -> String { |
| 380 |
format!( |
| 381 |
"{} {}", |
| 382 |
short_date(session.started_at), |
| 383 |
session.started_at.with_timezone(&Local).format("%-I:%M %p") |
| 384 |
) |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
fn ran_for(session: &TimeSession) -> String { |
| 389 |
session |
| 390 |
.duration_minutes |
| 391 |
.map_or_else(|| "active".to_owned(), |minutes| format!("{minutes}m")) |
| 392 |
} |
| 393 |
|
| 394 |
declare! { |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
shape time_section(task: &Task, sessions: &[TimeSession]) -> Vec<Node>; |
| 415 |
|
| 416 |
section "Time Tracking {time_label(task)}"; |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
proportion counted_i32(task.actual_minutes) counted_i32(estimate(task)) |
| 421 |
when estimate(task) over 0 { |
| 422 |
tone over_estimate(task); |
| 423 |
label "minutes"; |
| 424 |
} |
| 425 |
|
| 426 |
list { |
| 427 |
for session in sessions.iter() { |
| 428 |
include session_row(session); |
| 429 |
} |
| 430 |
} unless sessions.is_empty(); |
| 431 |
|
| 432 |
act "Track time" to post "/timer/start" with "task" task.id.to_string() |
| 433 |
unless task.has_active_timer(); |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
fn estimate(task: &Task) -> i32 { |
| 438 |
task.estimated_minutes.unwrap_or(0) |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
fn time_label(task: &Task) -> String { |
| 443 |
let tracked = format!("{}m tracked", task.actual_minutes); |
| 444 |
if estimate(task) > 0 { |
| 445 |
let over = if task.is_over_estimate() { |
| 446 |
", over" |
| 447 |
} else { |
| 448 |
"" |
| 449 |
}; |
| 450 |
format!("{tracked} / {}m est{over}", estimate(task)) |
| 451 |
} else { |
| 452 |
tracked |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
fn over_estimate(task: &Task) -> makeover_layout::Tone { |
| 458 |
if task.is_over_estimate() { |
| 459 |
makeover_layout::Tone::Danger |
| 460 |
} else { |
| 461 |
makeover_layout::Tone::Success |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
fn counted_i32(minutes: i32) -> u32 { |
| 467 |
u32::try_from(minutes).unwrap_or(u32::MAX) |
| 468 |
} |
| 469 |
|
| 470 |
declare! { |
| 471 |
|
| 472 |
shape annotation_row(annotation: &Annotation) -> Row; |
| 473 |
|
| 474 |
row &annotation.note { |
| 475 |
meta noted_at(annotation); |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
fn noted_at(annotation: &Annotation) -> String { |
| 481 |
format!( |
| 482 |
"{} {}", |
| 483 |
short_date(annotation.timestamp), |
| 484 |
annotation |
| 485 |
.timestamp |
| 486 |
.with_timezone(&Local) |
| 487 |
.format("%-I:%M %p") |
| 488 |
) |
| 489 |
} |
| 490 |
|
| 491 |
declare! { |
| 492 |
|
| 493 |
shape notes_section(task: &Task) -> Vec<Node>; |
| 494 |
|
| 495 |
section "Notes {task.annotations.len()}"; |
| 496 |
|
| 497 |
list { |
| 498 |
for annotation in task.annotations.iter() { |
| 499 |
include annotation_row(annotation); |
| 500 |
} |
| 501 |
} unless task.annotations.is_empty(); |
| 502 |
|
| 503 |
form post "/tasks/{task.id}/notes" { |
| 504 |
submit "Add"; |
| 505 |
|
| 506 |
field Text "note" "Note" { |
| 507 |
required; |
| 508 |
placeholder "Add note..."; |
| 509 |
} |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
#[derive(Clone, Copy, PartialEq, Eq)] |
| 521 |
enum Role { |
| 522 |
|
| 523 |
Blocker, |
| 524 |
|
| 525 |
Dependent, |
| 526 |
} |
| 527 |
|
| 528 |
impl Role { |
| 529 |
const fn as_str(self) -> &'static str { |
| 530 |
match self { |
| 531 |
Self::Blocker => "blocker", |
| 532 |
Self::Dependent => "dependent", |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
fn from_payload(raw: &str) -> Result<Self, RouteError> { |
| 537 |
match raw { |
| 538 |
"blocker" => Ok(Self::Blocker), |
| 539 |
"dependent" => Ok(Self::Dependent), |
| 540 |
_ => Err(RouteError::not_found("no such side of an edge")), |
| 541 |
} |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
declare! { |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
shape dependency_row(viewed: TaskId, entry: &LinkedTaskRef, role: Role) -> Row; |
| 554 |
|
| 555 |
row &entry.title { |
| 556 |
token Tag::badge(entry.status.as_str()).tone(edge_tone(entry)); |
| 557 |
|
| 558 |
for project in entry.project_name.iter() { |
| 559 |
meta project; |
| 560 |
} |
| 561 |
|
| 562 |
act "Remove" |
| 563 |
to post "/tasks/{viewed}/dependencies/{entry.id}/remove" |
| 564 |
with "role" role.as_str(); |
| 565 |
|
| 566 |
activate to get "/tasks/{entry.id}"; |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
fn edge_tone(entry: &LinkedTaskRef) -> makeover_layout::Tone { |
| 573 |
if entry.is_satisfied() { |
| 574 |
makeover_layout::Tone::Neutral |
| 575 |
} else { |
| 576 |
status_tone(&entry.status) |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
enum Standing { |
| 587 |
|
| 588 |
Cycle, |
| 589 |
|
| 590 |
Blocked, |
| 591 |
|
| 592 |
Ready, |
| 593 |
} |
| 594 |
|
| 595 |
impl Standing { |
| 596 |
|
| 597 |
fn of(task: &Task) -> Self { |
| 598 |
if task.graph.in_cycle { |
| 599 |
Self::Cycle |
| 600 |
} else if task.is_blocked() { |
| 601 |
Self::Blocked |
| 602 |
} else { |
| 603 |
Self::Ready |
| 604 |
} |
| 605 |
} |
| 606 |
} |
| 607 |
|
| 608 |
|
| 609 |
fn blocked_word(task: &Task) -> String { |
| 610 |
if task.graph.block_depth == 1 { |
| 611 |
"Blocked, 1 step away".to_owned() |
| 612 |
} else { |
| 613 |
format!("Blocked, {} steps away", task.graph.block_depth) |
| 614 |
} |
| 615 |
} |
| 616 |
|
| 617 |
|
| 618 |
fn unblocks_word(task: &Task) -> String { |
| 619 |
let n = task.graph.unblocks_count; |
| 620 |
format!("unblocks {n} task{}", if n == 1 { "" } else { "s" }) |
| 621 |
} |
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
fn no_edges(dependencies: &Dependencies) -> bool { |
| 626 |
dependencies.blockers.is_empty() && dependencies.dependents.is_empty() |
| 627 |
} |
| 628 |
|
| 629 |
|
| 630 |
struct Dependencies { |
| 631 |
|
| 632 |
blockers: Vec<LinkedTaskRef>, |
| 633 |
|
| 634 |
dependents: Vec<LinkedTaskRef>, |
| 635 |
|
| 636 |
candidates: Vec<Choice>, |
| 637 |
} |
| 638 |
|
| 639 |
declare! { |
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
shape dependencies_section(task: &Task, dependencies: &Dependencies) -> Vec<Node>; |
| 655 |
|
| 656 |
section "Dependencies"; |
| 657 |
|
| 658 |
given Standing::of(task) { |
| 659 |
Standing::Cycle -> badge "In a cycle" { tone Danger; } |
| 660 |
Standing::Blocked -> badge blocked_word(task) { tone Warning; } |
| 661 |
otherwise -> badge "Ready" { tone Success; } |
| 662 |
} |
| 663 |
|
| 664 |
text unblocks_word(task) when task.graph.unblocks_count over 0; |
| 665 |
|
| 666 |
toned "This task sits on a dependency cycle, so it can never become available. Remove \ |
| 667 |
one of the edges below to break it." |
| 668 |
makeover_layout::Tone::Danger |
| 669 |
when task.graph.in_cycle; |
| 670 |
|
| 671 |
subsection "Blocked by" unless dependencies.blockers.is_empty(); |
| 672 |
|
| 673 |
list { |
| 674 |
for entry in dependencies.blockers.iter() { |
| 675 |
include dependency_row(task.id, entry, Role::Blocker); |
| 676 |
} |
| 677 |
} unless dependencies.blockers.is_empty(); |
| 678 |
|
| 679 |
subsection "Blocks" unless dependencies.dependents.is_empty(); |
| 680 |
|
| 681 |
list { |
| 682 |
for entry in dependencies.dependents.iter() { |
| 683 |
include dependency_row(task.id, entry, Role::Dependent); |
| 684 |
} |
| 685 |
} unless dependencies.dependents.is_empty(); |
| 686 |
|
| 687 |
text "Nothing blocks this task and nothing waits on it." when no_edges(dependencies); |
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
form post "/tasks/{task.id}/blockers" |
| 693 |
when task.status is_not TaskStatus::Completed |
| 694 |
and not dependencies.candidates.is_empty() { |
| 695 |
submit "Add blocker"; |
| 696 |
|
| 697 |
field Select "blocker" "Must be completed first" { |
| 698 |
options dependencies.candidates.clone(); |
| 699 |
hint "This task stays unavailable until that one is done."; |
| 700 |
} |
| 701 |
} |
| 702 |
} |
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
fn blocker_candidates( |
| 710 |
state: &AppState, |
| 711 |
task: &Task, |
| 712 |
blockers: &[LinkedTaskRef], |
| 713 |
) -> Result<Vec<Choice>, RouteError> { |
| 714 |
let already: std::collections::HashSet<TaskId> = blockers.iter().map(|b| b.id).collect(); |
| 715 |
Ok(state |
| 716 |
.tasks |
| 717 |
.list_all(DESKTOP_USER_ID) |
| 718 |
.map_err(|error| RouteError::internal(error.to_string()))? |
| 719 |
.into_iter() |
| 720 |
.filter(|other| other.id != task.id && !already.contains(&other.id)) |
| 721 |
.map(|other| { |
| 722 |
let label = match &other.project_name { |
| 723 |
Some(project) => format!("{} ({project})", other.title), |
| 724 |
None => other.title.clone(), |
| 725 |
}; |
| 726 |
Choice::new(other.id.to_string(), label) |
| 727 |
}) |
| 728 |
.collect()) |
| 729 |
} |
| 730 |
|
| 731 |
|
| 732 |
struct Drawer { |
| 733 |
|
| 734 |
task: Task, |
| 735 |
|
| 736 |
sessions: Vec<TimeSession>, |
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
streak: Option<StreakInfo>, |
| 741 |
|
| 742 |
dependencies: Dependencies, |
| 743 |
} |
| 744 |
|
| 745 |
|
| 746 |
fn drawer(state: &AppState, id: TaskId) -> Result<Drawer, RouteError> { |
| 747 |
let task = load(state, id)?; |
| 748 |
let blockers = state |
| 749 |
.tasks |
| 750 |
.list_blockers(DESKTOP_USER_ID, id) |
| 751 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 752 |
Ok(Drawer { |
| 753 |
sessions: state |
| 754 |
.tasks |
| 755 |
.list_time_sessions(id, DESKTOP_USER_ID) |
| 756 |
.map_err(|error| RouteError::internal(error.to_string()))?, |
| 757 |
streak: streak_for(state, &task)?, |
| 758 |
dependencies: Dependencies { |
| 759 |
candidates: blocker_candidates(state, &task, &blockers)?, |
| 760 |
dependents: state |
| 761 |
.tasks |
| 762 |
.list_dependents(DESKTOP_USER_ID, id) |
| 763 |
.map_err(|error| RouteError::internal(error.to_string()))?, |
| 764 |
blockers, |
| 765 |
}, |
| 766 |
task, |
| 767 |
}) |
| 768 |
} |
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
fn offers_subtasks(task: &Task) -> bool { |
| 775 |
!task.subtasks.is_empty() || task.status != TaskStatus::Completed |
| 776 |
} |
| 777 |
|
| 778 |
declare! { |
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
shape screen(drawer: &Drawer) -> Screen; |
| 795 |
|
| 796 |
screen list_detail "Task" false { |
| 797 |
at_place super::shell::TASKS; |
| 798 |
|
| 799 |
region "task-band" as Band { |
| 800 |
page &drawer.task.title; |
| 801 |
|
| 802 |
act "Edit" to get "/tasks/{drawer.task.id}/edit"; |
| 803 |
act "Complete" to post "/tasks/{drawer.task.id}/complete" |
| 804 |
when drawer.task.status is_not TaskStatus::Completed; |
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
act "Delete" to post "/tasks/{drawer.task.id}/delete" { |
| 812 |
tone Danger; |
| 813 |
confirm "Are you sure you want to delete this task? This cannot be undone."; |
| 814 |
} |
| 815 |
} |
| 816 |
|
| 817 |
region "task-overview" as Pane { |
| 818 |
for streak in drawer.streak.iter() { |
| 819 |
extend habit_section(streak, drawer.task.id); |
| 820 |
} |
| 821 |
|
| 822 |
extend metadata(&drawer.task); |
| 823 |
extend subtasks_section(&drawer.task) when offers_subtasks(&drawer.task); |
| 824 |
extend dependencies_section(&drawer.task, &drawer.dependencies); |
| 825 |
extend time_section(&drawer.task, &drawer.sessions); |
| 826 |
extend notes_section(&drawer.task); |
| 827 |
} |
| 828 |
} |
| 829 |
} |
| 830 |
|
| 831 |
|
| 832 |
fn overview(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 833 |
Ok(screen(&drawer(state, task_id(&request)?)?).into()) |
| 834 |
} |
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
const EDIT_STATUSES: [&str; 3] = ["Pending", "Started", "Completed"]; |
| 842 |
|
| 843 |
|
| 844 |
const EDIT_PRIORITIES: [&str; 3] = ["Low", "Medium", "High"]; |
| 845 |
|
| 846 |
|
| 847 |
const EDIT_RECURRENCES: [&str; 4] = ["None", "Daily", "Weekly", "Monthly"]; |
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
fn due_value(task: &Task) -> String { |
| 856 |
task.due |
| 857 |
.map(|due| { |
| 858 |
due.with_timezone(&Local) |
| 859 |
.format("%Y-%m-%dT%H:%M") |
| 860 |
.to_string() |
| 861 |
}) |
| 862 |
.unwrap_or_default() |
| 863 |
} |
| 864 |
|
| 865 |
|
| 866 |
struct Editing { |
| 867 |
|
| 868 |
task: Task, |
| 869 |
|
| 870 |
projects: Vec<Choice>, |
| 871 |
|
| 872 |
contacts: Vec<Choice>, |
| 873 |
|
| 874 |
milestones: Vec<Choice>, |
| 875 |
|
| 876 |
errors: Vec<(String, String)>, |
| 877 |
|
| 878 |
submitted: quasi_router::Params, |
| 879 |
} |
| 880 |
|
| 881 |
|
| 882 |
fn editing( |
| 883 |
state: &AppState, |
| 884 |
task: Task, |
| 885 |
errors: &[(&str, String)], |
| 886 |
submitted: Option<&quasi_router::Params>, |
| 887 |
) -> Result<Editing, RouteError> { |
| 888 |
let offered = |none: &str, values: Vec<(String, String)>| -> Vec<Choice> { |
| 889 |
let mut options = vec![Choice::new("", none)]; |
| 890 |
options.extend(values.into_iter().map(|(id, label)| Choice::new(id, label))); |
| 891 |
options |
| 892 |
}; |
| 893 |
|
| 894 |
let projects = state |
| 895 |
.projects |
| 896 |
.list_all(DESKTOP_USER_ID) |
| 897 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 898 |
let contacts = state |
| 899 |
.contacts |
| 900 |
.list_all(DESKTOP_USER_ID) |
| 901 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 902 |
let milestones = match task.project_id { |
| 903 |
Some(project_id) => state |
| 904 |
.milestones |
| 905 |
.list_by_project(project_id, DESKTOP_USER_ID) |
| 906 |
.map_err(|error| RouteError::internal(error.to_string()))?, |
| 907 |
None => Vec::new(), |
| 908 |
}; |
| 909 |
|
| 910 |
Ok(Editing { |
| 911 |
projects: offered( |
| 912 |
"No Project", |
| 913 |
projects |
| 914 |
.into_iter() |
| 915 |
.map(|project| (project.id.to_string(), project.name)) |
| 916 |
.collect(), |
| 917 |
), |
| 918 |
contacts: offered( |
| 919 |
"No Contact", |
| 920 |
contacts |
| 921 |
.into_iter() |
| 922 |
.map(|contact| (contact.id.to_string(), contact.display_name)) |
| 923 |
.collect(), |
| 924 |
), |
| 925 |
milestones: offered( |
| 926 |
"No Milestone", |
| 927 |
milestones |
| 928 |
.into_iter() |
| 929 |
.map(|milestone| (milestone.id.to_string(), milestone.name)) |
| 930 |
.collect(), |
| 931 |
), |
| 932 |
errors: errors |
| 933 |
.iter() |
| 934 |
.map(|(field, message)| ((*field).to_owned(), message.clone())) |
| 935 |
.collect(), |
| 936 |
|
| 937 |
|
| 938 |
submitted: submitted.cloned().unwrap_or_default(), |
| 939 |
task, |
| 940 |
}) |
| 941 |
} |
| 942 |
|
| 943 |
impl Editing { |
| 944 |
|
| 945 |
fn refused(&self, name: &str) -> bool { |
| 946 |
self.errors.iter().any(|(field, _)| field == name) |
| 947 |
} |
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
fn refusal(&self, name: &str) -> String { |
| 952 |
self.errors |
| 953 |
.iter() |
| 954 |
.find(|(field, _)| field == name) |
| 955 |
.map_or_else(String::new, |(_, message)| message.clone()) |
| 956 |
} |
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
fn id_or_none(id: Option<impl ToString>) -> String { |
| 961 |
id.map(|id| id.to_string()).unwrap_or_default() |
| 962 |
} |
| 963 |
|
| 964 |
|
| 965 |
fn estimate_box(&self) -> String { |
| 966 |
Self::id_or_none(self.task.estimated_minutes) |
| 967 |
} |
| 968 |
} |
| 969 |
|
| 970 |
declare! { |
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
shape edit_screen(editing: &Editing) -> Screen; |
| 978 |
|
| 979 |
screen list_detail "Edit task" false { |
| 980 |
at_place super::shell::TASKS; |
| 981 |
|
| 982 |
region "task-band" as Band { |
| 983 |
page "Edit {editing.task.title}"; |
| 984 |
act "Cancel" to get "/tasks/{editing.task.id}"; |
| 985 |
} |
| 986 |
|
| 987 |
region "task-overview" as Pane { |
| 988 |
form post "/tasks/{editing.task.id}" { |
| 989 |
submit "Save task"; |
| 990 |
|
| 991 |
field Text "title" "Title" { |
| 992 |
required; |
| 993 |
value &editing.task.title; |
| 994 |
placeholder "What needs to be done?"; |
| 995 |
error editing.refusal("title") when editing.refused("title"); |
| 996 |
refilled &editing.submitted; |
| 997 |
} |
| 998 |
|
| 999 |
field Textarea "description" "Details" { |
| 1000 |
value &editing.task.description; |
| 1001 |
placeholder "Anything the title does not cover (optional)"; |
| 1002 |
error editing.refusal("description") when editing.refused("description"); |
| 1003 |
refilled &editing.submitted; |
| 1004 |
} |
| 1005 |
|
| 1006 |
field Select "project_id" "Project" { |
| 1007 |
options editing.projects.clone(); |
| 1008 |
value Editing::id_or_none(editing.task.project_id); |
| 1009 |
error editing.refusal("project_id") when editing.refused("project_id"); |
| 1010 |
refilled &editing.submitted; |
| 1011 |
} |
| 1012 |
|
| 1013 |
field Select "status" "Status" { |
| 1014 |
for status in EDIT_STATUSES { |
| 1015 |
option Choice::new(status, status); |
| 1016 |
} |
| 1017 |
value editing.task.status.as_str(); |
| 1018 |
error editing.refusal("status") when editing.refused("status"); |
| 1019 |
refilled &editing.submitted; |
| 1020 |
} |
| 1021 |
|
| 1022 |
field Select "priority" "Priority" { |
| 1023 |
for priority in EDIT_PRIORITIES { |
| 1024 |
option Choice::new(priority, priority); |
| 1025 |
} |
| 1026 |
value editing.task.priority.db_value(); |
| 1027 |
error editing.refusal("priority") when editing.refused("priority"); |
| 1028 |
refilled &editing.submitted; |
| 1029 |
} |
| 1030 |
|
| 1031 |
field Text "due" "Due Date (optional)" { |
| 1032 |
value due_value(&editing.task); |
| 1033 |
placeholder "tomorrow, friday 3pm, 2026-12-25..."; |
| 1034 |
error editing.refusal("due") when editing.refused("due"); |
| 1035 |
refilled &editing.submitted; |
| 1036 |
} |
| 1037 |
|
| 1038 |
field Text "tags" "Tags (comma-separated)" { |
| 1039 |
value editing.task.tags.join(", "); |
| 1040 |
placeholder "work, urgent, meeting"; |
| 1041 |
error editing.refusal("tags") when editing.refused("tags"); |
| 1042 |
refilled &editing.submitted; |
| 1043 |
} |
| 1044 |
|
| 1045 |
field Select "recurrence" "Recurrence" { |
| 1046 |
for pattern in EDIT_RECURRENCES { |
| 1047 |
option Choice::new(pattern, pattern); |
| 1048 |
} |
| 1049 |
value editing.task.recurrence.db_value(); |
| 1050 |
hint "Completing a recurring task auto-creates the next occurrence"; |
| 1051 |
error editing.refusal("recurrence") when editing.refused("recurrence"); |
| 1052 |
refilled &editing.submitted; |
| 1053 |
} |
| 1054 |
|
| 1055 |
field Number "estimated_minutes" "Estimated Time (minutes)" { |
| 1056 |
value editing.estimate_box(); |
| 1057 |
placeholder "e.g. 30, 60, 120"; |
| 1058 |
hint "Used for day plan scheduling and time tracking progress"; |
| 1059 |
error editing.refusal("estimated_minutes") |
| 1060 |
when editing.refused("estimated_minutes"); |
| 1061 |
refilled &editing.submitted; |
| 1062 |
} |
| 1063 |
|
| 1064 |
field Select "contact_id" "Contact" { |
| 1065 |
options editing.contacts.clone(); |
| 1066 |
value Editing::id_or_none(editing.task.contact_id); |
| 1067 |
error editing.refusal("contact_id") when editing.refused("contact_id"); |
| 1068 |
refilled &editing.submitted; |
| 1069 |
} |
| 1070 |
|
| 1071 |
field Select "milestone_id" "Milestone" { |
| 1072 |
options editing.milestones.clone(); |
| 1073 |
value Editing::id_or_none(editing.task.milestone_id); |
| 1074 |
hint "Group tasks into project phases; milestones are managed per project"; |
| 1075 |
error editing.refusal("milestone_id") when editing.refused("milestone_id"); |
| 1076 |
refilled &editing.submitted; |
| 1077 |
} |
| 1078 |
} |
| 1079 |
} |
| 1080 |
} |
| 1081 |
} |
| 1082 |
|
| 1083 |
|
| 1084 |
fn edit(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 1085 |
let task = load(state, task_id(&request)?)?; |
| 1086 |
Ok(edit_screen(&editing(state, task, &[], None)?).into()) |
| 1087 |
} |
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
fn validate_edit(title: &str) -> Vec<(&'static str, String)> { |
| 1095 |
let mut errors = Vec::new(); |
| 1096 |
if title.is_empty() { |
| 1097 |
errors.push(("title", "A task needs a title.".to_owned())); |
| 1098 |
} else if title.chars().count() > 80 { |
| 1099 |
errors.push(("title", "Maximum 80 characters".to_owned())); |
| 1100 |
} |
| 1101 |
errors |
| 1102 |
} |
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
fn update(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 1114 |
let id = task_id(&request)?; |
| 1115 |
let task = load(state, id)?; |
| 1116 |
|
| 1117 |
let field = |name: &str| { |
| 1118 |
request |
| 1119 |
.payload |
| 1120 |
.get(name) |
| 1121 |
.unwrap_or_default() |
| 1122 |
.trim() |
| 1123 |
.to_owned() |
| 1124 |
}; |
| 1125 |
let title = field("title"); |
| 1126 |
let description = field("description"); |
| 1127 |
let mut errors = validate_edit(&title); |
| 1128 |
|
| 1129 |
let status = super::parse_choice::<TaskStatus>(&request.payload, "status", &mut errors) |
| 1130 |
.filter(|status| EDIT_STATUSES.contains(&status.as_str())); |
| 1131 |
if status.is_none() && !errors.iter().any(|(name, _)| *name == "status") { |
| 1132 |
errors.push(("status", "Not a status a control can set.".to_owned())); |
| 1133 |
} |
| 1134 |
let priority = super::parse_choice::<Priority>(&request.payload, "priority", &mut errors); |
| 1135 |
let recurrence = super::parse_choice::<Recurrence>(&request.payload, "recurrence", &mut errors); |
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
let raw_due = field("due"); |
| 1144 |
let due = if raw_due.is_empty() { |
| 1145 |
None |
| 1146 |
} else { |
| 1147 |
match goingson_core::parse_natural_date(&raw_due, Local::now().naive_local()) |
| 1148 |
.and_then(|when| when.and_local_timezone(Local).single()) |
| 1149 |
{ |
| 1150 |
Some(when) => Some(when.with_timezone(&Utc)), |
| 1151 |
None => { |
| 1152 |
errors.push(( |
| 1153 |
"due", |
| 1154 |
"Date not recognized. Try \"tomorrow\", \"friday 3pm\", or \"2026-12-25\"." |
| 1155 |
.to_owned(), |
| 1156 |
)); |
| 1157 |
None |
| 1158 |
} |
| 1159 |
} |
| 1160 |
}; |
| 1161 |
|
| 1162 |
let estimated_minutes = match field("estimated_minutes").as_str() { |
| 1163 |
"" => None, |
| 1164 |
raw => match raw.parse::<i32>() { |
| 1165 |
Ok(minutes) if minutes >= 0 => Some(minutes), |
| 1166 |
_ => { |
| 1167 |
errors.push(("estimated_minutes", "A number of minutes.".to_owned())); |
| 1168 |
None |
| 1169 |
} |
| 1170 |
}, |
| 1171 |
}; |
| 1172 |
|
| 1173 |
let project_id = parse_optional_id(&request.payload, "project_id", &mut errors); |
| 1174 |
let contact_id = parse_optional_id(&request.payload, "contact_id", &mut errors); |
| 1175 |
let milestone_id = parse_optional_id(&request.payload, "milestone_id", &mut errors); |
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
if milestone_id.flatten().is_some() && project_id.flatten() != task.project_id { |
| 1182 |
errors.push(( |
| 1183 |
"milestone_id", |
| 1184 |
"Move the task first, then file it under a milestone of its new project.".to_owned(), |
| 1185 |
)); |
| 1186 |
} |
| 1187 |
|
| 1188 |
let (Some(status), Some(priority), Some(recurrence)) = (status, priority, recurrence) else { |
| 1189 |
return Ok(edit_screen(&editing(state, task, &errors, Some(&request.payload))?).into()); |
| 1190 |
}; |
| 1191 |
let (Some(project_id), Some(contact_id), Some(milestone_id)) = |
| 1192 |
(project_id, contact_id, milestone_id) |
| 1193 |
else { |
| 1194 |
return Ok(edit_screen(&editing(state, task, &errors, Some(&request.payload))?).into()); |
| 1195 |
}; |
| 1196 |
if !errors.is_empty() { |
| 1197 |
return Ok(edit_screen(&editing(state, task, &errors, Some(&request.payload))?).into()); |
| 1198 |
} |
| 1199 |
|
| 1200 |
let tags: Vec<String> = field("tags") |
| 1201 |
.split(',') |
| 1202 |
.map(|tag| tag.trim().to_owned()) |
| 1203 |
.filter(|tag| !tag.is_empty()) |
| 1204 |
.collect(); |
| 1205 |
|
| 1206 |
let context = state |
| 1207 |
.tasks |
| 1208 |
.get_update_context(id, DESKTOP_USER_ID) |
| 1209 |
.map_err(|error| RouteError::internal(error.to_string()))? |
| 1210 |
.ok_or_else(|| RouteError::not_found("no such task"))?; |
| 1211 |
|
| 1212 |
state |
| 1213 |
.tasks |
| 1214 |
.update( |
| 1215 |
id, |
| 1216 |
DESKTOP_USER_ID, |
| 1217 |
UpdateTask { |
| 1218 |
project_id, |
| 1219 |
milestone_id, |
| 1220 |
contact_id, |
| 1221 |
urgency: goingson_core::calculate_urgency( |
| 1222 |
&priority, |
| 1223 |
&status, |
| 1224 |
due.as_ref(), |
| 1225 |
&context.created_at, |
| 1226 |
&tags, |
| 1227 |
), |
| 1228 |
title, |
| 1229 |
description, |
| 1230 |
status, |
| 1231 |
priority, |
| 1232 |
due, |
| 1233 |
tags, |
| 1234 |
recurrence, |
| 1235 |
|
| 1236 |
|
| 1237 |
recurrence_rule: task.recurrence_rule.clone(), |
| 1238 |
scheduled_start: context.scheduled_start, |
| 1239 |
scheduled_duration: context.scheduled_duration, |
| 1240 |
estimated_minutes, |
| 1241 |
}, |
| 1242 |
) |
| 1243 |
.map_err(|error| RouteError::internal(error.to_string()))? |
| 1244 |
.ok_or_else(|| RouteError::not_found("no such task"))?; |
| 1245 |
|
| 1246 |
Ok(wrote(state, id)?.toast(makeover_layout::Tone::Success, "Task saved")) |
| 1247 |
} |
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
fn wrote(state: &AppState, id: TaskId) -> Result<Response, RouteError> { |
| 1255 |
Ok(screen(&drawer(state, id)?).into()) |
| 1256 |
} |
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
fn complete(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 1265 |
let id = task_id(&request)?; |
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
let task = state |
| 1270 |
.tasks |
| 1271 |
.get_by_id(id, DESKTOP_USER_ID) |
| 1272 |
.map_err(|error| RouteError::internal(error.to_string()))? |
| 1273 |
.ok_or_else(|| RouteError::not_found("no such task"))?; |
| 1274 |
super::move_to(state, &task, &TaskStatus::Completed)?; |
| 1275 |
wrote(state, id) |
| 1276 |
} |
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
fn remove(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 1288 |
let id = task_id(&request)?; |
| 1289 |
let deleted = state |
| 1290 |
.tasks |
| 1291 |
.delete(id, DESKTOP_USER_ID) |
| 1292 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 1293 |
if !deleted { |
| 1294 |
return Err(RouteError::not_found("no such task")); |
| 1295 |
} |
| 1296 |
Ok(Response::goto(Action::get("/tasks")).toast(makeover_layout::Tone::Success, "Task deleted")) |
| 1297 |
} |
| 1298 |
|
| 1299 |
|
| 1300 |
fn add_subtask(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 1301 |
let id = task_id(&request)?; |
| 1302 |
let text = request.payload.get("text").unwrap_or_default().trim(); |
| 1303 |
|
| 1304 |
|
| 1305 |
if !text.is_empty() { |
| 1306 |
state |
| 1307 |
.tasks |
| 1308 |
.add_subtask(id, DESKTOP_USER_ID, text) |
| 1309 |
.map_err(|error| RouteError::internal(error.to_string()))? |
| 1310 |
.ok_or_else(|| RouteError::not_found("no such task"))?; |
| 1311 |
} |
| 1312 |
wrote(state, id) |
| 1313 |
} |
| 1314 |
|
| 1315 |
|
| 1316 |
fn toggle_subtask( |
| 1317 |
state: &AppState, |
| 1318 |
request: quasi_router::Request, |
| 1319 |
) -> Result<Response, RouteError> { |
| 1320 |
let id = task_id(&request)?; |
| 1321 |
let raw = request |
| 1322 |
.captures |
| 1323 |
.get("sub") |
| 1324 |
.ok_or_else(|| RouteError::not_found("no subtask id"))?; |
| 1325 |
let sub = goingson_core::SubtaskId::from( |
| 1326 |
uuid::Uuid::parse_str(raw).map_err(|_| RouteError::not_found("not a subtask id"))?, |
| 1327 |
); |
| 1328 |
state |
| 1329 |
.tasks |
| 1330 |
.toggle_subtask(sub, DESKTOP_USER_ID) |
| 1331 |
.map_err(|error| RouteError::internal(error.to_string()))? |
| 1332 |
.ok_or_else(|| RouteError::not_found("no such subtask"))?; |
| 1333 |
wrote(state, id) |
| 1334 |
} |
| 1335 |
|
| 1336 |
|
| 1337 |
fn add_note(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 1338 |
let id = task_id(&request)?; |
| 1339 |
let note = request.payload.get("note").unwrap_or_default().trim(); |
| 1340 |
if !note.is_empty() { |
| 1341 |
state |
| 1342 |
.tasks |
| 1343 |
.add_annotation(id, DESKTOP_USER_ID, note) |
| 1344 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 1345 |
} |
| 1346 |
wrote(state, id) |
| 1347 |
} |
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
fn add_blocker(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 1357 |
let id = task_id(&request)?; |
| 1358 |
let raw = request.payload.get("blocker").unwrap_or_default(); |
| 1359 |
|
| 1360 |
|
| 1361 |
if raw.trim().is_empty() { |
| 1362 |
return wrote(state, id); |
| 1363 |
} |
| 1364 |
let blocker = TaskId::from( |
| 1365 |
uuid::Uuid::parse_str(raw.trim()).map_err(|_| RouteError::not_found("not a task id"))?, |
| 1366 |
); |
| 1367 |
state |
| 1368 |
.tasks |
| 1369 |
.add_dependency(DESKTOP_USER_ID, id, blocker) |
| 1370 |
.map_err(|error| RouteError::conflict(error.to_string()).as_toast())?; |
| 1371 |
wrote(state, id) |
| 1372 |
} |
| 1373 |
|
| 1374 |
|
| 1375 |
fn remove_dependency( |
| 1376 |
state: &AppState, |
| 1377 |
request: quasi_router::Request, |
| 1378 |
) -> Result<Response, RouteError> { |
| 1379 |
let id = task_id(&request)?; |
| 1380 |
let raw = request |
| 1381 |
.captures |
| 1382 |
.get("other") |
| 1383 |
.ok_or_else(|| RouteError::not_found("no task id"))?; |
| 1384 |
let other = TaskId::from( |
| 1385 |
uuid::Uuid::parse_str(raw).map_err(|_| RouteError::not_found("not a task id"))?, |
| 1386 |
); |
| 1387 |
let (blocked, blocker) = |
| 1388 |
match Role::from_payload(request.payload.get("role").unwrap_or_default()) { |
| 1389 |
Ok(Role::Blocker) => (id, other), |
| 1390 |
Ok(Role::Dependent) => (other, id), |
| 1391 |
Err(error) => return Err(error), |
| 1392 |
}; |
| 1393 |
let removed = state |
| 1394 |
.tasks |
| 1395 |
.remove_dependency(DESKTOP_USER_ID, blocked, blocker) |
| 1396 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 1397 |
if !removed { |
| 1398 |
return Err(RouteError::not_found("no such dependency")); |
| 1399 |
} |
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
wrote(state, id) |
| 1404 |
} |
| 1405 |
|
| 1406 |
|
| 1407 |
#[must_use] |
| 1408 |
pub fn routes(router: Router<AppState>) -> Router<AppState> { |
| 1409 |
router |
| 1410 |
.get("/tasks/{id}/edit", edit) |
| 1411 |
.get("/tasks/{id}", overview) |
| 1412 |
.post("/tasks/{id}", update) |
| 1413 |
.post("/tasks/{id}/blockers", add_blocker) |
| 1414 |
.post("/tasks/{id}/dependencies/{other}/remove", remove_dependency) |
| 1415 |
.post("/tasks/{id}/complete", complete) |
| 1416 |
.post("/tasks/{id}/delete", remove) |
| 1417 |
.post("/tasks/{id}/subtasks", add_subtask) |
| 1418 |
.post("/tasks/{id}/subtasks/{sub}/toggle", toggle_subtask) |
| 1419 |
.post("/tasks/{id}/notes", add_note) |
| 1420 |
} |
| 1421 |
|