use crate::reply::proposals::Proposal;
use crate::ui::Model;
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Proposing {
pub id: String,
pub typed: String,
pub posted: bool,
}
impl Proposing {
pub fn armed(&self) -> bool {
self.typed.trim() == self.id
}
}
impl Model {
pub fn name_proposal(&mut self, id: &str) {
let Some(pane) = self.configuring.as_mut() else {
return;
};
pane.proposal = Some(Proposing {
id: id.to_owned(),
..Proposing::default()
});
self.unwhole();
}
pub fn unname_proposal(&mut self) {
if let Some(pane) = self.configuring.as_mut() {
pane.proposal = None;
}
self.unwhole();
}
pub fn named_proposal(&self) -> Option<Proposing> {
self.configuring.as_ref()?.proposal.clone()
}
pub(crate) fn verdict_box(&mut self) -> Option<&mut String> {
Some(&mut self.configuring.as_mut()?.proposal.as_mut()?.typed)
}
pub fn staged_proposal(&self, id: &str) -> Option<Proposal> {
self.proposals
.as_ref()?
.rows
.iter()
.find(|row| row.id == id)
.cloned()
}
pub fn proposing(&self) -> Option<String> {
let id = self.named_proposal()?.id;
self.staged_proposal(&id).map(|row| row.id)
}
pub fn settle_proposal(&mut self, verdict: &str) {
let live = self.proposing().is_some();
let Some(workspace) = self.aim.as_ref().map(|aim| aim.address.clone()) else {
return;
};
let Some(held) = self
.configuring
.as_mut()
.and_then(|pane| pane.proposal.as_mut())
.filter(|held| live && held.armed())
else {
return;
};
held.posted = true;
let gesture = crate::verbs::proposal(workspace, held.id.clone(), verdict.to_owned());
self.outbox.push(crate::ui::Posted::act(gesture));
}
fn unwhole(&mut self) {
if let Some(held) = self.proposals.as_mut() {
held.whole = None;
}
}
}
#[cfg(test)]
mod tests;