Skip to content

Commit

Permalink
fix: using concise expression implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ShubhranshuSanjeev committed Jan 6, 2025
1 parent 8fe8f08 commit 2908496
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 77 deletions.
24 changes: 12 additions & 12 deletions crates/frontend/src/components/condition_pills.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
logic::{Conditions, Constant, Expression, Operator},
logic::{Condition, Conditions, Expression, Operator},
schema::HtmlDisplay,
};

Expand Down Expand Up @@ -32,10 +32,10 @@ pub fn use_condition_collapser() -> WindowListenerHandle {
pub fn condition_expression(
#[prop(into)] id: String,
#[prop(into)] list_id: String,
expression: Expression,
condition: Condition,
) -> impl IntoView {
let id = store_value(id);
let expression = store_value(expression);
let condition = store_value(condition);

let (expand_rs, expand_ws) = create_signal(false);
let condition_id_rs = use_context::<ReadSignal<ConditionId>>().expect(
Expand All @@ -57,12 +57,12 @@ pub fn condition_expression(
} else {
("condition-item-collapsed", "condition-value-collapsed")
};
let (dimension, operator, operands): (String, Operator, Vec<String>) = expression
.with_value(|exp| {
let (dimension, operator, operands): (String, Operator, Vec<String>) = condition
.with_value(|v| {
(
exp.variable_name(),
exp.to_operator(),
exp.to_constants_vec().iter().map(|c| c.html_display()).collect(),
v.variable.clone(),
<&Condition as Into<Operator>>::into(v),
v.expression.to_constants_vec().iter().map(|c| c.html_display()).collect(),
)
});
view! {
Expand All @@ -83,8 +83,8 @@ pub fn condition_expression(
{operator.to_string()}
</span>

{match expression.get_value() {
Expression::Between(Constant(c1), _, Constant(c2)) => {
{match condition.get_value().expression {
Expression::Between(c1, c2) => {
view! {
<>
<span class="font-mono font-semibold context_condition">
Expand Down Expand Up @@ -135,11 +135,11 @@ pub fn condition(
.get_value()
.iter()
.enumerate()
.map(|(idx, expression)| {
.map(|(idx, condition)| {
let item_id = format!("{}-{}", id, idx);
view! {
<ConditionExpression
expression=expression.clone()
condition=condition.clone()
id=item_id
list_id=id.clone()
/>
Expand Down
9 changes: 3 additions & 6 deletions crates/frontend/src/components/context_card.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,13 @@ pub fn context_card(
Column::default("VALUE".to_string()),
];

let actions_supported = show_actions
&& !conditions
.0
.iter()
.any(|expression| expression.variable_name() == "variantIds");
let actions_supported =
show_actions && !conditions.0.iter().any(|c| c.variable == "variantIds");

let edit_unsupported = conditions
.0
.iter()
.any(|expression| matches!(expression, Expression::Other(_, _)));
.any(|c| matches!(c.expression, Expression::Other(_, _)));

view! {
<div class="rounded-lg shadow bg-base-100 p-6 flex flex-col gap-4">
Expand Down
92 changes: 41 additions & 51 deletions crates/frontend/src/components/context_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub mod utils;
use std::collections::{HashMap, HashSet};

use crate::components::input::{Input, InputType};
use crate::logic::{Conditions, Constant, Expression, Operator, Variable};
use crate::logic::{Condition, Conditions, Expression, Operator};
use crate::schema::EnumVariants;
use crate::types::Dimension;
use crate::{
Expand All @@ -17,66 +17,56 @@ pub fn condition_input(
disabled: bool,
resolve_mode: bool,
allow_remove: bool,
expression: StoredValue<Expression>,
condition: StoredValue<Condition>,
input_type: StoredValue<InputType>,
schema_type: StoredValue<SchemaType>,
#[prop(into)] on_remove: Callback<String, ()>,
#[prop(into)] on_value_change: Callback<Expression, ()>,
#[prop(into)] on_operator_change: Callback<Operator, ()>,
) -> impl IntoView {
let (dimension, operator) =
expression.with_value(|v| (v.variable_name(), v.to_operator()));
let (dimension, operator): (String, Operator) =
condition.with_value(|v| (v.variable.clone(), v.into()));

let inputs: Vec<(Value, Callback<Value, ()>)> = match expression.get_value() {
Expression::Is(Variable(v), Constant(c)) => {
let inputs: Vec<(Value, Callback<Value, ()>)> = match condition.get_value().expression
{
Expression::Is(c) => {
vec![(
c,
Callback::new(move |value: Value| {
on_value_change
.call(Expression::Is(Variable(v.clone()), Constant(value)));
on_value_change.call(Expression::Is(value));
}),
)]
}
Expression::In(Variable(v), Constant(c)) => {
Expression::In(c) => {
vec![(
c,
Callback::new(move |value: Value| {
on_value_change
.call(Expression::In(Variable(v.clone()), Constant(value)));
on_value_change.call(Expression::In(value));
}),
)]
}
Expression::Has(Constant(c), Variable(v)) => {
Expression::Has(c) => {
vec![(
c,
Callback::new(move |value: Value| {
on_value_change
.call(Expression::Has(Constant(value), Variable(v.clone())));
on_value_change.call(Expression::Has(value));
}),
)]
}
Expression::Between(Constant(c1), Variable(v), Constant(c2)) => {
let v_clone = v.clone();
Expression::Between(c1, c2) => {
let c2_clone = c2.clone();
vec![
(
c1.clone(),
Callback::new(move |value: Value| {
on_value_change.call(Expression::Between(
Constant(value),
Variable(v_clone.clone()),
Constant(c2_clone.clone()),
));
on_value_change
.call(Expression::Between(value, c2_clone.clone()));
}),
),
(
c2.clone(),
Callback::new(move |value: Value| {
on_value_change.call(Expression::Between(
Constant(c1.clone()),
Variable(v.clone()),
Constant(value),
));
on_value_change.call(Expression::Between(c1.clone(), value));
}),
),
]
Expand Down Expand Up @@ -154,9 +144,13 @@ pub fn condition_input(
disabled=disabled
id=format!(
"{}-{}",
expression
condition
.with_value(|v| {
format!("{}-{}", v.variable_name(), v.to_operator())
format!(
"{}-{}",
v.variable,
(<&Condition as Into<Operator>>::into(v)),
)
}),
idx,
)
Expand All @@ -172,7 +166,7 @@ pub fn condition_input(
class="btn btn-ghost btn-circle btn-sm mt-1"
disabled=disabled
on:click=move |_| {
on_remove.call(expression.with_value(|v| v.variable_name()));
on_remove.call(condition.with_value(|v| v.variable.clone()));
}
>

Expand Down Expand Up @@ -206,7 +200,7 @@ where
let (used_dimensions_rs, used_dimensions_ws) = create_signal(
context
.iter()
.map(|expression| expression.variable_name())
.map(|condition| condition.variable.clone())
.collect::<HashSet<String>>(),
);
let (context_rs, context_ws) = create_signal(context.clone());
Expand Down Expand Up @@ -237,7 +231,10 @@ where
value.insert(dimension_name.clone());
});
context_ws.update(|value| {
value.push(Expression::is(dimension_name.as_str(), r#type))
value.push(Condition::new_with_default_expression(
dimension_name,
r#type,
))
});
}
// TODO show alert in case of invalid dimension
Expand All @@ -247,15 +244,15 @@ where
Callback::new(move |(idx, expression): (usize, Expression)| {
context_ws.update(|v| {
if idx < v.len() {
v[idx] = expression;
v[idx].expression = expression;
}
});
});

let on_value_change = Callback::new(move |(idx, expression): (usize, Expression)| {
context_ws.update(|v| {
if idx < v.len() {
v[idx] = expression;
v[idx].expression = expression;
}
})
});
Expand Down Expand Up @@ -299,46 +296,43 @@ where
.0
.into_iter()
.enumerate()
.collect::<Vec<(usize, Expression)>>()
.collect::<Vec<(usize, Condition)>>()
}

key=|(idx, expression)| {
key=|(idx, condition)| {
format!(
"{}-{}-{}",
expression.variable_name(),
condition.variable,
idx,
expression.to_operator(),
condition.expression.to_operator(),
)
}

children=move |(idx, expression)| {
children=move |(idx, condition)| {
let (schema_type, enum_variants) = dimension_map
.with_value(|v| {
let d = v.get(&expression.variable_name()).unwrap();
let d = v.get(&condition.variable).unwrap();
(
SchemaType::try_from(d.schema.clone()),
EnumVariants::try_from(d.schema.clone()),
)
});
let operator = Operator::from(&expression);
let dimension_name = expression.variable_name();
let operator = Operator::from(&condition);
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(&expression.variable_name());
&& !mandatory_dimensions.get_value().contains(&condition.variable);
let input_type = store_value(
InputType::from((
schema_type.get_value(),
enum_variants.unwrap(),
operator,
)),
);
let expression = store_value(expression);
let condition = store_value(condition);
let on_remove = move |d_name| on_remove.call((idx, d_name));
let on_value_change = move |expression| {
on_value_change.call((idx, expression))
Expand All @@ -347,19 +341,15 @@ where
on_operator_change
.call((
idx,
Expression::from((
dimension_name.as_str(),
schema_type.get_value(),
operator,
)),
Expression::from((schema_type.get_value(), operator)),
))
};
view! {
<ConditionInput
disabled
resolve_mode
allow_remove
expression
condition
input_type
schema_type
on_remove
Expand Down
10 changes: 6 additions & 4 deletions crates/frontend/src/components/experiment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use leptos::*;
use serde_json::{Map, Value};
use std::collections::HashMap;

use crate::{components::table::Table, schema::HtmlDisplay};
use crate::components::table::Table;

use crate::schema::HtmlDisplay;
use crate::types::{Experiment, ExperimentStatusType};
use crate::{
components::table::types::Column,
Expand Down Expand Up @@ -186,9 +187,10 @@ where

{contexts
.iter()
.map(|expression| {
let dimension = expression.variable_name();
let operand_views = expression
.map(|condition| {
let dimension = condition.variable.clone();
let operand_views = condition
.expression
.to_constants_vec()
.iter()
.map(|c| {
Expand Down
1 change: 1 addition & 0 deletions crates/frontend/src/logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,4 @@ impl TryFrom<&Context> for Conditions {
Self::from_context_json(&context.condition.as_ref())
}
}

9 changes: 5 additions & 4 deletions crates/frontend/src/pages/context_override.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::components::delete_modal::DeleteModal;
use crate::components::drawer::{close_drawer, open_drawer, Drawer, DrawerBtn};
use crate::components::override_form::OverrideForm;
use crate::components::skeleton::{Skeleton, SkeletonVariant};
use crate::logic::{Conditions, Expression};
use crate::logic::{Condition, Conditions};
use crate::providers::alert_provider::enqueue_alert;
use crate::providers::condition_collapse_provider::ConditionCollapseProvider;
use crate::providers::editor_provider::EditorProvider;
Expand Down Expand Up @@ -178,9 +178,10 @@ pub fn context_override() -> impl IntoView {
return;
}

let expression = Expression::is(dim.dimension.as_str(), r#type.unwrap());

default_ctx.push(expression);
default_ctx.push(Condition::new_with_default_expression(
dim.dimension.clone(),
r#type.unwrap(),
));
}

selected_context_ws.set(Some(Data {
Expand Down

0 comments on commit 2908496

Please sign in to comment.