forge
Explore
Log in

Don't you dare go Hollow

Drag a card across, or say where it goes

Browse files at this commit

4f9d1740c4b4a40ac2bc8a97bb2f51fc6b53a1cfAhmet Kaan GümüşParent: 6aab1b3
  • feat
    • drag a card across, or say where it goes
Show full message
✨ feat(board): drag a card across, or say where it goes

The move is one server call that carries the column the card was believed
to be in, so the store can refuse a drop decided against a board that has
already changed underneath it. A refusal is shown, not swallowed.

Two ways in, both real. The modal's STATUS control is a form and works
with no script at all — it draws its own submit button only when an
effect has proved the page is hydrated, and submits on change when it
has. Drag and drop is the enhancement on top: a card is draggable once
hydration runs, the column it is over shows the drop outline, and the
drop posts the same call.

A drop back into the card's own column posts nothing.
crates/dizey-web/src/auth.rsmodified+6 -0
@@ -32,6 +32,9 @@ pub enum Refusal {
32 BadDeadline,32 BadDeadline,
33 /// The link asked for would put a task behind itself.33 /// The link asked for would put a task behind itself.
34 Cycle,34 Cycle,
35+ /// The card was already moved out of the column this request thought it
36+ /// was in, by somebody else, while this person was deciding.
37+ MovedAlready,
35 /// No such task — or none this account may see. Deliberately one answer for38 /// No such task — or none this account may see. Deliberately one answer for
36 /// both.39 /// both.
37 NotFound,40 NotFound,
@@ -57,6 +60,9 @@ impl Refusal {
57 Refusal::EmptyComment => "Write something first.".to_string(),60 Refusal::EmptyComment => "Write something first.".to_string(),
58 Refusal::BadDeadline => "That is not a date.".to_string(),61 Refusal::BadDeadline => "That is not a date.".to_string(),
59 Refusal::Cycle => "That link would put this task behind itself.".to_string(),62 Refusal::Cycle => "That link would put this task behind itself.".to_string(),
63+ Refusal::MovedAlready => {
64+ "Somebody moved this card first. This is where it is now.".to_string()
65+ }
60 Refusal::NotFound => "No such task.".to_string(),66 Refusal::NotFound => "No such task.".to_string(),
61 Refusal::Unavailable => "Something went wrong. Try again.".to_string(),67 Refusal::Unavailable => "Something went wrong. Try again.".to_string(),
62 }68 }
crates/dizey-web/src/board.rsmodified+106 -1
@@ -99,6 +99,59 @@ pub async fn create_task(
99 Ok(None)99 Ok(None)
100 }100 }
101 101
102+/// Moves a card into a column and, with it, records the crossing.
103+///
104+/// `from_column_id` is the column the browser had the card in when the drag
105+/// started or the status control was opened. It travels with the request so
106+/// the store can refuse a drop that was decided against a board somebody else
107+/// has already changed, rather than silently overwriting their move.
108+#[server]
109+pub async fn move_card(
110+ task_id: String,
111+ from_column_id: String,
112+ to_column_id: String,
113+) -> Result<Option<Refusal>, ServerFnError> {
114+ use crate::detail::guard::writer_and_task;
115+ use crate::server::accounts;
116+ use dizey_core::board::Moved;
117+ use time::OffsetDateTime;
118+
119+ let (user, facts) = match writer_and_task(&task_id).await {
120+ Ok(pair) => pair,
121+ Err(refusal) => return Ok(Some(refusal)),
122+ };
123+ // The destination is checked against this task's own board before the
124+ // store is asked, so a column id from another board is refused by the
125+ // handler and not merely by a foreign key.
126+ let store = accounts().store().clone();
127+ let fail = |e: dizey_core::store::StoreError| ServerFnError::new(e.to_string());
128+ let columns = store.columns(&facts.board_id).await.map_err(fail)?;
129+ if !columns.iter().any(|column| column.id == to_column_id) {
130+ return Ok(Some(Refusal::Forbidden));
131+ }
132+
133+ match store
134+ .move_task(
135+ &task_id,
136+ &from_column_id,
137+ &to_column_id,
138+ &user.id,
139+ OffsetDateTime::now_utc(),
140+ )
141+ .await
142+ {
143+ // A card dropped back where it came from is not news and is not an
144+ // error: the board simply re-reads and looks the same.
145+ Ok(Moved::Recorded(_)) | Ok(Moved::Unchanged) => Ok(None),
146+ Ok(Moved::Stale) => Ok(Some(Refusal::MovedAlready)),
147+ Err(dizey_core::store::StoreError::NotFound) => Ok(Some(Refusal::NotFound)),
148+ Err(error) => {
149+ eprintln!("store error: {error}");
150+ Ok(Some(Refusal::Unavailable))
151+ }
152+ }
153+}
154+
102 /// Which cards the filter row is showing.155 /// Which cards the filter row is showing.
103 #[derive(Clone, Copy, Debug, PartialEq, Eq)]156 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
104 enum Filter {157 enum Filter {
@@ -174,6 +227,18 @@ fn BoardScreen(
174 let filter = RwSignal::new(Filter::All);227 let filter = RwSignal::new(Filter::All);
175 // Which column has its composer open, if any.228 // Which column has its composer open, if any.
176 let composing = RwSignal::new(None::<String>);229 let composing = RwSignal::new(None::<String>);
230+ // The card currently in the air, and the column it was picked up from.
231+ // The column it came from is the drag's claim about the board it started
232+ // on, and it travels with the drop so a move decided against a stale board
233+ // is refused rather than applied.
234+ let dragging = RwSignal::new(None::<(String, String)>);
235+ let moving = ServerAction::<MoveCard>::new();
236+ let moved = moving.value();
237+ Effect::new(move |_| {
238+ if matches!(moved.get(), Some(Ok(_))) {
239+ on_change();
240+ }
241+ });
177 let may_write = me.role.can_write_tasks();242 let may_write = me.role.can_write_tasks();
178 let my_id = StoredValue::new(me.id.clone());243 let my_id = StoredValue::new(me.id.clone());
179 244
@@ -231,12 +296,43 @@ fn BoardScreen(
231 }296 }
232 })}297 })}
233 </header>298 </header>
234- <div class="column-cards">299+ <div
300+ class="column-cards"
301+ class:column-drop=move || {
302+ dragging
303+ .get()
304+ .is_some_and(|(_, from)| from != *column_id.read_value())
305+ }
306+ on:dragover=move |event| {
307+ // Saying "yes, you may drop here" is what
308+ // preventing the default on dragover means.
309+ if dragging.get().is_some() {
310+ event.prevent_default();
311+ }
312+ }
313+ on:drop=move |event| {
314+ event.prevent_default();
315+ if let Some((task_id, from)) = dragging.get() {
316+ dragging.set(None);
317+ let to = column_id.read_value().clone();
318+ if from != to {
319+ moving
320+ .dispatch(MoveCard {
321+ task_id,
322+ from_column_id: from,
323+ to_column_id: to,
324+ });
325+ }
326+ }
327+ }
328+ >
235 <For each=move || shown.get() key=|card: &TaskCard| card.id.clone() let:card>329 <For each=move || shown.get() key=|card: &TaskCard| card.id.clone() let:card>
236 <Card330 <Card
237 card=card331 card=card
238 today=today332 today=today
239 done_column=is_done_column333 done_column=is_done_column
334+ draggable=may_write
335+ dragging=dragging
240 on_open=move |task_id| opened.set(Some(task_id))336 on_open=move |task_id| opened.set(Some(task_id))
241 />337 />
242 </For>338 </For>
@@ -351,6 +447,9 @@ fn Card(
351 card: TaskCard,447 card: TaskCard,
352 today: Date,448 today: Date,
353 done_column: bool,449 done_column: bool,
450+ /// A viewer cannot move work, so a viewer's cards do not lift.
451+ draggable: bool,
452+ dragging: RwSignal<Option<(String, String)>>,
354 on_open: impl Fn(String) + Copy + Send + Sync + 'static,453 on_open: impl Fn(String) + Copy + Send + Sync + 'static,
355 ) -> impl IntoView {454 ) -> impl IntoView {
356 let blocks = card.blocks.len();455 let blocks = card.blocks.len();
@@ -361,6 +460,7 @@ fn Card(
361 let comments = card.comment_count;460 let comments = card.comment_count;
362 let assignees = card.assignees.clone();461 let assignees = card.assignees.clone();
363 let id = StoredValue::new(card.id.clone());462 let id = StoredValue::new(card.id.clone());
463+ let from_column = StoredValue::new(card.column_id.clone());
364 let open = move || on_open(id.get_value());464 let open = move || on_open(id.get_value());
365 465
366 view! {466 view! {
@@ -371,6 +471,11 @@ fn Card(
371 class:card-done=done_column471 class:card-done=done_column
372 role="button"472 role="button"
373 tabindex="0"473 tabindex="0"
474+ draggable=draggable.then_some("true")
475+ on:dragstart=move |_| {
476+ dragging.set(Some((id.get_value(), from_column.get_value())));
477+ }
478+ on:dragend=move |_| dragging.set(None)
374 on:click=move |_| open()479 on:click=move |_| open()
375 on:keydown=move |event| {480 on:keydown=move |event| {
376 if event.key() == "Enter" || event.key() == " " {481 if event.key() == "Enter" || event.key() == " " {
crates/dizey-web/src/detail.rsmodified+100 -5
@@ -8,6 +8,7 @@
8 8
9 use dizey_core::board::Person;9 use dizey_core::board::Person;
10 use dizey_core::detail::{Comment, DeletionCost, DependencyEdge, TaskDetail};10 use dizey_core::detail::{Comment, DeletionCost, DependencyEdge, TaskDetail};
11+use leptos::either::Either;
11 use leptos::prelude::*;12 use leptos::prelude::*;
12 use serde::{Deserialize, Serialize};13 use serde::{Deserialize, Serialize};
13 use time::Date;14 use time::Date;
@@ -49,7 +50,7 @@ pub enum Direction {
49 }50 }
50 51
51 #[cfg(feature = "ssr")]52 #[cfg(feature = "ssr")]
52-mod guard {53+pub(crate) mod guard {
53 use crate::auth::Refusal;54 use crate::auth::Refusal;
54 use dizey_core::detail::TaskFacts;55 use dizey_core::detail::TaskFacts;
55 use dizey_core::store::User;56 use dizey_core::store::User;
@@ -432,6 +433,85 @@ pub async fn delete_task(task_id: String) -> Result<Option<Refusal>, ServerFnErr
432 433
433 // -- the screen -------------------------------------------------------------434 // -- the screen -------------------------------------------------------------
434 435
436+/// The artboard's STATUS control: the column the card sits in, and the way to
437+/// put it in another one.
438+///
439+/// It is a real `<form>` with a real `<select>`, so it works with wasm blocked
440+/// — that is what the submit button is for. When wasm is running, the change
441+/// event submits the form itself and the button is not drawn: picking a column
442+/// from a menu and then pressing a second control to confirm it is not what
443+/// the design draws, and not what anyone expects from a status field.
444+///
445+/// The column the card was in travels in a hidden field. It is the request's
446+/// claim about the board it was looking at, and the store refuses the move if
447+/// that claim has gone stale.
448+#[component]
449+fn StatusControl(
450+ task_id: StoredValue<String>,
451+ column_id: String,
452+ columns: Vec<dizey_core::board::Column>,
453+ action: ServerAction<crate::board::MoveCard>,
454+) -> impl IntoView {
455+ // An effect never runs on the server, so this is false through SSR and
456+ // true once — and only if — hydration happens. It is the honest answer to
457+ // "is script running here", which is the question the button depends on.
458+ let live = RwSignal::new(false);
459+ Effect::new(move |_| live.set(true));
460+
461+ let here = column_id.clone();
462+ let done_column = columns
463+ .iter()
464+ .find(|column| column.id == column_id)
465+ .map(|column| column.is_done)
466+ .unwrap_or(false);
467+
468+ view! {
469+ <ActionForm action=action attr:class="status-form">
470+ <input type="hidden" name="task_id" value=move || task_id.get_value()/>
471+ <input type="hidden" name="from_column_id" value=column_id.clone()/>
472+ <span class="status-dot" class:status-dot-done=done_column></span>
473+ <select
474+ class="status-select"
475+ name="to_column_id"
476+ on:change=move |event| {
477+ let _ = &event;
478+ #[cfg(feature = "hydrate")]
479+ {
480+ use leptos::wasm_bindgen::JsCast;
481+ if let Some(select) = event
482+ .target()
483+ .and_then(|target| {
484+ target.dyn_into::<leptos::web_sys::HtmlSelectElement>().ok()
485+ })
486+ {
487+ if let Some(form) = select.form() {
488+ let _ = form.request_submit();
489+ }
490+ }
491+ }
492+ }
493+ >
494+ {columns
495+ .iter()
496+ .map(|column| {
497+ let selected = column.id == here;
498+ view! {
499+ <option value=column.id.clone() selected=selected>
500+ {column.name.clone()}
501+ </option>
502+ }
503+ })
504+ .collect_view()}
505+ </select>
506+ <Show when=move || !live.get()>
507+ <button class="status-go" type="submit">
508+ "Move"
509+ </button>
510+ </Show>
511+ </ActionForm>
512+ }
513+}
514+
435 /// Whatever a refused call said, rendered where the form that asked for it is.515 /// Whatever a refused call said, rendered where the form that asked for it is.
436 /// A refusal shown at the far end of a scrolling modal is a refusal nobody516 /// A refusal shown at the far end of a scrolling modal is a refusal nobody
437 /// reads: the person clicked "Link a task" and the sentence has to land under517 /// reads: the person clicked "Link a task" and the sentence has to land under
@@ -545,6 +625,8 @@ fn DetailScreen(
545 let description = detail.description.clone();625 let description = detail.description.clone();
546 let description_field = detail.description.clone();626 let description_field = detail.description.clone();
547 let status = detail.column.name.clone();627 let status = detail.column.name.clone();
628+ let column_id = detail.column.id.clone();
629+ let columns = detail.columns.clone();
548 let deadline_input = detail.deadline_input();630 let deadline_input = detail.deadline_input();
549 let overdue = detail.is_overdue(today);631 let overdue = detail.is_overdue(today);
550 let deadline_label = detail.deadline_label(today);632 let deadline_label = detail.deadline_label(today);
@@ -555,12 +637,13 @@ fn DetailScreen(
555 let comment = ServerAction::<PostComment>::new();637 let comment = ServerAction::<PostComment>::new();
556 let link = ServerAction::<LinkTasks>::new();638 let link = ServerAction::<LinkTasks>::new();
557 let remove = ServerAction::<DeleteTask>::new();639 let remove = ServerAction::<DeleteTask>::new();
640+ let move_to = ServerAction::<crate::board::MoveCard>::new();
558 // Deleting is two steps: ask what it would cost, then say it out loud and641 // Deleting is two steps: ask what it would cost, then say it out loud and
559 // let the person decide. The artboard's red button had no confirmation and642 // let the person decide. The artboard's red button had no confirmation and
560 // this action reaches other people's tasks.643 // this action reaches other people's tasks.
561 let ask = ServerAction::<WhatDeleteCosts>::new();644 let ask = ServerAction::<WhatDeleteCosts>::new();
562 // Every action that lands re-reads the task and the board behind it.645 // Every action that lands re-reads the task and the board behind it.
563- for value in [save.value(), comment.value(), link.value()] {646+ for value in [save.value(), comment.value(), link.value(), move_to.value()] {
564 Effect::new(move |_| {647 Effect::new(move |_| {
565 if matches!(value.get(), Some(Ok(None))) {648 if matches!(value.get(), Some(Ok(None))) {
566 on_change();649 on_change();
@@ -589,9 +672,21 @@ fn DetailScreen(
589 <div class="detail-fields">672 <div class="detail-fields">
590 <div class="detail-field">673 <div class="detail-field">
591 <span class="detail-label">"STATUS"</span>674 <span class="detail-label">"STATUS"</span>
592- // Read-only this slice: moving a card is the transition the675+ {if may_write {
593- // mail rules watch, and it lands with the drag.676+ Either::Left(
594- <span class="detail-value">{status}</span>677+ view! {
678+ <StatusControl
679+ task_id=id
680+ column_id=column_id
681+ columns=columns
682+ action=move_to
683+ />
684+ },
685+ )
686+ } else {
687+ Either::Right(view! { <span class="detail-value">{status}</span> })
688+ }}
689+ {refused(move_to)}
595 </div>690 </div>
596 <div class="detail-field">691 <div class="detail-field">
597 <span class="detail-label">692 <span class="detail-label">
crates/dizey-web/tests/http.rsmodified+157 -0
@@ -522,3 +522,160 @@ async fn a_member_may_delete_and_the_delete_is_soft() {
522 .await;522 .await;
523 assert_eq!(answer.body, "{\"Err\":\"NotFound\"}");523 assert_eq!(answer.body, "{\"Err\":\"NotFound\"}");
524 }524 }
525+
526+/// Every column id on the board, in order, read off the wire the way the
527+/// browser would.
528+async fn columns_of(app: &App, cookie: &str) -> Vec<String> {
529+ let answer = app
530+ .post(path::<dizey_web::board::CurrentBoard>(), Some(cookie), &[])
531+ .await;
532+ assert_eq!(answer.status, StatusCode::OK, "{}", answer.body);
533+ answer
534+ .body
535+ .split("{\"column\":{\"id\":\"")
536+ .skip(1)
537+ .filter_map(|rest| rest.split('"').next())
538+ .map(str::to_string)
539+ .collect()
540+}
541+
542+#[tokio::test]
543+async fn a_viewer_who_posts_a_move_anyway_is_refused() {
544+ let app = App::open().await;
545+ let admin = admin(&app).await;
546+ let viewer = invited(&app, &admin, "quiet@dizey.sh", "Quiet Reader", Role::Viewer).await;
547+ let columns = columns_of(&app, &admin).await;
548+ let task = a_task(&app, &admin, &columns[0], "Stays in Backlog").await;
549+
550+ let answer = app
551+ .post(
552+ path::<dizey_web::board::MoveCard>(),
553+ Some(&viewer),
554+ &[
555+ ("task_id", &task),
556+ ("from_column_id", &columns[0]),
557+ ("to_column_id", &columns[1]),
558+ ],
559+ )
560+ .await;
561+
562+ assert_eq!(answer.status, StatusCode::OK, "{}", answer.body);
563+ assert_eq!(answer.body, "\"Forbidden\"");
564+
565+ // And nothing moved: the refusal is in the handler, not in the drawing.
566+ let answer = app
567+ .post(
568+ path::<dizey_web::detail::FetchTask>(),
569+ Some(&admin),
570+ &[("task_id", &task)],
571+ )
572+ .await;
573+ assert!(
574+ answer.body.contains(&columns[0]),
575+ "the refused move happened anyway: {}",
576+ answer.body
577+ );
578+}
579+
580+#[tokio::test]
581+async fn a_member_may_move_a_card() {
582+ let app = App::open().await;
583+ let admin = admin(&app).await;
584+ let member = invited(&app, &admin, "mo@dizey.sh", "Mo Dubois", Role::Member).await;
585+ let columns = columns_of(&app, &admin).await;
586+ let task = a_task(&app, &admin, &columns[0], "Gets picked up").await;
587+
588+ let answer = app
589+ .post(
590+ path::<dizey_web::board::MoveCard>(),
591+ Some(&member),
592+ &[
593+ ("task_id", &task),
594+ ("from_column_id", &columns[0]),
595+ ("to_column_id", &columns[1]),
596+ ],
597+ )
598+ .await;
599+ assert_eq!(answer.body, "null", "a member was refused: {}", answer.body);
600+
601+ let answer = app
602+ .post(
603+ path::<dizey_web::detail::FetchTask>(),
604+ Some(&admin),
605+ &[("task_id", &task)],
606+ )
607+ .await;
608+ assert!(answer.body.contains("\"moved\""), "no move in the activity");
609+}
610+
611+#[tokio::test]
612+async fn a_drop_decided_against_a_stale_board_is_refused() {
613+ let app = App::open().await;
614+ let admin = admin(&app).await;
615+ let member = invited(&app, &admin, "mo@dizey.sh", "Mo Dubois", Role::Member).await;
616+ let columns = columns_of(&app, &admin).await;
617+ let task = a_task(&app, &admin, &columns[0], "Contested").await;
618+
619+ // Two people picked the same card up out of Backlog.
620+ let first = app
621+ .post(
622+ path::<dizey_web::board::MoveCard>(),
623+ Some(&admin),
624+ &[
625+ ("task_id", &task),
626+ ("from_column_id", &columns[0]),
627+ ("to_column_id", &columns[1]),
628+ ],
629+ )
630+ .await;
631+ assert_eq!(first.body, "null");
632+
633+ let second = app
634+ .post(
635+ path::<dizey_web::board::MoveCard>(),
636+ Some(&member),
637+ &[
638+ ("task_id", &task),
639+ ("from_column_id", &columns[0]),
640+ ("to_column_id", &columns[2]),
641+ ],
642+ )
643+ .await;
644+ assert_eq!(second.body, "\"MovedAlready\"");
645+
646+ // The winner's move stands, and there is exactly one crossing.
647+ let answer = app
648+ .post(
649+ path::<dizey_web::detail::FetchTask>(),
650+ Some(&admin),
651+ &[("task_id", &task)],
652+ )
653+ .await;
654+ assert_eq!(
655+ answer.body.matches("\"moved\"").count(),
656+ 1,
657+ "the second drop wrote a crossing too: {}",
658+ answer.body
659+ );
660+}
661+
662+#[tokio::test]
663+async fn a_card_cannot_be_moved_into_another_boards_column() {
664+ let app = App::open().await;
665+ let admin = admin(&app).await;
666+ let columns = columns_of(&app, &admin).await;
667+ let task = a_task(&app, &admin, &columns[0], "Stays put").await;
668+
669+ let answer = app
670+ .post(
671+ path::<dizey_web::board::MoveCard>(),
672+ Some(&admin),
673+ &[
674+ ("task_id", &task),
675+ ("from_column_id", &columns[0]),
676+ ("to_column_id", "00000000-0000-0000-0000-000000000000"),
677+ ],
678+ )
679+ .await;
680+ assert_eq!(answer.body, "\"Forbidden\"");
681+}
style/main.scssmodified+54 -0
@@ -453,6 +453,14 @@ a {
453 }453 }
454 }454 }
455 455
456+/* Where a dragged card may land. Only columns it is not already in light up:
457+ the card's own column is not a destination. */
458+.column-drop {
459+ outline: 1px dashed var(--border-strong);
460+ outline-offset: 4px;
461+ border-radius: 8px;
462+}
463+
456 .card-done {464 .card-done {
457 background: #faf9f4;465 background: #faf9f4;
458 box-shadow: none;466 box-shadow: none;
@@ -729,6 +737,52 @@ a {
729 font-size: 13px;737 font-size: 13px;
730 }738 }
731 739
740+/* The artboard's STATUS control: a dot, the column name, and the chevron the
741+ select draws for itself. It is a form, so it works without script; the
742+ submit button beside it is drawn only when script is absent. */
743+.status-form {
744+ position: relative;
745+ display: flex;
746+ align-items: center;
747+ gap: 8px;
748+ min-height: 36px;
749+ padding: 0 10px;
750+ background: #faf9f6;
751+ border: 1px solid var(--border-strong);
752+ border-radius: var(--radius);
753+}
754+
755+.status-dot {
756+ width: 7px;
757+ height: 7px;
758+ border-radius: 50%;
759+ background: var(--accent);
760+ flex-shrink: 0;
761+}
762+
763+.status-dot-done { background: #5c7b46; }
764+
765+.status-select {
766+ flex-grow: 1;
767+ border: none;
768+ background: none;
769+ font: inherit;
770+ font-size: 13px;
771+ color: inherit;
772+ padding: 0;
773+ cursor: pointer;
774+ outline: none;
775+}
776+
777+.status-go {
778+ border: 1px solid var(--border-strong);
779+ border-radius: 4px;
780+ background: var(--surface);
781+ font-size: 11px;
782+ padding: 2px 6px;
783+ cursor: pointer;
784+}
785+
732 .detail-overdue {786 .detail-overdue {
733 border-color: #e8c9b8;787 border-color: #e8c9b8;
734 color: #a04a24;788 color: #a04a24;