| 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 |
#![allow(clippy::needless_pass_by_value)] |
| 50 |
|
| 51 |
use goingson_core::{DbValue as _, NewProject, Project, ProjectStatus, ProjectType}; |
| 52 |
use quasi_declare::declare; |
| 53 |
use quasi_router::layout::Tone; |
| 54 |
use quasi_router::screen::{Choice, Prose, Tag}; |
| 55 |
use quasi_router::{Action, Node, Response, RouteError, Router}; |
| 56 |
|
| 57 |
use super::parse_choice; |
| 58 |
use crate::state::{AppState, DESKTOP_USER_ID}; |
| 59 |
|
| 60 |
mod dashboard; |
| 61 |
|
| 62 |
#[cfg(test)] |
| 63 |
mod tests; |
| 64 |
|
| 65 |
|
| 66 |
fn retired(project: &Project) -> bool { |
| 67 |
matches!( |
| 68 |
project.status, |
| 69 |
ProjectStatus::Completed | ProjectStatus::Archived |
| 70 |
) |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
pub(super) fn type_label(project_type: &ProjectType) -> &'static str { |
| 75 |
match project_type { |
| 76 |
ProjectType::SideProject => "Side Project", |
| 77 |
ProjectType::Job => "Job", |
| 78 |
ProjectType::Company => "Company", |
| 79 |
ProjectType::Essay => "Essay", |
| 80 |
ProjectType::Article => "Article", |
| 81 |
ProjectType::Painting => "Painting", |
| 82 |
ProjectType::Other => "Other", |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
const NEW_TYPES: [ProjectType; 6] = [ |
| 91 |
ProjectType::SideProject, |
| 92 |
ProjectType::Job, |
| 93 |
ProjectType::Company, |
| 94 |
ProjectType::Essay, |
| 95 |
ProjectType::Article, |
| 96 |
ProjectType::Other, |
| 97 |
]; |
| 98 |
|
| 99 |
|
| 100 |
fn status_label(status: &ProjectStatus) -> &'static str { |
| 101 |
match status { |
| 102 |
ProjectStatus::Active => "Active", |
| 103 |
ProjectStatus::OnHold => "On Hold", |
| 104 |
ProjectStatus::Completed => "Completed", |
| 105 |
ProjectStatus::Archived => "Archived", |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
pub(super) const fn status_tone(status: &ProjectStatus) -> makeover_layout::Tone { |
| 114 |
match status { |
| 115 |
ProjectStatus::Active => makeover_layout::Tone::Info, |
| 116 |
ProjectStatus::OnHold => makeover_layout::Tone::Warning, |
| 117 |
ProjectStatus::Completed => makeover_layout::Tone::Success, |
| 118 |
ProjectStatus::Archived => makeover_layout::Tone::Neutral, |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
#[derive(Clone, Copy)] |
| 127 |
struct View { |
| 128 |
shared_only: bool, |
| 129 |
show_retired: bool, |
| 130 |
} |
| 131 |
|
| 132 |
impl View { |
| 133 |
|
| 134 |
fn of(request: &quasi_router::Request) -> Self { |
| 135 |
Self { |
| 136 |
shared_only: flag(request, "shared"), |
| 137 |
show_retired: flag(request, "retired"), |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
const fn sharing_toggled(self) -> Self { |
| 143 |
Self { |
| 144 |
shared_only: !self.shared_only, |
| 145 |
..self |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
const fn retired_toggled(self) -> Self { |
| 151 |
Self { |
| 152 |
show_retired: !self.show_retired, |
| 153 |
..self |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
enum Grid { |
| 163 |
|
| 164 |
Fresh, |
| 165 |
|
| 166 |
NoneShared, |
| 167 |
|
| 168 |
AllRetired, |
| 169 |
|
| 170 |
Showing, |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
struct Loaded { |
| 178 |
|
| 179 |
|
| 180 |
shown: Vec<Project>, |
| 181 |
|
| 182 |
grid: Grid, |
| 183 |
|
| 184 |
shared: usize, |
| 185 |
|
| 186 |
dormant: usize, |
| 187 |
view: View, |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
fn read(state: &AppState, view: View) -> Result<Loaded, RouteError> { |
| 192 |
let all = state |
| 193 |
.projects |
| 194 |
.list_all(DESKTOP_USER_ID) |
| 195 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 196 |
|
| 197 |
let shared = all.iter().filter(|p| p.group_id.is_some()).count(); |
| 198 |
let dormant = all.iter().filter(|p| retired(p)).count(); |
| 199 |
let nothing_at_all = all.is_empty(); |
| 200 |
|
| 201 |
let scoped: Vec<Project> = all |
| 202 |
.into_iter() |
| 203 |
.filter(|project| !view.shared_only || project.group_id.is_some()) |
| 204 |
.collect(); |
| 205 |
let nothing_scoped = scoped.is_empty(); |
| 206 |
|
| 207 |
let (live, sleeping): (Vec<Project>, Vec<Project>) = |
| 208 |
scoped.into_iter().partition(|project| !retired(project)); |
| 209 |
|
| 210 |
let (grid, shown) = if nothing_at_all { |
| 211 |
(Grid::Fresh, Vec::new()) |
| 212 |
} else if nothing_scoped { |
| 213 |
(Grid::NoneShared, Vec::new()) |
| 214 |
} else if live.is_empty() && !view.show_retired { |
| 215 |
(Grid::AllRetired, Vec::new()) |
| 216 |
} else if view.show_retired { |
| 217 |
(Grid::Showing, live.into_iter().chain(sleeping).collect()) |
| 218 |
} else { |
| 219 |
(Grid::Showing, live) |
| 220 |
}; |
| 221 |
|
| 222 |
Ok(Loaded { |
| 223 |
shown, |
| 224 |
grid, |
| 225 |
shared, |
| 226 |
dormant, |
| 227 |
view, |
| 228 |
}) |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
fn has_description(project: &Project) -> bool { |
| 233 |
!project.description.is_empty() |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
fn described(project: &Project) -> Prose { |
| 246 |
Prose::rich(&project.description) |
| 247 |
} |
| 248 |
|
| 249 |
declare! { |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
shape row_for(loaded: &Loaded, project: &Project) -> Row; |
| 261 |
|
| 262 |
row &project.name { |
| 263 |
token Tag::badge(type_label(&project.project_type)); |
| 264 |
token Tag::badge(status_label(&project.status)).tone(status_tone(&project.status)); |
| 265 |
token Tag::badge("Shared") when project.group_id.is_some(); |
| 266 |
secondary described(project) when has_description(project); |
| 267 |
activate to doing filtered(Action::get("/projects/{project.id}"), loaded.view); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
declare! { |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
shape grid(loaded: &Loaded) -> Node; |
| 278 |
|
| 279 |
given loaded.grid { |
| 280 |
Grid::Fresh -> empty "No projects yet." { |
| 281 |
offering "Create your first project" |
| 282 |
to doing filtered(Action::get("/projects/new"), loaded.view); |
| 283 |
} |
| 284 |
Grid::NoneShared -> empty "No shared projects yet. Share a project from its menu \ |
| 285 |
to see it here."; |
| 286 |
Grid::AllRetired -> empty "Every project is completed or archived."; |
| 287 |
otherwise -> list { |
| 288 |
for project in loaded.shown.iter() { |
| 289 |
include row_for(loaded, project); |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
fn offers_sharing(loaded: &Loaded) -> bool { |
| 300 |
loaded.shared > 0 || loaded.view.shared_only |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
fn retired_label(loaded: &Loaded) -> String { |
| 305 |
if loaded.view.show_retired { |
| 306 |
"Hide completed and archived".to_owned() |
| 307 |
} else { |
| 308 |
format!("Show {} completed or archived", loaded.dormant) |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
declare! { |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
shape screen(loaded: &Loaded) -> Screen; |
| 319 |
|
| 320 |
screen list_detail "Projects" false { |
| 321 |
at_place super::shell::PROJECTS; |
| 322 |
|
| 323 |
region "projects-band" as Band { |
| 324 |
page "Projects"; |
| 325 |
act "New project" to doing filtered(Action::get("/projects/new"), loaded.view); |
| 326 |
|
| 327 |
chip "Shared only" to doing list_action(loaded.view.sharing_toggled()) |
| 328 |
when offers_sharing(loaded) { |
| 329 |
latched loaded.view.shared_only; |
| 330 |
} |
| 331 |
|
| 332 |
act retired_label(loaded) to doing list_action(loaded.view.retired_toggled()) |
| 333 |
when loaded.dormant over 0; |
| 334 |
} |
| 335 |
|
| 336 |
region "projects-grid" as Pane { |
| 337 |
include grid(loaded); |
| 338 |
} |
| 339 |
|
| 340 |
region "projects-detail" as Pane { |
| 341 |
empty "Nothing selected"; |
| 342 |
} |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
fn flag(request: &quasi_router::Request, name: &str) -> bool { |
| 348 |
matches!(request.carried.get(name), Some("1" | "true")) |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
pub(super) fn filtered_by(action: Action, name: &str, on: bool) -> Action { |
| 357 |
if on { |
| 358 |
action.carrying(name, "1") |
| 359 |
} else { |
| 360 |
action |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
fn filtered(action: Action, view: View) -> Action { |
| 370 |
let action = filtered_by(action, "shared", view.shared_only); |
| 371 |
filtered_by(action, "retired", view.show_retired) |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
fn list_action(view: View) -> Action { |
| 376 |
filtered(Action::get("/projects/list"), view) |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
fn index(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 381 |
let view = View::of(&request); |
| 382 |
Ok(screen(&read(state, view)?).into()) |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
fn list(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 387 |
let view = View::of(&request); |
| 388 |
Ok(Response::fragment( |
| 389 |
"projects-grid", |
| 390 |
grid(&read(state, view)?), |
| 391 |
)) |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
pub(super) fn project_id( |
| 399 |
request: &quasi_router::Request, |
| 400 |
) -> Result<goingson_core::ProjectId, RouteError> { |
| 401 |
let raw = request |
| 402 |
.captures |
| 403 |
.get("id") |
| 404 |
.ok_or_else(|| RouteError::not_found("no project id"))?; |
| 405 |
Ok(goingson_core::ProjectId::from( |
| 406 |
uuid::Uuid::parse_str(raw).map_err(|_| RouteError::not_found("not a project id"))?, |
| 407 |
)) |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
fn showing(state: &AppState, request: &quasi_router::Request) -> Result<Showing, RouteError> { |
| 412 |
let id = project_id(request)?; |
| 413 |
let project = state |
| 414 |
.projects |
| 415 |
.get_by_id(id, DESKTOP_USER_ID) |
| 416 |
.map_err(|error| RouteError::internal(error.to_string()))? |
| 417 |
.ok_or_else(|| RouteError::not_found("no such project"))?; |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
let groups = if project.group_id.is_none() { |
| 422 |
known_groups(state)? |
| 423 |
} else { |
| 424 |
Vec::new() |
| 425 |
}; |
| 426 |
|
| 427 |
Ok(Showing { |
| 428 |
project, |
| 429 |
groups, |
| 430 |
view: View::of(request), |
| 431 |
}) |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
fn detail(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 436 |
let showing = showing(state, &request)?; |
| 437 |
Ok(Response::fragment( |
| 438 |
"projects-detail", |
| 439 |
Node::Region(detail_pane(&showing)), |
| 440 |
)) |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
const NEW_STATUSES: [ProjectStatus; 2] = [ProjectStatus::Active, ProjectStatus::OnHold]; |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
struct Asking<'a> { |
| 457 |
view: View, |
| 458 |
errors: &'a [(&'a str, String)], |
| 459 |
submitted: Option<&'a quasi_router::Params>, |
| 460 |
} |
| 461 |
|
| 462 |
impl Asking<'_> { |
| 463 |
|
| 464 |
const fn fresh(view: View) -> Self { |
| 465 |
Self { |
| 466 |
view, |
| 467 |
errors: &[], |
| 468 |
submitted: None, |
| 469 |
} |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
static NOTHING_TYPED: quasi_router::Params = quasi_router::Params::new(); |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
fn typed<'a>(asking: &Asking<'a>) -> &'a quasi_router::Params { |
| 483 |
asking.submitted.unwrap_or(&NOTHING_TYPED) |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
fn has_error(asking: &Asking, name: &str) -> bool { |
| 488 |
asking.errors.iter().any(|(field, _)| *field == name) |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
fn error_for(asking: &Asking, name: &str) -> String { |
| 493 |
asking |
| 494 |
.errors |
| 495 |
.iter() |
| 496 |
.find(|(field, _)| *field == name) |
| 497 |
.map(|(_, message)| message.clone()) |
| 498 |
.unwrap_or_default() |
| 499 |
} |
| 500 |
|
| 501 |
declare! { |
| 502 |
|
| 503 |
shape form_pane(asking: &Asking) -> Slot; |
| 504 |
|
| 505 |
region "projects-detail" as Pane { |
| 506 |
section "New project"; |
| 507 |
|
| 508 |
form doing filtered(Action::post("/projects"), asking.view) { |
| 509 |
submit "Create project"; |
| 510 |
|
| 511 |
field Text "name" "Project Name" { |
| 512 |
required; |
| 513 |
placeholder "My Awesome Project"; |
| 514 |
error error_for(asking, "name") when has_error(asking, "name"); |
| 515 |
refilled typed(asking); |
| 516 |
} |
| 517 |
|
| 518 |
field Textarea "description" "Description" { |
| 519 |
placeholder "What's this project about?"; |
| 520 |
error error_for(asking, "description") when has_error(asking, "description"); |
| 521 |
refilled typed(asking); |
| 522 |
} |
| 523 |
|
| 524 |
field Select "project_type" "Type" { |
| 525 |
for kind in NEW_TYPES.iter() { |
| 526 |
option Choice::new(kind.db_value(), type_label(kind)); |
| 527 |
} |
| 528 |
error error_for(asking, "project_type") when has_error(asking, "project_type"); |
| 529 |
refilled typed(asking); |
| 530 |
} |
| 531 |
|
| 532 |
field Select "status" "Status" { |
| 533 |
for status in NEW_STATUSES.iter() { |
| 534 |
option Choice::new(status.db_value(), status_label(status)); |
| 535 |
} |
| 536 |
error error_for(asking, "status") when has_error(asking, "status"); |
| 537 |
refilled typed(asking); |
| 538 |
} |
| 539 |
} |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
struct Showing { |
| 545 |
project: Project, |
| 546 |
|
| 547 |
|
| 548 |
groups: Vec<synckit_client::store::sync::KnownGroup>, |
| 549 |
view: View, |
| 550 |
} |
| 551 |
|
| 552 |
|
| 553 |
fn kind_and_status(showing: &Showing) -> String { |
| 554 |
format!( |
| 555 |
"{} · {}", |
| 556 |
type_label(&showing.project.project_type), |
| 557 |
status_label(&showing.project.status) |
| 558 |
) |
| 559 |
} |
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
fn offers_sharing_into(showing: &Showing) -> bool { |
| 567 |
showing.project.group_id.is_none() && !showing.groups.is_empty() |
| 568 |
} |
| 569 |
|
| 570 |
|
| 571 |
fn is_shared(showing: &Showing) -> bool { |
| 572 |
showing.project.group_id.is_some() |
| 573 |
} |
| 574 |
|
| 575 |
declare! { |
| 576 |
|
| 577 |
shape detail_pane(showing: &Showing) -> Slot; |
| 578 |
|
| 579 |
region "projects-detail" as Pane { |
| 580 |
section &showing.project.name; |
| 581 |
text kind_and_status(showing); |
| 582 |
text &showing.project.description when has_description(&showing.project); |
| 583 |
|
| 584 |
form doing filtered( |
| 585 |
Action::post("/projects/{showing.project.id}/share"), |
| 586 |
showing.view |
| 587 |
) when offers_sharing_into(showing) { |
| 588 |
submit "Share into a group"; |
| 589 |
|
| 590 |
field Select "group_id" "Group" { |
| 591 |
for group in showing.groups.iter() { |
| 592 |
option Choice::new(group.id.to_string(), &group.name); |
| 593 |
} |
| 594 |
required; |
| 595 |
hint "Everything in the project goes with it: its tasks, events, \ |
| 596 |
milestones and attachments."; |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
text "Shared into a group. Its tasks, events, milestones and attachments \ |
| 601 |
are shared with it." |
| 602 |
when is_shared(showing); |
| 603 |
|
| 604 |
act "Move back to personal" |
| 605 |
to doing filtered( |
| 606 |
Action::post("/projects/{showing.project.id}/unshare"), |
| 607 |
showing.view |
| 608 |
) |
| 609 |
when is_shared(showing) { |
| 610 |
confirm "Move this project and everything in it back to personal scope? \ |
| 611 |
Other members of the group will stop seeing it."; |
| 612 |
} |
| 613 |
|
| 614 |
act "Delete project" |
| 615 |
to doing filtered( |
| 616 |
Action::post("/projects/{showing.project.id}/delete"), |
| 617 |
showing.view |
| 618 |
) { |
| 619 |
tone Danger; |
| 620 |
confirm "Are you sure you want to delete this project? This cannot be undone."; |
| 621 |
} |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
|
| 626 |
fn new(_state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 627 |
Ok(Response::fragment( |
| 628 |
"projects-detail", |
| 629 |
Node::Region(form_pane(&Asking::fresh(View::of(&request)))), |
| 630 |
)) |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
fn validate(name: &str, description: &str) -> Vec<(&'static str, String)> { |
| 635 |
let mut errors = Vec::new(); |
| 636 |
if name.is_empty() { |
| 637 |
errors.push(("name", "A project needs a name.".to_owned())); |
| 638 |
} else if name.chars().count() > 100 { |
| 639 |
errors.push(("name", "Maximum 100 characters".to_owned())); |
| 640 |
} |
| 641 |
if description.chars().count() > 1000 { |
| 642 |
errors.push(("description", "Maximum 1000 characters".to_owned())); |
| 643 |
} |
| 644 |
errors |
| 645 |
} |
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
fn wrote(state: &AppState, view: View) -> Result<Response, RouteError> { |
| 661 |
Ok(screen(&read(state, view)?).into()) |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
fn create(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 666 |
let view = View::of(&request); |
| 667 |
|
| 668 |
let name = request |
| 669 |
.payload |
| 670 |
.get("name") |
| 671 |
.unwrap_or_default() |
| 672 |
.trim() |
| 673 |
.to_owned(); |
| 674 |
let description = request |
| 675 |
.payload |
| 676 |
.get("description") |
| 677 |
.unwrap_or_default() |
| 678 |
.trim() |
| 679 |
.to_owned(); |
| 680 |
|
| 681 |
let mut errors = validate(&name, &description); |
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
let project_type = parse_choice::<ProjectType>(&request.payload, "project_type", &mut errors); |
| 687 |
let status = parse_choice::<ProjectStatus>(&request.payload, "status", &mut errors) |
| 688 |
.filter(|status| NEW_STATUSES.contains(status)); |
| 689 |
if status.is_none() && !errors.iter().any(|(field, _)| *field == "status") { |
| 690 |
errors.push(("status", "Not a status a project starts in.".to_owned())); |
| 691 |
} |
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
let refused = Asking { |
| 696 |
view, |
| 697 |
errors: &errors, |
| 698 |
submitted: Some(&request.payload), |
| 699 |
}; |
| 700 |
let (Some(project_type), Some(status)) = (project_type, status) else { |
| 701 |
return Ok(Response::fragment( |
| 702 |
"projects-detail", |
| 703 |
Node::Region(form_pane(&refused)), |
| 704 |
)); |
| 705 |
}; |
| 706 |
if !errors.is_empty() { |
| 707 |
return Ok(Response::fragment( |
| 708 |
"projects-detail", |
| 709 |
Node::Region(form_pane(&refused)), |
| 710 |
)); |
| 711 |
} |
| 712 |
|
| 713 |
state |
| 714 |
.projects |
| 715 |
.create( |
| 716 |
DESKTOP_USER_ID, |
| 717 |
NewProject { |
| 718 |
name, |
| 719 |
description, |
| 720 |
project_type, |
| 721 |
status, |
| 722 |
}, |
| 723 |
) |
| 724 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 725 |
|
| 726 |
wrote(state, view) |
| 727 |
} |
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
fn remove(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 735 |
let id = project_id(&request)?; |
| 736 |
let deleted = state |
| 737 |
.projects |
| 738 |
.delete(id, DESKTOP_USER_ID) |
| 739 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 740 |
if !deleted { |
| 741 |
return Err(RouteError::not_found("no such project")); |
| 742 |
} |
| 743 |
wrote(state, View::of(&request)) |
| 744 |
} |
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
fn known_groups( |
| 755 |
state: &AppState, |
| 756 |
) -> Result<Vec<synckit_client::store::sync::KnownGroup>, RouteError> { |
| 757 |
let conn = state |
| 758 |
.db |
| 759 |
.conn() |
| 760 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 761 |
synckit_client::store::directory::groups(&conn) |
| 762 |
.map_err(|error| RouteError::internal(error.to_string())) |
| 763 |
} |
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
fn share(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 772 |
let id = project_id(&request)?; |
| 773 |
let group = request |
| 774 |
.payload |
| 775 |
.get("group_id") |
| 776 |
.unwrap_or_default() |
| 777 |
.trim() |
| 778 |
.to_owned(); |
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
let view = View::of(&request); |
| 784 |
if let Err(error) = crate::commands::group::share_project_local(state, &id.to_string(), &group) |
| 785 |
{ |
| 786 |
return Ok( |
| 787 |
Response::from(screen(&read(state, view)?)).toast(Tone::Danger, error.to_string()) |
| 788 |
); |
| 789 |
} |
| 790 |
|
| 791 |
Ok(Response::from(screen(&read(state, view)?)).toast( |
| 792 |
Tone::Success, |
| 793 |
"Shared. Everything in the project went with it.", |
| 794 |
)) |
| 795 |
} |
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
fn unshare(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 805 |
let id = project_id(&request)?; |
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
state |
| 811 |
.projects |
| 812 |
.get_by_id(id, DESKTOP_USER_ID) |
| 813 |
.map_err(|error| RouteError::internal(error.to_string()))? |
| 814 |
.ok_or_else(|| RouteError::not_found("no such project"))?; |
| 815 |
|
| 816 |
crate::commands::group::set_project_scope(&state.db, DESKTOP_USER_ID, &id.to_string(), None) |
| 817 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 818 |
|
| 819 |
wrote(state, View::of(&request)) |
| 820 |
} |
| 821 |
|
| 822 |
|
| 823 |
#[must_use] |
| 824 |
pub fn routes(router: Router<AppState>) -> Router<AppState> { |
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
let router = router |
| 829 |
.get("/projects", index) |
| 830 |
.get("/projects/list", list) |
| 831 |
.get("/projects/new", new) |
| 832 |
.get("/projects/{id}", detail) |
| 833 |
.post("/projects", create) |
| 834 |
.post("/projects/{id}/delete", remove) |
| 835 |
.post("/projects/{id}/share", share) |
| 836 |
.post("/projects/{id}/unshare", unshare); |
| 837 |
dashboard::routes(router) |
| 838 |
} |
| 839 |
|