Skip to content

Commit

Permalink
refactor: cleaned up redundant code
Browse files Browse the repository at this point in the history
  • Loading branch information
ShubhranshuSanjeev committed Oct 8, 2024
1 parent 67dc904 commit d86f41d
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 293 deletions.
108 changes: 49 additions & 59 deletions crates/frontend/src/components/condition_pills.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
pub mod types;
pub mod utils;
use crate::{
logic::{Condition, Conditions, Operand, Operator},
schema::HtmlDisplay,
};

use crate::components::condition_pills::types::ConditionOperator;

use self::types::Condition;
use leptos::{leptos_dom::helpers::WindowListenerHandle, *};
use serde_json::Value;
use wasm_bindgen::JsCast;
use web_sys::Element;

Expand Down Expand Up @@ -59,31 +57,16 @@ pub fn condition_expression(
} else {
("condition-item-collapsed", "condition-value-collapsed")
};

// Destructure the condition
let Condition { left_operand: dimension, operator, right_operand: value } = condition.get_value();

// Filter and convert values to strings for rendering
let filtered_vals: Vec<String> = value.into_iter().filter_map(|v|
if v.is_object() && v.get("var").is_some() {
None
} else {
match v {
Value::String(s) => Some(s.to_string()),
Value::Number(n) => Some(n.to_string()),
Value::Bool(b) => Some(b.to_string()),
Value::Array(arr) => {
Some(arr.iter().map(|v| v.to_string()).collect::<Vec<String>>().join(","))
let Condition { dimension, operator, operands } = condition.get_value();
let operand_str: Vec<String> = operands
.iter()
.filter_map(|operand| {
match operand {
Operand::Dimension(_) => None,
Operand::Value(v) => Some(v.html_display()),
}
Value::Object(o) => {
serde_json::to_string_pretty(&o).ok()
}
_ => None,
}
}
).collect();

// Render based on the operator type
})
.collect();
view! {
<li
id=id.get_value()
Expand All @@ -102,37 +85,38 @@ pub fn condition_expression(
{operator.to_string()}
</span>

{
match operator {
ConditionOperator::Between => {
if filtered_vals.len() == 2 {
view! {
<>
<span class="font-mono font-semibold context_condition">
{&filtered_vals[0]}
</span>
<span class="font-mono font-medium text-gray-650 context_condition">
{"and"}
</span>
<span class="font-mono font-semibold context_condition">
{&filtered_vals[1]}
</span>
</>
}.into_view()
} else {
view! { <span class="font-mono text-red-500">"Invalid between values"</span> }.into_view()
{match operator {
Operator::Between => {
if operand_str.len() == 2 {
view! {
<>
<span class="font-mono font-semibold context_condition">
{&operand_str[0]}
</span>
<span class="font-mono font-medium text-gray-650 context_condition">
{"and"}
</span>
<span class="font-mono font-semibold context_condition">
{&operand_str[1]}
</span>
</>
}
},
_ => {
let rendered_value = filtered_vals.join(", ");
.into_view()
} else {
view! {
<span class=value_class>
{rendered_value}
<span class="font-mono text-red-500">
"Invalid between values"
</span>
}.into_view()
}
.into_view()
}
}
}
_ => {
let rendered_value = operand_str.join(", ");
view! { <span class=value_class>{rendered_value}</span> }.into_view()
}
}}

</li>
}
}}
Expand All @@ -142,7 +126,7 @@ pub fn condition_expression(
#[component]
pub fn condition(
#[prop(into)] id: String,
#[prop(into)] conditions: Vec<Condition>,
#[prop(into)] conditions: Conditions,
#[prop(into, default=String::new())] class: String,
#[prop(default = true)] grouped_view: bool,
) -> impl IntoView {
Expand All @@ -160,11 +144,17 @@ pub fn condition(
.clone()>
{conditions
.get_value()
.into_iter()
.iter()
.enumerate()
.map(|(idx, condition)| {
let item_id = format!("{}-{}", id, idx);
view! { <ConditionExpression condition id=item_id list_id=id.clone()/> }
view! {
<ConditionExpression
condition=condition.clone()
id=item_id
list_id=id.clone()
/>
}
})
.collect::<Vec<_>>()}
</ol>
Expand Down
138 changes: 0 additions & 138 deletions crates/frontend/src/components/condition_pills/types.rs

This file was deleted.

18 changes: 0 additions & 18 deletions crates/frontend/src/components/condition_pills/utils.rs

This file was deleted.

10 changes: 5 additions & 5 deletions crates/frontend/src/components/context_card.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::components::condition_pills::types::ConditionOperator;
use leptos::*;
use serde_json::{Map, Value};

use crate::{
components::{
condition_pills::{types::Condition, Condition as ConditionComponent},
condition_pills::Condition as ConditionComponent,
table::{types::Column, Table},
},
logic::{Conditions, Operator},
types::Context,
};

Expand All @@ -25,7 +25,7 @@ pub fn context_card(
>,
#[prop(default=Callback::new(|_| {}))] handle_delete: Callback<String, ()>,
) -> impl IntoView {
let conditions: Vec<Condition> = (&context).try_into().unwrap_or(vec![]);
let conditions: Conditions = (&context).try_into().unwrap_or_default();

let override_table_rows = overrides
.clone()
Expand All @@ -50,11 +50,11 @@ pub fn context_card(
let actions_supported = show_actions
&& !conditions
.iter()
.any(|condition| condition.left_operand == "variantIds");
.any(|condition| condition.dimension == "variantIds");

let edit_unsupported = conditions
.iter()
.any(|condition| matches!(condition.operator, ConditionOperator::Other(_)));
.any(|condition| matches!(condition.operator, Operator::Other(_)));

view! {
<div class="rounded-lg shadow bg-base-100 p-6 flex flex-col gap-4">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use leptos::logging::log;
use serde_json::json;

use crate::{types::Experiment, utils::get_host};
use crate::{types::ExperimentResponse, utils::get_host};

pub async fn conclude_experiment(
exp_id: String,
variant_id: String,
tenant: &String,
) -> Result<Experiment, String> {
) -> Result<ExperimentResponse, String> {
let client = reqwest::Client::new();
let host = get_host();
match client
Expand All @@ -20,7 +20,7 @@ pub async fn conclude_experiment(
Ok(experiment) => {
log!("experiment response {:?}", experiment);
Ok(experiment
.json::<Experiment>()
.json::<ExperimentResponse>()
.await
.map_err(|err| err.to_string())?)
}
Expand Down
6 changes: 3 additions & 3 deletions crates/frontend/src/components/experiment_ramp_form/utils.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use leptos::logging::log;
use serde_json::json;

use crate::{types::Experiment, utils::get_host};
use crate::{types::ExperimentResponse, utils::get_host};

pub async fn ramp_experiment(
exp_id: &String,
percent: u8,
tenant: &String,
) -> Result<Experiment, String> {
) -> Result<ExperimentResponse, String> {
let client = reqwest::Client::new();
let host = get_host();
match client
Expand All @@ -20,7 +20,7 @@ pub async fn ramp_experiment(
Ok(experiment) => {
log!("experiment response {:?}", experiment);
Ok(experiment
.json::<Experiment>()
.json::<ExperimentResponse>()
.await
.map_err(|err| err.to_string())?)
}
Expand Down
Loading

0 comments on commit d86f41d

Please sign in to comment.