about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2023-06-02T12·56+0300
committertazjin <tazjin@tvl.su>2023-06-06T11·43+0000
commit138f1ca1b90a1b0a3d1c703246c8eb8f9ff8b31a (patch)
treee91a08dcaeeae8fd1d9bb7d4110d3b2e7a277c93
parent6f0ddbac06a9a72908b8b0b955cd743d3ae82378 (diff)
feat(corp/rih): wire up captcha solving callback r/6234
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 <tazjin@tvl.su>
-rw-r--r--corp/rih/src/home.html3
-rw-r--r--corp/rih/src/main.rs43
2 files changed, 35 insertions, 11 deletions
diff --git a/corp/rih/src/home.html b/corp/rih/src/home.html
index 95a77877d3..7f1ff5f332 100644
--- a/corp/rih/src/home.html
+++ b/corp/rih/src/home.html
@@ -1,12 +1,13 @@
 html! {
 <main>
   <script>
-    {r#"function captchaOnload(sitekey) {
+    {r#"function captchaOnload(sitekey, callback) {
       if (window.smartCaptcha) {
         const container = document.getElementById('captcha-container');
         const widgetId = window.smartCaptcha.render(container, {
             sitekey: sitekey,
             hl: 'en',
+            callback: callback,
         });
       }
     }"#}
diff --git a/corp/rih/src/main.rs b/corp/rih/src/main.rs
index 2fed9dfd8f..a419e2d9bf 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<String>,
+
+    // Captcha callback closure which needs to be kept alive for the
+    // lifecycle of the app.
+    captcha_callback: Closure<dyn FnMut(String)>,
 }
 
 #[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>) -> Self {
-        let mut new = Self::default();
-
-        if let Ok(record) = LocalStorage::get("record") {
-            new.record = record;
+    fn create(ctx: &Context<Self>) -> 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<Self>, 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<Self>, 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(),
+            );
         }
     }
 }