|
1 |
+ |
//! Contexts, authored as spans rather than checked off a week.
|
|
2 |
+ |
//!
|
|
3 |
+ |
//! <!-- wiki: quasi-overview -->
|
|
4 |
+ |
//!
|
|
5 |
+ |
//! A context is a state that frames days, as distinct from an event that
|
|
6 |
+ |
//! occupies them. The model landed 2026-08-18 (`c5f74e0f`) and shipped with two
|
|
7 |
+ |
//! ways in, both narrow: migration 067 converts a multi-day event or a
|
|
8 |
+ |
//! `vacation_days` run, and the weekly review's seven checkboxes write spans
|
|
9 |
+ |
//! through `commands::weekly_review::set_vacation_week`. The checkboxes can only
|
|
10 |
+ |
//! say *Vacation*, only inside one week, and cannot name what they wrote. A
|
|
11 |
+ |
//! trip, an illness and a sprint are `ContextKind` variants nothing could
|
|
12 |
+ |
//! create.
|
|
13 |
+ |
//!
|
|
14 |
+ |
//! This is the place to record one as what it is: a label, a kind, and two
|
|
15 |
+ |
//! dates.
|
|
16 |
+ |
//!
|
|
17 |
+ |
//! # Where it goes, and why not the calendar
|
|
18 |
+ |
//!
|
|
19 |
+ |
//! Not a calendar. `4a1237b6` still stands: a context sits behind the day's axis
|
|
20 |
+ |
//! rather than on it, and putting it on a timeline is the conflation the model
|
|
21 |
+ |
//! exists to undo. Its own screen, marked at the Day place, reached from the day
|
|
22 |
+ |
//! band beside the banners it explains. The nav is untouched: [`shell`]'s places
|
|
23 |
+ |
//! are the transcription of `navigation.js`'s `TAB_GROUPS`, and a twelfth place
|
|
24 |
+ |
//! would be this module answering a product question rather than a placement
|
|
25 |
+ |
//! one.
|
|
26 |
+ |
//!
|
|
27 |
+ |
//! # The shape
|
|
28 |
+ |
//!
|
|
29 |
+ |
//! - `GET /contexts` — the document: the list, and a pane.
|
|
30 |
+ |
//! - `GET /contexts/new` — the create form, in the pane.
|
|
31 |
+ |
//! - `GET /contexts/{id}` — the edit form, in the pane, filled.
|
|
32 |
+ |
//! - `POST /contexts` — create.
|
|
33 |
+ |
//! - `POST /contexts/{id}` — re-label, re-kind, re-span.
|
|
34 |
+ |
//! - `POST /contexts/{id}/delete` — delete, which puts a migrated event back.
|
|
35 |
+ |
//!
|
|
36 |
+ |
//! Every write answers with the whole screen for [`super::projects::wrote`]'s
|
|
37 |
+ |
//! reason: a write lands in the list and in the pane at once, and a `Response`
|
|
38 |
+ |
//! names one region.
|
|
39 |
+ |
//!
|
|
40 |
+ |
//! # What this port could not say, and did not fake
|
|
41 |
+ |
//!
|
|
42 |
+ |
//! **A date interval is one question with two ends, and there is no way to say
|
|
43 |
+ |
//! so.** `FieldKind::Interval` exists and is exactly the right shape: it carries
|
|
44 |
+ |
//! two names, it says the ends constrain each other, and it gives a crossing
|
|
45 |
+ |
//! fault one place to be reported instead of two. What it cannot say is that its
|
|
46 |
+ |
//! ends are dates. The kind is the value's kind, so an interval's ends are
|
|
47 |
+ |
//! whatever `Interval` draws, and `quasi_webview::node::refill` emits them as
|
|
48 |
+ |
//! the numeric pair its two measured sites wanted (audiofiles' BPM axes, the MNW
|
|
49 |
+ |
//! server's price pair).
|
|
50 |
+ |
//!
|
|
51 |
+ |
//! So the span is two `FieldKind::Date` fields here, and the crossing rule is
|
|
52 |
+ |
//! checked in [`validate`] and reported on the start. That is the honest
|
|
53 |
+ |
//! description of two controls with no stated relationship, and it is worse than
|
|
54 |
+ |
//! the one member would be: "off from the 3rd to the 17th" is one decision, and
|
|
55 |
+ |
//! this screen exists because storing its projection as if it were the fact felt
|
|
56 |
+ |
//! wrong to write.
|
|
57 |
+ |
//!
|
|
58 |
+ |
//! Recorded here rather than filed as a task, in `events`' style: it is a
|
|
59 |
+ |
//! vocabulary gap seen from a second screen, and what it wants is `Interval`
|
|
60 |
+ |
//! learning what its ends are rather than a new member.
|
|
61 |
+ |
|
|
62 |
+ |
// Handlers take their request by value because `quasi_router::Handler` is a
|
|
63 |
+ |
// plain `fn(&S, Request)` pointer, so the signature is the router's and not a
|
|
64 |
+ |
// choice made here.
|
|
65 |
+ |
#![allow(clippy::needless_pass_by_value)]
|
|
66 |
+ |
|
|
67 |
+ |
use chrono::NaiveDate;
|
|
68 |
+ |
use goingson_core::id_types::ContextId;
|
|
69 |
+ |
use goingson_core::models::{Context, ContextKind};
|
|
70 |
+ |
use quasi_router::screen::{Act, Choice, Field, Row, Tag};
|
|
71 |
+ |
use quasi_router::{Action, Node, RegionKind, Response, RouteError, Router, Screen, Slot};
|
|
72 |
+ |
|
|
73 |
+ |
use crate::state::{AppState, DESKTOP_USER_ID};
|
|
74 |
+ |
|
|
75 |
+ |
#[cfg(test)]
|
|
76 |
+ |
mod tests;
|
|
77 |
+ |
|
|
78 |
+ |
/// The kinds a person can choose.
|
|
79 |
+ |
///
|
|
80 |
+ |
/// Every variant except `Other`, which is what an unknown stored kind parses to
|
|
81 |
+ |
/// rather than something anyone picks: offering it would ask the user to file a
|
|
82 |
+ |
/// context as "something else" when the label is already where they say what it
|
|
83 |
+ |
/// is.
|
|
84 |
+ |
const KINDS: &[ContextKind] = &[
|
|
85 |
+ |
ContextKind::Vacation,
|
|
86 |
+ |
ContextKind::Trip,
|
|
87 |
+ |
ContextKind::Illness,
|
|
88 |
+ |
ContextKind::Sprint,
|
|
89 |
+ |
];
|
|
90 |
+ |
|
|
91 |
+ |
/// What a kind is called on screen.
|
|
92 |
+ |
///
|
|
93 |
+ |
/// Apart from [`ContextKind::as_str`], which is the stored value and the wire's.
|
|
94 |
+ |
/// A display name that drifted from the stored one would be a second vocabulary;
|
|
95 |
+ |
/// these agree today and are separate so that a rename of either is not
|
|
96 |
+ |
/// automatically a migration.
|
|
97 |
+ |
fn kind_label(kind: ContextKind) -> &'static str {
|
|
98 |
+ |
match kind {
|
|
99 |
+ |
ContextKind::Vacation => "Vacation",
|
|
100 |
+ |
ContextKind::Trip => "Trip",
|
|
101 |
+ |
ContextKind::Illness => "Illness",
|
|
102 |
+ |
ContextKind::Sprint => "Sprint",
|
|
103 |
+ |
ContextKind::Other => "Other",
|
|
104 |
+ |
}
|
|
105 |
+ |
}
|
|
106 |
+ |
|
|
107 |
+ |
/// Every context, earliest first.
|
|
108 |
+ |
fn all(state: &AppState) -> Result<Vec<Context>, RouteError> {
|
|
109 |
+ |
state
|
|
110 |
+ |
.contexts
|
|
111 |
+ |
.list_all(DESKTOP_USER_ID)
|
|
112 |
+ |
.map_err(|error| RouteError::internal(error.to_string()))
|
|
113 |
+ |
}
|
|
114 |
+ |
|
|
115 |
+ |
/// One context by id.
|
|
116 |
+ |
///
|
|
117 |
+ |
/// `ContextRepository` has no `get`, and adding one for this screen would be
|
|
118 |
+ |
/// storage work the task said this is not. The list is the whole set a person
|
|
119 |
+ |
/// authored, so scanning it costs nothing a person would notice.
|
|
120 |
+ |
fn load(state: &AppState, id: ContextId) -> Result<Context, RouteError> {
|
|
121 |
+ |
all(state)?
|
|
122 |
+ |
.into_iter()
|
|
123 |
+ |
.find(|context| context.id == id)
|
|
124 |
+ |
.ok_or_else(|| RouteError::not_found("no such context"))
|
|
125 |
+ |
}
|
|
126 |
+ |
|
|
127 |
+ |
/// The id in the path.
|
|
128 |
+ |
fn context_id(request: &quasi_router::Request) -> Result<ContextId, RouteError> {
|
|
129 |
+ |
let raw = request
|
|
130 |
+ |
.captures
|
|
131 |
+ |
.get("id")
|
|
132 |
+ |
.ok_or_else(|| RouteError::not_found("no context id"))?;
|
|
133 |
+ |
Ok(ContextId::from(
|
|
134 |
+ |
uuid::Uuid::parse_str(raw).map_err(|_| RouteError::not_found("not a context id"))?,
|
|
135 |
+ |
))
|
|
136 |
+ |
}
|
|
137 |
+ |
|
|
138 |
+ |
/// How a context reads in the list.
|
|
139 |
+ |
///
|
|
140 |
+ |
/// The span is the secondary line rather than a token, because it is what the
|
|
141 |
+ |
/// record *is*: a label without its dates is not a context, and a token would
|
|
142 |
+ |
/// rank it beside the kind.
|
|
143 |
+ |
fn row_for(context: &Context, current: bool) -> Row {
|
|
144 |
+ |
let days = context.days();
|
|
145 |
+ |
let mut row = Row::new(&context.label)
|
|
146 |
+ |
.token(Tag::badge(kind_label(context.kind)))
|
|
147 |
+ |
.secondary(format!(
|
|
148 |
+ |
"{} to {} ({} day{})",
|
|
149 |
+ |
context.starts_on.format("%-d %b %Y"),
|
|
150 |
+ |
context.ends_on.format("%-d %b %Y"),
|
|
151 |
+ |
days,
|
|
152 |
+ |
if days == 1 { "" } else { "s" }
|
|
153 |
+ |
));
|
|
154 |
+ |
|
|
155 |
+ |
if context.migrated_from_event_id.is_some() {
|
|
156 |
+ |
row = row.token(Tag::badge("From an event"));
|
|
157 |
+ |
}
|
|
158 |
+ |
|
|
159 |
+ |
row.current = current;
|
|
160 |
+ |
row.activate = Some(Action::get(format!("/contexts/{}", context.id)));
|
|
161 |
+ |
row
|
|
162 |
+ |
}
|
|
163 |
+ |
|
|
164 |
+ |
/// The list of contexts.
|
|
165 |
+ |
fn list_node(state: &AppState, current: Option<ContextId>) -> Result<Node, RouteError> {
|
|
166 |
+ |
let contexts = all(state)?;
|
|
167 |
+ |
if contexts.is_empty() {
|
|
168 |
+ |
return Ok(Node::empty("No contexts yet.").offering(Act::new(
|
|
169 |
+ |
"Record your first context",
|
|
170 |
+ |
Action::get("/contexts/new"),
|
|
171 |
+ |
)));
|
|
172 |
+ |
}
|
|
173 |
+ |
Ok(Node::list(contexts.iter().map(|context| {
|
|
174 |
+ |
row_for(context, current == Some(context.id))
|
|
175 |
+ |
})))
|
|
176 |
+ |
}
|
|
177 |
+ |
|
|
178 |
+ |
/// The form's fields, for a create or an edit.
|
|
179 |
+ |
///
|
|
180 |
+ |
/// `existing` fills them for an edit; `submitted` refills them after a refusal,
|
|
181 |
+ |
/// and wins, because what the user just typed is nearer to what they meant than
|
|
182 |
+ |
/// what is stored. [`Field::refilled`] is the same repair `1c4a66a4` closed on
|
|
183 |
+ |
/// quasi-router.
|
|
184 |
+ |
fn form_fields(
|
|
185 |
+ |
existing: Option<&Context>,
|
|
186 |
+ |
errors: &[(&str, String)],
|
|
187 |
+ |
submitted: Option<&quasi_router::Params>,
|
|
188 |
+ |
) -> Vec<Field> {
|
|
189 |
+ |
let error_for = |name: &str| {
|
|
190 |
+ |
errors
|
|
191 |
+ |
.iter()
|
|
192 |
+ |
.find(|(field, _)| *field == name)
|
|
193 |
+ |
.map(|(_, message)| message.clone())
|
|
194 |
+ |
};
|
|
195 |
+ |
let apply = |field: Field, name: &str| match error_for(name) {
|
|
196 |
+ |
Some(message) => field.error(message),
|
|
197 |
+ |
None => field,
|
|
198 |
+ |
};
|
|
199 |
+ |
|
|
200 |
+ |
let mut label = Field::new(makeover_layout::FieldKind::Text, "label", "Label").required();
|
|
201 |
+ |
label.placeholder = Some("Two weeks in Lisbon".to_owned());
|
|
202 |
+ |
if let Some(context) = existing {
|
|
203 |
+ |
label = label.value(context.label.clone());
|
|
204 |
+ |
}
|
|
205 |
+ |
|
|
206 |
+ |
let mut kind = Field::select(
|
|
207 |
+ |
"kind",
|
|
208 |
+ |
"Kind",
|
|
209 |
+ |
KINDS
|
|
210 |
+ |
.iter()
|
|
211 |
+ |
.map(|kind| Choice::new(kind.as_str(), kind_label(*kind)))
|
|
212 |
+ |
.collect(),
|
|
213 |
+ |
);
|
|
214 |
+ |
if let Some(context) = existing {
|
|
215 |
+ |
kind = kind.value(context.kind.as_str());
|
|
216 |
+ |
}
|
|
217 |
+ |
|
|
218 |
+ |
// Two fields rather than one interval. See the module header.
|
|
219 |
+ |
let mut starts = Field::new(makeover_layout::FieldKind::Date, "starts_on", "First day")
|
|
220 |
+ |
.required()
|
|
221 |
+ |
.hint("The first day inside it.");
|
|
222 |
+ |
let mut ends = Field::new(makeover_layout::FieldKind::Date, "ends_on", "Last day")
|
|
223 |
+ |
.required()
|
|
224 |
+ |
.hint("Inclusive: off until the 17th means the 17th is off.");
|
|
225 |
+ |
if let Some(context) = existing {
|
|
226 |
+ |
starts = starts.value(context.starts_on.to_string());
|
|
227 |
+ |
ends = ends.value(context.ends_on.to_string());
|
|
228 |
+ |
}
|
|
229 |
+ |
|
|
230 |
+ |
let fields = vec![
|
|
231 |
+ |
apply(label, "label"),
|
|
232 |
+ |
apply(kind, "kind"),
|
|
233 |
+ |
apply(starts, "starts_on"),
|
|
234 |
+ |
apply(ends, "ends_on"),
|
|
235 |
+ |
];
|
|
236 |
+ |
|
|
237 |
+ |
match submitted {
|
|
238 |
+ |
Some(params) => fields
|
|
239 |
+ |
.into_iter()
|
|
240 |
+ |
.map(|field| field.refilled(params))
|
|
241 |
+ |
.collect(),
|
|
242 |
+ |
None => fields,
|
|
243 |
+ |
}
|
|
244 |
+ |
}
|
|
245 |
+ |
|
|
246 |
+ |
/// The pane as a region, which is what a fragment answers with.
|
|
247 |
+ |
fn form_pane(
|
|
248 |
+ |
existing: Option<&Context>,
|
|
249 |
+ |
errors: &[(&str, String)],
|
|
250 |
+ |
submitted: Option<&quasi_router::Params>,
|
|
251 |
+ |
) -> Node {
|
|
252 |
+ |
Node::Region(form_slot(existing, errors, submitted))
|
|
253 |
+ |
}
|
|
254 |
+ |
|
|
255 |
+ |
/// The pane: a form, and for an edit the things only an edit can offer.
|
|
256 |
+ |
fn form_slot(
|
|
257 |
+ |
existing: Option<&Context>,
|
|
258 |
+ |
errors: &[(&str, String)],
|
|
259 |
+ |
submitted: Option<&quasi_router::Params>,
|
|
260 |
+ |
) -> Slot {
|
|
261 |
+ |
let mut pane = Slot::new("contexts-detail", RegionKind::Pane);
|
|
262 |
+ |
pane = match existing {
|
|
263 |
+ |
None => pane.with(Node::section("New context")),
|
|
264 |
+ |
Some(context) => pane.with(Node::section(format!("Editing {}", context.label))),
|
|
265 |
+ |
};
|
|
266 |
+ |
|
|
267 |
+ |
// Provenance, and the reversal it buys, said before the controls rather than
|
|
268 |
+ |
// after: deleting a migrated context is the one delete on this screen that
|
|
269 |
+ |
// does something to a record the user did not author here.
|
|
270 |
+ |
if existing.is_some_and(|context| context.migrated_from_event_id.is_some()) {
|
|
271 |
+ |
pane = pane.with(Node::Notice {
|
|
272 |
+ |
kind: makeover_layout::Notice::Banner,
|
|
273 |
+ |
tone: makeover_layout::Tone::Info,
|
|
274 |
+ |
text: "Converted from an event. Deleting this puts that event back on the timeline, \
|
|
275 |
+ |
with its times intact."
|
|
276 |
+ |
.to_owned(),
|
|
277 |
+ |
});
|
|
278 |
+ |
}
|
|
279 |
+ |
|
|
280 |
+ |
pane = pane.with(Node::Form {
|
|
281 |
+ |
action: match existing {
|
|
282 |
+ |
None => Action::post("/contexts"),
|
|
283 |
+ |
Some(context) => Action::post(format!("/contexts/{}", context.id)),
|
|
284 |
+ |
},
|
|
285 |
+ |
submit: match existing {
|
|
286 |
+ |
None => "Record context".to_owned(),
|
|
287 |
+ |
Some(_) => "Save".to_owned(),
|
|
288 |
+ |
},
|
|
289 |
+ |
fields: form_fields(existing, errors, submitted),
|
|
290 |
+ |
});
|
|
291 |
+ |
|
|
292 |
+ |
if let Some(context) = existing {
|
|
293 |
+ |
pane = pane.with(Node::Act(
|
|
294 |
+ |
Act::new(
|
|
295 |
+ |
"Delete",
|
|
296 |
+ |
Action::post(format!("/contexts/{}/delete", context.id)),
|
|
297 |
+ |
)
|
|
298 |
+ |
.tone(makeover_layout::Tone::Danger)
|
|
299 |
+ |
.confirm(match context.migrated_from_event_id {
|
|
300 |
+ |
Some(_) => "Delete this context and put its event back?",
|
|
301 |
+ |
None => "Delete this context?",
|
|
302 |
+ |
}),
|
|
303 |
+ |
));
|
|
304 |
+ |
}
|
|
305 |
+ |
|
|
306 |
+ |
pane
|
|
307 |
+ |
}
|
|
308 |
+ |
|
|
309 |
+ |
/// The whole screen.
|
|
310 |
+ |
fn screen(
|
|
311 |
+ |
state: &AppState,
|
|
312 |
+ |
current: Option<&Context>,
|
|
313 |
+ |
errors: &[(&str, String)],
|
|
314 |
+ |
submitted: Option<&quasi_router::Params>,
|
|
315 |
+ |
) -> Result<Screen, RouteError> {
|
|
316 |
+ |
let band = Slot::new("contexts-band", RegionKind::Band)
|
|
317 |
+ |
.with(Node::page("Contexts"))
|
|
318 |
+ |
.with(Node::act("New context", Action::get("/contexts/new")));
|
|
319 |
+ |
|
|
320 |
+ |
let pane = match current {
|
|
321 |
+ |
Some(context) => form_slot(Some(context), errors, submitted),
|
|
322 |
+ |
None => {
|
|
323 |
+ |
Slot::new("contexts-detail", RegionKind::Pane).with(Node::empty("Nothing selected"))
|
|
324 |
+ |
}
|
|
325 |
+ |
};
|
|
326 |
+ |
|
|
327 |
+ |
Ok(Screen::list_detail("Contexts", false)
|
|
328 |
+ |
// The Day place rather than a twelfth one: a context frames a day, and
|
|
329 |
+ |
// the day view is where its banner is read.
|
|
330 |
+ |
.at_place(super::shell::DAY)
|
|
331 |
+ |
.with(band)
|
|
332 |
+ |
.with(
|
|
333 |
+ |
Slot::new("contexts-list", RegionKind::Pane)
|
|
334 |
+ |
.with(list_node(state, current.map(|context| context.id))?),
|
|
335 |
+ |
)
|
|
336 |
+ |
.with(pane))
|
|
337 |
+ |
}
|
|
338 |
+ |
|
|
339 |
+ |
/// The document.
|
|
340 |
+ |
fn index(state: &AppState, _request: quasi_router::Request) -> Result<Response, RouteError> {
|
|
341 |
+ |
Ok(screen(state, None, &[], None)?.into())
|
|
342 |
+ |
}
|
|
343 |
+ |
|
|
344 |
+ |
/// The create form, in the pane.
|
|
345 |
+ |
fn new(_state: &AppState, _request: quasi_router::Request) -> Result<Response, RouteError> {
|
|
346 |
+ |
Ok(Response::fragment(
|
|
347 |
+ |
"contexts-detail",
|
|
348 |
+ |
form_pane(None, &[], None),
|
|
349 |
+ |
))
|
|
350 |
+ |
}
|
|
351 |
+ |
|
|
352 |
+ |
/// The edit form, filled, in the pane.
|
|
353 |
+ |
fn detail(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> {
|
|
354 |
+ |
let context = load(state, context_id(&request)?)?;
|
|
355 |
+ |
Ok(Response::fragment(
|
|
356 |
+ |
"contexts-detail",
|
|
357 |
+ |
form_pane(Some(&context), &[], None),
|
|
358 |
+ |
))
|
|
359 |
+ |
}
|
|
360 |
+ |
|
|
361 |
+ |
/// What a context has to be before it can be recorded.
|
|
362 |
+ |
///
|
|
363 |
+ |
/// The crossing rule is checked rather than corrected. `ContextRepository`
|
|
364 |
+ |
/// orders the ends on the way in, on the argument that a span typed backwards is
|
|
365 |
+ |
/// a slip whose days are unambiguous, and that is right for a migration reading
|
|
366 |
+ |
/// stored data. It is wrong for a form: silently swapping what someone typed
|
|
367 |
+ |
/// tells them nothing, and the next thing they do is wonder why the dates moved.
|
|
368 |
+ |
fn validate(
|
|
369 |
+ |
label: &str,
|
|
370 |
+ |
starts_on: Option<NaiveDate>,
|
|
371 |
+ |
ends_on: Option<NaiveDate>,
|
|
372 |
+ |
) -> Vec<(&'static str, String)> {
|
|
373 |
+ |
let mut errors = Vec::new();
|
|
374 |
+ |
if label.is_empty() {
|
|
375 |
+ |
errors.push(("label", "A context needs a label.".to_owned()));
|
|
376 |
+ |
} else if label.chars().count() > 100 {
|
|
377 |
+ |
errors.push(("label", "Maximum 100 characters".to_owned()));
|
|
378 |
+ |
}
|
|
379 |
+ |
if starts_on.is_none() {
|
|
380 |
+ |
errors.push(("starts_on", "Needs a first day.".to_owned()));
|
|
381 |
+ |
}
|
|
382 |
+ |
if ends_on.is_none() {
|
|
383 |
+ |
errors.push(("ends_on", "Needs a last day.".to_owned()));
|
|
384 |
+ |
}
|
|
385 |
+ |
if let (Some(starts), Some(ends)) = (starts_on, ends_on)
|
|
386 |
+ |
&& starts > ends
|
|
387 |
+ |
{
|
|
388 |
+ |
errors.push((
|
|
389 |
+ |
"starts_on",
|
|
390 |
+ |
"The first day is after the last one.".to_owned(),
|
|
391 |
+ |
));
|
|
392 |
+ |
}
|
|
393 |
+ |
errors
|
|
394 |
+ |
}
|
|
395 |
+ |
|
|
396 |
+ |
/// A date out of the payload, or `None` if it is missing or unparseable.
|
|
397 |
+ |
///
|
|
398 |
+ |
/// Both cases are the same answer here because both are reported the same way:
|
|
399 |
+ |
/// `FieldKind::Date` states the wire format, so a value that will not parse did
|
|
400 |
+ |
/// not come from the control.
|
|
401 |
+ |
fn date(params: &quasi_router::Params, name: &str) -> Option<NaiveDate> {
|
|
402 |
+ |
params.get(name)?.trim().parse().ok()
|
|
403 |
+ |
}
|
|
404 |
+ |
|
|
405 |
+ |
/// What every write answers with.
|
|
406 |
+ |
///
|
|
407 |
+ |
/// The whole screen, for [`super::projects`]'s reason: a write lands in the list
|
|
408 |
+ |
/// and in the pane at once, and a `Response` names one region. `outerMorph`
|
|
409 |
+ |
/// keeps focus and scroll across it.
|
|
410 |
+ |
fn wrote(state: &AppState, message: &str) -> Result<Response, RouteError> {
|
|
411 |
+ |
Ok(Response::from(screen(state, None, &[], None)?)
|
|
412 |
+ |
.toast(makeover_layout::Tone::Success, message))
|
|
413 |
+ |
}
|
|
414 |
+ |
|
|
415 |
+ |
/// The fields a write reads, validated together.
|
|
416 |
+ |
struct Submitted {
|
|
417 |
+ |
label: String,
|
|
418 |
+ |
kind: ContextKind,
|
|
419 |
+ |
starts_on: NaiveDate,
|
|
420 |
+ |
ends_on: NaiveDate,
|
|
421 |
+ |
}
|
|
422 |
+ |
|
|
423 |
+ |
/// Read and check a submission, or hand back the form saying why not.
|
|
424 |
+ |
///
|
|
425 |
+ |
/// Every complaint at once. Answering with the first one found is how a form is
|
|
426 |
+ |
/// fixed one round trip per mistake.
|
|
427 |
+ |
fn submitted(
|
|
428 |
+ |
request: &quasi_router::Request,
|
|
429 |
+ |
existing: Option<&Context>,
|
|
430 |
+ |
) -> Result<Submitted, Box<Node>> {
|
|
431 |
+ |
let label = request
|
|
432 |
+ |
.payload
|
|
433 |
+ |
.get("label")
|
|
434 |
+ |
.unwrap_or_default()
|
|
435 |
+ |
.trim()
|
|
436 |
+ |
.to_owned();
|
|
437 |
+ |
let starts_on = date(&request.payload, "starts_on");
|
|
438 |
+ |
let ends_on = date(&request.payload, "ends_on");
|
|
439 |
+ |
|
|
440 |
+ |
let errors = validate(&label, starts_on, ends_on);
|
|
441 |
+ |
if !errors.is_empty() {
|
|
442 |
+ |
return Err(Box::new(form_pane(
|
|
443 |
+ |
existing,
|
|
444 |
+ |
&errors,
|
|
445 |
+ |
Some(&request.payload),
|
|
446 |
+ |
)));
|
|
447 |
+ |
}
|
|
448 |
+ |
|
|
449 |
+ |
// Unlike a status or a project type, an unknown kind cannot be refused:
|
|
450 |
+ |
// `ContextKind::parse` maps one to `Other` and cannot fail, deliberately,
|
|
451 |
+ |
// because a row written by a newer client is still a real span of days.
|
|
452 |
+ |
Ok(Submitted {
|
|
453 |
+ |
label,
|
|
454 |
+ |
kind: ContextKind::parse(request.payload.get("kind").unwrap_or_default()),
|
|
455 |
+ |
starts_on: starts_on.expect("validated"),
|
|
456 |
+ |
ends_on: ends_on.expect("validated"),
|
|
457 |
+ |
})
|
|
458 |
+ |
}
|
|
459 |
+ |
|
|
460 |
+ |
/// Record a context.
|
|
461 |
+ |
fn create(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> {
|
|
462 |
+ |
let fields = match submitted(&request, None) {
|
|
463 |
+ |
Ok(fields) => fields,
|
|
464 |
+ |
Err(pane) => return Ok(Response::fragment("contexts-detail", *pane)),
|
|
465 |
+ |
};
|
|
466 |
+ |
|
|
467 |
+ |
state
|
|
468 |
+ |
.contexts
|
|
469 |
+ |
.create(
|
|
470 |
+ |
DESKTOP_USER_ID,
|
|
471 |
+ |
&fields.label,
|
|
472 |
+ |
fields.kind,
|
|
473 |
+ |
fields.starts_on,
|
|
474 |
+ |
fields.ends_on,
|
|
475 |
+ |
)
|
|
476 |
+ |
.map_err(|error| RouteError::internal(error.to_string()))?;
|
|
477 |
+ |
|
|
478 |
+ |
wrote(state, "Context recorded.")
|
|
479 |
+ |
}
|
|
480 |
+ |
|
|
481 |
+ |
/// Re-label, re-kind or re-span one.
|
|
482 |
+ |
fn update(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> {
|
|
483 |
+ |
let id = context_id(&request)?;
|
|
484 |
+ |
let existing = load(state, id)?;
|
|
485 |
+ |
let fields = match submitted(&request, Some(&existing)) {
|
|
486 |
+ |
Ok(fields) => fields,
|
|
487 |
+ |
Err(pane) => return Ok(Response::fragment("contexts-detail", *pane)),
|
|
488 |
+ |
};
|
|
489 |
+ |
|
|
490 |
+ |
state
|
|
491 |
+ |
.contexts
|
|
492 |
+ |
.update(
|
|
493 |
+ |
DESKTOP_USER_ID,
|
|
494 |
+ |
id,
|
|
495 |
+ |
&fields.label,
|
|
496 |
+ |
fields.kind,
|
|
497 |
+ |
fields.starts_on,
|
|
498 |
+ |
fields.ends_on,
|
|
499 |
+ |
)
|
|
500 |
+ |
.map_err(|error| RouteError::internal(error.to_string()))?;
|