Skip to content

Commit

Permalink
refactor: Enhance context form and display unsupported operators in o…
Browse files Browse the repository at this point in the history
…verrides UI
  • Loading branch information
sauraww committed Sep 2, 2024
1 parent 188683b commit 62a3522
Show file tree
Hide file tree
Showing 14 changed files with 765 additions and 498 deletions.
62 changes: 44 additions & 18 deletions crates/frontend/src/components/condition_pills.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::components::condition_pills::types::ConditionOperator;

use self::types::Condition;
use leptos::*;
use serde_json::Value;

#[component]
pub fn condition_pills(#[prop(into)] conditions: Vec<Condition>) -> impl IntoView {
Expand All @@ -15,6 +16,25 @@ pub fn condition_pills(#[prop(into)] conditions: Vec<Condition>) -> impl IntoVie
let dimension = condition.left_operand;
let op = condition.operator;
let val = condition.right_operand;
let filtered_vals: Vec<String> = val.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
}

}
).collect();
view! {
<span class="inline-flex items-center rounded-md bg-gray-50 px-2 py-1 text-xs ring-1 ring-inset ring-purple-700/10 shadow-md gap-x-2">
<span class="font-mono font-medium context_condition text-gray-500">
Expand All @@ -26,30 +46,36 @@ pub fn condition_pills(#[prop(into)] conditions: Vec<Condition>) -> impl IntoVie

{match op {
ConditionOperator::Between => {
let split_val: Vec<String> = val
.clone()
.split(',')
.map(String::from)
.collect();
view! {
<>
<span class="font-mono font-semibold context_condition">
{&split_val[0]}
</span>
<span class="font-mono font-medium text-gray-650 context_condition ">
{"and"}
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>
</>
}
} else {
view! {
<>
<span>
</span>
<span class="font-mono font-semibold context_condition">
{&split_val[1]}
</span>
</>
}
</>
}
}
}
_ => {

let rendered_values = filtered_vals.join(",");
view! {
<>
<span class="font-mono font-semibold context_condition">
{val}
{rendered_values}
</span>
</>
}
Expand Down
38 changes: 17 additions & 21 deletions crates/frontend/src/components/condition_pills/types.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::fmt::Display;

use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};

use crate::types::Context;

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ConditionOperator {
Is,
In,
Expand All @@ -25,6 +26,18 @@ impl Display for ConditionOperator {
}
}

impl From<String> for ConditionOperator {
fn from(op: String) -> Self {
match op.as_str() {
"==" => ConditionOperator::Is,
"<=" => ConditionOperator::Between,
"in" => ConditionOperator::In,
"has" => ConditionOperator::Has,
other => ConditionOperator::Other(other.to_string()),
}
}
}

impl From<(String, &Vec<Value>)> for ConditionOperator {
fn from(value: (String, &Vec<Value>)) -> Self {
let (operator, operands) = value;
Expand All @@ -50,11 +63,11 @@ impl From<(String, &Vec<Value>)> for ConditionOperator {
}
}

#[derive(Clone)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Condition {
pub left_operand: String,
pub operator: ConditionOperator,
pub right_operand: String,
pub right_operand: Vec<Value>,
}

impl TryFrom<&Map<String, Value>> for Condition {
Expand All @@ -76,27 +89,10 @@ impl TryFrom<&Map<String, Value>> for Condition {
})
.unwrap_or("");

let other_operands = operands
.iter()
.filter_map(|item| {
if item.is_object() && item.as_object().unwrap().contains_key("var") {
return None;
}

match item {
Value::Null => String::from("null"),
Value::String(v) => v.clone(),
_ => format!("{}", item),
}
.into()
})
.collect::<Vec<String>>()
.join(",");

return Ok(Condition {
operator,
left_operand: dimension_name.to_owned(),
right_operand: other_operands,
right_operand: operands.to_vec(),
});
}

Expand Down
50 changes: 33 additions & 17 deletions crates/frontend/src/components/context_card.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use serde_json::{Map, Value};

use crate::{
components::{
condition_pills::{types::Condition, ConditionPills},
condition_pills::{
types::{Condition, ConditionOperator},
ConditionPills,
},
table::{types::Column, Table},
},
types::Context,
Expand All @@ -18,6 +21,7 @@ pub fn context_card(
handle_delete: Callback<String, ()>,
) -> impl IntoView {
let conditions: Vec<Condition> = (&context).try_into().unwrap_or(vec![]);
let conditions_clone = conditions.clone();
let override_table_rows = overrides
.clone()
.into_iter()
Expand All @@ -44,25 +48,37 @@ pub fn context_card(
"Condition"
</h3>
<i class="ri-arrow-right-fill ri-xl text-blue-500"></i>
<ConditionPills conditions=conditions/>
<ConditionPills conditions=conditions.clone()/>
</div>
<div class="flex space-x-4">
<Show when=move || {
// Check if no condition has `ConditionOperator::Other`
!conditions.iter().any(|condition| matches!(condition.operator, ConditionOperator::Other(_)))
}>
<div class="flex space-x-4">
<i
class="ri-pencil-line ri-xl text-blue-500 cursor-pointer"
on:click=move |_| {
handle_edit.call((context.get_value(), overrides.get_value()))
}
></i>
<i
class="ri-file-copy-line ri-xl text-blue-500 cursor-pointer"
on:click=move |_| {
handle_clone.call((context.get_value(), overrides.get_value()))
}
></i>
</div>
</Show>
<Show when=move || {
conditions_clone.iter().any(|condition| matches!(condition.operator, ConditionOperator::Other(_)))
}>
<span class="badge badge-warning text-xs ml-2 flex items-center">
{"Edit Unsupported"}
</span>
</Show>
<i
class="ri-pencil-line ri-xl text-blue-500 cursor-pointer"
on:click=move |_| {
handle_edit.call((context.get_value(), overrides.get_value()))
}
>
</i>
<i
class="ri-file-copy-line ri-xl text-blue-500 cursor-pointer"
on:click=move |_| {
handle_clone.call((context.get_value(), overrides.get_value()))
}
>
</i>
<i
class="ri-delete-bin-5-line ri-xl text-blue-500 cursor-pointer"
class="ri-delete-bin-5-line ri-xl text-red-500 cursor-pointer"
on:click=move |_| { handle_delete.call(context.get_value().id) }
></i>
</div>
Expand Down
Loading

0 comments on commit 62a3522

Please sign in to comment.