Finished agent update & Creation

This commit is contained in:
Franklin 2023-04-25 18:41:40 -04:00
parent e06651f14c
commit f31dd3c4d2
2 changed files with 86 additions and 6 deletions

View File

@ -9,7 +9,7 @@ use jl_types::{
payloads::{
contact::ContactPayload,
location::NewLocationPayload,
project::{NewProjectPayload, UpdateProjectPayload}, unit::UpdateUnitPayload, agent::UpdateAgentPayload,
project::{NewProjectPayload, UpdateProjectPayload}, unit::UpdateUnitPayload, agent::{UpdateAgentPayload, NewAgentPayload},
},
project_card::ProjectCardDto,
},
@ -201,6 +201,18 @@ pub async fn get_location_with_city_and_district(
.await
}
pub async fn create_new_agent(agent: NewAgentPayload) -> Result<Agent, err::Error> {
perform_request_without_client(
BASE_URL.into(),
Method::POST,
format!("admin/agent"),
Some(agent),
200,
Vec::new(),
None,
)
.await
}
pub async fn create_new_project(project: NewProjectPayload) -> Result<Project, err::Error> {
perform_request_without_client(
BASE_URL.into(),

View File

@ -1,8 +1,8 @@
use jl_types::domain::{agent::Agent, credential::CredentialType};
use jl_types::{domain::{agent::Agent, credential::CredentialType}, dto::payloads::agent::{NewAgentPayload, UpdateAgentPayload}};
use yew::prelude::*;
use yew_router::prelude::use_navigator;
use crate::{pages::admin::edit::{EditType}, components::{textfield::TextField, dropdown::DropDown, single_media_picker::SingleMediaPicker}, };
use crate::{pages::admin::edit::{EditType}, components::{textfield::TextField, dropdown::DropDown, single_media_picker::SingleMediaPicker}, api::backend::{update_agent, create_new_agent}, routes::main_router::Route, };
#[derive(Properties, PartialEq, Clone)]
pub struct AgentFieldsProps {
@ -12,7 +12,7 @@ pub struct AgentFieldsProps {
#[function_component(AgentFields)]
pub fn agent_fields(props: &AgentFieldsProps) -> Html {
let _navigator = use_navigator().unwrap();
let navigator = use_navigator().unwrap();
let user_typed = use_state(|| false);
let agent_opt = props.agent.clone();
@ -34,7 +34,7 @@ pub fn agent_fields(props: &AgentFieldsProps) -> Html {
let agent_shortcode_handle = use_state_eq(|| String::new());
let profile_picture_url_handle = use_state_eq(|| String::new());
let credential_handle = use_state_eq(|| String::new());
let credential_type = use_state_eq(|| Some(CredentialType::PhoneNumber));
let credential_type = use_state_eq(|| None);
// Fields definition & Update on value recieved
if !*user_typed {
@ -62,8 +62,76 @@ pub fn agent_fields(props: &AgentFieldsProps) -> Html {
});
}
let update_button_onclick = {
let navigator = navigator.clone();
let edit_type = props.edittype.clone();
// Fields to be sent to the backend
let agent_name_handle = agent_name_handle.clone();
let agent_shortcode_handle = agent_shortcode_handle.clone();
let profile_picture_url_handle = profile_picture_url_handle.clone();
let credential_handle = credential_handle.clone();
let credential_type = credential_type.clone();
Callback::from(move |_: MouseEvent| {
let navigator = navigator.clone();
let edit_type = edit_type.clone();
// Fields to be sent to the backend
let agent_name_handle = agent_name_handle.clone();
let profile_picture_url_handle = profile_picture_url_handle.clone();
let credential_handle = credential_handle.clone();
let credential_type_handle = credential_type.clone();
wasm_bindgen_futures::spawn_local(async move {
if (*agent_name_handle).clone().is_empty() {
log::error!("Missing field agent_name");
return;
}
if (*profile_picture_url_handle).clone().is_empty() {
log::error!("Missing field profile_picture_url");
return;
}
if (*credential_handle).clone().is_empty() {
log::error!("Missing field credential");
return;
}
let agent_name = (*agent_name_handle).clone();
let profile_picture_url = (*profile_picture_url_handle).clone();
let credential = (*credential_handle).clone();
let credential_type = if let Some(credential_type) = (*credential_type_handle).clone(){
credential_type
} else {
log::error!("Missing field credential_type");
return;
};
match edit_type {
EditType::New => {
let agent = NewAgentPayload {
credential,
credential_type,
full_name: agent_name,
profile_picture_url
};
match create_new_agent(agent).await {
Ok(_) => navigator.push(&Route::AdminAgents),
Err(error) => log::error!("Error updating agent: {error}")
};
},
EditType::Existing(id) => {
let agent = UpdateAgentPayload {
id,
credential: Some(credential),
credential_type: Some(credential_type),
full_name: Some(agent_name),
profile_picture_url: Some(profile_picture_url)
};
match update_agent(agent).await {
Ok(_) => navigator.push(&Route::AdminAgents),
Err(error) => log::error!("Error updating agent: {error}")
};
}
};
});
})
};
html! {