Skip to content

Commit

Permalink
Merge pull request #160 from avored/153-component-field-support-needed
Browse files Browse the repository at this point in the history
153 component field support needed
  • Loading branch information
indpurvesh authored Jun 2, 2024
2 parents 2aec301 + 7d44bea commit 30a382f
Show file tree
Hide file tree
Showing 20 changed files with 250 additions and 93 deletions.
1 change: 0 additions & 1 deletion avored_better_query/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ where

let decoded = decode_binary(&bytes).into_owned();
let decoded = String::from_utf8_lossy(&decoded).into_owned();
println!("--> avored qs: {}", decoded);
match serde_qs::from_str(&decoded) {
Ok(value) => Ok(AvoRedForm(value)),
Err(_) => Err(AvoRedError::ParseFormError)
Expand Down
File renamed without changes.
4 changes: 4 additions & 0 deletions react-admin/src/pages/auth/Logout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ function Logout() {
localStorage.clear();
redirect("/admin/login");
}, [redirect]); // Added "redirect" to the dependency array

return (
<></>
)
}

export default Logout;
31 changes: 21 additions & 10 deletions react-admin/src/pages/page/PageCreate.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,30 @@ function PageCreate() {
switch (componentField.field_type) {
case "textarea":
return <div>Textarea</div>;
case "radio":
return (
<div className="p-3">
<input id={componentField.identifier} type="radio" value={componentField.identifier}
className="w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 focus:ring-primary-500 dark:focus:ring-primary-600 dark:ring-offset-gray-800 focus:ring-2 dark:bg-gray-700 dark:border-gray-600"/>
<label htmlFor={componentField.identifier}
className="ms-2 text-sm font-medium text-gray-900 dark:text-gray-300">
{componentField.name}
</label>
</div>
);
case "text":
default:
return (
<div>
<InputField
label={componentField.name}
type="text"
name={componentField.identifier}
onChange={(e) =>
componentFieldContentOnChange(componentField.id, e.target.value)
}
/>
</div>
<div>
<InputField
label={componentField.name}
type="text"
name={componentField.identifier}
onChange={(e) =>
componentFieldContentOnChange(componentField.id, e.target.value)
}
/>
</div>
);
}
};
Expand Down
4 changes: 0 additions & 4 deletions src/api/handlers/admin_user/store_admin_user_api_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ pub async fn store_admin_user_api_handler(
while let Some(field) = multipart.next_field().await.expect("cant find next field") {
let name = field.name().expect("field name missing");

println!("field name: {name}");

match name {
"image" => {
let s: String = rand::thread_rng()
Expand Down Expand Up @@ -137,8 +135,6 @@ pub async fn store_admin_user_api_handler(
// //@todo return validation error response
// }

// println!("")

let password = payload.password.as_bytes();
let salt = SaltString::from_b64(&state.config.password_salt)?;

Expand Down
11 changes: 9 additions & 2 deletions src/api/handlers/component/request/store_component_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@ use crate::models::validation_error::ErrorMessage;
pub struct StoreComponentRequest {
pub name: String,
pub identifier: String,
pub fields: Vec<Field>,
pub fields: Vec<FieldRequest>,
}

#[derive(Deserialize, Debug, Clone, Default)]
pub struct Field {
pub struct FieldRequest {
pub name: String,
pub identifier: String,
pub field_type: String,
pub field_data: Option<Vec<FieldDataRequest>>
}

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

impl StoreComponentRequest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ pub struct UpdatableField {
pub name: String,
pub identifier: String,
pub field_type: String,
pub field_data: Option<Vec<UpdatableFieldDataRequest>>
}


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

impl UpdateComponentRequest {
Expand Down
17 changes: 15 additions & 2 deletions src/api/handlers/component/store_component_api_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use crate::{
};
use axum::{Extension, extract::State, Json};
use serde::Serialize;
use crate::api::handlers::component::request::store_component_request::StoreComponentRequest;
use crate::api::handlers::component::request::store_component_request::{FieldDataRequest, StoreComponentRequest};
use crate::error::Error;
use crate::models::component_model::{ComponentModel, CreatableComponent};
use crate::models::field_model::CreatableFieldModel;
use crate::models::field_model::{CreatableFieldDataModel, CreatableFieldModel};
use crate::models::token_claim_model::LoggedInUser;
use crate::models::validation_error::ErrorResponse;

Expand Down Expand Up @@ -43,10 +43,23 @@ pub async fn store_component_api_handler(
.await?;

for payload_field in payload.fields {
let mut creatable_field_data: Vec<CreatableFieldDataModel> = vec![];

let create_field_data_requests: Vec<FieldDataRequest> = payload_field.field_data.unwrap_or_else(|| vec![]);

for create_field_data_request in create_field_data_requests {
let create_field_data_option = CreatableFieldDataModel {
label: create_field_data_request.label,
value: create_field_data_request.value
};
creatable_field_data.push(create_field_data_option);
}

let creatable_field = CreatableFieldModel {
name: payload_field.name,
identifier: payload_field.identifier,
field_type: payload_field.field_type,
field_data: Some(creatable_field_data),
logged_in_username: logged_in_user.email.clone(),
};

Expand Down
34 changes: 15 additions & 19 deletions src/api/handlers/component/update_component_api_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use crate::{
};
use axum::{Extension, extract::{Path as AxumPath, State}, Json, response::IntoResponse};
use serde::Serialize;
use crate::api::handlers::component::request::update_component_request::UpdateComponentRequest;
use crate::api::handlers::component::request::update_component_request::{UpdatableFieldDataRequest, UpdateComponentRequest};
use crate::error::Error;
use crate::models::component_model::{ComponentModel, UpdatableComponentModel};
use crate::models::field_model::UpdatableFieldModel;
use crate::models::field_model::{UpdatableFieldDataModel, UpdatableFieldModel};
use crate::models::token_claim_model::LoggedInUser;
use crate::models::validation_error::ErrorResponse;

Expand Down Expand Up @@ -45,13 +45,24 @@ pub async fn update_component_api_handler(

for payload_field in payload.fields {

println!("Payload Fields: {payload_field:?}");
let mut updatable_field_data: Vec<UpdatableFieldDataModel> = vec![];
let update_field_data_requests: Vec<UpdatableFieldDataRequest> = payload_field.field_data.unwrap_or_else(|| vec![]);

for update_field_data_request in update_field_data_requests {
let update_field_data_option = UpdatableFieldDataModel {
label: update_field_data_request.label,
value: update_field_data_request.value
};
updatable_field_data.push(update_field_data_option);
}

//@todo check for field ID and if not exist then create field and attached field
let updatable_field = UpdatableFieldModel {
id: payload_field.id,
name: payload_field.name,
identifier: payload_field.identifier,
field_type: payload_field.field_type,
field_data: Some(updatable_field_data),
logged_in_username: logged_in_user.email.clone(),
};
let updated_field = state
Expand All @@ -60,22 +71,7 @@ pub async fn update_component_api_handler(
.await?;

updated_component_model.fields.push(updated_field);
//
// // println!("Created component {:?}", created_component.clone());
// // println!("Created Field {:?}", created_field.clone());
//
// state
// .component_service
// .attach_component_with_field(
// &state.db,
// created_component.clone(),
// created_field.clone(),
// "[email protected]".to_string(),
// )
// .await?;
// // println!("ATTACHED: {:?}", created_field.clone());
//
// created_component.fields.push(created_field);

}


Expand Down
2 changes: 0 additions & 2 deletions src/api/handlers/page/store_page_api_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ pub async fn store_page_api_handler(
creatable_page.component_contents.push(creatable_component_content_model);
}

// println!("Payload GENERATED: {:?}", creatable_page);

let created_page_model = state
.page_service
.create_page(&state.db, creatable_page, logged_in_user)
Expand Down
1 change: 1 addition & 0 deletions src/api/handlers/setup/post_setup_avored_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ pub async fn post_setup_avored_handler(
DEFINE FIELD name ON TABLE fields TYPE string;
DEFINE FIELD identifier ON TABLE fields TYPE string;
DEFINE FIELD field_type ON TABLE fields TYPE string;
DEFINE FIELD field_data ON TABLE fields TYPE array;
DEFINE FIELD created_by ON TABLE fields TYPE string;
DEFINE FIELD updated_by ON TABLE fields TYPE string;
DEFINE FIELD created_at ON TABLE fields TYPE datetime;
Expand Down
1 change: 0 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ impl From<lettre::error::Error> for Error {

impl IntoResponse for Error {
fn into_response(self) -> Response {
// println!("->> {:<12} - {self:?}", "INTO_RES");
let response = match self {
Error::BadRequestError(str) => {
// let tets = serde_json::to_string(&str);
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/require_jwt_authentication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub async fn require_jwt_authentication (

// if claims.sub.len() <= 0 {
// is_token_valid = true;
// // println!("Claim admin_user {claims:?}");
//
// }
//
// if is_token_valid {
Expand Down
83 changes: 83 additions & 0 deletions src/models/field_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,19 @@ pub struct FieldModel {
pub name: String,
pub identifier: String,
pub field_type: String,
pub field_data: Option<Vec<FieldDataModel>>,
pub created_at: Datetime,
pub updated_at: Datetime,
pub created_by: String,
pub updated_by: String,
}

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

impl TryFrom<Object> for FieldModel {
type Error = Error;
fn try_from(val: Object) -> Result<FieldModel> {
Expand Down Expand Up @@ -109,11 +116,39 @@ impl TryFrom<Object> for FieldModel {
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_model: FieldDataModel = object.try_into()?;

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

// let field_data = None;

Ok(FieldModel {
id,
name,
identifier,
field_type,
field_data: Some(field_data),
created_at,
updated_at,
created_by,
Expand All @@ -122,19 +157,61 @@ impl TryFrom<Object> for FieldModel {
}
}


impl TryFrom<Object> for FieldDataModel {
type Error = Error;
fn try_from(val: Object) -> Result<FieldDataModel> {
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(FieldDataModel {
label,
value
})
}
}


#[derive(Serialize, Debug, Deserialize, Clone)]
pub struct CreatableFieldModel {
pub name: String,
pub identifier: String,
pub field_type: String,
pub field_data: Option<Vec<CreatableFieldDataModel>>,
pub logged_in_username: String,
}

#[derive(Serialize, Debug, Deserialize, Clone, Default)]
pub struct CreatableFieldDataModel {
pub label: String,
pub value: String,
}
#[derive(Serialize, Debug, Deserialize, Clone)]
pub struct UpdatableFieldModel {
pub id: String,
pub name: String,
pub identifier: String,
pub field_type: String,
pub field_data: Option<Vec<UpdatableFieldDataModel>>,
pub logged_in_username: String,
}

Expand All @@ -143,3 +220,9 @@ pub struct FieldPagination {
pub data: Vec<FieldModel>,
pub pagination: Pagination,
}

#[derive(Serialize, Debug, Deserialize, Clone)]
pub struct UpdatableFieldDataModel {
pub label: String,
pub value: String,
}
2 changes: 1 addition & 1 deletion src/repositories/asset_repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl AssetRepository {
// Some(object) => object,
// None => Err(Error::Generic("no record found")),
// };
// // println!("RESULT_OBJECT: {result_object:?}");
//
// let asset_model: Result<AssetModel> = result_object?.try_into();
//
// asset_model
Expand Down
Loading

0 comments on commit 30a382f

Please sign in to comment.