Skip to content

Commit

Permalink
add support for select dropdown
Browse files Browse the repository at this point in the history
  • Loading branch information
indpurvesh committed Jul 3, 2024
1 parent b759c67 commit 9fb2e60
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 51 deletions.
5 changes: 3 additions & 2 deletions react-admin/src/pages/page/PageCreate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ function PageCreate() {
};

const submitHandler = async (data: ICreatablePage) => {
// console.log(data)
mutate(data);
};

Expand Down Expand Up @@ -243,8 +244,8 @@ function PageCreate() {
>
<PlusIcon className="text-primary-500 h-6 w-6" />
<span className="text-sm ml-1 text-primary-500">
{t("add_component")}
</span>
{t("add_component")}
</span>
</button>
</div>

Expand Down
2 changes: 1 addition & 1 deletion react-admin/src/pages/page/PageEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function PageEdit() {
const getSelectFieldDataOptionCurrentValue = ((pageComponentIndex: number, componentFieldIndex: number) => {

let val = getValues(`components_content.${pageComponentIndex}.fields.${componentFieldIndex}.field_content`)
console.log(val)

if (val) {
return [val]
}
Expand Down
8 changes: 7 additions & 1 deletion react-admin/src/types/page/ICreatablePage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@ export interface ICreatablePageComponentFieldModel {
identifier: string;
field_type: string;
field_content: string;
field_data?: Array<any>;
field_data?: Array<ICreatablePageComponentFieldDataModel>;
created_at: string;
updated_at: string;
created_by: string;
updated_by: string;
}

export interface ICreatablePageComponentFieldDataModel {
label: string;
value: string;
field_data_content: string;
}
9 changes: 8 additions & 1 deletion react-admin/src/types/page/IEditablePage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,16 @@ export interface IEditablePageComponentFieldModel {
identifier: string;
field_type: string;
field_content: string;
field_data?: Array<any>;
field_data?: Array<IEditablePageComponentFieldDataModel>;
created_at: string;
updated_at: string;
created_by: string;
updated_by: string;
}


export interface IEditablePageComponentFieldDataModel {
label: string;
value: string;
field_data_content: string;
}
7 changes: 7 additions & 0 deletions src/api/handlers/page/request/store_page_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ pub struct CreatableComponentFieldContentRequest {
pub identifier: String,
pub field_type: String,
pub field_content: String,
pub field_data: Option<Vec<CreatablePageComponentFieldDataOptionRequest>>
}

#[derive(Deserialize, Debug, Clone, Default)]
pub struct CreatablePageComponentFieldDataOptionRequest {
pub label: String,
pub value: String,
}


Expand Down
14 changes: 9 additions & 5 deletions src/api/handlers/page/request/update_page_request.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
use rust_i18n::t;
use serde::Deserialize;

use crate::models::validation_error::{ErrorMessage, Validate};

#[derive(Deserialize, Debug, Clone, Default)]
pub struct UpdatePageRequest {
// #[validate(length(min = 1, message = "The name is a required field."))]
pub name: String,

// #[validate(length(min = 1, message = "The identifier is a required field."))]
pub identifier: String,

pub components_content: Vec<UpdatableComponentContentRequest>,
}

Expand All @@ -31,8 +26,17 @@ pub struct UpdatableComponentFieldContentRequest {
pub identifier: String,
pub field_type: String,
pub field_content: String,
pub field_data: Option<Vec<EditablePageComponentFieldDataOptionRequest>>
}


#[derive(Deserialize, Debug, Clone, Default)]
pub struct EditablePageComponentFieldDataOptionRequest {
pub label: String,
pub value: String,
}


impl UpdatePageRequest {
pub fn validate(&self) -> crate::error::Result<Vec<ErrorMessage>> {
let mut errors: Vec<ErrorMessage> = vec![];
Expand Down
14 changes: 13 additions & 1 deletion src/api/handlers/page/store_page_api_handler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::Arc;

use crate::error::Error;
use crate::models::page_model::{CreatableComponentContentModel, CreatableComponentFieldContentModel, CreatablePageModel, PageModel};
use crate::models::page_model::{CreatableComponentContentModel, CreatableComponentFieldContentModel, CreatablePageModel, PageModel, CreatablePageComponentFieldDataModel};
use crate::models::validation_error::ErrorResponse;
use crate::{
avored_state::AvoRedState, error::Result
Expand Down Expand Up @@ -54,12 +54,24 @@ pub async fn store_page_api_handler(
};

for payload_component_fields_data in payload_component_content.fields {
let mut payload_field_data_model_options: Vec<CreatablePageComponentFieldDataModel> = Vec::new();
let payload_field_options_data = payload_component_fields_data.field_data.unwrap_or(Vec::new());

for payload_component_field_data_option in payload_field_options_data {
let creatable_page_field_option_data = CreatablePageComponentFieldDataModel {
label: payload_component_field_data_option.label,
value: payload_component_field_data_option.value,
};
payload_field_data_model_options.push(creatable_page_field_option_data);
}

let creatable_component_field_content = CreatableComponentFieldContentModel {
id: payload_component_fields_data.id,
name: payload_component_fields_data.name,
identifier: payload_component_fields_data.identifier,
field_type: payload_component_fields_data.field_type,
field_content: payload_component_fields_data.field_content,
field_data: payload_field_data_model_options
};

creatable_component_content_model.fields.push(creatable_component_field_content);
Expand Down
14 changes: 13 additions & 1 deletion src/api/handlers/page/update_page_api_handler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::Arc;

use crate::error::Error;
use crate::models::page_model::{PageModel, UpdatableComponentContentModel, UpdatableComponentFieldContentModel, UpdatablePageModel};
use crate::models::page_model::{PageModel, UpdatableComponentContentModel, UpdatableComponentFieldContentModel, UpdatablePageComponentFieldDataModel, UpdatablePageModel};
use crate::models::validation_error::ErrorResponse;
use crate::{
api::handlers::page::request::update_page_request::UpdatePageRequest,
Expand Down Expand Up @@ -57,12 +57,24 @@ pub async fn update_page_api_handler(
};

for payload_component_fields_data in payload_component_content.fields {
let mut payload_field_data_model_options: Vec<UpdatablePageComponentFieldDataModel> = Vec::new();
let payload_field_options_data = payload_component_fields_data.field_data.unwrap_or(Vec::new());

for payload_component_field_data_option in payload_field_options_data {
let creatable_page_field_option_data = UpdatablePageComponentFieldDataModel {
label: payload_component_field_data_option.label,
value: payload_component_field_data_option.value,
};
payload_field_data_model_options.push(creatable_page_field_option_data);
}

let updatable_component_field_content = UpdatableComponentFieldContentModel {
id: payload_component_fields_data.id,
name: payload_component_fields_data.name,
identifier: payload_component_fields_data.identifier,
field_type: payload_component_fields_data.field_type,
field_content: payload_component_fields_data.field_content,
field_data: payload_field_data_model_options
};

updatable_component_content_model.fields.push(updatable_component_field_content);
Expand Down
97 changes: 82 additions & 15 deletions src/models/page_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ pub struct ComponentFieldModel {
pub identifier: String,
pub field_type: String,
pub field_content: String,
pub field_data: Vec<PageComponentFieldDataOption>
}

#[derive(Serialize, Debug, Deserialize, Clone, Default)]
pub struct PageComponentFieldDataOption {
pub label: String,
pub value: String
}

#[derive(Serialize, Debug, Deserialize, Clone, Default)]
Expand Down Expand Up @@ -195,18 +202,6 @@ impl TryFrom<Object> for ComponentContentModel {
None => Vec::new(),
};

// let field_type = match val.get("field_type") {
// Some(val) => {
// let value = match val.clone() {
// Value::Strand(v) => v.as_string(),
// _ => String::from(""),
// };
// value
// }
// None => String::from(""),
// };


Ok(ComponentContentModel {
id,
name,
Expand All @@ -216,8 +211,6 @@ impl TryFrom<Object> for ComponentContentModel {
}
}



impl TryFrom<Object> for ComponentFieldModel {
type Error = Error;
fn try_from(val: Object) -> Result<ComponentFieldModel> {
Expand Down Expand Up @@ -277,13 +270,71 @@ impl TryFrom<Object> for ComponentFieldModel {
}
None => String::from(""),
};

let field_data = match val.get("field_data") {
Some(val) => {
let value = match val.clone() {
Value::Array(v) => {
let mut arr = Vec::new();

for array in v.into_iter() {
let object = match array.clone() {
Value::Object(v) => v,
_ => surrealdb::sql::Object::default(),
};

let field_data_option: PageComponentFieldDataOption = object.try_into()?;

arr.push(field_data_option)
}
arr
}
_ => Vec::new(),
};
value
}
None => Vec::new(),
};

Ok(ComponentFieldModel {
id,
name,
identifier,
field_type,
field_content
field_content,
field_data
})
}
}

impl TryFrom<Object> for PageComponentFieldDataOption {
type Error = Error;
fn try_from(val: Object) -> Result<PageComponentFieldDataOption> {
let label = match val.get("label") {
Some(val) => {
let value = match val.clone() {
Value::Strand(v) => v.as_string(),
_ => String::from(""),
};
value
}
None => String::from(""),
};
let value = match val.get("value") {
Some(val) => {
let value = match val.clone() {
Value::Strand(v) => v.as_string(),
_ => String::from(""),
};
value
}
None => String::from(""),
};


Ok(PageComponentFieldDataOption {
label,
value
})
}
}
Expand Down Expand Up @@ -322,6 +373,14 @@ pub struct CreatableComponentFieldContentModel {
pub identifier: String,
pub field_type: String,
pub field_content: String,
pub field_data: Vec<CreatablePageComponentFieldDataModel>
}


#[derive(Deserialize, Debug, Clone, Default, Serialize)]
pub struct CreatablePageComponentFieldDataModel {
pub label: String,
pub value: String,
}

#[derive(Serialize, Debug, Deserialize, Clone)]
Expand Down Expand Up @@ -351,4 +410,12 @@ pub struct UpdatableComponentFieldContentModel {
pub identifier: String,
pub field_type: String,
pub field_content: String,
pub field_data: Vec<UpdatablePageComponentFieldDataModel>
}


#[derive(Deserialize, Debug, Clone, Default, Serialize)]
pub struct UpdatablePageComponentFieldDataModel {
pub label: String,
pub value: String,
}
Loading

0 comments on commit 9fb2e60

Please sign in to comment.