Skip to content

Commit

Permalink
feat: update inputs in default config
Browse files Browse the repository at this point in the history
  • Loading branch information
Datron committed Nov 5, 2024
1 parent e7075be commit 3f59418
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 51 deletions.
127 changes: 78 additions & 49 deletions crates/frontend/src/components/default_config_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn default_config_form<NF>(
#[prop(default = String::new())] config_key: String,
#[prop(default = String::new())] config_type: String,
#[prop(default = Value::Null)] type_schema: Value,
#[prop(default = String::new())] config_value: String,
#[prop(default = Value::Null)] config_value: Value,
#[prop(default = None)] function_name: Option<Value>,
#[prop(default = None)] prefix: Option<String>,
handle_submit: NF,
Expand All @@ -38,20 +38,12 @@ where
{
let tenant_rs = use_context::<ReadSignal<String>>().unwrap();

let (config_key, set_config_key) = create_signal(config_key);
let (config_key_rs, config_key_ws) = create_signal(config_key);
let (config_type_rs, config_type_ws) = create_signal(config_type);
let (config_schema_rs, config_schema_ws) = create_signal(type_schema);
let (config_value, set_config_value) = create_signal(config_value);
let (function_name, set_function_name) = create_signal(function_name);
let (config_value_rs, config_value_ws) = create_signal(config_value);
let (function_name_rs, function_name_ws) = create_signal(function_name);
let (req_inprogess_rs, req_inprogress_ws) = create_signal(false);
let string_to_value_closure = |val: String| {
Value::from_str(&val).unwrap_or_else(|_| {
// do this for Value::String, since for some reason from_str
// cannot convert unquoted rust strings to Value::String
Value::from_str(format!("\"{}\"", val).as_str())
.expect("Invalid default config value")
})
};

let functions_resource: Resource<String, Vec<crate::types::FunctionResponse>> =
create_blocking_resource(
Expand All @@ -76,7 +68,7 @@ where

let handle_select_dropdown_option =
Callback::new(move |selected_function: FunctionsName| {
set_function_name.update(|value| {
function_name_ws.update(|value| {
let function_name = selected_function.clone();
leptos::logging::log!("function selected: {:?}", function_name);
let fun_name = match function_name.as_str() {
Expand All @@ -92,14 +84,14 @@ where
let on_submit = move |ev: MouseEvent| {
req_inprogress_ws.set(true);
ev.prevent_default();
let f_name = prefix
.clone()
.map_or_else(|| config_key.get(), |prefix| prefix + &config_key.get());
let f_name = prefix.clone().map_or_else(
|| config_key_rs.get(),
|prefix| prefix + &config_key_rs.get(),
);
let f_schema = config_schema_rs.get();
let f_value = config_value.get();
let f_value = config_value_rs.get();

let fun_name = function_name.get();
let f_value = string_to_value_closure(f_value);
let fun_name = function_name_rs.get();

let payload = DefaultConfigCreateReq {
schema: f_schema,
Expand Down Expand Up @@ -133,6 +125,7 @@ where
});
};
view! {
<EditorProvider>
<form class="form-control w-full space-y-4 bg-white text-gray-700 font-mono">

<div class="form-control">
Expand All @@ -144,10 +137,10 @@ where
type="text"
placeholder="Key"
class="input input-bordered w-full max-w-md"
value=config_key.get_untracked()
value=config_key_rs.get_untracked()
on:change=move |ev| {
let value = event_target_value(&ev);
set_config_key.set(value);
config_key_ws.set(value);
}
/>

Expand Down Expand Up @@ -183,16 +176,16 @@ where
config_schema_ws.set(selected_item.type_schema);
})
/>
<EditorProvider>
<Input
id="type-schema"
class="mt-5 rounded-md resize-y w-full max-w-md pt-3"
schema_type=config_type_schema
value=config_schema_rs.get()
on_change=Callback::new(move |new_config_schema| config_schema_ws.set(new_config_schema))
r#type=InputType::Monaco
/>
</EditorProvider>

<Input
id="type-schema"
class="mt-5 rounded-md resize-y w-full max-w-md pt-3"
schema_type=config_type_schema
value=config_schema_rs.get()
on_change=Callback::new(move |new_config_schema| config_schema_ws.set(new_config_schema))
r#type=InputType::Monaco
/>

</div>
}
}}
Expand All @@ -209,9 +202,9 @@ where
view! {
<EnumDropdown
schema
config_value=config_value.get()
config_value=config_value_rs.get().as_str().map(String::from).unwrap_or_default()
handle_change=Callback::new(move |selected_enum: String| {
set_config_value.set(selected_enum);
config_value_ws.set(Value::String(selected_enum));
})

class=String::from("mt-2")
Expand All @@ -220,49 +213,84 @@ where
.into_view()
}
"BOOLEAN" => {
let value = config_value.get();
if value.is_empty() {
set_config_value
.set(value.parse::<bool>().unwrap_or(false).to_string());
let value = config_value_rs.get();
if value.is_null() {
config_value_ws.set(Value::Bool(false));
}
view! {
<BooleanToggle
value={value.parse::<bool>().unwrap_or(false)}
on_change=Callback::new(move |flag: bool| {
set_config_value.set(flag.to_string());
})
value=value.as_bool().unwrap_or(false)
on_change=Callback::new(move |flag: bool| config_value_ws.set(Value::Bool(flag)))
/>
}
.into_view()
}
"INTEGER" => {
view! {
<input
type="number"
placeholder="Value"
class="input input-bordered w-full max-w-md"
value=config_value_rs.get().as_i64()
on:change=move |ev| {
logging::log!("{:?}", event_target_value(&ev));
let entered_integer = i64::from_str(&event_target_value(&ev)).unwrap_or(0);
config_value_ws.set(json!(entered_integer));
}
/>
}
.into_view()
}
"NUMBER" | "INTEGER" => {
"NUMBER" => {
view! {
<input
type="number"
placeholder="Value"
class="input input-bordered w-full max-w-md"
value=config_value.get()
value=config_value_rs.get().as_f64()
on:change=move |ev| {
logging::log!("{:?}", event_target_value(& ev));
set_config_value.set(event_target_value(&ev));
logging::log!("{:?}", event_target_value(&ev));
let entered_decimal = f64::from_str(&event_target_value(&ev)).unwrap_or_default();
config_value_ws.set(json!(entered_decimal));
}
/>
}
.into_view()
}
"OBJECT" => {
let config_type_schema = SchemaType::Single(JsonSchemaType::from(&config_schema_rs.get()));
view! {
<Input
id="default-config-value"
class="mt-5 rounded-md resize-y w-full max-w-md pt-3"
schema_type=config_type_schema
value=config_value_rs.get()
on_change=Callback::new(move |new_default_config: Value| {
logging::log!("{:?}", new_default_config);
config_value_ws.set(new_default_config);
})
r#type=InputType::Monaco
/>

}.into_view()
},
_ => {
let config_value = match config_value_rs.get() {
Value::String(s) => s,
_ => String::new()
};
view! {
<textarea
type="text"
placeholder="Value"
class="input input-bordered w-full max-w-md pt-3"
on:change=move |ev| {
logging::log!("{:?}", event_target_value(& ev));
set_config_value.set(event_target_value(&ev));
logging::log!("{:?}", event_target_value(&ev));
config_value_ws.set(Value::String(event_target_value(&ev)));
}
>

{config_value.get()}
{config_value}
</textarea>
}
.into_view()
Expand Down Expand Up @@ -305,7 +333,7 @@ where
<Dropdown
dropdown_width="w-100"
dropdown_icon="".to_string()
dropdown_text=function_name
dropdown_text=function_name_rs
.get()
.and_then(|v| match v {
Value::String(s) => Some(s),
Expand Down Expand Up @@ -347,5 +375,6 @@ where
}

</form>
</EditorProvider>
}
}
4 changes: 2 additions & 2 deletions crates/frontend/src/pages/default_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::collections::HashSet;
#[derive(Clone, Debug, Default)]
pub struct RowData {
pub key: String,
pub value: String,
pub value: Value,
pub schema: Value,
pub function_name: Option<Value>,
}
Expand Down Expand Up @@ -75,7 +75,7 @@ pub fn default_config() -> impl IntoView {
let actions_col_formatter = move |_: &str, row: &Map<String, Value>| {
let row_key = row["key"].to_string().replace('"', "");
let is_folder = row_key.contains('.');
let row_value = row["value"].to_string().replace('"', "");
let row_value = row["value"].clone();

let schema = row["schema"].clone().to_string();
let schema_object =
Expand Down

0 comments on commit 3f59418

Please sign in to comment.