Preserve behavior drafts until document commit succeeds
This commit is contained in:
@@ -103,7 +103,28 @@ struct Draft {
|
||||
values: BTreeMap<Id, String>,
|
||||
}
|
||||
impl State {
|
||||
pub(super) fn pending_owner(&self) -> Option<Id> {
|
||||
self.draft.as_ref().map(|d| d.owner)
|
||||
}
|
||||
|
||||
pub(super) fn prepare_pending(&self, p: &Project) -> Result<Command, String> {
|
||||
let command = self
|
||||
.draft
|
||||
.as_ref()
|
||||
.ok_or("Choose a scenario tool first.")?
|
||||
.command(p)?;
|
||||
execute_batch(p, std::slice::from_ref(&command)).map_err(|e| e.to_string())?;
|
||||
Ok(command)
|
||||
}
|
||||
|
||||
pub(super) fn acknowledge_commit(&mut self) {
|
||||
self.draft = None;
|
||||
}
|
||||
|
||||
pub fn update(&mut self, p: &Project, event: Event) -> Result<Option<Command>, String> {
|
||||
if self.draft.is_some() && matches!(&event, Event::Start(..)) {
|
||||
return Err(PENDING_DRAFT.into());
|
||||
}
|
||||
match event {
|
||||
Event::Start(owner, tool) => {
|
||||
if p.elements.get(&owner).map(oa_core::Element::kind) != Some(Kind::Scenario) {
|
||||
@@ -127,14 +148,8 @@ impl State {
|
||||
values: BTreeMap::new(),
|
||||
});
|
||||
}
|
||||
Event::Cancel => self.draft = None,
|
||||
Event::Submit => {
|
||||
let d = self.draft.as_ref().ok_or("Choose a scenario tool first.")?;
|
||||
let command = d.command(p)?;
|
||||
execute_batch(p, std::slice::from_ref(&command)).map_err(|e| e.to_string())?;
|
||||
self.draft = None;
|
||||
return Ok(Some(command));
|
||||
}
|
||||
Event::Cancel => self.acknowledge_commit(),
|
||||
Event::Submit => return self.prepare_pending(p).map(Some),
|
||||
other => {
|
||||
if let Some(d) = &mut self.draft {
|
||||
match other {
|
||||
|
||||
Reference in New Issue
Block a user