Skip to content

Commit

Permalink
fix: JSONSchema generated forms for contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
ShubhranshuSanjeev committed Nov 18, 2024
1 parent c155bb0 commit f6a3529
Show file tree
Hide file tree
Showing 23 changed files with 876 additions and 1,214 deletions.
64 changes: 25 additions & 39 deletions crates/frontend/src/components/condition_pills.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
pub mod types;
pub mod utils;
use crate::{
logic::{Condition, Conditions, Operand, Operator},
schema::HtmlDisplay,
};

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

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

use self::types::Condition;

use derive_more::{Deref, DerefMut};

#[derive(Debug, Clone, Deref, DerefMut, Default)]
Expand Down Expand Up @@ -60,30 +57,13 @@ pub fn condition_expression(
} else {
("condition-item-collapsed", "condition-value-collapsed")
};
let Condition { left_operand: dimension, operator, right_operand: value } = condition
.get_value();
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(","),
)
}
Value::Object(o) => serde_json::to_string_pretty(&o).ok(),
_ => None,
}
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()),
}
})
.collect();
Expand All @@ -106,18 +86,18 @@ pub fn condition_expression(
</span>

{match operator {
ConditionOperator::Between => {
if filtered_vals.len() == 2 {
Operator::Between => {
if operand_str.len() == 2 {
view! {
<>
<span class="font-mono font-semibold context_condition">
{&filtered_vals[0]}
{&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">
{&filtered_vals[1]}
{&operand_str[1]}
</span>
</>
}
Expand All @@ -132,7 +112,7 @@ pub fn condition_expression(
}
}
_ => {
let rendered_value = filtered_vals.join(", ");
let rendered_value = operand_str.join(", ");
view! { <span class=value_class>{rendered_value}</span> }.into_view()
}
}}
Expand All @@ -146,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 @@ -164,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.

16 changes: 9 additions & 7 deletions crates/frontend/src/components/context_card.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ use leptos::*;
use serde_json::{Map, Value};
use superposition_types::Context;

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

#[component]
Expand Down Expand Up @@ -50,12 +52,12 @@ pub fn context_card(
&& !conditions
.0
.iter()
.any(|condition| condition.left_operand == "variantIds");
.any(|condition| condition.dimension == "variantIds");

let edit_unsupported = conditions
.0
.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 Expand Up @@ -109,7 +111,7 @@ pub fn context_card(
<div class="pl-5">
<ConditionComponent
// Clone only once before reusing in multiple closures
conditions=conditions.0
conditions=conditions
id=context_id.get_value()
class="xl:w-[400px] h-fit"
/>
Expand Down
Loading

0 comments on commit f6a3529

Please sign in to comment.