Skip to content

Commit

Permalink
fix: removing unwraps
Browse files Browse the repository at this point in the history
  • Loading branch information
ShubhranshuSanjeev committed Oct 4, 2024
1 parent ec6b6d1 commit 67dc904
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 73 deletions.
75 changes: 37 additions & 38 deletions crates/frontend/src/components/context_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::{
};
use leptos::*;
use serde_json::Value;
use web_sys::MouseEvent;

#[component]
pub fn condition_input(
Expand Down Expand Up @@ -146,7 +145,6 @@ pub fn context_form<NF>(
dimensions: Vec<Dimension>,
#[prop(default = false)] disabled: bool,
#[prop(default = false)] resolve_mode: bool,
#[prop(default = false)] is_standalone: bool,
#[prop(default = String::new())] heading_sub_text: String,
#[prop(default = DropdownDirection::Right)] dropdown_direction: DropdownDirection,
) -> impl IntoView
Expand Down Expand Up @@ -179,12 +177,6 @@ where

let last_idx = create_memo(move |_| context_rs.get().len().max(1) - 1);

let on_click = move |event: MouseEvent| {
event.prevent_default();
logging::log!("Context form submit");
//TODO: submit logic for this
};

create_effect(move |_| {
let f_context = context_rs.get(); // context will now be a Value
logging::log!("Context form effect {:?}", f_context);
Expand All @@ -193,27 +185,33 @@ where

let on_select_dimension = Callback::new(move |selected_dimension: Dimension| {
let dimension_name = selected_dimension.dimension;
let r#type = SchemaType::try_from(selected_dimension.schema).unwrap();

used_dimensions_ws.update(|value: &mut HashSet<String>| {
value.insert(dimension_name.clone());
});
context_ws.update(|value| {
value.push(
Condition::try_from((Operator::Is, dimension_name, r#type)).unwrap(),
)
});
if let Ok(r#type) = SchemaType::try_from(selected_dimension.schema) {
used_dimensions_ws.update(|value: &mut HashSet<String>| {
value.insert(dimension_name.clone());
});
context_ws.update(|value| {
value.push(
Condition::try_from((Operator::Is, dimension_name, r#type)).unwrap(),
)
});
}
// TODO show alert in case of invalid dimension
});

let on_operator_change = Callback::new(
move |(idx, d_name, d_type, operator): (usize, String, SchemaType, Operator)| {
context_ws.update(|v| {
if idx < v.len() {
v[idx].operator = operator.clone();
v[idx].operands = Operands::try_from((operator, d_name, d_type))
.unwrap_or(Operands(vec![]))
}
})
if let Ok(operands) =
Operands::try_from((&operator, d_name.as_str(), &d_type))
{
context_ws.update(|v| {
if idx < v.len() {
v[idx].operator = operator;
v[idx].operands = operands.clone();
}
})
}
// TODO show alert in case of invalid dimension operator combinations
},
);

Expand Down Expand Up @@ -278,14 +276,22 @@ where
}

children=move |(idx, condition)| {
let schema = dimension_map
let (schema_type, enum_variants) = dimension_map
.with_value(|v| {
v.get(&condition.dimension).unwrap().schema.clone()
// if this panics then something is wrong
let d = v.get(&condition.dimension).unwrap();
(
SchemaType::try_from(d.schema.clone()),
EnumVariants::try_from(d.schema.clone()),
)
});
let schema_type = store_value(
SchemaType::try_from(schema.clone()).unwrap(),
);
let enum_variants = EnumVariants::try_from(schema);
if schema_type.is_err() || enum_variants.is_err() {
return view! {
<span class="text-sm red"> An error occured </span>
}.into_view()
}

let schema_type = store_value(schema_type.unwrap());
let allow_remove = !disabled
&& !mandatory_dimensions.get_value().contains(&condition.dimension);
let input_type = store_value(
Expand Down Expand Up @@ -335,7 +341,7 @@ where
view! {}.into_view()
}
}}
}
}.into_view()
}
/>

Expand Down Expand Up @@ -368,12 +374,5 @@ where
</div>
</div>
</div>
<Show when=move || is_standalone>
<div class="flex justify-end">
<button class="btn" on:click:undelegated=on_click disabled=disabled>
Save
</button>
</div>
</Show>
}
}
2 changes: 0 additions & 2 deletions crates/frontend/src/components/experiment_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,9 @@ where
let context = f_context.get();
view! {
<ContextForm
// dimensions will now be a Vec<Dimension>
dimensions=dimensions.get_value()
context=context
handle_change=handle_context_form_change
is_standalone=false
disabled=edit
heading_sub_text=String::from(
"Define rules under which this experiment would run",
Expand Down
14 changes: 0 additions & 14 deletions crates/frontend/src/components/override_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::{
};
use leptos::*;
use serde_json::Value;
use web_sys::MouseEvent;

#[component]
fn type_badge(r#type: Option<SchemaType>) -> impl IntoView {
Expand Down Expand Up @@ -112,7 +111,6 @@ pub fn override_form<NF>(
default_config: Vec<DefaultConfig>,
handle_change: NF,
#[prop(into, default=String::new())] id: String,
#[prop(default = false)] is_standalone: bool,
#[prop(default = false)] disable_remove: bool,
#[prop(default = true)] show_add_override: bool,
#[prop(into, default = None)] handle_key_remove: Option<Callback<String, ()>>,
Expand All @@ -133,11 +131,6 @@ where
.map(|ele| (ele.clone().key, ele))
.collect();

let on_submit = move |event: MouseEvent| {
event.prevent_default();
logging::log!("{:?}", overrides.get());
};

let handle_config_key_select = Callback::new(move |default_config: DefaultConfig| {
let config_key = default_config.key;

Expand Down Expand Up @@ -279,13 +272,6 @@ where
</div>
</div>
</div>
<Show when=move || is_standalone>
<div class="flex justify-end mt-4">
<button class="btn" on:click:undelegated=on_submit>
Save
</button>
</div>
</Show>
</div>
}
}
2 changes: 0 additions & 2 deletions crates/frontend/src/components/variant_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ where
overrides=overrides
default_config=default_config.get_value()
handle_change=handle_change
is_standalone=false
show_add_override=false
handle_key_remove=Some(Callback::new(on_key_remove))
/>
Expand All @@ -306,7 +305,6 @@ where
overrides=overrides
default_config=default_config.get_value()
handle_change=handle_change
is_standalone=false
show_add_override=false
disable_remove=true
/>
Expand Down
7 changes: 4 additions & 3 deletions crates/frontend/src/logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ impl FromIterator<Value> for Operands {
}
}

impl TryFrom<(Operator, String, SchemaType)> for Operands {
impl TryFrom<(&Operator, &str, &SchemaType)> for Operands {
type Error = String;
fn try_from(
(operator, d_name, r#type): (Operator, String, SchemaType),
(operator, d_name, r#type): (&Operator, &str, &SchemaType),
) -> Result<Self, Self::Error> {
match operator {
Operator::Is => Ok(Operands(vec![
Expand All @@ -68,6 +68,7 @@ impl TryFrom<(Operator, String, SchemaType)> for Operands {
Operand::Dimension(json!({ "var": d_name })),
Operand::Value(r#type.default_value()),
])),
// TODO fix this as there will be cases of unsupported operators
_ => Err(String::from("unsupported operator")),
}
}
Expand Down Expand Up @@ -230,7 +231,7 @@ impl TryFrom<(Operator, String, SchemaType)> for Condition {
Ok(Condition {
dimension: d_name.clone(),
operator: operator.clone(),
operands: Operands::try_from((operator, d_name, r#type))?,
operands: Operands::try_from((&operator, d_name.as_str(), &r#type))?,
})
}
}
Expand Down
19 changes: 6 additions & 13 deletions crates/frontend/src/pages/context_override.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ fn form(
<ContextForm
dimensions=dimensions.get_value()
context=context.get_untracked()
is_standalone=false
handle_change=move |new_context| {
set_context
.update(|value| {
Expand All @@ -104,7 +103,6 @@ fn form(
<OverrideForm
overrides=overrides.get_untracked()
default_config=default_config
is_standalone=false
handle_change=move |new_overrides| {
set_overrides
.update(|value| {
Expand Down Expand Up @@ -162,24 +160,19 @@ pub fn context_override() -> impl IntoView {
let handle_context_create = Callback::new(move |_| {
set_form_mode.set(Some(FormMode::Create));
let PageResource { dimensions, .. } = page_resource.get().unwrap_or_default();
let context_with_mandatory_dimensions = dimensions
.into_iter()
let def_context = dimensions
.iter()
.filter_map(|v| {
if !v.mandatory { return None; }
let dimension_name = v.dimension;
let r#type = SchemaType::try_from(v.schema).unwrap();

if v.mandatory {
Some(
let condition = SchemaType::try_from(v.schema).map(|v| {
Condition::try_from((Operator::Is, dimension_name, r#type))
.unwrap(),
)
} else {
None
}
});
})
.collect::<Conditions>();
set_selected_data.set(Some(Data {
context: context_with_mandatory_dimensions,
context: def_context,
overrides: vec![],
}));
open_drawer("context_and_override_drawer");
Expand Down
1 change: 0 additions & 1 deletion crates/frontend/src/pages/home.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,6 @@ pub fn home() -> impl IntoView {
context=Conditions::default()
heading_sub_text="Query your configs".to_string()
dropdown_direction=DropdownDirection::Right
is_standalone=false
resolve_mode=true
handle_change=move |new_context| {
context_ws
Expand Down

0 comments on commit 67dc904

Please sign in to comment.