diff --git a/corp/rih/Cargo.lock b/corp/rih/Cargo.lock index 7e472e5fe..b9d5c9c33 100644 --- a/corp/rih/Cargo.lock +++ b/corp/rih/Cargo.lock @@ -1093,6 +1093,7 @@ dependencies = [ "serde_urlencoded", "static_markdown", "wasm-bindgen", + "wasm-bindgen-futures", "web-sys", "yew", "yew-router", diff --git a/corp/rih/Cargo.toml b/corp/rih/Cargo.toml index 2dd8cb7b9..1ef53bb5d 100644 --- a/corp/rih/Cargo.toml +++ b/corp/rih/Cargo.toml @@ -16,6 +16,7 @@ serde_json = "1.0" serde_urlencoded = "*" # pinned by yew yew = { version = "0.20", features = ["csr"] } yew-router = "0.17" +wasm-bindgen-futures = "0.4" # needs to be in sync with nixpkgs wasm-bindgen = "= 0.2.84" diff --git a/corp/rih/src/home.html b/corp/rih/src/home.html index df77ee1e8..697daf514 100644 --- a/corp/rih/src/home.html +++ b/corp/rih/src/home.html @@ -99,6 +99,7 @@ html! {

{"Let's get started with you telling us a bit about what kind of job you would like!"}

+ if !self.submitted {
@@ -172,12 +173,18 @@ html! {

{"This page is still under construction! Please reach out at contact@ if you have any questions."}

+ } else { +
+

{"Thank you for submitting your data! We will reach out to confirm your email address, and further if any matches are found. You can contact us at contact@russiaishiring.com with any questions you might have."}

+
+ }
diff --git a/corp/rih/src/main.rs b/corp/rih/src/main.rs index 02cb350d1..9b7e38422 100644 --- a/corp/rih/src/main.rs +++ b/corp/rih/src/main.rs @@ -2,7 +2,9 @@ use fuzzy_matcher::skim::SkimMatcherV2; use fuzzy_matcher::FuzzyMatcher; use gloo::console; use gloo::history::{BrowserHistory, History}; +use gloo::net::http; use gloo::storage::{LocalStorage, Storage}; +use gloo::utils::format::JsValueSerdeExt; use rand::seq::IteratorRandom; use rand::thread_rng; use serde::{Deserialize, Serialize}; @@ -24,6 +26,13 @@ const CAPTCHA_KEY: &'static str = "ysc1_K7iOi3FSmsyO8pZGu8Im2iQClCtPsVx7jSRyhyCV #[cfg(not(debug_assertions))] const CAPTCHA_KEY: &'static str = "ysc1_a3LVlaDRDMwU8CLSZ0WKENTI2exyOxz5J2c6x28P5339d410"; +// Form data is submitted to different endpoints in dev/prod. +#[cfg(debug_assertions)] +const SUBMIT_URL: &'static str = "http://localhost:9090/submit"; + +#[cfg(not(debug_assertions))] +const SUBMIT_URL: &'static str = "https://api.russiaishiring.com/submit"; + /// This code ends up being compiled for the native and for the /// webassembly architectures during the build & test process. /// However, the `rust_iso3166` crate exposes a different API (!) @@ -83,7 +92,7 @@ enum Route { /// Represents a single record as filled in by a user. This is the /// primary data structure we want to populate and persist somewhere. -#[derive(Default, Debug, Deserialize, Serialize)] +#[derive(Clone, Default, Debug, Deserialize, Serialize)] struct Record { // Personal information name: String, @@ -127,6 +136,9 @@ struct App { // Captcha callback closure which needs to be kept alive for the // lifecycle of the app. captcha_callback: Closure, + + // Has data been submitted already by this user? + submitted: bool, } #[derive(Clone, Debug)] @@ -146,7 +158,9 @@ enum Msg { SetPosition(String), SetJobDetails(String), SetWorkBackground(String), + CaptchaSolved(String), + Submit, } /// Callback handler for adding a technology. @@ -306,6 +320,42 @@ fn render_technologies(link: &Scope, technologies: &BTreeSet) -> Ht } } +/// Submit the collected data to the backend service. +async fn submit_data(captcha_token: &str, record: &Record) -> bool { + let response = http::Request::get(SUBMIT_URL) + .method(http::Method::POST) + .json(&serde_json::json!({ + "captcha_token": captcha_token, + "record": record, + })) + .expect("serialising a serde_json::Value can not fail") + .send() + .await + .unwrap(); + + // currently there is nothing we can actually do with the response + // here, but we should add some way to communicate back some + // server errors etc., even if the whole thing should be as + // forgiving as possible. + response.ok() +} + +/// Handle the submit event, if all data was successfully collected. +fn handle_submit(app: &App, link: Scope) -> Msg { + let token = app.captcha_token.as_ref().unwrap().clone(); + let record = app.record.clone(); + + wasm_bindgen_futures::spawn_local(async move { + if !submit_data(&token, &record).await { + console::warn!("failed to submit data for some reason"); + } else { + console::log!("submitted data successfully"); + } + }); + + Msg::NoOp +} + impl Component for App { type Message = Msg; type Properties = (); @@ -323,10 +373,11 @@ impl Component for App { link.send_message(Msg::CaptchaSolved(val)); })) }, + submitted: false, } } - fn update(&mut self, _ctx: &Context, msg: Self::Message) -> bool { + fn update(&mut self, ctx: &Context, msg: Self::Message) -> bool { console::log!("handling ", format!("{:?}", msg)); let (state_change, view_change) = match msg { Msg::NoOp => (false, false), @@ -397,6 +448,17 @@ impl Component for App { self.captcha_token = Some(token); (false, true) } + + Msg::Submit => { + if self.record.is_complete() && self.captcha_token.is_some() { + self.submitted = true; + handle_submit(self, ctx.link().clone()); + (false, true) + } else { + console::warn!("submitted data, but form or captcha was not ready"); + (false, false) + } + } }; if state_change {