From 138f1ca1b90a1b0a3d1c703246c8eb8f9ff8b31a Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Fri, 2 Jun 2023 15:56:47 +0300 Subject: feat(corp/rih): wire up captcha solving callback This turned out a lot nicer than I expected it to be. Change-Id: I427670644eba789ea2037423fa9af8e632b19b34 Reviewed-on: https://cl.tvl.fyi/c/depot/+/8695 Tested-by: BuildkiteCI Reviewed-by: tazjin --- corp/rih/src/main.rs | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) (limited to 'corp/rih/src/main.rs') diff --git a/corp/rih/src/main.rs b/corp/rih/src/main.rs index 2fed9dfd8f4a..a419e2d9bfc8 100644 --- a/corp/rih/src/main.rs +++ b/corp/rih/src/main.rs @@ -98,7 +98,6 @@ struct Record { work_background: String, } -#[derive(Default)] struct App { // The record being populated. record: Record, @@ -111,6 +110,13 @@ struct App { // History handler. history: BrowserHistory, + + // Captcha token, if the captcha has been solved. + captcha_token: Option, + + // Captcha callback closure which needs to be kept alive for the + // lifecycle of the app. + captcha_callback: Closure, } #[derive(Clone, Debug)] @@ -130,6 +136,7 @@ enum Msg { SetPosition(String), SetJobDetails(String), SetWorkBackground(String), + CaptchaSolved(String), } /// Callback handler for adding a technology. @@ -293,14 +300,20 @@ impl Component for App { type Message = Msg; type Properties = (); - fn create(_ctx: &Context) -> Self { - let mut new = Self::default(); - - if let Ok(record) = LocalStorage::get("record") { - new.record = record; + fn create(ctx: &Context) -> Self { + App { + record: LocalStorage::get("record").unwrap_or_default(), + citizenship_focus: false, + citizenship_query: String::default(), + history: BrowserHistory::default(), + captcha_token: None, + captcha_callback: { + let link = ctx.link().clone(); + Closure::wrap(Box::new(move |val| { + link.send_message(Msg::CaptchaSolved(val)); + })) + }, } - - new } fn update(&mut self, _ctx: &Context, msg: Self::Message) -> bool { @@ -369,6 +382,11 @@ impl Component for App { self.record.work_background = background; (true, false) } + + Msg::CaptchaSolved(token) => { + self.captcha_token = Some(token); + (false, true) + } }; if state_change { @@ -399,8 +417,13 @@ impl Component for App { fn rendered(&mut self, _: &Context, first_render: bool) { if first_render { - let func = js_sys::Function::new_with_args("key", "captchaOnload(key)"); - let _ = func.call1(&JsValue::NULL, &JsValue::from_str(CAPTCHA_KEY)); + let func = + js_sys::Function::new_with_args("key, callback", "captchaOnload(key, callback)"); + let _ = func.call2( + &JsValue::NULL, + &JsValue::from_str(CAPTCHA_KEY), + &self.captcha_callback.as_ref().unchecked_ref(), + ); } } } -- cgit 1.4.1