refactor: Generalize form state handling

This commit is contained in:
Erik Kundt
2023-09-05 11:46:37 +02:00
parent 6cce1fd1a6
commit 9b3095e470
2 changed files with 34 additions and 60 deletions
+25 -15
View File
@@ -7,10 +7,9 @@ use radicle_tui::ui::widget::common::container::{
AppHeader, GlobalListener, LabeledContainer, Popup,
};
use radicle_tui::ui::widget::common::context::{ContextBar, Shortcuts};
use radicle_tui::ui::widget::common::form::{Form, TextInput};
use radicle_tui::ui::widget::common::form::Form;
use radicle_tui::ui::widget::common::list::PropertyList;
use radicle_tui::ui::widget::home::{Dashboard, IssueBrowser, PatchBrowser};
use radicle_tui::ui::widget::issue::NewForm;
use radicle_tui::ui::widget::{issue, patch};
use radicle_tui::ui::widget::Widget;
@@ -184,7 +183,7 @@ impl tuirealm::Component<Message, NoUserEvent> for Widget<issue::NewForm> {
Event::Keyboard(KeyEvent {
code: Key::Enter, ..
}) => {
self.perform(Cmd::Custom(TextInput::CMD_NEWLINE));
self.perform(Cmd::Custom(Form::CMD_NEWLINE));
Some(Message::Tick)
}
Event::Keyboard(KeyEvent {
@@ -192,35 +191,46 @@ impl tuirealm::Component<Message, NoUserEvent> for Widget<issue::NewForm> {
modifiers: KeyModifiers::CONTROL,
}) => {
match self.perform(Cmd::Submit) {
CmdResult::Submit(State::Map(inputs)) => {
CmdResult::Submit(State::Linked(mut states)) => {
let mut missing_values = vec![];
let title = match inputs.get(NewForm::INPUT_TITLE) {
Some(StateValue::String(title)) if !title.is_empty() => {
let title = match states.front() {
Some(State::One(StateValue::String(title))) if !title.is_empty() => {
Some(title.clone())
}
_ => None,
};
let tags = match inputs.get(NewForm::INPUT_TAGS) {
Some(StateValue::String(tags)) => Some(tags.clone()),
states.pop_front();
let tags = match states.front() {
Some(State::One(StateValue::String(tags))) => Some(tags.clone()),
_ => Some(String::from("[]")),
};
let assignees = match inputs.get(NewForm::INPUT_ASSIGNESS) {
Some(StateValue::String(assignees)) => Some(assignees.clone()),
states.pop_front();
let assignees = match states.front() {
Some(State::One(StateValue::String(assignees))) => {
Some(assignees.clone())
}
_ => Some(String::from("[]")),
};
let description = match inputs.get(NewForm::INPUT_DESCRIPTION) {
Some(StateValue::String(description)) if !description.is_empty() => {
states.pop_front();
let description = match states.front() {
Some(State::One(StateValue::String(description)))
if !description.is_empty() =>
{
Some(description.clone())
}
_ => None,
};
states.pop_front();
if title.is_none() {
missing_values.push(NewForm::INPUT_TITLE);
missing_values.push("title");
}
if description.is_none() {
missing_values.push(NewForm::INPUT_DESCRIPTION);
missing_values.push("description");
}
// show error popup if missing.
@@ -263,7 +273,7 @@ impl tuirealm::Component<Message, NoUserEvent> for Widget<issue::NewForm> {
code: Key::Char('v'),
modifiers: KeyModifiers::CONTROL,
}) => {
self.perform(Cmd::Custom(TextInput::CMD_PASTE));
self.perform(Cmd::Custom(Form::CMD_PASTE));
Some(Message::Tick)
}
Event::Keyboard(KeyEvent {
+9 -45
View File
@@ -1,12 +1,9 @@
use std::collections::HashMap;
use radicle::cob::thread::Comment;
use radicle::cob::thread::CommentId;
use radicle::cob::issue::Issue;
use radicle::cob::issue::IssueId;
use tuirealm::tui::layout::{Constraint, Direction, Layout};
use tuirealm::StateValue;
use super::common::container::{Container, LabeledContainer};
use super::common::context::{ContextBar, Progress};
@@ -20,7 +17,8 @@ use crate::ui::cob;
use crate::ui::cob::IssueItem;
use crate::ui::context::Context;
use crate::ui::theme::Theme;
use crate::ui::widget::common::form::TextInput;
use crate::ui::widget::common::form::TextArea;
use crate::ui::widget::common::form::TextField;
use super::*;
@@ -254,28 +252,17 @@ pub struct NewForm {
}
impl NewForm {
pub const INPUT_TITLE: &str = "title";
pub const INPUT_TAGS: &str = "tags";
pub const INPUT_ASSIGNESS: &str = "assignees";
pub const INPUT_DESCRIPTION: &str = "description";
pub fn new(theme: &Theme) -> Self {
use tuirealm::props::Layout;
let title = Widget::new(TextInput::new(theme.clone(), "Title", false, true));
let tags = Widget::new(TextInput::new(
theme.clone(),
"Labels (bug, ...)",
false,
true,
));
let assignees = Widget::new(TextInput::new(
let title = Widget::new(TextField::new(theme.clone(), "Title")).to_boxed();
let tags = Widget::new(TextField::new(theme.clone(), "Labels (bug, ...)")).to_boxed();
let assignees = Widget::new(TextField::new(
theme.clone(),
"Assignees (z6MkvAdxCp1oLVVTsqYvev9YrhSN3gBQNUSM45hhy4pgkexk, ...)",
false,
true,
));
let description = Widget::new(TextInput::new(theme.clone(), "Description", true, false));
))
.to_boxed();
let description = Widget::new(TextArea::new(theme.clone(), "Description")).to_boxed();
let mut form = Widget::new(Form::new(
theme.clone(),
@@ -314,30 +301,7 @@ impl WidgetComponent for NewForm {
}
fn perform(&mut self, _properties: &Props, cmd: Cmd) -> CmdResult {
match self.form.perform(cmd) {
CmdResult::Submit(State::Vec(values)) => {
let inputs = HashMap::from([
(
Self::INPUT_TITLE.to_owned(),
values.get(0).unwrap_or(&StateValue::None).clone(),
),
(
Self::INPUT_TAGS.to_owned(),
values.get(1).unwrap_or(&StateValue::None).clone(),
),
(
Self::INPUT_ASSIGNESS.to_owned(),
values.get(2).unwrap_or(&StateValue::None).clone(),
),
(
Self::INPUT_DESCRIPTION.to_owned(),
values.get(3).unwrap_or(&StateValue::None).clone(),
),
]);
CmdResult::Submit(State::Map(inputs))
}
_ => CmdResult::None,
}
self.form.perform(cmd)
}
}