Skip to content

Commit

Permalink
fix: moved override create, edit & clone to page
Browse files Browse the repository at this point in the history
  • Loading branch information
ShubhranshuSanjeev committed Nov 19, 2024
1 parent dff29e3 commit a4d1c91
Show file tree
Hide file tree
Showing 16 changed files with 584 additions and 343 deletions.
22 changes: 21 additions & 1 deletion crates/frontend/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use superposition_types::Config;
use crate::{
types::{
ConfigVersionListResponse, DefaultConfig, Dimension, ExperimentResponse,
ExperimentsResponse, FetchTypeTemplateResponse, FunctionResponse, ListFilters, TypeTemplate,
ExperimentsResponse, FetchTypeTemplateResponse, FunctionResponse, ListFilters, TypeTemplate, Context,
},
utils::{
construct_request_headers, get_host, parse_json_response, request,
Expand Down Expand Up @@ -297,3 +297,23 @@ pub async fn fetch_type(
.await
.map_err(err_handler)
}

pub async fn fetch_context(
tenant: &str,
id: &str,
) -> Result<Context, ServerFnError> {
let host = use_host_server();
let url = format!("{host}/context/{id}");
let err_handler = |e: String| ServerFnError::new(e.to_string());
let response = request::<()>(
url,
reqwest::Method::GET,
None,
construct_request_headers(&[("x-tenant", &tenant)]).map_err(err_handler)?,
)
.await
.map_err(err_handler)?;
parse_json_response::<Context>(response)
.await
.map_err(err_handler)
}
1 change: 1 addition & 0 deletions crates/frontend/src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ pub mod table;
pub mod toast;
pub mod type_template_form;
pub mod variant_form;
pub mod contextual_override_form;
34 changes: 8 additions & 26 deletions crates/frontend/src/components/context_card.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use leptos::*;
use leptos_router::A;
use serde_json::{Map, Value};
use superposition_types::Context;

Expand All @@ -15,14 +16,6 @@ pub fn context_card(
context: Context,
overrides: Map<String, Value>,
#[prop(default = true)] show_actions: bool,
#[prop(default=Callback::new(|_| {}))] handle_edit: Callback<
(Context, Map<String, Value>),
(),
>,
#[prop(default=Callback::new(|_| {}))] handle_clone: Callback<
(Context, Map<String, Value>),
(),
>,
#[prop(default=Callback::new(|_| {}))] handle_delete: Callback<String, ()>,
) -> impl IntoView {
let conditions: Conditions = (&context).try_into().unwrap_or_default();
Expand All @@ -38,10 +31,7 @@ pub fn context_card(
})
.collect::<Vec<Map<String, Value>>>();

// Clone context and overrides for use in event handlers
let context_id = store_value(context.id.clone());
let context = store_value(context);
let overrides = store_value(overrides);

let table_columns = vec![
Column::default("KEY".to_string()),
Expand All @@ -68,21 +58,15 @@ pub fn context_card(
<Show when=move || actions_supported>
<div class="h-fit text-right space-x-4">
<Show when=move || !edit_unsupported>
<i
class="ri-pencil-line ri-lg text-blue-500 cursor-pointer"
on:click=move |_| {
handle_edit.call((context.get_value(), overrides.get_value()));
}
>
</i>
<A href=format!("{}/update", context_id.get_value())>
<i class="ri-pencil-line ri-lg text-blue-500 cursor-pointer"></i>
</A>

<A href=format!("new?clone_from={}", context_id.get_value())>
<i
class="ri-file-copy-line ri-lg text-blue-500 cursor-pointer"
on:click=move |_| {
handle_clone.call((context.get_value(), overrides.get_value()));
}
>
</i>
></i>
</A>

</Show>
<Show when=move || edit_unsupported>
Expand All @@ -96,8 +80,7 @@ pub fn context_card(
let context_id = context_id.get_value();
handle_delete.call(context_id);
}
>
</i>
></i>

</div>
</Show>
Expand All @@ -110,7 +93,6 @@ pub fn context_card(

<div class="pl-5">
<ConditionComponent
// Clone only once before reusing in multiple closures
conditions=conditions
id=context_id.get_value()
class="xl:w-[400px] h-fit"
Expand Down
6 changes: 4 additions & 2 deletions crates/frontend/src/components/context_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ pub fn context_form<NF>(
#[prop(default = false)] disabled: bool,
#[prop(default = false)] resolve_mode: bool,
#[prop(default = String::new())] heading_sub_text: String,
#[prop(default = DropdownDirection::Right)] dropdown_direction: DropdownDirection,
#[prop(default = DropdownDirection::Left)] dropdown_direction: DropdownDirection,
) -> impl IntoView
where
NF: Fn(Conditions) + 'static,
Expand Down Expand Up @@ -255,6 +255,7 @@ where
<Dropdown
dropdown_icon="ri-add-line".to_string()
dropdown_text="Add Context".to_string()
dropdown_width="w-fit"
dropdown_btn_type=DropdownBtnType::Link
dropdown_options=dimensions
disabled=disabled
Expand All @@ -270,9 +271,10 @@ where
<Show when=move || context_rs.get().is_empty()>
<div class="flex justify-center">
<Dropdown
dropdown_width="w-80"
dropdown_width="w-fit"
dropdown_icon="ri-add-line".to_string()
dropdown_text="Add Context".to_string()
dropdown_btn_type=DropdownBtnType::Link
dropdown_direction
dropdown_options=dimensions.get_value()
disabled=disabled
Expand Down
104 changes: 104 additions & 0 deletions crates/frontend/src/components/contextual_override_form.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
use leptos::*;
use serde_json::{Map, Value};

use crate::{
components::{
button::Button,
context_form::{
utils::{create_context, update_context},
ContextForm,
},
override_form::OverrideForm,
},
logic::Conditions,
types::{DefaultConfig, Dimension},
};

#[component]
pub fn contextual_override_form(
edit: bool,
#[prop(into, default=String::new())] class: String,
#[prop(into, default=String::new())] width: String,
context: Conditions,
overrides: Vec<(String, Value)>,
dimensions: Vec<Dimension>,
default_config: Vec<DefaultConfig>,
on_submit: Callback<(), ()>,
) -> impl IntoView {
let tenant_rs = use_context::<Signal<String>>().unwrap();
let (context, set_context) = create_signal(context);
let (overrides, set_overrides) = create_signal(overrides);
let dimensions = StoredValue::new(dimensions);
let (req_inprogess_rs, req_inprogress_ws) = create_signal(false);

let on_submit = move |_| {
req_inprogress_ws.set(true);
spawn_local(async move {
let f_context = context.get();
let f_overrides = overrides.get();
let result = if edit {
update_context(
tenant_rs.get().clone(),
Map::from_iter(f_overrides),
f_context,
)
.await
} else {
create_context(
tenant_rs.get().clone(),
Map::from_iter(f_overrides),
f_context,
)
.await
};

match result {
Ok(_) => {
logging::log!("Context and overrides submitted successfully");
on_submit.call(());
}
Err(e) => {
logging::log!("Error submitting context and overrides: {:?}", e);
}
}
req_inprogress_ws.set(false);
});
};
view! {
<div class=format!("relative {}", width)>
<div class=format!("flex flex-col gap-8 pb-20 {}", class)>
<ContextForm
dimensions=dimensions.get_value()
context=context.get_untracked()
handle_change=move |new_context| {
set_context
.update(|value| {
*value = new_context;
});
}

disabled=edit
/>
<OverrideForm
overrides=overrides.get_untracked()
default_config=default_config
handle_change=move |new_overrides| {
set_overrides
.update(|value| {
*value = new_overrides;
});
}
/>
</div>
<div class="w-full absolute bottom-0 right-0 p-4 flex justify-end items-end bg-white border-r border-b border-l rounded-2xl rounded-t-none">
{move || {
let loading = req_inprogess_rs.get();
view! {
<Button text="Submit".to_string() on_click=on_submit.clone() loading />
}
}}

</div>
</div>
}
}
6 changes: 2 additions & 4 deletions crates/frontend/src/components/dropdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ where
view! {
<div
id=id
class=format!("dropdown {}", dropdown_width)
class=format!("relative dropdown {}", dropdown_width)
class=("disable-click", disabled)
class=("dropdown-right", dropdown_direction == DropdownDirection::Right)
class=("dropdown-left", dropdown_direction == DropdownDirection::Left)
Expand All @@ -83,9 +83,7 @@ where
</label>
<ul
tabindex="0"
class=format!(
"w-full min-w-[16rem] dropdown-content z-[1] menu flex-nowrap p-2 shadow bg-base-100 rounded-box max-h-96 overflow-y-scroll overflow-x-hidden",
)
class="w-64 dropdown-content z-[1] menu flex-nowrap p-2 shadow bg-base-100 rounded-box max-h-96 overflow-y-scroll overflow-x-hidden"
>

{if searchable {
Expand Down
2 changes: 2 additions & 0 deletions crates/frontend/src/components/experiment_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod utils;
use self::utils::{create_experiment, update_experiment};
use crate::components::button::Button;
use crate::components::context_form::ContextForm;
use crate::components::dropdown::DropdownDirection;
use crate::components::variant_form::VariantForm;
use crate::types::{DefaultConfig, Dimension, VariantFormT, VariantType};
use leptos::*;
Expand Down Expand Up @@ -149,6 +150,7 @@ pub fn experiment_form(
context=context
handle_change=handle_context_form_change
disabled=edit
dropdown_direction=DropdownDirection::Down
heading_sub_text=String::from(
"Define rules under which this experiment would run",
)
Expand Down
Loading

0 comments on commit a4d1c91

Please sign in to comment.