| 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 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
#![allow(clippy::needless_pass_by_value)] |
| 60 |
|
| 61 |
use chrono::NaiveDate; |
| 62 |
use goingson_core::id_types::ContextId; |
| 63 |
use goingson_core::models::{Context, ContextKind}; |
| 64 |
use quasi_declare::declare; |
| 65 |
use quasi_router::layout::Tone; |
| 66 |
use quasi_router::screen::{Choice, Tag}; |
| 67 |
use quasi_router::{Node, Response, RouteError, Router, Slot}; |
| 68 |
|
| 69 |
use crate::state::{AppState, DESKTOP_USER_ID}; |
| 70 |
|
| 71 |
#[cfg(test)] |
| 72 |
mod tests; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
const KINDS: &[ContextKind] = &[ |
| 81 |
ContextKind::Vacation, |
| 82 |
ContextKind::Trip, |
| 83 |
ContextKind::Illness, |
| 84 |
ContextKind::Sprint, |
| 85 |
]; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
fn kind_label(kind: ContextKind) -> &'static str { |
| 94 |
match kind { |
| 95 |
ContextKind::Vacation => "Vacation", |
| 96 |
ContextKind::Trip => "Trip", |
| 97 |
ContextKind::Illness => "Illness", |
| 98 |
ContextKind::Sprint => "Sprint", |
| 99 |
ContextKind::Other => "Other", |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
fn all(state: &AppState) -> Result<Vec<Context>, RouteError> { |
| 105 |
state |
| 106 |
.contexts |
| 107 |
.list_all(DESKTOP_USER_ID) |
| 108 |
.map_err(|error| RouteError::internal(error.to_string())) |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
fn load(state: &AppState, id: ContextId) -> Result<Context, RouteError> { |
| 117 |
all(state)? |
| 118 |
.into_iter() |
| 119 |
.find(|context| context.id == id) |
| 120 |
.ok_or_else(|| RouteError::not_found("no such context")) |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
fn context_id(request: &quasi_router::Request) -> Result<ContextId, RouteError> { |
| 125 |
let raw = request |
| 126 |
.captures |
| 127 |
.get("id") |
| 128 |
.ok_or_else(|| RouteError::not_found("no context id"))?; |
| 129 |
Ok(ContextId::from( |
| 130 |
uuid::Uuid::parse_str(raw).map_err(|_| RouteError::not_found("not a context id"))?, |
| 131 |
)) |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
fn span_words(context: &Context) -> String { |
| 137 |
let days = context.days(); |
| 138 |
format!( |
| 139 |
"{} to {} ({} day{})", |
| 140 |
context.starts_on.format("%-d %b %Y"), |
| 141 |
context.ends_on.format("%-d %b %Y"), |
| 142 |
days, |
| 143 |
if days == 1 { "" } else { "s" } |
| 144 |
) |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
fn is_current(context: &Context, current: Option<ContextId>) -> bool { |
| 149 |
current == Some(context.id) |
| 150 |
} |
| 151 |
|
| 152 |
declare! { |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
shape row_for(context: &Context, current: bool) -> Row; |
| 159 |
|
| 160 |
row &context.label { |
| 161 |
token Tag::badge(kind_label(context.kind)); |
| 162 |
secondary span_words(context); |
| 163 |
token Tag::badge("From an event") when context.migrated_from_event_id.is_some(); |
| 164 |
current current; |
| 165 |
activate to get "/contexts/{context.id}"; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
declare! { |
| 170 |
|
| 171 |
shape list_node(contexts: &[Context], current: Option<ContextId>) -> Node; |
| 172 |
|
| 173 |
given contexts.is_empty() { |
| 174 |
true -> empty "No contexts yet." { |
| 175 |
offering "Record your first context" to get "/contexts/new"; |
| 176 |
} |
| 177 |
otherwise -> list { |
| 178 |
for context in contexts.iter() { |
| 179 |
include row_for(context, is_current(context, current)); |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
struct Editing<'a> { |
| 193 |
existing: Option<&'a Context>, |
| 194 |
errors: &'a [(&'a str, String)], |
| 195 |
submitted: Option<&'a quasi_router::Params>, |
| 196 |
} |
| 197 |
|
| 198 |
impl<'a> Editing<'a> { |
| 199 |
|
| 200 |
const fn fresh() -> Self { |
| 201 |
Self { |
| 202 |
existing: None, |
| 203 |
errors: &[], |
| 204 |
submitted: None, |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
const fn of(context: &'a Context) -> Self { |
| 210 |
Self { |
| 211 |
existing: Some(context), |
| 212 |
errors: &[], |
| 213 |
submitted: None, |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
fn is_edit(editing: &Editing) -> bool { |
| 220 |
editing.existing.is_some() |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
fn pane_heading(editing: &Editing) -> String { |
| 225 |
match editing.existing { |
| 226 |
None => "New context".to_owned(), |
| 227 |
Some(context) => format!("Editing {}", context.label), |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
fn from_event(editing: &Editing) -> bool { |
| 233 |
editing |
| 234 |
.existing |
| 235 |
.is_some_and(|context| context.migrated_from_event_id.is_some()) |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
fn form_path(editing: &Editing) -> String { |
| 240 |
match editing.existing { |
| 241 |
None => "/contexts".to_owned(), |
| 242 |
Some(context) => format!("/contexts/{}", context.id), |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
fn submit_label(editing: &Editing) -> &'static str { |
| 248 |
match editing.existing { |
| 249 |
None => "Record context", |
| 250 |
Some(_) => "Save", |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
fn existing_id(editing: &Editing) -> String { |
| 259 |
editing |
| 260 |
.existing |
| 261 |
.map(|context| context.id.to_string()) |
| 262 |
.unwrap_or_default() |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
fn delete_question(editing: &Editing) -> &'static str { |
| 271 |
if from_event(editing) { |
| 272 |
"Delete this context and put its event back?" |
| 273 |
} else { |
| 274 |
"Delete this context?" |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
fn stored_label(editing: &Editing) -> Option<String> { |
| 280 |
editing.existing.map(|context| context.label.clone()) |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
fn stored_kind<'a>(editing: &Editing<'a>) -> Option<&'a str> { |
| 285 |
editing.existing.map(|context| context.kind.as_str()) |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
fn stored_start(editing: &Editing) -> Option<String> { |
| 290 |
editing |
| 291 |
.existing |
| 292 |
.map(|context| context.starts_on.to_string()) |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
fn stored_end(editing: &Editing) -> Option<String> { |
| 297 |
editing.existing.map(|context| context.ends_on.to_string()) |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
fn has_error(editing: &Editing, name: &str) -> bool { |
| 302 |
editing.errors.iter().any(|(field, _)| *field == name) |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
fn error_for(editing: &Editing, name: &str) -> String { |
| 307 |
editing |
| 308 |
.errors |
| 309 |
.iter() |
| 310 |
.find(|(field, _)| *field == name) |
| 311 |
.map(|(_, message)| message.clone()) |
| 312 |
.unwrap_or_default() |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
static NOTHING_TYPED: quasi_router::Params = quasi_router::Params::new(); |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
fn typed<'a>(editing: &Editing<'a>) -> &'a quasi_router::Params { |
| 325 |
editing.submitted.unwrap_or(&NOTHING_TYPED) |
| 326 |
} |
| 327 |
|
| 328 |
declare! { |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
shape form_slot(editing: &Editing) -> Slot; |
| 337 |
|
| 338 |
region "contexts-detail" as Pane { |
| 339 |
section pane_heading(editing); |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
banner Tone::Info |
| 344 |
"Converted from an event. Deleting this puts that event back on the \ |
| 345 |
timeline, with its times intact." |
| 346 |
when from_event(editing); |
| 347 |
|
| 348 |
form post form_path(editing) { |
| 349 |
submit submit_label(editing); |
| 350 |
|
| 351 |
field Text "label" "Label" { |
| 352 |
required; |
| 353 |
placeholder "Two weeks in Lisbon"; |
| 354 |
for label in stored_label(editing).into_iter() { |
| 355 |
value label; |
| 356 |
} |
| 357 |
error error_for(editing, "label") when has_error(editing, "label"); |
| 358 |
refilled typed(editing); |
| 359 |
} |
| 360 |
|
| 361 |
field Select "kind" "Kind" { |
| 362 |
for kind in KINDS.iter().copied() { |
| 363 |
option Choice::new(kind.as_str(), kind_label(kind)); |
| 364 |
} |
| 365 |
for kind in stored_kind(editing).into_iter() { |
| 366 |
value kind; |
| 367 |
} |
| 368 |
error error_for(editing, "kind") when has_error(editing, "kind"); |
| 369 |
refilled typed(editing); |
| 370 |
} |
| 371 |
|
| 372 |
field Date "starts_on" "First day" { |
| 373 |
required; |
| 374 |
hint "The first day inside it."; |
| 375 |
for start in stored_start(editing).into_iter() { |
| 376 |
value start; |
| 377 |
} |
| 378 |
error error_for(editing, "starts_on") when has_error(editing, "starts_on"); |
| 379 |
refilled typed(editing); |
| 380 |
} |
| 381 |
|
| 382 |
field Date "ends_on" "Last day" { |
| 383 |
required; |
| 384 |
hint "Inclusive: off until the 17th means the 17th is off."; |
| 385 |
for end in stored_end(editing).into_iter() { |
| 386 |
value end; |
| 387 |
} |
| 388 |
error error_for(editing, "ends_on") when has_error(editing, "ends_on"); |
| 389 |
refilled typed(editing); |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
act "Delete" to post "/contexts/{existing_id(editing)}/delete" when is_edit(editing) { |
| 394 |
tone Danger; |
| 395 |
confirm delete_question(editing); |
| 396 |
} |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
declare! { |
| 401 |
|
| 402 |
shape idle_pane() -> Slot; |
| 403 |
|
| 404 |
region "contexts-detail" as Pane { |
| 405 |
empty "Nothing selected"; |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
declare! { |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
shape screen(contexts: &[Context], current: Option<ContextId>, pane: Slot) -> Screen; |
| 415 |
|
| 416 |
screen list_detail "Contexts" false { |
| 417 |
at_place super::shell::DAY; |
| 418 |
|
| 419 |
region "contexts-band" as Band { |
| 420 |
page "Contexts"; |
| 421 |
act "New context" to get "/contexts/new"; |
| 422 |
} |
| 423 |
|
| 424 |
region "contexts-list" as Pane { |
| 425 |
include list_node(contexts, current); |
| 426 |
} |
| 427 |
|
| 428 |
include pane; |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
fn document(state: &AppState, message: Option<&str>) -> Result<Response, RouteError> { |
| 434 |
let contexts = all(state)?; |
| 435 |
let screen = screen(&contexts, None, idle_pane()); |
| 436 |
Ok(match message { |
| 437 |
Some(said) => Response::from(screen).toast(Tone::Success, said), |
| 438 |
None => screen.into(), |
| 439 |
}) |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
fn index(state: &AppState, _request: quasi_router::Request) -> Result<Response, RouteError> { |
| 444 |
document(state, None) |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
fn new(_state: &AppState, _request: quasi_router::Request) -> Result<Response, RouteError> { |
| 449 |
Ok(Response::fragment( |
| 450 |
"contexts-detail", |
| 451 |
Node::Region(form_slot(&Editing::fresh())), |
| 452 |
)) |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
fn detail(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 457 |
let context = load(state, context_id(&request)?)?; |
| 458 |
Ok(Response::fragment( |
| 459 |
"contexts-detail", |
| 460 |
Node::Region(form_slot(&Editing::of(&context))), |
| 461 |
)) |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
fn validate( |
| 472 |
label: &str, |
| 473 |
starts_on: Option<NaiveDate>, |
| 474 |
ends_on: Option<NaiveDate>, |
| 475 |
) -> Vec<(&'static str, String)> { |
| 476 |
let mut errors = Vec::new(); |
| 477 |
if label.is_empty() { |
| 478 |
errors.push(("label", "A context needs a label.".to_owned())); |
| 479 |
} else if label.chars().count() > 100 { |
| 480 |
errors.push(("label", "Maximum 100 characters".to_owned())); |
| 481 |
} |
| 482 |
if starts_on.is_none() { |
| 483 |
errors.push(("starts_on", "Needs a first day.".to_owned())); |
| 484 |
} |
| 485 |
if ends_on.is_none() { |
| 486 |
errors.push(("ends_on", "Needs a last day.".to_owned())); |
| 487 |
} |
| 488 |
if let (Some(starts), Some(ends)) = (starts_on, ends_on) |
| 489 |
&& starts > ends |
| 490 |
{ |
| 491 |
errors.push(( |
| 492 |
"starts_on", |
| 493 |
"The first day is after the last one.".to_owned(), |
| 494 |
)); |
| 495 |
} |
| 496 |
errors |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
fn date(params: &quasi_router::Params, name: &str) -> Option<NaiveDate> { |
| 505 |
params.get(name)?.trim().parse().ok() |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
fn wrote(state: &AppState, message: &str) -> Result<Response, RouteError> { |
| 514 |
document(state, Some(message)) |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
struct Submitted { |
| 519 |
label: String, |
| 520 |
kind: ContextKind, |
| 521 |
starts_on: NaiveDate, |
| 522 |
ends_on: NaiveDate, |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
fn submitted( |
| 530 |
request: &quasi_router::Request, |
| 531 |
existing: Option<&Context>, |
| 532 |
) -> Result<Submitted, Box<Node>> { |
| 533 |
let label = request |
| 534 |
.payload |
| 535 |
.get("label") |
| 536 |
.unwrap_or_default() |
| 537 |
.trim() |
| 538 |
.to_owned(); |
| 539 |
let starts_on = date(&request.payload, "starts_on"); |
| 540 |
let ends_on = date(&request.payload, "ends_on"); |
| 541 |
|
| 542 |
let errors = validate(&label, starts_on, ends_on); |
| 543 |
if !errors.is_empty() { |
| 544 |
return Err(Box::new(Node::Region(form_slot(&Editing { |
| 545 |
existing, |
| 546 |
errors: &errors, |
| 547 |
submitted: Some(&request.payload), |
| 548 |
})))); |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
Ok(Submitted { |
| 555 |
label, |
| 556 |
kind: ContextKind::parse(request.payload.get("kind").unwrap_or_default()), |
| 557 |
starts_on: starts_on.expect("validated"), |
| 558 |
ends_on: ends_on.expect("validated"), |
| 559 |
}) |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
fn create(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 564 |
let fields = match submitted(&request, None) { |
| 565 |
Ok(fields) => fields, |
| 566 |
Err(pane) => return Ok(Response::fragment("contexts-detail", *pane)), |
| 567 |
}; |
| 568 |
|
| 569 |
state |
| 570 |
.contexts |
| 571 |
.create( |
| 572 |
DESKTOP_USER_ID, |
| 573 |
&fields.label, |
| 574 |
fields.kind, |
| 575 |
fields.starts_on, |
| 576 |
fields.ends_on, |
| 577 |
) |
| 578 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 579 |
|
| 580 |
wrote(state, "Context recorded.") |
| 581 |
} |
| 582 |
|
| 583 |
|
| 584 |
fn update(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 585 |
let id = context_id(&request)?; |
| 586 |
let existing = load(state, id)?; |
| 587 |
let fields = match submitted(&request, Some(&existing)) { |
| 588 |
Ok(fields) => fields, |
| 589 |
Err(pane) => return Ok(Response::fragment("contexts-detail", *pane)), |
| 590 |
}; |
| 591 |
|
| 592 |
state |
| 593 |
.contexts |
| 594 |
.update( |
| 595 |
DESKTOP_USER_ID, |
| 596 |
id, |
| 597 |
&fields.label, |
| 598 |
fields.kind, |
| 599 |
fields.starts_on, |
| 600 |
fields.ends_on, |
| 601 |
) |
| 602 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 603 |
|
| 604 |
wrote(state, "Context saved.") |
| 605 |
} |
| 606 |
|
| 607 |
|
| 608 |
fn remove(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 609 |
let id = context_id(&request)?; |
| 610 |
|
| 611 |
|
| 612 |
let existing = load(state, id)?; |
| 613 |
|
| 614 |
state |
| 615 |
.contexts |
| 616 |
.delete(DESKTOP_USER_ID, id) |
| 617 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 618 |
|
| 619 |
wrote( |
| 620 |
state, |
| 621 |
match existing.migrated_from_event_id { |
| 622 |
Some(_) => "Context deleted, and its event is back on the timeline.", |
| 623 |
None => "Context deleted.", |
| 624 |
}, |
| 625 |
) |
| 626 |
} |
| 627 |
|
| 628 |
|
| 629 |
#[must_use] |
| 630 |
pub fn routes(router: Router<AppState>) -> Router<AppState> { |
| 631 |
router |
| 632 |
.get("/contexts", index) |
| 633 |
.get("/contexts/new", new) |
| 634 |
.get("/contexts/{id}", detail) |
| 635 |
.post("/contexts", create) |
| 636 |
.post("/contexts/{id}", update) |
| 637 |
.post("/contexts/{id}/delete", remove) |
| 638 |
} |
| 639 |
|