Skip to content

Commit

Permalink
Merge pull request #149 from virto-network/fix/initiative-forms
Browse files Browse the repository at this point in the history
Fix/initiative forms
  • Loading branch information
b-avb authored Oct 25, 2024
2 parents d67f4f9 + 744ea21 commit 2eeeaa8
Show file tree
Hide file tree
Showing 11 changed files with 406 additions and 229 deletions.
24 changes: 9 additions & 15 deletions src/components/atoms/combo_input.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use super::dropdown::ElementSize;
use crate::components::atoms::{dropdown::DropdownItem, input::InputType, Dropdown, Input};
use dioxus::prelude::*;
use dioxus_std::{i18n::use_i18, translate};
use crate::components::atoms::{
dropdown::DropdownItem, input::InputType, Dropdown, Input,
};
use super::dropdown::ElementSize;
#[derive(PartialEq, Clone, Debug)]
pub enum ComboInputOption {
Dropdown(DropdownItem),
Expand Down Expand Up @@ -34,16 +32,12 @@ pub fn ComboInput(props: ComboInputProps) -> Element {
let mut option_value = use_signal(|| props.value.option.clone());
let mut input_value = use_signal::<String>(|| props.value.input.clone());
let mut items = vec![];
let dropdown_options = use_signal::<
Vec<DropdownItem>,
>(|| {
let dropdown_options = use_signal::<Vec<DropdownItem>>(|| {
let Some(options) = props.options else {
return vec![
DropdownItem {
key: "Wallet".to_string(),
value: translate!(i18, "onboard.invite.form.wallet.label"),
},
];
return vec![DropdownItem {
key: "Wallet".to_string(),
value: translate!(i18, "onboard.invite.form.wallet.label"),
}];
};
options
});
Expand All @@ -61,7 +55,7 @@ pub fn ComboInput(props: ComboInputProps) -> Element {
size: props.size.clone(),
itype: InputType::Date,
placeholder: props.placeholder.clone(),
error: None,
error: props.error.clone(),
on_input: move |event: Event<FormData>| {
option_value.set(ComboInputOption::Date(event.value().clone()));
props.on_change.call(ComboInputValue { option: ComboInputOption::Date(event.value().clone()), input: input_value().clone() })
Expand Down Expand Up @@ -93,7 +87,7 @@ pub fn ComboInput(props: ComboInputProps) -> Element {
message: props.value.input.clone(),
size: props.size,
placeholder: props.placeholder,
error: None,
error: props.error,
right_text: props.right_text,
on_input: move |event: Event<FormData>| {
input_value.set(event.value().clone());
Expand Down
16 changes: 13 additions & 3 deletions src/components/atoms/input_tags.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use dioxus::prelude::*;
use super::dropdown::ElementSize;
use crate::components::atoms::icons::Close;
use crate::components::atoms::{Icon, IconButton, WarningSign};
use super::dropdown::ElementSize;
use dioxus::prelude::*;
#[derive(PartialEq, Props, Clone)]
pub struct InputTagsEvent {
pub tags: Vec<String>,
Expand Down Expand Up @@ -36,7 +36,17 @@ pub fn InputTags(props: InputTagsProps) -> Element {
};

let is_active = use_signal::<bool>(|| false);
let mut tags = use_signal::<Vec<String>>(|| vec![]);
let mut tags = use_signal::<Vec<String>>(|| {
if props.message.len() > 0 {
props
.message
.split(",")
.map(|e| String::from(e))
.collect::<Vec<String>>()
} else {
vec![]
}
});
let mut complete_value = use_signal(|| String::new());
let mut new_value = use_signal(|| String::new());
let mut temporal_value = use_signal(|| String::new());
Expand Down
17 changes: 13 additions & 4 deletions src/components/molecules/actions/members.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
use dioxus::prelude::*;
use dioxus_std::{i18n::use_i18, translate};
use std::str::FromStr;

use crate::{
components::atoms::{
combo_input::{ComboInputOption, ComboInputValue},
dropdown::{DropdownItem, ElementSize},
icon_button::Variant, AddPlus, ComboInput, Icon, IconButton, MinusCircle,
icon_button::Variant,
AddPlus, ComboInput, Icon, IconButton, MinusCircle,
},
hooks::use_initiative::{
use_initiative, ActionItem, AddMembersAction, MediumOptions, MemberItem,
},
};
use dioxus::prelude::*;
use dioxus_std::{i18n::use_i18, translate};
#[derive(PartialEq, Props, Clone)]
pub struct VotingProps {
index: usize,
Expand All @@ -27,7 +30,7 @@ pub fn MembersAction(props: VotingProps) -> Element {
}, value: match member.medium.clone() {
MediumOptions::Wallet => translate!(i18, "onboard.invite.form.wallet.label"),
} };

rsx!(
li {
ComboInput {
Expand All @@ -39,6 +42,12 @@ pub fn MembersAction(props: VotingProps) -> Element {
placeholder: match member.medium {
MediumOptions::Wallet => translate!(i18, "onboard.invite.form.wallet.placeholder"),
},
error: {
match sp_core::sr25519::Public::from_str(&member.account) {
Ok(_) => None,
Err(_) => Some(translate!(i18, "onboard.invite.form.error.invalid_address")),
}
},
on_change: move |event: ComboInputValue| {
let medium = match event.option {
ComboInputOption::Dropdown(value) => {
Expand Down
17 changes: 15 additions & 2 deletions src/components/molecules/actions/transfer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::str::FromStr;

use crate::{
components::atoms::{
dropdown::ElementSize, icon_button::Variant, AddPlus, Icon, IconButton,
Expand Down Expand Up @@ -32,7 +34,12 @@ pub fn TransferAction(props: VotingProps) -> Element {
message: transfer.account.clone(),
size: ElementSize::Small,
placeholder: translate!(i18, "initiative.steps.actions.community_transfer.dest.placeholder"),
error: None,
error: {
match sp_core::sr25519::Public::from_str(&transfer.account) {
Ok(_) => None,
Err(_) => Some(translate!(i18, "initiative.steps.actions.error.invalid_address")),
}
},
on_input: move |event: Event<FormData>| {
if let ActionItem::CommunityTransfer(ref mut meta) = initiative.get_action(props.index) {
meta.transfers[index_meta].account = event.value() ;
Expand All @@ -46,7 +53,13 @@ pub fn TransferAction(props: VotingProps) -> Element {
message: (transfer.value / KUSAMA_PRECISION_DECIMALS).to_string(),
size: ElementSize::Small,
placeholder: translate!(i18, "initiative.steps.actions.community_transfer.amount.placeholder"),
error: None,
error: {
if transfer.value > 0 {
None
} else {
Some(translate!(i18, "initiative.steps.actions.error.amount"))
}
},
right_text: {
rsx!(
span { class: "input--right__text",
Expand Down
21 changes: 13 additions & 8 deletions src/components/molecules/actions/treasury.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use dioxus::prelude::*;
use dioxus_std::{i18n::use_i18, translate};
use crate::{
components::atoms::{
dropdown::{DropdownItem, ElementSize},
icon_button::Variant, input::InputType, AddPlus, Icon, IconButton, Input,
MinusCircle,
},
hooks::use_initiative::{
use_initiative, ActionItem, KusamaTreasury, KusamaTreasuryAction,
icon_button::Variant,
input::InputType,
AddPlus, Icon, IconButton, Input, MinusCircle,
},
hooks::use_initiative::{use_initiative, ActionItem, KusamaTreasury, KusamaTreasuryAction},
};
use dioxus::prelude::*;
use dioxus_std::{i18n::use_i18, translate};
#[derive(PartialEq, Props, Clone)]
pub struct VotingProps {
index: usize,
Expand Down Expand Up @@ -62,7 +61,13 @@ pub fn TreasuryAction(props: VotingProps) -> Element {
message: (period.amount / KUSAMA_PRECISION_DECIMALS).to_string(),
size: ElementSize::Small,
placeholder: translate!(i18, "initiative.steps.actions.kusama_treasury.placeholder"),
error: None,
error: {
if period.amount > 0 {
None
} else {
Some(translate!(i18, "initiative.steps.actions.error.amount"))
}
},
right_text: {
rsx!(
span { class: "input--right__text",
Expand Down
27 changes: 20 additions & 7 deletions src/components/molecules/actions/voting.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use dioxus::prelude::*;
use dioxus_std::{i18n::use_i18, translate};
use crate::{
components::atoms::{
combo_input::{ComboInputOption, ComboInputValue},
dropdown::{DropdownItem, ElementSize},
icon_button::Variant, AddPlus, ComboInput, Icon, IconButton, Input, MinusCircle,
RadioButton,
icon_button::Variant,
AddPlus, ComboInput, Icon, IconButton, Input, MinusCircle, RadioButton,
},
hooks::use_initiative::{
use_initiative, ActionItem, ConvictionVote, StandardVote, VoteType,
VotingOpenGov, VotingOpenGovAction,
use_initiative, ActionItem, ConvictionVote, StandardVote, VoteType, VotingOpenGov,
VotingOpenGovAction,
},
};
use dioxus::prelude::*;
use dioxus_std::{i18n::use_i18, translate};
#[derive(PartialEq, Props, Clone)]
pub struct VotingProps {
index: usize,
Expand All @@ -33,7 +33,13 @@ pub fn VotingAction(props: VotingProps) -> Element {
message: if proposal.poll_index > 0 { proposal.poll_index.to_string() } else { String::new() },
size: ElementSize::Small,
placeholder: "Ex: 000",
error: None,
error: {
if proposal.poll_index > 0 {
None
} else {
Some(translate!(i18, "initiative.steps.actions.error.amount"))
}
},
label: translate!(i18, "initiative.steps.actions.voting_open_gov.poll_index"),
on_input: move |event: Event<FormData>| {
if let ActionItem::VotingOpenGov(ref mut meta) = initiative.get_action(props.index) {
Expand Down Expand Up @@ -64,6 +70,13 @@ pub fn VotingAction(props: VotingProps) -> Element {
}),
input: if vote.balance / KUSAMA_PRECISION_DECIMALS > 0 { (vote.balance / KUSAMA_PRECISION_DECIMALS).to_string() } else { String::new() },
},
error: {
if vote.balance > 0 {
None
} else {
Some(translate!(i18, "initiative.steps.actions.error.amount"))
}
},
placeholder: translate!(i18, "initiative.steps.actions.voting_open_gov.standard.balance"),
right_text: {
rsx!(
Expand Down
26 changes: 18 additions & 8 deletions src/components/molecules/initiative/actions.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use dioxus::prelude::*;
use dioxus_std::{i18n::use_i18, translate};
use crate::{
components::{
atoms::{
dropdown::ElementSize, icon_button::Variant, AddPlus, Dropdown, Icon,
IconButton, SubstractLine,
dropdown::ElementSize, icon_button::Variant, AddPlus, Dropdown, Icon, IconButton,
SubstractLine,
},
molecules::{MembersAction, TransferAction, TreasuryAction, VotingAction},
},
hooks::use_initiative::{
use_initiative, ActionItem, AddMembersAction, MediumOptions, MemberItem,
},
};
use dioxus::prelude::*;
use dioxus_std::{i18n::use_i18, translate};
#[component]
pub fn InitiativeActions() -> Element {
let i18 = use_i18();
Expand Down Expand Up @@ -54,9 +54,9 @@ pub fn InitiativeActions() -> Element {
default: None,
on_change: move |event: usize| {
let options = initiative.get_actions_options();

let to_assign = &options[event];

initiative.update_action(index, initiative.to_action_option(to_assign.key.clone()));
},
body: items.clone()
Expand Down Expand Up @@ -108,14 +108,24 @@ pub fn InitiativeActions() -> Element {
placeholder: translate!(i18, "header.cta.account"),
size: ElementSize::Small,
default: None,
on_change: move |_: usize| {},
on_change: move |event: usize| {
let options = initiative.get_actions_options();

let to_assign = &options[event];
let action = initiative.to_action_option(to_assign.key.clone());

initiative.push_action(action);
},
body: items
}
IconButton {
variant: Variant::Round,
size: ElementSize::Small,
class: "button--action",
body: rsx!(Icon { icon : AddPlus, height : 24, width : 24, fill : "var(--fill-00)" }),
disabled: actions_lock.len() == 0,
body: rsx! {
Icon { icon: AddPlus, height: 24, width: 24, fill: "var(--fill-00)" }
},
on_click: move |_| {
initiative
.push_action(
Expand Down
Loading

0 comments on commit 2eeeaa8

Please sign in to comment.