From 89fe0495cb2b588d184e003fa8a6338d4aa4e585 Mon Sep 17 00:00:00 2001 From: Franklin Date: Fri, 14 Apr 2023 18:00:19 -0400 Subject: [PATCH] Added contact request logic and connected it to the backend --- Cargo.lock | 2 ++ Cargo.toml | 1 + src/api/backend/mod.rs | 15 +++++++++- src/pages/contact.rs | 64 ++++++++++++++++++++++++++++++++++++++---- src/pages/search.rs | 13 +++++---- src/utils/input.rs | 16 +++++++++++ src/utils/mod.rs | 3 +- 7 files changed, 101 insertions(+), 13 deletions(-) create mode 100644 src/utils/input.rs diff --git a/Cargo.lock b/Cargo.lock index 70ab70f..6ae120a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -721,6 +721,7 @@ dependencies = [ "wasm-bindgen", "wasm-bindgen-futures", "wasm-logger", + "web-sys", "yew", "yew-router", "yew-utils", @@ -734,6 +735,7 @@ dependencies = [ "chrono", "chrono-tz", "format_num", + "rand", "serde", "serde_json", "uuid", diff --git a/Cargo.toml b/Cargo.toml index 416360c..debeb7f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,7 @@ wasm-bindgen = "0.2.84" wasm-bindgen-futures = "0.4.34" stdweb = "0.4.20" js-sys = "0.3" +web-sys = "0.3.61" # other libs reqwest = { version = "0.11.11", features = ["rustls-tls", "json", "blocking"], default-features = false } diff --git a/src/api/backend/mod.rs b/src/api/backend/mod.rs index e1cd32c..98e2954 100644 --- a/src/api/backend/mod.rs +++ b/src/api/backend/mod.rs @@ -1,6 +1,6 @@ use std::collections::HashSet; -use jl_types::{dto::{filters::Filter, listing::Listing, project_card::ProjectCardDto}, domain::agent::Agent}; +use jl_types::{dto::{filters::Filter, listing::Listing, project_card::ProjectCardDto, payloads::contact::ContactPayload}, domain::{agent::Agent, count::Count, contact::Contact}}; use reqwest::Method; use uuid::Uuid; @@ -26,4 +26,17 @@ pub async fn get_project_listing(project_id: &Uuid) -> Result Result, err::Error> { perform_request_without_client::>(BASE_URL.into(), Method::GET, format!("read/agent"), None, 200, Vec::new(), None).await +} + +pub async fn get_all_page_visits_count() -> Result { + perform_request_without_client::(BASE_URL.into(), Method::GET, format!("admin/visits/count"), None, 200, Vec::new(), None).await +} +pub async fn get_all_contacts_count() -> Result { + perform_request_without_client::(BASE_URL.into(), Method::GET, format!("admin/contacts/count"), None, 200, Vec::new(), None).await +} +pub async fn get_all_contacts() -> Result, err::Error> { + perform_request_without_client::>(BASE_URL.into(), Method::GET, format!("admin/contacts"), None, 200, Vec::new(), None).await +} +pub async fn create_new_contact_request(contact: ContactPayload) -> Result<(), err::Error> { + perform_request_without_client(BASE_URL.into(), Method::POST, format!("read/contact"), Some(contact), 200, Vec::new(), None).await } \ No newline at end of file diff --git a/src/pages/contact.rs b/src/pages/contact.rs index 950aad8..9016bfd 100644 --- a/src/pages/contact.rs +++ b/src/pages/contact.rs @@ -1,10 +1,62 @@ +use jl_types::dto::payloads::contact::ContactPayload; use yew::prelude::*; -use crate::components::{nav_bar::NavigationBar, footer::PageFooter}; +use crate::{components::{nav_bar::NavigationBar, footer::PageFooter}, utils::input::{get_value_from_input_event, get_value_from_textarea_event}, api::backend::create_new_contact_request}; #[function_component(ContactPage)] pub fn contact_page() -> Html { + + let first_name = use_state(|| String::new()); + let last_name = use_state(|| String::new()); + let credential = use_state(|| String::new()); + let messsage = use_state(|| String::new()); + + //TODO: Think about how this renders each time an input is typed onto. (Pretty sure it does, as use_state re-renders the whole component it's in when called) + let on_fn_input_changed = { + let first_name = first_name.clone(); + Callback::from(move |e: InputEvent| { + first_name.set(get_value_from_input_event(e)); + }) + }; + let on_ln_input_changed = { + let last_name = last_name.clone(); + Callback::from(move |e: InputEvent| { + last_name.set(get_value_from_input_event(e)); + }) + }; + let on_credential_input_changed = { + let credential = credential.clone(); + Callback::from(move |e: InputEvent| { + credential.set(get_value_from_input_event(e)); + }) + }; + let on_message_input_changed = { + let messsage = messsage.clone(); + Callback::from(move |e: InputEvent| { + messsage.set(get_value_from_textarea_event(e)); + }) + }; + let onclick_button = { + let first_name = first_name.clone(); + let last_name = last_name.clone(); + let credential = credential.clone(); + let messsage = messsage.clone(); + Callback::from(move |_| { + let contact = ContactPayload { + first_name: (*first_name).clone(), + last_name: (*last_name).clone(), + credential: (*credential).clone(), + message: (*messsage).clone() + }; + wasm_bindgen_futures::spawn_local(async move { + match create_new_contact_request(contact).await { + Ok(_) => {}, + Err(error) => log::error!("Error in sending a contact request to the backend: {error}") + }; + }); + }) + }; html! { <> @@ -23,25 +75,25 @@ pub fn contact_page() -> Html {
// First name
{"Nombre"}
- +
// Last name
{"Apellido"}
- +
// Email or Phone number
{"Número de Teléfono o Correo"}
- +
// Message
{"Mensaje"}
-