Skip to content

Commit

Permalink
refactor: moved json-logic related types to logic.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
ShubhranshuSanjeev committed Oct 4, 2024
1 parent c9549da commit ec6b6d1
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 185 deletions.
47 changes: 20 additions & 27 deletions crates/frontend/src/components/context_form.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
pub mod types;
pub mod utils;
use std::collections::{HashMap, HashSet};

use crate::components::input::{Input, InputType};
use crate::logic::{Condition, Conditions, Operand, Operands, Operator};
use crate::schema::EnumVariants;
use crate::types::Dimension;
use crate::{
Expand All @@ -13,8 +13,6 @@ use leptos::*;
use serde_json::Value;
use web_sys::MouseEvent;

use self::types::*;

#[component]
pub fn condition_input(
disabled: bool,
Expand Down Expand Up @@ -144,13 +142,13 @@ pub fn condition_input(
#[component]
pub fn context_form<NF>(
handle_change: NF,
context: Conditions,
dimensions: Vec<Dimension>,
#[prop(default = false)] disabled: bool,
#[prop(default = false)] resolve_mode: bool,
#[prop(default = false)] is_standalone: bool,
context: Conditions,
#[prop(default = String::new())] heading_sub_text: String,
#[prop(default = false)] disabled: bool,
#[prop(default = DropdownDirection::Right)] dropdown_direction: DropdownDirection,
#[prop(default = false)] resolve_mode: bool,
) -> impl IntoView
where
NF: Fn(Conditions) + 'static,
Expand All @@ -161,30 +159,25 @@ where
.map(|v| (v.dimension.clone(), v.clone()))
.collect::<HashMap<String, Dimension>>(),
);
let (used_dimensions, set_used_dimensions) = create_signal(
let (used_dimensions_rs, used_dimensions_ws) = create_signal(
context
.iter()
.map(|condition| condition.dimension.clone())
.collect::<HashSet<String>>(),
);
let (context, set_context) = create_signal(context.clone());
let (context_rs, context_ws) = create_signal(context.clone());

let dimensions = StoredValue::new(dimensions);
let mandatory_dimensions = StoredValue::new(
dimensions
.get_value()
.into_iter()
.filter_map(|dim| {
if dim.mandatory {
Some(dim.dimension)
} else {
None
}
})
.filter(|dim| dim.mandatory)
.map(|dim| dim.dimension)
.collect::<HashSet<String>>(),
);

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

let on_click = move |event: MouseEvent| {
event.prevent_default();
Expand All @@ -193,7 +186,7 @@ where
};

create_effect(move |_| {
let f_context = context.get(); // context will now be a Value
let f_context = context_rs.get(); // context will now be a Value
logging::log!("Context form effect {:?}", f_context);
handle_change(f_context.clone()); // handle_change now expects Value
});
Expand All @@ -202,10 +195,10 @@ where
let dimension_name = selected_dimension.dimension;
let r#type = SchemaType::try_from(selected_dimension.schema).unwrap();

set_used_dimensions.update(|value: &mut HashSet<String>| {
used_dimensions_ws.update(|value: &mut HashSet<String>| {
value.insert(dimension_name.clone());
});
set_context.update(|value| {
context_ws.update(|value| {
value.push(
Condition::try_from((Operator::Is, dimension_name, r#type)).unwrap(),
)
Expand All @@ -214,7 +207,7 @@ where

let on_operator_change = Callback::new(
move |(idx, d_name, d_type, operator): (usize, String, SchemaType, Operator)| {
set_context.update(|v| {
context_ws.update(|v| {
if idx < v.len() {
v[idx].operator = operator.clone();
v[idx].operands = Operands::try_from((operator, d_name, d_type))
Expand All @@ -226,7 +219,7 @@ where

let on_value_change =
Callback::new(move |(idx, operand_idx, value): (usize, usize, Value)| {
set_context.update(|v| {
context_ws.update(|v| {
if idx < v.len() {
let operands = &(v[idx].operands);
if operand_idx < operands.len()
Expand All @@ -239,10 +232,10 @@ where
});

let on_remove = Callback::new(move |(idx, d_name): (usize, String)| {
set_used_dimensions.update(|value| {
used_dimensions_ws.update(|value| {
value.remove(&d_name);
});
set_context.update(|v| {
context_ws.update(|v| {
v.remove(idx);
});
});
Expand All @@ -257,7 +250,7 @@ where
</div>
<div class="card w-full bg-slate-50">
<div class="card-body">
<Show when=move || context.get().is_empty()>
<Show when=move || context_rs.get().is_empty()>
<div class="flex justify-center">
<Dropdown
dropdown_width="w-80"
Expand All @@ -272,7 +265,7 @@ where
</Show>
<For
each=move || {
context
context_rs
.get()
.0
.into_iter()
Expand Down Expand Up @@ -346,15 +339,15 @@ where
}
/>

<Show when=move || { !context.get().is_empty() && !disabled }>
<Show when=move || { !context_rs.get().is_empty() && !disabled }>
<div class="mt-4">

{move || {
let dimensions = dimensions
.get_value()
.into_iter()
.filter(|dimension| {
!used_dimensions.get().contains(&dimension.dimension)
!used_dimensions_rs.get().contains(&dimension.dimension)
})
.collect::<Vec<Dimension>>();
view! {
Expand Down
7 changes: 3 additions & 4 deletions crates/frontend/src/components/experiment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ use std::rc::Rc;

use leptos::*;

use crate::{components::{
context_form::types::{Condition, Operand},
table::Table,
}, schema::HtmlDisplay};
use crate::{components::table::Table, schema::HtmlDisplay};

use crate::logic::{Condition, Operand};

use self::utils::gen_variant_table;
use crate::types::{Experiment, ExperimentStatusType};
Expand Down
28 changes: 15 additions & 13 deletions crates/frontend/src/components/experiment_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::types::{DefaultConfig, Dimension, VariantFormT, VariantType};
use leptos::*;
use web_sys::MouseEvent;

use super::context_form::types::Conditions;
use crate::logic::Conditions;

fn default_variants_for_form() -> Vec<(String, VariantFormT)> {
vec![
Expand Down Expand Up @@ -146,7 +146,8 @@ where
let context = f_context.get();
view! {
<ContextForm
dimensions=dimensions.get_value() // dimensions will now be a Vec<Dimension>
// dimensions will now be a Vec<Dimension>
dimensions=dimensions.get_value()
context=context
handle_change=handle_context_form_change
is_standalone=false
Expand Down Expand Up @@ -175,17 +176,18 @@ where
}}

<div class="flex justify-start mt-8">
{ move || {
let loading = req_inprogess_rs.get();
view! {
<Button
class="pl-[70px] pr-[70px] w-48 h-12".to_string()
text="Submit".to_string()
on_click=on_submit.clone()
loading
/>
}
}}
{move || {
let loading = req_inprogess_rs.get();
view! {
<Button
class="pl-[70px] pr-[70px] w-48 h-12".to_string()
text="Submit".to_string()
on_click=on_submit.clone()
loading
/>
}
}}

</div>
</div>
}
Expand Down
4 changes: 2 additions & 2 deletions crates/frontend/src/components/experiment_form/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::types::{ExperimentCreateRequest, ExperimentUpdateRequest};
use crate::components::context_form::types::Conditions;
use crate::logic::Conditions;
use crate::types::VariantFormT;
use crate::utils::{construct_request_headers, get_host, parse_json_response, request};
use serde_json::Value;
Expand All @@ -20,7 +20,7 @@ pub async fn create_experiment(
let payload = ExperimentCreateRequest {
name,
variants: FromIterator::from_iter(variants),
context: conditions.to_context_json()
context: conditions.to_context_json(),
};

let _ = validate_experiment(&payload)?;
Expand Down
14 changes: 6 additions & 8 deletions crates/frontend/src/components/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
schema::{EnumVariants, HtmlDisplay, JsonSchemaType, SchemaType},
};

use super::context_form::types::Operator;
use crate::logic::Operator;

#[derive(Debug, Clone, PartialEq)]
pub enum InputType {
Expand Down Expand Up @@ -139,11 +139,9 @@ fn parse_input(
schema_type: SchemaType,
op: &Option<Operator>,
) -> Result<Value, String> {
let parse_single = |r#type: &JsonSchemaType| {
match op {
Some(op) => parse_with_operator(&value, r#type, op),
None => parse(&value, r#type)
}
let parse_single = |r#type: &JsonSchemaType| match op {
Some(op) => parse_with_operator(&value, r#type, op),
None => parse(&value, r#type),
};

match schema_type {
Expand Down Expand Up @@ -478,13 +476,13 @@ pub fn monaco_input(
pub fn input(
value: Value,
schema_type: SchemaType,
#[prop(into)]on_change: Callback<Value, ()>,
#[prop(into)] on_change: Callback<Value, ()>,
#[prop(into)] r#type: InputType,
#[prop(default = false)] disabled: bool,
#[prop(into, default = String::new())] id: String,
#[prop(into, default = String::new())] class: String,
#[prop(into, default = String::new())] name: String,
#[prop(default = None)] operator: Option<Operator>
#[prop(default = None)] operator: Option<Operator>,
) -> impl IntoView {
match r#type {
InputType::Toggle => match value.as_bool() {
Expand Down
1 change: 1 addition & 0 deletions crates/frontend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod api;
pub mod app;
pub mod components;
pub mod hoc;
pub mod logic;
pub mod pages;
pub mod providers;
pub mod schema;
Expand Down
File renamed without changes.
61 changes: 29 additions & 32 deletions crates/frontend/src/pages/context_override.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use crate::api::{delete_context, fetch_default_config, fetch_dimensions};
use crate::components::alert::AlertType;
use crate::components::button::Button;
use crate::components::context_card::ContextCard;
use crate::components::context_form::types::{Condition, Conditions, Operator};
use crate::components::context_form::utils::{create_context, update_context};
use crate::components::context_form::ContextForm;
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::{Condition, Conditions, Operator};
use crate::providers::alert_provider::enqueue_alert;
use crate::providers::condition_collapse_provider::ConditionCollapseProvider;
use crate::providers::editor_provider::EditorProvider;
Expand Down Expand Up @@ -114,17 +114,18 @@ fn form(
/>

<div class="flex justify-start w-full mt-10">
{ move || {
let loading = req_inprogess_rs.get();
view! {
<Button
class="pl-[70px] pr-[70px] w-48 h-12".to_string()
text="Submit".to_string()
on_click=on_submit.clone()
loading
/>
}
}}
{move || {
let loading = req_inprogess_rs.get();
view! {
<Button
class="pl-[70px] pr-[70px] w-48 h-12".to_string()
text="Submit".to_string()
on_click=on_submit.clone()
loading
/>
}
}}

</div>
}
}
Expand Down Expand Up @@ -353,37 +354,33 @@ pub fn context_override() -> impl IntoView {
})
.collect::<Vec<(Context, Map<String, Value>)>>();
let is_empty = ctx_n_overrides.is_empty();


view! {
<Show when=move || is_empty>
<div class="flex-row" style="margin-top:20rem;">
<div class="flex justify-center text-gray-400">
<i class="ri-file-add-line ri-xl"></i>
<i class="ri-file-add-line ri-xl"></i>
</div>
<div class="flex mt-4 font-semibold items-center text-gray-400 text-xl justify-center">
"Start with creating an override"
"Start with creating an override"
</div>
</div>
</Show>
<ConditionCollapseProvider>

{
ctx_n_overrides
.into_iter()
.map(|(context, overrides)| {
view! {
<ContextCard
context=context
overrides=overrides
handle_edit=handle_context_edit
handle_clone=handle_context_clone
handle_delete=handle_context_delete
/>
}
})
.collect_view()
}
{ctx_n_overrides
.into_iter()
.map(|(context, overrides)| {
view! {
<ContextCard
context=context
overrides=overrides
handle_edit=handle_context_edit
handle_clone=handle_context_clone
handle_delete=handle_context_delete
/>
}
})
.collect_view()}

</ConditionCollapseProvider>
}
Expand Down
Loading

0 comments on commit ec6b6d1

Please sign in to comment.