Skip to content

Commit

Permalink
feat: new and update page for custom_types
Browse files Browse the repository at this point in the history
  • Loading branch information
ShubhranshuSanjeev committed Nov 11, 2024
1 parent 574d6e7 commit 04c564f
Show file tree
Hide file tree
Showing 15 changed files with 246 additions and 147 deletions.
15 changes: 15 additions & 0 deletions crates/context_aware_config/src/api/type_templates/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub fn endpoints() -> Scope {
.service(create_type)
.service(update_type)
.service(delete_type)
.service(get_type)
}

#[post("")]
Expand Down Expand Up @@ -145,3 +146,17 @@ async fn list_types(
"data": custom_types
})))
}

#[get("/{type_name}")]
async fn get_type(
db_conn: DbConnection,
path: Path<TypeTemplateName>,
) -> superposition::Result<HttpResponse> {
let DbConnection(mut conn) = db_conn;
let type_name: String = path.into_inner().into();

let custom_type = dsl::type_templates
.find(type_name)
.get_result::<TypeTemplates>(&mut conn)?;
Ok(HttpResponse::Ok().json(custom_type))
}
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,
ExperimentsResponse, FetchTypeTemplateResponse, FunctionResponse, ListFilters, TypeTemplate,
},
utils::{
construct_request_headers, get_host, parse_json_response, request,
Expand Down Expand Up @@ -277,3 +277,23 @@ pub async fn fetch_types(
.await
.map_err(err_handler)
}

pub async fn fetch_type(
tenant: &str,
name: &str,
) -> Result<TypeTemplate, ServerFnError> {
let host = use_host_server();
let url = format!("{host}/types/{name}");
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::<TypeTemplate>(response)
.await
.map_err(err_handler)
}
54 changes: 52 additions & 2 deletions crates/frontend/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ use serde_json::json;
use crate::hoc::layout::Layout;
use crate::pages::config_version::ConfigVersion;
use crate::pages::config_version_list::ConfigVersionList;
use crate::pages::create_experiment::CreateExperiment;
use crate::pages::dimensions::Dimensions;
use crate::pages::experiment_list::ExperimentList;
use crate::pages::function::{
function_create::CreateFunctionView, function_list::FunctionList, FunctionPage,
};
use crate::pages::new_custom_types::NewCustomTypes;
use crate::pages::new_experiment::NewExperiment;
use crate::pages::update_custom_types::UpdateCustomTypes;
use crate::pages::{
context_override::ContextOverride, custom_types::TypesPage,
default_config::DefaultConfig, experiment::ExperimentPage, home::Home,
Expand Down Expand Up @@ -105,6 +107,18 @@ pub fn app(app_envs: Envs) -> impl IntoView {
}
/>

<Route
ssr=SsrMode::Async
path="/admin/:tenant/dimensions/new"
view=move || {
view! {
<Layout>
<Dimensions />
</Layout>
}
}
/>

<Route
ssr=SsrMode::Async
path="/admin/:tenant/function"
Expand Down Expand Up @@ -159,7 +173,7 @@ pub fn app(app_envs: Envs) -> impl IntoView {
view=move || {
view! {
<Layout>
<CreateExperiment />
<NewExperiment />
</Layout>
}
}
Expand Down Expand Up @@ -189,6 +203,18 @@ pub fn app(app_envs: Envs) -> impl IntoView {
}
/>

<Route
ssr=SsrMode::Async
path="/admin/:tenant/default-config/new"
view=move || {
view! {
<Layout>
<DefaultConfig />
</Layout>
}
}
/>

<Route
ssr=SsrMode::Async
path="/admin/:tenant/overrides"
Expand Down Expand Up @@ -225,6 +251,30 @@ pub fn app(app_envs: Envs) -> impl IntoView {
}
/>

<Route
ssr=SsrMode::Async
path="/admin/:tenant/types/new"
view=move || {
view! {
<Layout>
<NewCustomTypes />
</Layout>
}
}
/>

<Route
ssr=SsrMode::Async
path="/admin/:tenant/types/:type_name/update"
view=move || {
view! {
<Layout>
<UpdateCustomTypes />
</Layout>
}
}
/>

<Route
ssr=SsrMode::Async
path="/admin/:tenant/config/versions"
Expand Down
20 changes: 7 additions & 13 deletions crates/frontend/src/components/context_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,11 @@ pub fn condition_input(
} = condition.get_value();

view! {
<div class="flex gap-x-6">
<div class="form-control">
<label class="label font-mono text-sm">
<span class="label-text">Dimension</span>
<div class="flex flex-wrap gap-y-2 gap-x-6">
<div class="form-control w-full">
<label class="label font-mono font-bold">
<span class="label-text underline">{dimension}</span>
</label>
<input
value=dimension.clone()
class="input w-full max-w-xs"
name="context-dimension-name"
disabled=true
/>
</div>
<div class="form-control w-20">
<label class="label font-medium font-mono text-sm">
Expand Down Expand Up @@ -80,7 +74,7 @@ pub fn condition_input(
</select>

</div>
<div class="form-control">
<div class="form-control flex-1">
<label class="label font-mono text-sm">
<span class="label-text">Value</span>
</label>
Expand All @@ -104,6 +98,7 @@ pub fn condition_input(
}

r#type=input_type.get_value()
width="w-full"
disabled=disabled
id=format!(
"{}-{}",
Expand All @@ -112,7 +107,6 @@ pub fn condition_input(
idx,
)

class="w-100"
name=""
operator=Some(condition.with_value(|v| v.operator.clone()))
/>
Expand Down Expand Up @@ -241,7 +235,7 @@ where

view! {
<div class="form-control w-full">
<div class="gap-1">
<div class="flex justify-between">
<label class="label flex-col justify-center items-start">
<span class="label-text font-semibold text-base">Context</span>
<span class="label-text text-slate-400">{heading_sub_text}</span>
Expand Down
16 changes: 8 additions & 8 deletions crates/frontend/src/components/dropdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn dropdown<T>(
#[prop(into, default = String::new())] dropdown_icon: String,
#[prop(default = DropdownDirection::Right)] dropdown_direction: DropdownDirection,
#[prop(default = DropdownBtnType::Outline)] dropdown_btn_type: DropdownBtnType,
#[prop(into, default = String::from("w-96"))] dropdown_width: String,
#[prop(into, default = String::new())] dropdown_width: String,
#[prop(default = false)] disabled: bool,
#[prop(default = true)] searchable: bool,
#[prop(into, default = String::new())] name: String,
Expand All @@ -50,18 +50,18 @@ where
});

let btn_class = match dropdown_btn_type {
DropdownBtnType::Outline => "btn btn-sm text-xs m-1 w-full btn-purple-outline",
DropdownBtnType::Link => "btn btn-sm text-xs m-1 w-full btn-purple-link",
DropdownBtnType::Fill => "btn btn-sm text-xs m-1 w-full btn-purple-fill",
DropdownBtnType::Select => "select select-bordered w-[28rem] items-center",
DropdownBtnType::Outline => "btn btn-sm text-xs m-1 btn-purple-outline",
DropdownBtnType::Link => "btn btn-sm text-xs m-1 btn-purple-link",
DropdownBtnType::Fill => "btn btn-sm text-xs m-1 btn-purple-fill",
DropdownBtnType::Select => "select select-bordered items-center",
};

let node_ref = create_node_ref::<html::Input>();

view! {
<div
id=id
class="dropdown"
class=format!("dropdown {}", dropdown_width)
class=("disable-click", disabled)
class=("dropdown-right", dropdown_direction == DropdownDirection::Right)
class=("dropdown-left", dropdown_direction == DropdownDirection::Left)
Expand All @@ -70,7 +70,7 @@ where
>
<label
tabindex="0"
class=format!("{} {}", class, btn_class)
class=format!("w-full {} {}", class, btn_class)
on:click:undelegated=move |_| {
if let Some(element) = node_ref.get() {
let _ = element.focus();
Expand All @@ -84,7 +84,7 @@ where
<ul
tabindex="0"
class=format!(
"{dropdown_width} dropdown-content z-[1] menu flex-nowrap p-2 shadow bg-base-100 rounded-box max-h-96 overflow-y-scroll overflow-x-hidden",
"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",
)
>

Expand Down
1 change: 0 additions & 1 deletion crates/frontend/src/components/experiment_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ pub fn experiment_form(
let context = f_context.get();
view! {
<ContextForm
// dimensions will now be a Vec<Dimension>
dimensions=dimensions.get_value()
context=context
handle_change=handle_context_form_change
Expand Down
22 changes: 13 additions & 9 deletions crates/frontend/src/components/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ pub fn select(
id: String,
name: String,
class: String,
width: String,
disabled: bool,
value: Value,
options: Vec<Value>,
Expand All @@ -220,7 +221,7 @@ pub fn select(
name=name.clone()
class=class.clone()
disabled=disabled
dropdown_width="w-100"
dropdown_width=width.clone()
dropdown_icon="".to_string()
dropdown_text=dropdown_text
dropdown_direction=DropdownDirection::Down
Expand All @@ -241,6 +242,7 @@ fn basic_input(
id: String,
name: String,
class: String,
width: String,
r#type: InputType,
disabled: bool,
required: bool,
Expand All @@ -261,11 +263,11 @@ fn basic_input(
}

view! {
<div class="flex flex-col gap-1">
<div class=format!("flex flex-col gap-y-1 {}", width)>
<input
id=id
name=name
class=format!("input input-bordered {}", class)
class=format!("input input-bordered {} w-full", class)
required=required
disabled=disabled
type=r#type.to_html_input_type()
Expand Down Expand Up @@ -306,6 +308,7 @@ fn basic_input(
pub fn monaco_input(
id: String,
class: String,
width: String,
value: Value,
on_change: Callback<Value, ()>,
schema_type: SchemaType,
Expand Down Expand Up @@ -366,7 +369,7 @@ pub fn monaco_input(
});

view! {
<div class=format!("relative border rounded-lg bg-white p-2 {}", class)>
<div class=format!("relative border rounded-lg bg-white p-2 {} {}", class, width)>
<Show when=move || {
editor_rs.with(|v| v.id != id.get_value())
}>
Expand All @@ -379,8 +382,7 @@ pub fn monaco_input(
on:click=move |e| {
on_edit_click.call(e);
}
>
</i>
></i>
</div>
<andypf-json-viewer
indent="4"
Expand Down Expand Up @@ -492,27 +494,29 @@ pub fn input(
#[prop(default = false)] disabled: bool,
#[prop(into, default = String::new())] id: String,
#[prop(into, default = String::new())] class: String,
#[prop(into, default = String::new())] width: String,
#[prop(into, default = String::new())] name: String,
#[prop(default = None)] operator: Option<Operator>,
) -> impl IntoView {
match r#type {
InputType::Toggle => match value.as_bool() {
Some(ref v) => {
view! { <Toggle value=v.clone() on_change class name/> }.into_view()
view! { <Toggle value=v.clone() on_change class name /> }.into_view()
}
None => view! { <span>An error occured</span> }.into_view(),
},
InputType::Select(ref options) => view! { <Select id name class value on_change disabled options=options.0.clone()/> }
InputType::Select(ref options) => view! { <Select id name class width value on_change disabled options=options.0.clone() /> }
.into_view(),
InputType::Monaco => {
view! { <MonacoInput id class value on_change schema_type operator/> }.into_view()
view! { <MonacoInput id class width value on_change schema_type operator /> }.into_view()
}
_ => {
view! {
<BasicInput
id=id
name=name
class=class
width=width
disabled=disabled
required=true
r#type=r#type
Expand Down
Loading

0 comments on commit 04c564f

Please sign in to comment.